summaryrefslogtreecommitdiff
blob: 8e61c4d86bee19272f884d375bd72a2baaa5770c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# 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;

use 5.10.1;
use strict;
use warnings;

use parent qw(Bugzilla::Extension);

use Bugzilla::Extension::Workflows::Util;
use Bugzilla::Extension::Workflows::GroupApproval;
use Bugzilla::Extension::Workflows::GroupRequest;
use Bugzilla::Extension::Workflows::ManageComponents;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Token;
use Bugzilla::Extension::RuleEngine::Util qw(message_2_array);

our $VERSION = '0.01';

BEGIN {
  *Bugzilla::User::can_admin_group_workflow = \&can_admin_group_workflow;
  *Bugzilla::User::apply_for_groups         = \&apply_for_groups;

  *Bugzilla::Group::group_workflow   = \&group_workflow;
  *Bugzilla::Group::can_apply_for    = \&can_apply_for;
  *Bugzilla::Group::can_approve      = \&can_approve;
  *Bugzilla::Group::can_invite       = \&can_invite;
  *Bugzilla::Flag::user_can_edit     = \&_flag_user_can_edit;
  *Bugzilla::FlagType::user_can_edit = \&_flagtype_user_can_edit;
}

# See the documentation of Bugzilla::Hook ("perldoc Bugzilla::Hook"
# in the bugzilla directory) for a list of all available hooks.
sub install_before_final_checks {
  my ($self, $args) = @_;

  my $dbh = Bugzilla->dbh;

  my $group = Bugzilla::Group->new({name => 'approvals_admin'});

  unless ($group) {
    $group = Bugzilla::Group->create({
      name        => "approvals_admin",
      description => "Admin group for approvals",
      isbuggroup  => 1,
      isactive    => 1,
      secure_mail => 1,
      category    => 'Admin',
    });
  }

  my $trusted_group = Bugzilla::Group->new({name => 'trusted_approvers'});

  unless ($trusted_group) {
    $trusted_group = Bugzilla::Group->create({
      name        => "trusted_approvers",
      description => "Users who are trusted and can bypass manager sign off",
      isbuggroup  => 1,
      isactive    => 0,
      secure_mail => 0,
      category    => 'Admin',
    });
  }

  my $classif = Bugzilla::Classification->new({name => 'Workflows'});

  unless ($classif) {
    $classif
      = Bugzilla::Classification->create({
      name => "Workflows", description => "User driven workflows",
      });
  }

  my $bvp
    = Bugzilla::Extension::BugViewPlus::Template->new({name => 'Group Request'});

  unless ($bvp) {
    $bvp = Bugzilla::Extension::BugViewPlus::Template->create({
      name        => 'Group Request',
      is_active   => 1,
      description => 'Group membership request text',
      content =>
        "Please explain why you are requesting membership in these groups:\n\n",
    });
  }

  my $prod = Bugzilla::Product->new({name => 'Group Membership'});
  unless ($prod) {

    $dbh->bz_start_transaction();
    $prod = Bugzilla::Product->create({
      classification     => $classif->name,
      name               => "Group Membership",
      description        => "Workflow to manage group membership requests.",
      isactive           => 1,
      allows_unconfirmed => 0,
      version            => '1.0',
    });

    $prod->set_group_controls(
      $group,
      {
        entry         => 1,
        editbugs      => 1,
        membercontrol => CONTROLMAPMANDATORY,
        othercontrol  => CONTROLMAPMANDATORY
      }
    );
    $prod->set_always_private(1);
    $prod->update();

    Bugzilla::Component->create({
      product      => $prod,
      name         => "Self service",
      description  => "Allows a user to start a request for their own memberships.",
      initialowner => Bugzilla->params->{system_user},
      bvp_template_id => $bvp->id,
    });
    $dbh->bz_commit_transaction();
  }

  return;
}

sub install_update_db {

  return;
}

sub db_schema_abstract_schema {
  my ($self, $args) = @_;

  my $schema = $args->{schema};

  ## Group Request
  $schema->{workflow_groupreq} = {
    FIELDS => [
      id          => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1},
      managed     => {TYPE => 'BOOLEAN',      NOTNULL => 1, DEFAULT    => 'FALSE'},
      invite_only => {TYPE => 'BOOLEAN',      NOTNULL => 1, DEFAULT    => 'FALSE'},
      regex       => {TYPE => 'TINYTEXT',     NOTNULL => 1, DEFAULT    => "''"},
      group_id    => {
        TYPE    => 'INT3',
        NOTNULL => 1,
        REFERENCES =>
          {TABLE => 'groups', COLUMN => 'id', DELETE => 'CASCADE', NOTNULL => 1}
      },
      flag_id =>
        {TYPE => 'INT3', REFERENCES => {TABLE => 'flagtypes', COLUMN => 'id'}},
    ],
    INDEXES => [
      workflow_groupreq_group_unique_idx =>
        {FIELDS => [qw(group_id)], TYPE => 'UNIQUE'},
    ],
  };

  return;
}

sub page_before_template {
  my ($self, $args) = @_;

  my $page = $args->{page_id};
  my $vars = $args->{vars};

  if ($page =~ m{^workflows/group_request\.}) {
    page_group_request($vars);
  }
  elsif ($page =~ m{^workflows/group_req_admin\.}) {
    page_group_admin($vars);
  }
  elsif ($page =~ m{^workflows/manage_components\.}) {
    page_manage_components($vars);
  }
  return;
}

sub template_before_process {
  my ($self, $args) = @_;
  my $file = $args->{'file'};
  my $vars = $args->{vars};

  my $user;

  if ($file eq 'global/header.html.tmpl') {
    $user = Bugzilla->user;
    if ($user) {
      my $dbh = Bugzilla->dbh;

      my $SQL = <<'EOS';
SELECT count(*)
FROM flags
WHERE
  requestee_id = ?
  AND status = '?'
  AND type_id IN
    ( SELECT id
      FROM flagtypes
      WHERE name LIKE 'approvals_request_%')
EOS

      my $count = $dbh->selectrow_array($SQL, undef, $user->id);
      if ($count) {
        message_2_array($vars);

        push(
          @{$vars->{message}},
          ['outstanding_personal_group_requests', {personal_requests => $count}]
        );
      }

      $SQL = <<'EOS';
SELECT count(*)
FROM flags
WHERE
bug_id IN (
  SELECT bug_id
  FROM bugs
  WHERE product_id = (SELECT id FROM products WHERE name = 'Group Membership')
  AND bug_status <> 'CLOSED'
)
AND status = '?'
AND type_id IN
  ( SELECT id
    FROM flagtypes
    WHERE name LIKE 'approvals_request_%'
    AND grant_group_id IN 
      ( SELECT group_id
        FROM user_group_map
        WHERE user_id = ?
        UNION
        SELECT grantor_id
        FROM group_group_map
        WHERE grant_type = 0
        AND member_id IN
          ( SELECT group_id
            FROM user_group_map
            WHERE user_id = ?
          )
      )
  )
EOS

      $count = $dbh->selectrow_array($SQL, undef, $user->id, $user->id);

      if ($count) {
        message_2_array($vars);

        push(
          @{$vars->{message}},
          ['outstanding_approval_group_requests', {approval_group_requests => $count}]
        );
      }
    }
  }

  return;
}

sub webservice {
  my ($self, $args) = @_;

  my $dispatch = $args->{dispatch};
  $dispatch->{GroupRequest}
    = "Bugzilla::Extension::Workflows::WebService::GroupRequest";

  $dispatch->{ManageComponents}
    = "Bugzilla::Extension::Workflows::WebService::ManageComponents";

  return;
}

sub bug_end_of_update {
  my ($self, $args) = @_;
  my $dbh = Bugzilla->dbh;

  my ($bug, $old_bug, $timestamp, $changes)
    = @$args{qw(bug old_bug timestamp changes)};

  if ($bug->product eq 'Group Membership' && $bug->component eq 'Self service') {
    group_membership_bug_update($args);
  }

  return;
}

sub _flag_user_can_edit {
  my ($self, $user) = @_;

  my $can_set_flag = 1;
  my $flag_type    = $self->type;
  my $flag_name    = $flag_type->name;
  if ($flag_name =~ m/^approvals_request_(.+)$/) {
    my $group_name = $1;
    $group_name =~ s/\./ /g;
    my $group   = Bugzilla::Group->check({name => $group_name});
    my $subject = $self->bug->short_desc;
    $subject =~ m/Group membership request for (.*)$/;
    my $uname     = $1;
    my $requestor = Bugzilla::User->check($uname);
    $can_set_flag = $group->can_approve($user, $requestor);
  }

  return $can_set_flag;
}

sub _flagtype_user_can_edit {
  my ($self, $user, $bug) = @_;

  my $can_edit = 0;

  my $flag_name = $self->name;
  if ($flag_name =~ m/^approvals_request_(.+)$/) {
    my $group_name = $1;
    $group_name =~ s/\./ /g;
    my $group   = Bugzilla::Group->check({name => $group_name});
    my $subject = $bug->short_desc;
    $subject =~ m/Group membership request for (.*)$/;
    my $uname     = $1;
    my $requestor = Bugzilla::User->check($uname);
    $can_edit = $group->can_approve($user, $requestor);
  }
  else {
    $can_edit = ($self->is_requestable && $user->can_request_flag($self));
  }

  return $can_edit;
}

__PACKAGE__->NAME;

__END__

=head1 NAME

Bugzilla::Extension::Workflows

=head1 DESCRIPTION

This extensions allows creation of custom workflows within Bugzilla.

=head1 Workflows

L<Group Approval|Bugzilla::Extension::Workflows::GroupApproval>