blib/lib/BZ/Client/User.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 134 CC 18 SCAR 20.4
CC
18
Cov
0%
update
20.0
get
12.0
offer_account_by_email
6.0
CRAP 7.7
1 #!/bin/false
2 # PODNAME: BZ::Client::User
3 # ABSTRACT: Creates and edits user accounts in Bugzilla
4  
5 use strict;
6 use warnings 'all';
7  
8 package BZ::Client::User;
9 $BZ::Client::User::VERSION = '4.9';
10  
11 use parent qw( BZ::Client::API );
12  
13 # See https://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/User.html
14  
15 ## functions
16  
17 sub offer_account_by_email {
18 my($class, $client, $params) = @_;
19 unless (ref $params) {
20 $params = { email => $params }
21 }
22 $client->log('debug', $class . '::offer_account_by_email: Inviting');
23 return $class->api_call($client, 'User.offer_account_by_email', $params, { empty_response_ok => 1});
24 }
25  
26 sub get {
27 my($class, $client, $params) = @_;
28 $client->log('debug', $class . '::get: Asking.');
29 if ($params->{'include_disabled'}) {
30 $params->{'include_disabled'} = BZ::Client::XMLRPC::boolean::TRUE()
31 }
32 else {
33 $params->{'include_disabled'} = BZ::Client::XMLRPC::boolean::FALSE()
34 }
35 my $users = $class->_returns_array($client, 'User.get', $params, 'users');
36 my @result;
37 for my $user (@$users) {
38 push(@result, $class->new(%{$user}));
39 }
40 $client->log('debug', $class . '::get: Got ' . scalar(@result));
41 return wantarray ? @result : \@result
42 }
43  
44 ## methods
45  
46 sub create {
47 my($class, $client, $params) = @_;
48 return $class->_create($client, 'User.create', $params);
49 }
50  
51 sub update {
52 my($class, $client, $params) = @_;
53 $client->log('debug', $class . '::update: Updating.');
54 if (defined $params->{'email_enabled'}) {
55 if ($params->{'email_enabled'}) {
56 $params->{'email_enabled'} = BZ::Client::XMLRPC::boolean::TRUE()
57 }
58 else {
59 $params->{'email_enabled'} = BZ::Client::XMLRPC::boolean::FALSE()
60 }
61 }
62 my $users = $class->_returns_array($client, 'User.update', $params, 'users');
63 my @result;
64 for my $user (@$users) {
65 push(@result, $class->new(%{$user}));
66 }
67 $client->log('debug', $class . '::update: Got ' . scalar(@result));
68 return wantarray ? @result : \@result
69 }
70  
71 # Internal helper for accessors
72 sub _accessor {
73 my ($self, $field, $val) = @_;
74 if (@_ > 2) {
75 $self->{$field} = $val;
76 } else {
77 return $self->{$field};
78 }
79 }
80  
81 # Accessors for common user fields
82 sub id {
83 my $self = shift;
84 return $self->_accessor('id', @_ ? $_[0] : ());
85 }
86  
87 sub name {
88 my $self = shift;
89 return $self->_accessor('name', @_ ? $_[0] : ());
90 }
91  
92 sub email {
93 my $self = shift;
94 return $self->_accessor('email', @_ ? $_[0] : ());
95 }
96  
97 sub real_name {
98 my $self = shift;
99 return $self->_accessor('real_name', @_ ? $_[0] : ());
100 }
101  
102 sub login {
103 my $self = shift;
104 return $self->_accessor('login', @_ ? $_[0] : ());
105 }
106  
107 sub can_login {
108 my $self = shift;
109 return $self->_accessor('can_login', @_ ? $_[0] : ());
110 }
111  
112 sub email_enabled {
113 my $self = shift;
114 return $self->_accessor('email_enabled', @_ ? $_[0] : ());
115 }
116  
117 sub groups {
118 my $self = shift;
119 return $self->_accessor('groups', @_ ? $_[0] : ());
120 }
121  
122 sub saved_searches {
123 my $self = shift;
124 return $self->_accessor('saved_searches', @_ ? $_[0] : ());
125 }
126  
127 sub last_visit {
128 my $self = shift;
129 return $self->_accessor('last_visit', @_ ? $_[0] : ());
130 }
131  
132 1;
133  
134 __END__
135  
136 =pod
137  
138 =encoding UTF-8
139  
140 =head1 NAME
141  
142 BZ::Client::User - Creates and edits user accounts in Bugzilla
143  
144 =head1 VERSION
145  
146 version 4.9
147  
148 =head1 SYNOPSIS
149  
150 This class provides methods for accessing information about the Bugzilla
151 servers installation.
152  
153 my $client = BZ::Client->new( url => $url,
154 user => $user,
155 password => $password );
156  
157 my $ok = BZ::Client::User->offer_account_by_email( $client, 'email@address' );
158 my $users = BZ::Client::User->get( $client, \%params );
159 my $id = BZ::Client::User->create( $client, \%params );
160 my $users = BZ::Client::User->update( $client, \%params );
161  
162 =head1 CLASS METHODS
163  
164 This section lists the class methods, which are available in this module.
165  
166 =head2 offer_account_by_email
167  
168 my $ok = BZ::Client::User->offer_account_by_email( $client, 'email@address' );
169 my $ok = BZ::Client::User->offer_account_by_email( $client, \%params );
170  
171 Sends an email to the user, offering to create an account. The user will have to click on a URL in the email, and choose their password and real name.
172  
173 This is the recommended way to create a Bugzilla account.
174  
175 =head3 Parameters
176  
177 =over 4
178  
179 =item email
180  
181 I<email> (string) The email address to send the offer to.
182  
183 Note: email can be provided as the single option as a scalar as shown above.
184  
185 =back
186  
187 =head3 Returns
188  
189 nothing if successful
190  
191 =head3 Errors
192  
193 =over 4
194  
195 =item 50 - Email parameter missing
196  
197 The email parameter was not provided.
198  
199 =item 500 - Account Already Exists
200  
201 An account with that email address already exists in Bugzilla.
202  
203 =item 501 - Illegal Email Address
204  
205 This Bugzilla does not allow you to create accounts with the format of email address you specified. Account creation may be entirely disabled.
206  
207 =back
208  
209 =head2 get
210  
211 my $users = BZ::Client::User->get( $client, \%params );
212  
213 Gets information about user accounts in Bugzilla.
214  
215 =head3 History
216  
217 Added in Bugzilla 3.4
218  
219 =head3 Parameters
220  
221 Note: At least one of L</ids>, L</names>, or L</match> must be specified.
222  
223 Note: Users will only appear once in the returned list, even if that user is matched by more than one search argument.
224  
225 In addition to the parameters below, this method also accepts the standard L<BZ::Client::Bug/include_fields> and L<BZ::Client::Bug/exclude_fields> arguments.
226  
227 =over 4
228  
229 =item ids (array)
230  
231 An array of integers, representing user ID's.
232  
233 Logged-out users cannot pass this parameter to this function. If they try, they will get an error. Logged-in users will get an error if they specify the ID of a user they cannot see.
234  
235 =item names (array)
236  
237 An array of login names (strings).
238  
239 =item match (array)
240  
241 An array of strings. This works just like "user matching" in Bugzilla itself. Users will be returned whose real name or login name contains any one of the specified strings. Users that you cannot see will not be included in the returned list.
242  
243 Most installations have a limit on how many matches are returned for each string, which defaults to C<1000> but can be changed by the Bugzilla administrator.
244  
245 Logged-out users cannot use this argument, and an error will be thrown if they try. (This is to make it harder for spammers to harvest email addresses from Bugzilla, and also to enforce the user visibility restrictions that are implemented on some Bugzillas.)
246  
247 =item limit (int)
248  
249 Limit the number of users matched by the L</match> parameter. If value is greater than the system limit, the system limit will be used. This parameter is only used when user matching using the L</match> parameter is being performed.
250  
251 =item group_ids (array)
252  
253 I<group_ids> is an array of numeric ids for groups that a user can be in.
254 If this is specified, it limits the return value to users who are in any of the groups specified.
255  
256 Added in Bugzilla 4.0
257  
258 =item groups (array)
259  
260 I<groups> is an array of names of groups that a user can be in.
261 If this is specified, it limits the return value to users who are in any of the groups specified.
262  
263 Added in Bugzilla 4.0
264  
265 =item include_disabled (boolean)
266  
267 By default, when using the L</match> parameter, disabled users are excluded from the returned results unless their full username is identical to the match string. Setting L</include_disabled> to C<true> will include disabled users in the returned results even if their username doesn't fully match the input string.
268  
269 Added in Bugzilla 4.0, default behaviour for L</match> was then changed to exclude disabled users.
270  
271 =back
272  
273 =head3 Returns
274  
275 A hash containing one item, C<users>, that is an array of hashes. Each hash describes a user, and has the following items:
276  
277 =over 4
278  
279 =item id
280  
281 I<id> (int) The unique integer ID that Bugzilla uses to represent this user. Even if the user's login name changes, this will not change.
282  
283 =item real_name
284  
285 I<real_name> (string) The actual name of the user. May be blank.
286  
287 =item email
288  
289 I<email> (string) The email address of the user.
290  
291 =item name
292  
293 I<name> (string) The login name of the user. Note that in some situations this is different than their email.
294  
295 =item can_login
296  
297 I<can_login> (boolean) A boolean value to indicate if the user can login into bugzilla.
298  
299 =item email_enabled
300  
301 I<email_enabled> (boolean) A boolean value to indicate if bug-related mail will be sent to the user or not.
302  
303 =item login_denied_text
304  
305 I<login_denied_text> (string) A text field that holds the reason for disabling a user from logging into bugzilla, if empty then the user account is enabled. Otherwise, it is disabled/closed.
306  
307 =item groups
308  
309 I<groups> (array) An array of group hashes the user is a member of. If the currently logged in user is querying his own account or is a member of the C<editusers> group, the array will contain all the groups that the user is a member of. Otherwise, the array will only contain groups that the logged in user can bless. Each hash describes the group and contains the following items:
310  
311 Added in Bugzilla 4.4
312  
313 =over 4
314  
315 =item id
316  
317 I<id> (int) The group ID
318  
319 =item name
320  
321 I<name> (string) The name of the group
322  
323 =item description
324  
325 I<description> (string) The description for the group
326  
327 =back
328  
329 =item saved_searches
330  
331 I<saved_searches> (array) An array of hashes, each of which represents a user's saved search and has the following keys:
332  
333 Added in Bugzilla 4.4
334  
335 =over 4
336  
337 =item id
338  
339 I<id> (int) An integer ID uniquely identifying the saved search.
340  
341 =item name
342  
343 I<name> (string) The name of the saved search.
344  
345 =item query
346  
347 I<query> (string) The CGI parameters for the saved search.
348  
349 =back
350  
351 =item saved_reports
352  
353 I<saved_reports> (array) An array of hashes, each of which represents a user's saved report and has the following keys:
354  
355 Added in Bugzilla 4.4
356  
357 =over 4
358  
359 =item id
360  
361 I<id> (int) An integer ID uniquely identifying the saved report.
362  
363 =item name
364  
365 I<name> (string) The name of the saved report.
366  
367 =item query
368  
369 I<query> (string) The CGI parameters for the saved report.
370  
371 =back
372  
373 Note: If you are not logged in to Bugzilla when you call this function, you will only be returned the C<id>, C<name>, and C<real_name> items.
374 If you are logged in and not in C<editusers> group, you will only be returned the C<id>, C<name>, C<real_name>, C<email>, C<can_login>, and C<groups> items.
375 The C<groups> returned are filtered based on your permission to bless each group.
376 The L</saved_searches> and L</saved_reports> items are only returned if you are querying your own account, even if you are in the C<editusers> group.
377  
378 =back
379  
380 =head3 Errors
381  
382 =over 4
383  
384 =item 51 - Bad Login Name or Group ID
385  
386 You passed an invalid login name in the L</names> array or a bad group ID in the L</group_ids> argument.
387  
388 =item 52 - Invalid Parameter
389  
390 The value used must be an integer greater then zero.
391  
392 =item 304 - Authorization Required
393  
394 You are logged in, but you are not authorized to see one of the users you wanted to get information about by user ID.
395  
396 =item 505 - User Access By Id or User-Matching Denied
397  
398 Logged-out users cannot use the C<ids> or C<match> arguments to this function.
399  
400 =item 804 - Invalid Group Name
401  
402 You passed a group name in the C<groups> argument which either does not exist or you do not belong to it.
403  
404 Added in Bugzilla 4.0.9 and 4.2.4, when it also became illegal to pass a group name you don't belong to.
405  
406 =back
407  
408 =head2 new
409  
410 my $user = BZ::Client::User->new( id => $id );
411  
412 Creates a new instance with the given ID.
413  
414 =head2 create
415  
416 my $id = BZ::Client::User->create( $client, \%params );
417  
418 Creates a user account directly in Bugzilla, password and all.
419 Instead of this, you should use L</offer_account_by_email> when possible, because that makes sure that the email address specified can actually receive an email.
420 This function does not check that.
421  
422 You must be logged in and have the C<editusers> privilege in order to call this function.
423  
424 Params:
425  
426 =over 4
427  
428 =item email
429  
430 I<email> (string) - The email address for the new user.
431  
432 B<Required>.
433  
434 =item full_name
435  
436 I<full_name> (string) The user's full name. Will be set to empty if not specified.
437  
438 =item password
439  
440 I<password> (string) The password for the new user account, in plain text. It will be stripped of leading and trailing whitespace. If blank or not specified, the newly created account will exist in Bugzilla, but will not be allowed to log in using DB authentication until a password is set either by the user (through resetting their password) or by the administrator.
441  
442 =back
443  
444 =head3 Returns
445  
446 The numeric ID of the user that was created.
447  
448 =head3 Errors
449  
450 The same as L</offer_account_by_email>. If a password is specified, the function may also throw:
451  
452 =over 4
453  
454 =item 502 - Password Too Short
455  
456 The password specified is too short. (Usually, this means the password is under three characters.)
457  
458 =item 503 - Password Too Long
459  
460 Removed in Bugzilla 3.6
461  
462 =back
463  
464 =head2 update
465  
466 my $users = BZ::Client::User->update( $client, \%params );
467  
468 Updates user accounts in Bugzilla.
469  
470 =head3 Parameters
471  
472 =over 4
473  
474 =item ids
475  
476 I<array> Contains ID's of user to update.
477  
478 =item names
479  
480 I<array> Contains email/login of user to update.
481  
482 =item full_name
483  
484 I<full_name> (string) The new name of the user.
485  
486 =item email
487  
488 I<email> (string) The email of the user. Note that email used to login to bugzilla. Also note that you can only update one user at a time when changing the login name / email. (An error will be thrown if you try to update this field for multiple users at once.)
489  
490 =item password
491  
492 I<password> (string) The password of the user.
493  
494 =item email_enabled
495  
496 I<email_enabled> (boolean) A boolean value to enable/disable sending bug-related mail to the user.
497  
498 =item login_denied_text
499  
500 I<login_denied_text> (string) A text field that holds the reason for disabling a user from logging into Bugzilla, if empty then the user account is enabled otherwise it is disabled/closed.
501  
502 =back
503  
504 =head3 Returns
505  
506 An array of hashes with the following fields:
507  
508 =over 4
509  
510 =item id
511  
512 I<id> (int) The ID of the user that was updated.
513  
514 =item changes
515  
516 I<changes> (hash) The changes that were actually done on this user. The keys are the names of the fields that were changed, and the values are a hash with two keys:
517  
518 =item added
519  
520 I<added> (string) The values that were added to this field, possibly a comma-and-space-separated list if multiple values were added.
521  
522 =item removed
523  
524 I<removed> (string) The values that were removed from this field, possibly a comma-and-space-separated list if multiple values were removed.
525  
526 =back
527  
528 =head3 Errors
529  
530 =over 4
531  
532 =item 51 - Bad Login Name
533  
534 You passed an invalid login name in the C<names> array.
535  
536 =item 304 - Authorization Required
537  
538 Logged-in users are not authorized to edit other users.
539  
540 =back
541  
542 =head1 EXCEPTION HANDLING
543  
544 See L<BZ::Client::Exception>
545  
546 =head1 SEE ALSO
547  
548 L<BZ::Client>, L<BZ::Client::API>, L<Bugzilla API|https://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/User.html>
549  
550 =head1 INSTANCE METHODS
551  
552 =head2 id, name, email, real_name, login, can_login, email_enabled, groups, saved_searches, last_visit
553  
554 $id = $user->id;
555 $name = $user->name;
556 $email = $user->email;
557 $real_name = $user->real_name;
558 $login = $user->login;
559 $can_login = $user->can_login;
560 $enabled = $user->email_enabled;
561 $groups = $user->groups;
562 $searches = $user->saved_searches;
563 $last_visit = $user->last_visit;
564  
565 Returns or sets the corresponding field for the user object. These accessors are provided for consistency with other BZ::Client classes.
566  
567 =head1 AUTHORS
568  
569 =over 4
570  
571 =item *
572  
573 Dean Hamstead <dean@bytefoundry.com.au>
574  
575 =item *
576  
577 Jochen Wiedmann <jochen.wiedmann@gmail.com>
578  
579 =back
580  
581 =head1 COPYRIGHT AND LICENSE
582  
583 This software is copyright (c) 2026 by Dean Hamstad.
584  
585 This is free software; you can redistribute it and/or modify it under
586 the same terms as the Perl 5 programming language system itself.
587  
588 =cut