From 110e9309a0418b63c1d919936c3e1d13396b9fc3 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Thu, 14 Sep 2006 07:33:41 +0000 Subject: Bug 346597: Have Bugzilla::Group implement Bugzilla::Object->create, and make checksetup.pl use it Patch By Max Kanat-Alexander r=LpSolit, a=myk --- Bugzilla/Group.pm | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'Bugzilla/Group.pm') diff --git a/Bugzilla/Group.pm b/Bugzilla/Group.pm index 5fc88c047..93a78331c 100644 --- a/Bugzilla/Group.pm +++ b/Bugzilla/Group.pm @@ -27,6 +27,7 @@ use strict; package Bugzilla::Group; use base qw(Bugzilla::Object); +use Bugzilla::Constants; use Bugzilla::Util; use Bugzilla::Error; @@ -47,6 +48,15 @@ use constant DB_TABLE => 'groups'; use constant LIST_ORDER => 'isbuggroup, name'; +use constant VALIDATORS => { + name => \&_check_name, + description => \&_check_description, + userregexp => \&_check_user_regexp, + isbuggroup => \&_check_is_bug_group, +}; + +use constant REQUIRED_CREATE_FIELDS => qw(name description isbuggroup); + ############################### #### Accessors ###### ############################### @@ -56,10 +66,46 @@ sub is_bug_group { return $_[0]->{'isbuggroup'}; } sub user_regexp { return $_[0]->{'userregexp'}; } sub is_active { return $_[0]->{'isactive'}; } +############################### +#### Methods #### +############################### + +sub _rederive_regexp { + my ($self) = @_; + RederiveRegexp($self->user_regexp, $self->id); +} + ################################ ##### Module Subroutines ### ################################ +sub create { + my $class = shift; + my ($params) = @_; + my $dbh = Bugzilla->dbh; + + print "Creating group $params->{name}...\n" unless i_am_cgi(); + + my $group = $class->SUPER::create(@_); + + # Since we created a new group, give the "admin" group all privileges + # initially. + my $admin = new Bugzilla::Group({name => 'admin'}); + # This function is also used to create the "admin" group itself, + # so there's a chance it won't exist yet. + if ($admin) { + my $sth = $dbh->prepare('INSERT INTO group_group_map + (member_id, grantor_id, grant_type) + VALUES (?, ?, ?)'); + $sth->execute($admin->id, $group->id, GROUP_MEMBERSHIP); + $sth->execute($admin->id, $group->id, GROUP_BLESS); + $sth->execute($admin->id, $group->id, GROUP_VISIBLE); + } + + $group->_rederive_regexp() if $group->user_regexp; + return $group; +} + sub ValidateGroupName { my ($name, @users) = (@_); my $dbh = Bugzilla->dbh; @@ -79,6 +125,65 @@ sub ValidateGroupName { return $ret; } +# This sub is not perldoc'ed because we expect it to go away and +# just become the _rederive_regexp private method. +sub RederiveRegexp { + my ($regexp, $gid) = @_; + my $dbh = Bugzilla->dbh; + my $sth = $dbh->prepare("SELECT userid, login_name, group_id + FROM profiles + LEFT JOIN user_group_map + ON user_group_map.user_id = profiles.userid + AND group_id = ? + AND grant_type = ? + AND isbless = 0"); + my $sthadd = $dbh->prepare("INSERT INTO user_group_map + (user_id, group_id, grant_type, isbless) + VALUES (?, ?, ?, 0)"); + my $sthdel = $dbh->prepare("DELETE FROM user_group_map + WHERE user_id = ? AND group_id = ? + AND grant_type = ? and isbless = 0"); + $sth->execute($gid, GRANT_REGEXP); + while (my ($uid, $login, $present) = $sth->fetchrow_array()) { + if (($regexp =~ /\S+/) && ($login =~ m/$regexp/i)) + { + $sthadd->execute($uid, $gid, GRANT_REGEXP) unless $present; + } else { + $sthdel->execute($uid, $gid, GRANT_REGEXP) if $present; + } + } +} + +############################### +### Validators ### +############################### + +sub _check_name { + my ($invocant, $name) = @_; + $name = trim($name); + $name || ThrowUserError("empty_group_name"); + my $exists = new Bugzilla::Group({name => $name }); + ThrowUserError("group_exists", { name => $name }) if $exists; + return $name; +} + +sub _check_description { + my ($invocant, $desc) = @_; + $desc = trim($desc); + $desc || ThrowUserError("empty_group_description"); + return $desc; +} + +sub _check_user_regexp { + my ($invocant, $regex) = @_; + $regex = trim($regex) || ''; + ThrowUserError("invalid_regexp") unless (eval {qr/$regex/}); + return $regex; +} + +sub _check_is_bug_group { + return $_[1] ? 1 : 0; +} 1; __END__ @@ -113,6 +218,13 @@ provides, in addition to any methods documented below. =over +=item C + +Note that in addition to what L +normally does, this function also makes the new group be inherited +by the C group. That is, the C group will automatically +be a member of this group. + =item C Description: ValidateGroupName checks to see if ANY of the users -- cgit v1.2.3-24-g4f1b