-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.pm
549 lines (443 loc) · 18 KB
/
Extension.pm
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#sub install_update_db {
my $dbh = Bugzilla->dbh;
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the AssigneeList Bugzilla Extension.
#
# The Initial Developer of the Original Code is Ali Ustek
# Portions created by the Initial Developer are Copyright (C) 2011 the
# Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Ali Ustek <[email protected]>
package Bugzilla::Extension::AssigneeList;
use strict;
use base qw(Bugzilla::Extension);
use Bugzilla::Util qw(get_text);
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Token;
use Bugzilla::Util;
use Carp;
use Bugzilla::Extension::AssigneeList::Constants;
use Bugzilla::Extension::AssigneeList::Group;
use Bugzilla::Extension::AssigneeList::Assignee;
our $VERSION = '0.01';
BEGIN {
*Bugzilla::Component::assignees = \&_assignees;
*Bugzilla::Component::assignee_groups = \&_assignee_groups;
*Bugzilla::Product::user_can_see = \&_user_can_see;
}
###########################
# Database & Installation #
###########################
sub db_schema_abstract_schema {
my ($self, $args) = @_;
$args->{'schema'}->{'componentleadsgroup'} = {
FIELDS => [
id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1},
component_id => {TYPE => 'INT2', NOTNULL => 1,
REFERENCES => {TABLE => 'components',
COLUMN => 'id',
DELETE => 'CASCADE'}},
name => {TYPE => 'varchar(64)', NOTNULL => 1},
sortkey => {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0},
],
INDEXES => [
componentleadsgroup_name_component_id_idx =>
{FIELDS => [qw(component_id name)], TYPE => 'UNIQUE'},
componentleadsgroup_sortkey_idx => ['sortkey'],
],
};
$args->{'schema'}->{'componentleads'} = {
FIELDS => [
id => {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1},
user_id => {TYPE => 'INT3', NOTNULL => 1,
REFERENCES => {TABLE => 'profiles',
COLUMN => 'userid',
DELETE => 'CASCADE'}},
group_id => {TYPE => 'INT3', NOTNULL => 1,
REFERENCES => {TABLE => 'componentleadsgroup',
COLUMN => 'id',
DELETE => 'CASCADE'}},
name => {TYPE => 'varchar(64)', NOTNULL => 1},
sortkey => {TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0},
],
INDEXES => [
componentleads_id_idx =>
{FIELDS => [qw(group_id user_id)], TYPE => 'UNIQUE'},
componentleads_group_id_idx => ['group_id'],
componentleads_sortkey_idx => ['sortkey'],
],
};
}
sub install_update_db {
my $dbh = Bugzilla->dbh;
my @components = Bugzilla::Component->get_all;
foreach my $component (@components) {
my ($group_id) = $dbh->selectrow_array(
"SELECT id FROM componentleadsgroup WHERE component_id=?", undef,
$component->id);
if(!$group_id) {
# print "Adding components initial owner to assignee list\n";
my $group = Bugzilla::Extension::AssigneeList::Group->create(
{name => 'Default',component => $component});
my $owner = new Bugzilla::User($component->{'initialowner'});
Bugzilla::Extension::AssigneeList::Assignee->create({
name => $owner->name ? $owner->name : $owner->login,
sortkey => 0,
user => $owner,
group => $group,
});
}
}
}
###########
# Objects #
###########
sub object_end_of_create {
my ($self, $args) = @_;
my $object = $args->{'object'};
if ($object->isa('Bugzilla::Component')) {
my $group = Bugzilla::Extension::AssigneeList::Group->create(
{name => 'Default',component => $object});
my $owner = new Bugzilla::User($object->{'initialowner'});
Bugzilla::Extension::AssigneeList::Assignee->create({
name => $owner->name ? $owner->name : $owner->login,
sortkey => 0,
user => $owner,
group => $group,
});
}
}
sub object_before_set {
my ($self, $args) = @_;
my ($object, $field, $value) = @$args{qw(object field value)};
if ($object->isa('Bugzilla::Component')) {
if ($field eq 'initialowner') {
my $oldowner = new Bugzilla::User($object->{'initialowner'});
my $newowner = new Bugzilla::User({name => $value});
my $dbh = Bugzilla->dbh;
my $id = $dbh->selectrow_array(qq{
SELECT L.id FROM componentleads AS L
LEFT JOIN componentleadsgroup AS G ON G.id = L.group_id
WHERE
G.component_id = $object->{'id'} AND
L.user_id = $oldowner->{'userid'}}, undef);
my $old_assignee = new Bugzilla::Extension::AssigneeList::Assignee($id);
my $group = new Bugzilla::Extension::AssigneeList::Group(
$old_assignee->group_id);
Bugzilla::Extension::AssigneeList::Assignee->create({
name => $newowner->name ? $newowner->name : $newowner->login,
sortkey => 0,
user => $newowner,
group => $group,
});
$old_assignee->remove_from_db;
}
}
}
#########
# Pages #
#########
sub template_before_create {
my ($self, $args) = @_;
my $config = $args->{'config'};
my $constants = $config->{CONSTANTS};
$constants->{MAX_LABEL_LENGTH} = MAX_LABEL_LENGTH;
}
sub page_before_template {
my ($self, $args) = @_;
my $page = $args->{page_id};
my $vars = $args->{vars};
if ($page =~ m{^assigneelist/.*}) {
my $cgi = Bugzilla->cgi;
my $template = Bugzilla->template;
#
# Preliminary checks:
#
my $user = Bugzilla->login(LOGIN_REQUIRED);
print $cgi->header();
$user->in_group('editcomponents')
|| scalar(@{$user->get_products_by_permission('editcomponents')})
|| ThrowUserError("auth_failure", {group => "editcomponents",
action => "edit",
object => "components"});
#
# often used variables
#
my $product_name = trim($cgi->param('product') || '');
my $comp_name = trim($cgi->param('component') || '');
my $action = trim($cgi->param('action') || '');
my $showbugcounts = (defined $cgi->param('showbugcounts'));
#
# product = '' -> Show nice list of products
#
unless ($product_name) {
my $selectable_products = $user->get_selectable_products;
# If the user has editcomponents privs for some products only,
# we have to restrict the list of products to display.
unless ($user->in_group('editcomponents')) {
$selectable_products = $user->get_products_by_permission('editcomponents');
}
$vars->{'products'} = $selectable_products;
$vars->{'showbugcounts'} = $showbugcounts;
$template->process("admin/components/select-product.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
my $product = $user->check_can_admin_product($product_name);
#
# comp_name='' -> Show nice list of components
#
unless ($comp_name) {
$vars->{'showbugcounts'} = $showbugcounts;
$vars->{'product'} = $product;
$template->process("admin/components/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
$vars->{'comp'} =
Bugzilla::Component->check({ product => $product, name => $comp_name });
$vars->{'product'} = $product;
#
# User can edit given component, goto page requested
#
if ($page =~ m{^assigneelist/list\.}) {
_page_assignees($vars, $action);
}
elsif ($page =~ m{^assigneelist/group/list\.}) {
_page_assigneegroups($vars, $action);
}
}
}
# Method to take care of all Assigne actions
sub _page_assignees {
my ($vars, $action) = @_;
my $template = Bugzilla->template;
my $cgi = Bugzilla->cgi;
my $token = $cgi->param('token');
if ($action eq '' || $action eq 'list') {
$template->process("pages/assigneelist/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'add') {
$vars->{'token'} = issue_session_token('add_assignee');
$template->process("pages/assigneelist/create.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'new') {
my $group = new Bugzilla::Extension::AssigneeList::Group($cgi->param('group'));
my $user = new Bugzilla::User({name => $cgi->param('user_login')});
my $assignee = Bugzilla::Extension::AssigneeList::Assignee->create({
name => $cgi->param('name'),
sortkey => $cgi->param('sortkey'),
user => $user,
group => $group,
});
$vars->{'message'} = 'assignee_created';
$vars->{'assignee'} = $assignee;
delete_token($token);
$template->process("pages/assigneelist/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
# After this point changes are made to an existing assignee
my $assignee = new Bugzilla::Extension::AssigneeList::Assignee($cgi->param('assignee_id'));
$vars->{'assignee'} = $assignee;
if ($action eq 'update') {
check_token_data($token, 'edit_assignee');
my $group = new Bugzilla::Extension::AssigneeList::Group($cgi->param('group'));
$assignee->set_name($cgi->param('name_new'));
$assignee->set_group($group);
$assignee->set_sortkey($cgi->param('sortkey'));
my $changes = $assignee->update();
$vars->{'message'} = 'assignee_updated';
$vars->{'changes'} = $changes;
delete_token($token);
$template->process("pages/assigneelist/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'del') {
$vars->{'token'} = issue_session_token('delete_assignee');
$template->process("pages/assigneelist/confirm-delete.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'delete') {
check_token_data($token, 'delete_assignee');
$assignee->remove_from_db;
$vars->{'message'} = 'assignee_deleted';
$template->process("pages/assigneelist/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'edit') {
$vars->{'token'} = issue_session_token('edit_assignee');
$template->process("pages/assigneelist/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
# No valid action found
ThrowUserError('unknown_action', {action => $action});
}
# Method to take care of all AssigneGroup actions
sub _page_assigneegroups {
my ($vars, $action) = @_;
my $template = Bugzilla->template;
my $cgi = Bugzilla->cgi;
my $token = $cgi->param('token');
if ($action eq '' || $action eq 'list') {
$template->process("pages/assigneelist/group/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'add') {
$vars->{'token'} = issue_session_token('add_assignee_group');
$template->process("pages/assigneelist/group/create.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'new') {
my $group = Bugzilla::Extension::AssigneeList::Group->create({
name => $cgi->param('name'),
sortkey => $cgi->param('sortkey'),
component => $vars->{'comp'}
});
$vars->{'message'} = 'assignee_group_created';
$vars->{'group'} = $group;
delete_token($token);
$template->process("pages/assigneelist/group/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
# After this point changes are made to an existing group
my $group = new Bugzilla::Extension::AssigneeList::Group({
component => $vars->{'comp'},
name => $cgi->param('name')
});
$vars->{'group'} = $group;
if ($action eq 'del') {
$vars->{'token'} = issue_session_token('delete_assignee_group');
$template->process("pages/assigneelist/group/confirm-delete.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'delete') {
check_token_data($token, 'delete_assignee_group');
$group->remove_from_db;
$vars->{'message'} = 'assignee_group_deleted';
$template->process("pages/assigneelist/group/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'edit') {
$vars->{'token'} = issue_session_token('edit_assignee_group');
$template->process("pages/assigneelist/group/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'update') {
check_token_data($token, 'edit_assignee_group');
$group->set_name($cgi->param('name_new'));
$group->set_sortkey($cgi->param('sortkey'));
my $changes = $group->update();
$vars->{'message'} = 'assignee_group_updated';
$vars->{'changes'} = $changes;
delete_token($token);
$template->process("pages/assigneelist/group/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
# No valid action found
ThrowUserError('unknown_action', {action => $action});
}
############################
# Component Object Methods #
############################
sub _assignees {
my $self = shift;
my $dbh = Bugzilla->dbh;
if (!defined $self->{assignees}) {
my $ids = $dbh->selectcol_arrayref(q{
SELECT L.id FROM componentleads AS L
LEFT JOIN componentleadsgroup AS G ON G.id = L.group_id
LEFT JOIN profiles AS P ON P.userid = L.user_id
WHERE
G.component_id = ? AND
P.disabledtext = ''
ORDER BY L.group_id, L.sortkey}, undef, $self->id);
require Bugzilla::Extension::AssigneeList::Assignee;
$self->{assignees} = Bugzilla::Extension::AssigneeList::Assignee->new_from_list($ids);
}
return $self->{assignees};
}
sub _assignee_groups {
my $self = shift;
my $dbh = Bugzilla->dbh;
if (!defined $self->{assignee_groups}) {
my $ids = $dbh->selectcol_arrayref(q{
SELECT id FROM componentleadsgroup
WHERE component_id = ?
ORDER BY sortkey}, undef, $self->id);
require Bugzilla::Extension::AssigneeList::Group;
$self->{assignee_groups} = Bugzilla::Extension::AssigneeList::Group->new_from_list($ids);
}
return $self->{assignee_groups};
}
##########################
# Product Object Methods #
##########################
sub _user_can_see {
my $self = shift;
require Bugzilla::User;
my @custusers;
my @allusers = @{Bugzilla->user->get_userlist};
foreach my $user (@allusers) {
my $user_obj = new Bugzilla::User({name => $user->{login}});
push(@custusers, $user) if $user_obj->can_see_product($self->name);
}
return \@custusers;
}
##########################
__PACKAGE__->NAME;
=head1 NAME
Bugzilla::Extension::AssigneeList - Object for a Bugzilla AssigneeList extension.
=head1 SYNOPSIS
my @assignees = $componet->assignees();
my @assignee_groups = $componet->assignee_groups();
my @visible_to_users = $product->visible_to_users();
=head1 DESCRIPTION
This package converts bug assignee field to a list of users to select assignee from,
each component would have a list of user to whom bugs can be assinged.
The methods that are added to other Bugzilla classes are listed below.
=head2 Custom Added Methods
=item C<assignees()>
Description: Returns an array of assignee objects belonging to the component.
Params: none.
Returns: An array of Bugzilla::Extension::AssigneeList::Assignee object.
=item C<assignee_groups()>
Description: Returns an array of Assignee Group objects belonging to the component.
Params: none
Returns: An array of Bugzilla::Extension::AssigneeList::Group object.
=item C<user_can_see()>
Description: Returns an array of User objects whom the product is visible.
Params: none
Returns: An array of Bugzilla::User object.
=back
=head1 SEE ALSO
L<Bugzilla::Component>
L<Bugzilla::Product>
L<Bugzilla::User>