diff options
Diffstat (limited to 'extensions/Workflows/lib/GroupRequest.pm')
-rw-r--r-- | extensions/Workflows/lib/GroupRequest.pm | 455 |
1 files changed, 455 insertions, 0 deletions
diff --git a/extensions/Workflows/lib/GroupRequest.pm b/extensions/Workflows/lib/GroupRequest.pm new file mode 100644 index 000000000..9a1e78257 --- /dev/null +++ b/extensions/Workflows/lib/GroupRequest.pm @@ -0,0 +1,455 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package Bugzilla::Extension::Workflows::GroupRequest; + +use strict; +use warnings; +use 5.10.1; +use base qw(Bugzilla::Object); + +use Bugzilla::Constants; +use Bugzilla::Util; +use Bugzilla::Error; +use Bugzilla::User; +use Bugzilla::Group; +use Bugzilla::FlagType; + +use constant DB_TABLE => 'workflow_groupreq'; +use constant LIST_ORDER => 'id'; + +use constant DB_COLUMNS => qw( + id + managed + invite_only + regex + group_id + flag_id +); + +use constant UPDATE_COLUMNS => qw( + managed + invite_only + regex + group_id + flag_id +); + +use constant VALIDATORS => { + managed => \&Bugzilla::Object::check_boolean, + invite_only => \&Bugzilla::Object::check_boolean, + regex => \&_check_regex, + group_id => \&_check_group, + flag_id => \&_check_flag_type, +}; + +sub _check_group { + my ($self, $group) = @_; + return unless $group; + + trick_taint($group); + $group = Bugzilla::Group->check({id => $group}); + return $group->id; +} + +sub _check_flag_type { + my ($self, $flag_type) = @_; + return unless $flag_type; + + trick_taint($flag_type); + $flag_type = Bugzilla::FlagType->check({id => $flag_type}); + return $flag_type->id; +} + +sub _check_regex { + my ($self, $regex) = @_; + $regex = trim($regex) || ''; + ThrowUserError("invalid_regexp") unless (eval {qr/$regex/}); + return ($regex); +} + +sub create { + my $class = shift; + my $dbh = Bugzilla->dbh; + + $dbh->bz_start_transaction(); + $class->check_required_create_fields(@_); + my $params = $class->run_create_validators(@_); + + my $wfg = $class->insert_create_data($params); + + my $product = Bugzilla::Product->new({name => 'Group Membership'}); + + my $flag_name = 'approvals_request_' . $wfg->group->name; + $flag_name =~ s/\s/\./g; + my $flag_type = Bugzilla::FlagType->new({name => $flag_name}); + + my $requesteeble = 1; + + unless ($flag_type) { + my %args = ( + sortkey => 1, + name => $flag_name, + inclusions => [$product->id . ':0'], + description => 'Manage group requests for ' . $wfg->group->name, + is_requestable => 1, + exclusions => [], + is_multiplicable => 0, + request_group => 'approvals_admin', + is_active => 1, + target_type => 'bug', + view_group => '', + category => '', + ); + + $flag_type = Bugzilla::FlagType->create(\%args); + } + + $flag_type->set_is_specifically_requestable($requesteeble); + $flag_type->update(); + + $wfg->set_flag_id($flag_type->id); + + $wfg->update(); + + $dbh->bz_commit_transaction(); + + return $wfg; +} + +sub set_managed { $_[0]->set('managed', $_[1]); return; } +sub set_invite_only { $_[0]->set('invite_only', $_[1]); return; } +sub set_regex { $_[0]->set('regex', $_[1]); return; } +sub set_group_id { $_[0]->set('group_id', $_[1]); return; } +sub set_flag_id { $_[0]->set('flag_id', $_[1]); return; } + +sub set_approval_group { + my ($self, $group_name) = @_; + my $approval_group; + + if ($group_name && $group_name ne '') { + trick_taint($group_name); + $approval_group = Bugzilla::Group->check({name => $group_name}); + ThrowUserError("approvals_invalid_approval_group", {group => $approval_group}) + unless ($approval_group->is_active_bug_group()); + } + + my $product = Bugzilla::Product->new({name => 'Group Membership'}); + + my $flag_type = $self->flag_type; + my $current_group = $flag_type->grant_group(); + + $product->set_group_controls($approval_group, + {membercontrol => CONTROLMAPSHOWN, othercontrol => CONTROLMAPSHOWN,}) + if ($approval_group && $approval_group->is_active_bug_group()); + + $product->update; + + my $requesteeble = 1; + my $grant_group = ''; + + if ($approval_group) { + $requesteeble = 0; + $grant_group = $approval_group->name; + } + + $flag_type->set_is_specifically_requestable($requesteeble); + $flag_type->set_grant_group($grant_group); + $flag_type->update; + + return; +} + +sub set_cc_list { + my ($self, $cc_list) = @_; + + my $dbh = Bugzilla->dbh; + + foreach my $name (split(/,\s*/, $cc_list)) { + my $user = Bugzilla::User->check($name); + } + + delete $self->{'ccs'}; + delete $self->{'cc_list'}; + + my $flag_type = $self->flag_type; + $flag_type->set_cc_list($cc_list); + $flag_type->update; + + return; +} + +sub managed { return ($_[0]->{managed}); } +sub invite_only { return ($_[0]->{invite_only}); } +sub regex { return ($_[0]->{regex}); } +sub group_id { return ($_[0]->{group_id}); } +sub flag_id { return ($_[0]->{flag_id}); } + +sub ccs { + my $self = shift; + my $dbh = Bugzilla->dbh; + + if (!defined $self->{'ccs'}) { + $self->{'cc_list'} = $self->cc_list; + + my @ccs; + + foreach my $name (split(/,\s*/, $self->cc_list)) { + my $user = Bugzilla::User->check($name); + push(@ccs, $user); + } + + $self->{'ccs'} = \@ccs; + } + + return $self->{'ccs'}; +} + +sub cc_list { + my $self = shift; + my $dbh = Bugzilla->dbh; + + if (!defined $self->{'cc_list'}) { + $self->{'cc_list'} = $self->flag_type->cc_list; + } + + return $self->{'cc_list'}; +} + +sub group { + my $self = shift; + if (!defined $self->{'group'}) { + $self->{'group'} = Bugzilla::Group->new($self->{group_id}); + } + + return $self->{'group'}; +} + +sub approval_group { + my $self = shift; + if (!defined $self->{'approval_group'}) { + my $flag_type = $self->flag_type; + $self->{'approval_group'} = $flag_type->grant_group; + } + + return $self->{'approval_group'}; +} + +sub flag_type { + my $self = shift; + + if (!defined $self->{'flag_type'}) { + $self->{'flag_type'} = Bugzilla::FlagType->new($self->{flag_id}); + } + + return $self->{'flag_type'}; +} + +1; + +__END__ + +=head1 NAME + +Bugzilla::Extension::Workflows::GroupRequest + +=head1 DESCRIPTION + +This class encapsulates the GroupRequest Workflow object. + +=head1 SYNOPSIS + + use Bugzilla::Extension::Workflows::GroupRequest; + + +=head1 METHODS + +=head2 C<create> + +Description: + The constructor is used to create a GroupRequest Workflow object. + +Params: + A hash of fields + +Returns: + A Bugzilla::Extension::Workflows::GroupRequest object. + +=head2 C<set_managed> + +Description: + Change if a group is currently being managed. + +Params: + B<boolean> + +Returns: + Nothing. + +=head2 C<set_invite_only> + +Description: + Change if a group is invite only. + +Params: + B<boolean> + +Returns: + Nothing. + +=head2 C<set_regex> + +Description: + Change the regular expression used to decide who can apply for this group. + The regex will be tested for validity. + +Params: + B<string> + +Returns: + Nothing. + +=head2 C<set_group_id> + +Description: + Change the group this record relates to. The group will be validated. + +Params: + B<integer> + +Returns: + Nothing. + +=head2 C<set_flag_id> + +Description: + Change the flag this record is using. The flag will be validated. + +Params: + B<integer> + +Returns: + Nothing. + +=head2 C<set_cc_list> + +Description: + Change the CC list for this record's flag type. All users will be validated. + +Params: + B<string> A comma separated list of user logins. + +Returns: + Nothing. + +=head2 C<managed> + +Description: + Is this group being managed? + +Params: + None. + +Returns: + B<Boolean> + +=head2 C<invite_only> + +Description: + Is this group invite only? + +Params: + None. + +Returns: + B<Boolean> + +=head2 C<regex> + +Description: + +Params: + None. + +Returns: + B<string> + +=head2 C<group_id> + +Description: + The ID of the Group for this record. + +Params: + None. + +Returns: + B<integer> + +=head2 C<flag_id> + +Description: + The ID of the FlagType for this record. + +Params: + None. + +Returns: + B<integer> + +=head2 C<ccs> + +Description: + Who are the users being CC'd? + +Params: + None. + +Returns: + An array of L<Bugzilla::User> objects. + +=head2 C<cc_list> + +Description: + Who are the users being CC'd? + +Params: + None. + +Returns: + A B<string> containing a comma separated list of user logins. + +=head2 C<group> + +Description: + What group does this record relate to? + +Params: + None. + +Returns: + A L<Bugzilla::Group> object. + +=head2 C<approval_group> + +Description: + Which group can grant approvals? + +Params: + None. + +Returns: + A L<Bugzilla::Group> object. + +=head2 C<flag_type> + +Description: + Which FlagType is the record using? + +Params: + None. + +Returns: + A L<Bugzilla::FlagType> object. + +=cut |