forked from bugzilla/bugzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.pm
3395 lines (2655 loc) · 101 KB
/
User.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
package Bugzilla::User;
use 5.10.1;
use strict;
use warnings;
use Bugzilla::Error;
use Bugzilla::Util;
use Bugzilla::Constants;
use Bugzilla::Search::Recent;
use Bugzilla::User::Setting;
use Bugzilla::Product;
use Bugzilla::Classification;
use Bugzilla::Field;
use Bugzilla::Group;
use Bugzilla::BugUserLastVisit;
use Bugzilla::Hook;
use DateTime::TimeZone;
use List::Util qw(max);
use List::MoreUtils qw(any);
use Scalar::Util qw(blessed);
use Storable qw(dclone);
use URI;
use URI::QueryParam;
use parent qw(Bugzilla::Object Exporter);
@Bugzilla::User::EXPORT = qw(is_available_username
login_to_id validate_password validate_password_check
USER_MATCH_MULTIPLE USER_MATCH_FAILED USER_MATCH_SUCCESS
MATCH_SKIP_CONFIRM
);
#####################################################################
# Constants
#####################################################################
use constant USER_MATCH_MULTIPLE => -1;
use constant USER_MATCH_FAILED => 0;
use constant USER_MATCH_SUCCESS => 1;
use constant MATCH_SKIP_CONFIRM => 1;
use constant DEFAULT_USER => {
'userid' => 0,
'realname' => '',
'login_name' => '',
'showmybugslink' => 0,
'disabledtext' => '',
'disable_mail' => 0,
'is_enabled' => 1,
};
use constant DB_TABLE => 'profiles';
# XXX Note that Bugzilla::User->name does not return the same thing
# that you passed in for "name" to new(). That's because historically
# Bugzilla::User used "name" for the realname field. This should be
# fixed one day.
sub DB_COLUMNS {
my $dbh = Bugzilla->dbh;
return (
'profiles.userid',
'profiles.login_name',
'profiles.realname',
'profiles.mybugslink AS showmybugslink',
'profiles.disabledtext',
'profiles.disable_mail',
'profiles.extern_id',
'profiles.is_enabled',
$dbh->sql_date_format('last_seen_date', '%Y-%m-%d') . ' AS last_seen_date',
),
;
}
use constant NAME_FIELD => 'login_name';
use constant ID_FIELD => 'userid';
use constant LIST_ORDER => NAME_FIELD;
use constant VALIDATORS => {
cryptpassword => \&_check_password,
disable_mail => \&_check_disable_mail,
disabledtext => \&_check_disabledtext,
login_name => \&check_login_name,
realname => \&_check_realname,
extern_id => \&_check_extern_id,
is_enabled => \&_check_is_enabled,
};
sub UPDATE_COLUMNS {
my $self = shift;
my @cols = qw(
disable_mail
disabledtext
login_name
realname
extern_id
is_enabled
);
push(@cols, 'cryptpassword') if exists $self->{cryptpassword};
return @cols;
}
use constant VALIDATOR_DEPENDENCIES => {is_enabled => ['disabledtext'],};
use constant EXTRA_REQUIRED_FIELDS => qw(is_enabled);
################################################################################
# Functions
################################################################################
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my ($param) = @_;
my $user = {%{DEFAULT_USER()}};
bless($user, $class);
return $user unless $param;
if (ref($param) eq 'HASH') {
if (defined $param->{extern_id}) {
$param = {condition => 'extern_id = ?', values => [$param->{extern_id}]};
$_[0] = $param;
}
}
return $class->SUPER::new(@_);
}
sub super_user {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my ($param) = @_;
my $user = {%{DEFAULT_USER()}};
$user->{groups} = [Bugzilla::Group->get_all];
$user->{bless_groups} = [Bugzilla::Group->get_all];
bless $user, $class;
return $user;
}
sub _update_groups {
my $self = shift;
my $group_changes = shift;
my $changes = shift;
my $dbh = Bugzilla->dbh;
# Update group settings.
my $sth_add_mapping = $dbh->prepare(qq{INSERT INTO user_group_map (
user_id, group_id, isbless, grant_type
) VALUES (
?, ?, ?, ?
)
}
);
my $sth_remove_mapping = $dbh->prepare(qq{DELETE FROM user_group_map
WHERE user_id = ?
AND group_id = ?
AND isbless = ?
AND grant_type = ?
}
);
foreach my $is_bless (keys %$group_changes) {
my ($removed, $added) = @{$group_changes->{$is_bless}};
foreach my $group (@$removed) {
$sth_remove_mapping->execute($self->id, $group->id, $is_bless, GRANT_DIRECT);
}
foreach my $group (@$added) {
$sth_add_mapping->execute($self->id, $group->id, $is_bless, GRANT_DIRECT);
}
if (!$is_bless) {
my $query = qq{
INSERT INTO profiles_activity
(userid, who, profiles_when, fieldid, oldvalue, newvalue)
VALUES ( ?, ?, now(), ?, ?, ?)
};
$dbh->do(
$query, undef, $self->id, Bugzilla->user->id,
get_field_id('bug_group'),
join(', ', map { $_->name } @$removed),
join(', ', map { $_->name } @$added)
);
}
else {
# XXX: should create profiles_activity entries for blesser changes.
}
Bugzilla->memcached->clear_config({key => 'user_groups.' . $self->id});
my $type = $is_bless ? 'bless_groups' : 'groups';
$changes->{$type} = [[map { $_->name } @$removed], [map { $_->name } @$added],];
}
}
sub update {
my $self = shift;
my $options = shift;
my $group_changes = delete $self->{_group_changes};
my $changes = $self->SUPER::update(@_);
my $dbh = Bugzilla->dbh;
$self->_update_groups($group_changes, $changes);
if (exists $changes->{login_name}) {
# Delete all the tokens related to the userid
$dbh->do('DELETE FROM tokens WHERE userid = ?', undef, $self->id)
unless $options->{keep_tokens};
# And rederive regex groups
$self->derive_regexp_groups();
}
# Logout the user if necessary.
Bugzilla->logout_user($self)
if (
!$options->{keep_session}
&& ( exists $changes->{login_name}
|| exists $changes->{disabledtext}
|| exists $changes->{cryptpassword})
);
# XXX Can update profiles_activity here as soon as it understands
# field names like login_name.
return $changes;
}
################################################################################
# Validators
################################################################################
sub _check_disable_mail { return $_[1] ? 1 : 0; }
sub _check_disabledtext { return trim($_[1]) || ''; }
# Check whether the extern_id is unique.
sub _check_extern_id {
my ($invocant, $extern_id) = @_;
$extern_id = trim($extern_id);
return undef unless defined($extern_id) && $extern_id ne "";
if (!ref($invocant) || $invocant->extern_id ne $extern_id) {
my $existing_login = $invocant->new({extern_id => $extern_id});
if ($existing_login) {
ThrowUserError('extern_id_exists',
{extern_id => $extern_id, existing_login_name => $existing_login->login});
}
}
return $extern_id;
}
# This is public since createaccount.cgi needs to use it before issuing
# a token for account creation.
sub check_login_name {
my ($invocant, $name) = @_;
$name = trim($name);
$name || ThrowUserError('user_login_required');
check_email_syntax($name);
# Check the name if it's a new user, or if we're changing the name.
if (!ref($invocant) || lc($invocant->login) ne lc($name)) {
my @params = ($name);
push(@params, $invocant->login) if ref($invocant);
is_available_username(@params)
|| ThrowUserError('account_exists', {email => $name});
}
return $name;
}
sub _check_password {
my ($self, $pass) = @_;
# If the password is '*', do not encrypt it or validate it further--we
# are creating a user who should not be able to log in using DB
# authentication.
return $pass if $pass eq '*';
validate_password($pass);
my $cryptpassword = bz_crypt($pass);
return $cryptpassword;
}
sub _check_realname { return trim($_[1]) || ''; }
sub _check_is_enabled {
my ($invocant, $is_enabled, undef, $params) = @_;
# is_enabled is set automatically on creation depending on whether
# disabledtext is empty (enabled) or not empty (disabled).
# When updating the user, is_enabled is set by calling set_disabledtext().
# Any value passed into this validator is ignored.
my $disabledtext
= ref($invocant) ? $invocant->disabledtext : $params->{disabledtext};
return $disabledtext ? 0 : 1;
}
################################################################################
# Mutators
################################################################################
sub set_disable_mail { $_[0]->set('disable_mail', $_[1]); }
sub set_email_enabled { $_[0]->set('disable_mail', !$_[1]); }
sub set_extern_id { $_[0]->set('extern_id', $_[1]); }
sub set_login {
my ($self, $login) = @_;
$self->set('login_name', $login);
delete $self->{identity};
delete $self->{nick};
}
sub set_name {
my ($self, $name) = @_;
$self->set('realname', $name);
delete $self->{identity};
}
sub set_password { $_[0]->set('cryptpassword', $_[1]); }
sub set_disabledtext {
$_[0]->set('disabledtext', $_[1]);
$_[0]->set('is_enabled', $_[1] ? 0 : 1);
}
sub set_groups {
my $self = shift;
$self->_set_groups(GROUP_MEMBERSHIP, @_);
}
sub set_bless_groups {
my $self = shift;
# The person making the change needs to be in the editusers group
Bugzilla->user->in_group('editusers') || ThrowUserError(
"auth_failure",
{
group => "editusers",
reason => "cant_bless",
action => "edit",
object => "users"
}
);
$self->_set_groups(GROUP_BLESS, @_);
}
sub _set_groups {
my $self = shift;
my $is_bless = shift;
my $changes = shift;
my $dbh = Bugzilla->dbh;
# The person making the change is $user, $self is the person being changed
my $user = Bugzilla->user;
# Input is a hash of arrays. Key is 'set', 'add' or 'remove'. The array
# is a list of group ids and/or names.
# First turn the arrays into group objects.
$changes = $self->_set_groups_to_object($changes);
# Get a list of the groups the user currently is a member of
my $ids = $dbh->selectcol_arrayref(
q{SELECT DISTINCT group_id
FROM user_group_map
WHERE user_id = ? AND isbless = ? AND grant_type = ?}, undef, $self->id,
$is_bless, GRANT_DIRECT
);
my $current_groups = Bugzilla::Group->new_from_list($ids);
my $new_groups = dclone($current_groups);
# Record the changes
if (exists $changes->{set}) {
$new_groups = $changes->{set};
# We need to check the user has bless rights on the existing groups
# If they don't, then we need to add them back to new_groups
foreach my $group (@$current_groups) {
if (!$user->can_bless($group->id)) {
push @$new_groups, $group unless grep { $_->id eq $group->id } @$new_groups;
}
}
}
else {
foreach my $group (@{$changes->{remove} // []}) {
@$new_groups = grep { $_->id ne $group->id } @$new_groups;
}
foreach my $group (@{$changes->{add} // []}) {
push @$new_groups, $group unless grep { $_->id eq $group->id } @$new_groups;
}
}
# Stash the changes, so self->update can actually make them
my @diffs = diff_arrays($current_groups, $new_groups, 'id');
if (scalar(@{$diffs[0]}) || scalar(@{$diffs[1]})) {
$self->{_group_changes}{$is_bless} = \@diffs;
}
}
sub _set_groups_to_object {
my $self = shift;
my $changes = shift;
my $user = Bugzilla->user;
foreach my $key (keys %$changes) {
# Check we were given an array
unless (ref($changes->{$key}) eq 'ARRAY') {
ThrowCodeError('param_invalid', {param => $changes->{$key}, function => $key});
}
# Go through the array, and turn items into group objects
my @groups = ();
foreach my $value (@{$changes->{$key}}) {
my $type = $value =~ /^\d+$/ ? 'id' : 'name';
my $group = Bugzilla::Group->new({$type => $value});
if (!$group || !$user->can_bless($group->id)) {
ThrowUserError('auth_failure',
{group => $value, reason => 'cant_bless', action => 'edit', object => 'users'});
}
push @groups, $group;
}
$changes->{$key} = \@groups;
}
return $changes;
}
sub update_last_seen_date {
my $self = shift;
return unless $self->id;
my $dbh = Bugzilla->dbh;
my $date = $dbh->selectrow_array(
'SELECT ' . $dbh->sql_date_format('NOW()', '%Y-%m-%d'));
if (!$self->last_seen_date or $date ne $self->last_seen_date) {
$self->{last_seen_date} = $date;
# We don't use the normal update() routine here as we only
# want to update the last_seen_date column, not any other
# pending changes
$dbh->do("UPDATE profiles SET last_seen_date = ? WHERE userid = ?",
undef, $date, $self->id);
Bugzilla->memcached->clear({table => 'profiles', id => $self->id});
}
}
################################################################################
# Methods
################################################################################
# Accessors for user attributes
sub name { $_[0]->{realname}; }
sub login { $_[0]->{login_name}; }
sub extern_id { $_[0]->{extern_id}; }
sub email { $_[0]->login . Bugzilla->params->{'emailsuffix'}; }
sub disabledtext { $_[0]->{'disabledtext'}; }
sub is_enabled { $_[0]->{'is_enabled'} ? 1 : 0; }
sub showmybugslink { $_[0]->{showmybugslink}; }
sub email_disabled { $_[0]->{disable_mail}; }
sub email_enabled { !($_[0]->{disable_mail}); }
sub last_seen_date { $_[0]->{last_seen_date}; }
sub cryptpassword {
my $self = shift;
# We don't store it because we never want it in the object (we
# don't want to accidentally dump even the hash somewhere).
my ($pw)
= Bugzilla->dbh->selectrow_array(
'SELECT cryptpassword FROM profiles WHERE userid = ?',
undef, $self->id);
return $pw;
}
sub set_authorizer {
my ($self, $authorizer) = @_;
$self->{authorizer} = $authorizer;
}
sub authorizer {
my ($self) = @_;
if (!$self->{authorizer}) {
require Bugzilla::Auth;
$self->{authorizer} = new Bugzilla::Auth();
}
return $self->{authorizer};
}
# Generate a string to identify the user by name + login if the user
# has a name or by login only if they don't.
sub identity {
my $self = shift;
return "" unless $self->id;
if (!defined $self->{identity}) {
$self->{identity}
= $self->name ? $self->name . " <" . $self->login . ">" : $self->login;
}
return $self->{identity};
}
sub nick {
my $self = shift;
return "" unless $self->id;
if (!defined $self->{nick}) {
$self->{nick} = (split(/@/, $self->login, 2))[0];
}
return $self->{nick};
}
sub queries {
my $self = shift;
return $self->{queries} if defined $self->{queries};
return [] unless $self->id;
my $dbh = Bugzilla->dbh;
my $query_ids
= $dbh->selectcol_arrayref('SELECT id FROM namedqueries WHERE userid = ?',
undef, $self->id);
require Bugzilla::Search::Saved;
$self->{queries} = Bugzilla::Search::Saved->new_from_list($query_ids);
# We preload link_in_footer from here as this information is always requested.
# This only works if the user object represents the current logged in user.
Bugzilla::Search::Saved::preload($self->{queries})
if $self->id == Bugzilla->user->id;
return $self->{queries};
}
sub queries_subscribed {
my $self = shift;
return $self->{queries_subscribed} if defined $self->{queries_subscribed};
return [] unless $self->id;
# Exclude the user's own queries.
my @my_query_ids = map($_->id, @{$self->queries});
my $query_id_string = join(',', @my_query_ids) || '-1';
# Only show subscriptions that we can still actually see. If a
# user changes the shared group of a query, our subscription
# will remain but we won't have access to the query anymore.
my $subscribed_query_ids = Bugzilla->dbh->selectcol_arrayref(
"SELECT lif.namedquery_id
FROM namedqueries_link_in_footer lif
INNER JOIN namedquery_group_map ngm
ON ngm.namedquery_id = lif.namedquery_id
WHERE lif.user_id = ?
AND lif.namedquery_id NOT IN ($query_id_string)
AND " . $self->groups_in_sql, undef, $self->id
);
require Bugzilla::Search::Saved;
$self->{queries_subscribed}
= Bugzilla::Search::Saved->new_from_list($subscribed_query_ids);
return $self->{queries_subscribed};
}
sub queries_available {
my $self = shift;
return $self->{queries_available} if defined $self->{queries_available};
return [] unless $self->id;
# Exclude the user's own queries.
my @my_query_ids = map($_->id, @{$self->queries});
my $query_id_string = join(',', @my_query_ids) || '-1';
my $avail_query_ids = Bugzilla->dbh->selectcol_arrayref(
'SELECT namedquery_id FROM namedquery_group_map
WHERE ' . $self->groups_in_sql . "
AND namedquery_id NOT IN ($query_id_string)"
);
require Bugzilla::Search::Saved;
$self->{queries_available}
= Bugzilla::Search::Saved->new_from_list($avail_query_ids);
return $self->{queries_available};
}
sub tags {
my $self = shift;
my $dbh = Bugzilla->dbh;
if (!defined $self->{tags}) {
# We must use LEFT JOIN instead of INNER JOIN as we may be
# in the process of inserting a new tag to some bugs,
# in which case there are no bugs with this tag yet.
$self->{tags} = $dbh->selectall_hashref(
'SELECT name, id, COUNT(bug_id) AS bug_count
FROM tag
LEFT JOIN bug_tag ON bug_tag.tag_id = tag.id
WHERE user_id = ? ' . $dbh->sql_group_by('id', 'name'), 'name', undef,
$self->id
);
}
return $self->{tags};
}
sub bugs_ignored {
my ($self) = @_;
my $dbh = Bugzilla->dbh;
if (!defined $self->{'bugs_ignored'}) {
$self->{'bugs_ignored'} = $dbh->selectall_arrayref(
'SELECT bugs.bug_id AS id,
bugs.bug_status AS status,
bugs.short_desc AS summary
FROM bugs
INNER JOIN email_bug_ignore
ON bugs.bug_id = email_bug_ignore.bug_id
WHERE user_id = ?', {Slice => {}}, $self->id
);
# Go ahead and load these into the visible bugs cache
# to speed up can_see_bug checks later
$self->visible_bugs([map { $_->{'id'} } @{$self->{'bugs_ignored'}}]);
}
return $self->{'bugs_ignored'};
}
sub is_bug_ignored {
my ($self, $bug_id) = @_;
return (grep { $_->{'id'} == $bug_id } @{$self->bugs_ignored}) ? 1 : 0;
}
##########################
# Saved Recent Bug Lists #
##########################
sub recent_searches {
my $self = shift;
$self->{recent_searches}
||= Bugzilla::Search::Recent->match({user_id => $self->id});
return $self->{recent_searches};
}
sub recent_search_containing {
my ($self, $bug_id) = @_;
my $searches = $self->recent_searches;
foreach my $search (@$searches) {
return $search if grep($_ == $bug_id, @{$search->bug_list});
}
return undef;
}
sub recent_search_for {
my ($self, $bug) = @_;
my $params = Bugzilla->input_params;
my $cgi = Bugzilla->cgi;
if ($self->id) {
# First see if there's a list_id parameter in the query string.
my $list_id = $params->{list_id};
if (!$list_id) {
# If not, check for "list_id" in the query string of the referer.
my $referer = $cgi->referer;
if ($referer) {
my $uri = URI->new($referer);
if ($uri->path =~ /buglist\.cgi$/) {
$list_id = $uri->query_param('list_id') || $uri->query_param('regetlastlist');
}
}
}
if ($list_id && $list_id ne 'cookie') {
# If we got a bad list_id (either some other user's or an expired
# one) don't crash, just don't return that list.
my $search = Bugzilla::Search::Recent->check_quietly({id => $list_id});
return $search if $search;
}
# If there's no list_id, see if the current bug's id is contained
# in any of the user's saved lists.
my $search = $self->recent_search_containing($bug->id);
return $search if $search;
}
# Finally (or always, if we're logged out), if there's a BUGLIST cookie
# and the selected bug is in the list, then return the cookie as a fake
# Search::Recent object.
if (my $list = $cgi->cookie('BUGLIST')) {
# Also split on colons, which was used as a separator in old cookies.
my @bug_ids = split(/[:-]/, $list);
if (grep { $_ == $bug->id } @bug_ids) {
my $search = Bugzilla::Search::Recent->new_from_cookie(\@bug_ids);
return $search;
}
}
return undef;
}
sub save_last_search {
my ($self, $params) = @_;
my ($bug_ids, $order, $vars, $list_id) = @$params{qw(bugs order vars list_id)};
my $cgi = Bugzilla->cgi;
if ($order) {
$cgi->send_cookie(
-name => 'LASTORDER',
-value => $order,
-expires => 'Fri, 01-Jan-2038 00:00:00 GMT'
);
}
return if !@$bug_ids;
my $search;
if ($self->id) {
on_main_db {
if ($list_id) {
$search = Bugzilla::Search::Recent->check_quietly({id => $list_id});
}
if ($search) {
if (join(',', @{$search->bug_list}) ne join(',', @$bug_ids)) {
$search->set_bug_list($bug_ids);
}
if (!$search->list_order || $order ne $search->list_order) {
$search->set_list_order($order);
}
$search->update();
}
else {
# If we already have an existing search with a totally
# identical bug list, then don't create a new one. This
# prevents people from writing over their whole
# recent-search list by just refreshing a saved search
# (which doesn't have list_id in the header) over and over.
my $list_string = join(',', @$bug_ids);
my $existing_search = Bugzilla::Search::Recent->match(
{user_id => $self->id, bug_list => $list_string});
if (!scalar(@$existing_search)) {
$search
= Bugzilla::Search::Recent->create({
user_id => $self->id, bug_list => $bug_ids, list_order => $order
});
}
else {
$search = $existing_search->[0];
}
}
};
delete $self->{recent_searches};
}
# Logged-out users use a cookie to store a single last search. We don't
# override that cookie with the logged-in user's latest search, because
# if they did one search while logged out and another while logged in,
# they may still want to navigate through the search they made while
# logged out.
else {
my $bug_list = join('-', @$bug_ids);
if (length($bug_list) < 4000) {
$cgi->send_cookie(
-name => 'BUGLIST',
-value => $bug_list,
-expires => 'Fri, 01-Jan-2038 00:00:00 GMT'
);
}
else {
$cgi->remove_cookie('BUGLIST');
$vars->{'toolong'} = 1;
}
}
return $search;
}
sub reports {
my $self = shift;
return $self->{reports} if defined $self->{reports};
return [] unless $self->id;
my $dbh = Bugzilla->dbh;
my $report_ids
= $dbh->selectcol_arrayref('SELECT id FROM reports WHERE user_id = ?',
undef, $self->id);
require Bugzilla::Report;
$self->{reports} = Bugzilla::Report->new_from_list($report_ids);
return $self->{reports};
}
sub flush_reports_cache {
my $self = shift;
delete $self->{reports};
}
sub settings {
my ($self) = @_;
return $self->{'settings'} if (defined $self->{'settings'});
# IF the user is logged in
# THEN get the user's settings
# ELSE get default settings
if ($self->id) {
$self->{'settings'} = get_all_settings($self->id);
}
else {
$self->{'settings'} = get_defaults();
}
return $self->{'settings'};
}
sub setting {
my ($self, $name) = @_;
return $self->settings->{$name}->{'value'};
}
sub timezone {
my $self = shift;
if (!defined $self->{timezone}) {
my $tz = $self->setting('timezone');
if ($tz eq 'local') {
# The user wants the local timezone of the server.
$self->{timezone} = Bugzilla->local_timezone;
}
else {
$self->{timezone} = DateTime::TimeZone->new(name => $tz);
}
}
return $self->{timezone};
}
sub flush_queries_cache {
my $self = shift;
delete $self->{queries};
delete $self->{queries_subscribed};
delete $self->{queries_available};
}
sub groups {
my $self = shift;
return $self->{groups} if defined $self->{groups};
return [] unless $self->id;
my $user_groups_key = "user_groups." . $self->id;
my $groups = Bugzilla->memcached->get_config({key => $user_groups_key});
if (!$groups) {
my $dbh = Bugzilla->dbh;
my $groups_to_check = $dbh->selectcol_arrayref(
"SELECT DISTINCT group_id
FROM user_group_map
WHERE user_id = ? AND isbless = 0", undef, $self->id
);
my $grant_type_key = 'group_grant_type_' . GROUP_MEMBERSHIP;
my $membership_rows
= Bugzilla->memcached->get_config({key => $grant_type_key,});
if (!$membership_rows) {
$membership_rows = $dbh->selectall_arrayref(
"SELECT DISTINCT grantor_id, member_id
FROM group_group_map
WHERE grant_type = " . GROUP_MEMBERSHIP
);
Bugzilla->memcached->set_config({
key => $grant_type_key, data => $membership_rows,
});
}
my %group_membership;
foreach my $row (@$membership_rows) {
my ($grantor_id, $member_id) = @$row;
push(@{$group_membership{$member_id}}, $grantor_id);
}
# Let's walk the groups hierarchy tree (using FIFO)
# On the first iteration it's pre-filled with direct groups
# membership. Later on, each group can add its own members into the
# FIFO. Circular dependencies are eliminated by checking
# $checked_groups{$member_id} hash values.
# As a result, %groups will have all the groups we are the member of.
my %checked_groups;
my %groups;
while (scalar(@$groups_to_check) > 0) {
# Pop the head group from FIFO
my $member_id = shift @$groups_to_check;
# Skip the group if we have already checked it
if (!$checked_groups{$member_id}) {
# Mark group as checked
$checked_groups{$member_id} = 1;
# Add all its members to the FIFO check list
# %group_membership contains arrays of group members
# for all groups. Accessible by group number.
my $members = $group_membership{$member_id};
my @new_to_check = grep(!$checked_groups{$_}, @$members);
push(@$groups_to_check, @new_to_check);
$groups{$member_id} = 1;
}
}
$groups = [keys %groups];
Bugzilla->memcached->set_config({key => $user_groups_key, data => $groups,});
}
$self->{groups} = Bugzilla::Group->new_from_list($groups);
return $self->{groups};
}
sub last_visited {
my ($self, $ids) = @_;
return Bugzilla::BugUserLastVisit->match({
user_id => $self->id, $ids ? (bug_id => $ids) : ()
});
}
sub is_involved_in_bug {
my ($self, $bug) = @_;
my $user_id = $self->id;
my $user_login = $self->login;
return unless $user_id;
return 1 if $user_id == $bug->assigned_to->id;
return 1 if $user_id == $bug->reporter->id;
if (Bugzilla->params->{'useqacontact'} and $bug->qa_contact) {
return 1 if $user_id == $bug->qa_contact->id;
}
return any { $user_login eq $_ } @{$bug->cc};
}
# It turns out that calling ->id on objects a few hundred thousand
# times is pretty slow. (It showed up as a significant time contributor
# when profiling xt/search.t.) So we cache the group ids separately from
# groups for functions that need the group ids.
sub _group_ids {
my ($self) = @_;
$self->{group_ids} ||= [map { $_->id } @{$self->groups}];
return $self->{group_ids};
}
sub groups_as_string {
my $self = shift;
my $ids = $self->_group_ids;
return scalar(@$ids) ? join(',', @$ids) : '-1';
}
sub groups_in_sql {
my ($self, $field) = @_;
$field ||= 'group_id';
my $ids = $self->_group_ids;
$ids = [-1] if !scalar @$ids;
return Bugzilla->dbh->sql_in($field, $ids);
}
sub bless_groups {
my $self = shift;
return $self->{'bless_groups'} if defined $self->{'bless_groups'};
return [] unless $self->id;
if ($self->in_group('editusers')) {
# Users having editusers permissions may bless all groups.
$self->{'bless_groups'} = [Bugzilla::Group->get_all];
return $self->{'bless_groups'};
}
if (Bugzilla->params->{usevisibilitygroups}
&& !@{$self->visible_groups_inherited})
{