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

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 64 CC 7 SCAR 23.5
CC
7
Cov
0%
update
20.0
get
12.0
offer_account_by_email
6.0
CRAP 10.5
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.4001';
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(undef, $client, $params) = @_;
48 return _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 1;
72  
73 __END__
74  
75 =pod
76  
77 =encoding UTF-8
78  
79 =head1 NAME
80  
81 BZ::Client::User - Creates and edits user accounts in Bugzilla
82  
83 =head1 VERSION
84  
85 version 4.4001
86  
87 =head1 SYNOPSIS
88  
89 This class provides methods for accessing information about the Bugzilla
90 servers installation.
91  
92 my $client = BZ::Client->new( url => $url,
93 user => $user,
94 password => $password );
95  
96 my $ok = BZ::Client::User->offer_account_by_email( $client, 'email@address' );
97 my $users = BZ::Client::User->get( $client, \%params );
98 my $id = BZ::Client::User->create( $client, \%params );
99 my $users = BZ::Client::User->update( $client, \%params );
100  
101 =head1 CLASS METHODS
102  
103 This section lists the class methods, which are available in this module.
104  
105 =head2 offer_account_by_email
106  
107 my $ok = BZ::Client::User->offer_account_by_email( $client, 'email@address' );
108 my $ok = BZ::Client::User->offer_account_by_email( $client, \%params );
109  
110 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.
111  
112 This is the recommended way to create a Bugzilla account.
113  
114 =head3 Parameters
115  
116 =over 4
117  
118 =item email
119  
120 I<email> (string) The email address to send the offer to.
121  
122 Note: email can be provided as the single option as a scalar as shown above.
123  
124 =back
125  
126 =head3 Returns
127  
128 nothing if successful
129  
130 =head3 Errors
131  
132 =over 4
133  
134 =item 50 - Email parameter missing
135  
136 The email parameter was not provided.
137  
138 =item 500 - Account Already Exists
139  
140 An account with that email address already exists in Bugzilla.
141  
142 =item 501 - Illegal Email Address
143  
144 This Bugzilla does not allow you to create accounts with the format of email address you specified. Account creation may be entirely disabled.
145  
146 =back
147  
148 =head2 get
149  
150 my $users = BZ::Client::User->get( $client, \%params );
151  
152 Gets information about user accounts in Bugzilla.
153  
154 =head3 History
155  
156 Added in Bugzilla 3.4
157  
158 =head3 Parameters
159  
160 Note: At least one of L</ids>, L</names>, or L</match> must be specified.
161  
162 Note: Users will only appear once in the returned list, even if that user is matched by more than one search argument.
163  
164 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.
165  
166 =over 4
167  
168 =item ids (array)
169  
170 An array of integers, representing user ID's.
171  
172 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.
173  
174 =item names (array)
175  
176 An array of login names (strings).
177  
178 =item match (array)
179  
180 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.
181  
182 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.
183  
184 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.)
185  
186 =item limit (int)
187  
188 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.
189  
190 =item group_ids (array)
191  
192 I<group_ids> is an array of numeric ids for groups that a user can be in.
193 If this is specified, it limits the return value to users who are in any of the groups specified.
194  
195 Added in Bugzilla 4.0
196  
197 =item groups (array)
198  
199 I<groups> is an array of names of groups that a user can be in.
200 If this is specified, it limits the return value to users who are in any of the groups specified.
201  
202 Added in Bugzilla 4.0
203  
204 =item include_disabled (boolean)
205  
206 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.
207  
208 Added in Bugzilla 4.0, default behaviour for L</match> was then changed to exclude disabled users.
209  
210 =back
211  
212 =head3 Returns
213  
214 A hash containing one item, C<users>, that is an array of hashes. Each hash describes a user, and has the following items:
215  
216 =over 4
217  
218 =item id
219  
220 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.
221  
222 =item real_name
223  
224 I<real_name> (string) The actual name of the user. May be blank.
225  
226 =item email
227  
228 I<email> (string) The email address of the user.
229  
230 =item name
231  
232 I<name> (string) The login name of the user. Note that in some situations this is different than their email.
233  
234 =item can_login
235  
236 I<can_login> (boolean) A boolean value to indicate if the user can login into bugzilla.
237  
238 =item email_enabled
239  
240 I<email_enabled> (boolean) A boolean value to indicate if bug-related mail will be sent to the user or not.
241  
242 =item login_denied_text
243  
244 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.
245  
246 =item groups
247  
248 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:
249  
250 Added in Bugzilla 4.4
251  
252 =over 4
253  
254 =item id
255  
256 I<id> (int) The group ID
257  
258 =item name
259  
260 I<name> (string) The name of the group
261  
262 =item description
263  
264 I<description> (string) The description for the group
265  
266 =back
267  
268 =item saved_searches
269  
270 I<saved_searches> (array) An array of hashes, each of which represents a user's saved search and has the following keys:
271  
272 Added in Bugzilla 4.4
273  
274 =over 4
275  
276 =item id
277  
278 I<id> (int) An integer ID uniquely identifying the saved search.
279  
280 =item name
281  
282 I<name> (string) The name of the saved search.
283  
284 =item query
285  
286 I<query> (string) The CGI parameters for the saved search.
287  
288 =back
289  
290 =item saved_reports
291  
292 I<saved_reports> (array) An array of hashes, each of which represents a user's saved report and has the following keys:
293  
294 Added in Bugzilla 4.4
295  
296 =over 4
297  
298 =item id
299  
300 I<id> (int) An integer ID uniquely identifying the saved report.
301  
302 =item name
303  
304 I<name> (string) The name of the saved report.
305  
306 =item query
307  
308 I<query> (string) The CGI parameters for the saved report.
309  
310 =back
311  
312 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.
313 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.
314 The C<groups> returned are filtered based on your permission to bless each group.
315 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.
316  
317 =back
318  
319 =head3 Errors
320  
321 =over 4
322  
323 =item 51 - Bad Login Name or Group ID
324  
325 You passed an invalid login name in the L</names> array or a bad group ID in the L</group_ids> argument.
326  
327 =item 52 - Invalid Parameter
328  
329 The value used must be an integer greater then zero.
330  
331 =item 304 - Authorization Required
332  
333 You are logged in, but you are not authorized to see one of the users you wanted to get information about by user ID.
334  
335 =item 505 - User Access By Id or User-Matching Denied
336  
337 Logged-out users cannot use the C<ids> or C<match> arguments to this function.
338  
339 =item 804 - Invalid Group Name
340  
341 You passed a group name in the C<groups> argument which either does not exist or you do not belong to it.
342  
343 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.
344  
345 =back
346  
347 =head2 new
348  
349 my $user = BZ::Client::User->new( id => $id );
350  
351 Creates a new instance with the given ID.
352  
353 =head2 create
354  
355 my $id = BZ::Client::User->create( $client, \%params );
356  
357 Creates a user account directly in Bugzilla, password and all.
358 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.
359 This function does not check that.
360  
361 You must be logged in and have the C<editusers> privilege in order to call this function.
362  
363 Params:
364  
365 =over 4
366  
367 =item email
368  
369 I<email> (string) - The email address for the new user.
370  
371 B<Required>.
372  
373 =item full_name
374  
375 I<full_name> (string) The user's full name. Will be set to empty if not specified.
376  
377 =item password
378  
379 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.
380  
381 =back
382  
383 =head3 Returns
384  
385 The numeric ID of the user that was created.
386  
387 =head3 Errors
388  
389 The same as L</offer_account_by_email>. If a password is specified, the function may also throw:
390  
391 =over 4
392  
393 =item 502 - Password Too Short
394  
395 The password specified is too short. (Usually, this means the password is under three characters.)
396  
397 =item 503 - Password Too Long
398  
399 Removed in Bugzilla 3.6
400  
401 =back
402  
403 =head2 update
404  
405 my $users = BZ::Client::User->update( $client, \%params );
406  
407 Updates user accounts in Bugzilla.
408  
409 =head3 Parameters
410  
411 =over 4
412  
413 =item ids
414  
415 I<array> Contains ID's of user to update.
416  
417 =item names
418  
419 I<array> Contains email/login of user to update.
420  
421 =item full_name
422  
423 I<full_name> (string) The new name of the user.
424  
425 =item email
426  
427 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.)
428  
429 =item password
430  
431 I<password> (string) The password of the user.
432  
433 =item email_enabled
434  
435 I<email_enabled> (boolean) A boolean value to enable/disable sending bug-related mail to the user.
436  
437 =item login_denied_text
438  
439 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.
440  
441 =back
442  
443 =head3 Returns
444  
445 An array of hashes with the following fields:
446  
447 =over 4
448  
449 =item id
450  
451 I<id> (int) The ID of the user that was updated.
452  
453 =item changes
454  
455 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:
456  
457 =item added
458  
459 I<added> (string) The values that were added to this field, possibly a comma-and-space-separated list if multiple values were added.
460  
461 =item removed
462  
463 I<removed> (string) The values that were removed from this field, possibly a comma-and-space-separated list if multiple values were removed.
464  
465 =back
466  
467 =head3 Errors
468  
469 =over 4
470  
471 =item 51 - Bad Login Name
472  
473 You passed an invalid login name in the C<names> array.
474  
475 =item 304 - Authorization Required
476  
477 Logged-in users are not authorized to edit other users.
478  
479 =back
480  
481 =head1 EXCEPTION HANDLING
482  
483 See L<BZ::Client::Exception>
484  
485 =head1 SEE ALSO
486  
487 L<BZ::Client>, L<BZ::Client::API>, L<Bugzilla API|https://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/User.html>
488  
489 =head1 AUTHORS
490  
491 =over 4
492  
493 =item *
494  
495 Dean Hamstead <dean@bytefoundry.com.au>
496  
497 =item *
498  
499 Jochen Wiedmann <jochen.wiedmann@gmail.com>
500  
501 =back
502  
503 =head1 COPYRIGHT AND LICENSE
504  
505 This software is copyright (c) 2017 by Dean Hamstad.
506  
507 This is free software; you can redistribute it and/or modify it under
508 the same terms as the Perl 5 programming language system itself.
509  
510 =cut