User.pm 17.2 KB
Newer Older
1 2 3
# 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/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8 9 10 11

package Bugzilla::WebService::User;

use strict;
use base qw(Bugzilla::WebService);
12

13 14
use Bugzilla;
use Bugzilla::Constants;
15
use Bugzilla::Error;
16
use Bugzilla::Group;
17 18
use Bugzilla::User;
use Bugzilla::Util qw(trim);
19
use Bugzilla::WebService::Util qw(filter validate);
20

21 22 23 24 25 26
# Don't need auth to login
use constant LOGIN_EXEMPT => {
    login => 1,
    offer_account_by_email => 1,
};

27 28 29 30
use constant READ_ONLY => qw(
    get
);

31 32 33
##############
# User Login #
##############
34 35

sub login {
36 37
    my ($self, $params) = @_;
    my $remember = $params->{remember};
38 39 40 41 42 43 44

    # Username and password params are required 
    foreach my $param ("login", "password") {
        defined $params->{$param} 
            || ThrowCodeError('param_required', { param => $param });
    }

45 46 47 48 49 50 51 52 53 54 55
    # Convert $remember from a boolean 0/1 value to a CGI-compatible one.
    if (defined($remember)) {
        $remember = $remember? 'on': '';
    }
    else {
        # Use Bugzilla's default if $remember is not supplied.
        $remember =
            Bugzilla->params->{'rememberlogin'} eq 'defaulton'? 'on': '';
    }

    # Make sure the CGI user info class works if necessary.
56 57 58 59
    my $input_params = Bugzilla->input_params;
    $input_params->{'Bugzilla_login'} =  $params->{login};
    $input_params->{'Bugzilla_password'} = $params->{password};
    $input_params->{'Bugzilla_remember'} = $remember;
60

61
    Bugzilla->login();
62
    return { id => $self->type('int', Bugzilla->user->id) };
63 64 65 66 67
}

sub logout {
    my $self = shift;
    Bugzilla->logout;
68
    return undef;
69 70
}

71 72 73 74 75 76 77 78 79 80
#################
# User Creation #
#################

sub offer_account_by_email {
    my $self = shift;
    my ($params) = @_;
    my $email = trim($params->{email})
        || ThrowCodeError('param_required', { param => 'email' });

81 82
    Bugzilla->user->check_account_creation_enabled;
    Bugzilla->user->check_and_send_account_creation_confirmation($email);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    return undef;
}

sub create {
    my $self = shift;
    my ($params) = @_;

    Bugzilla->user->in_group('editusers') 
        || ThrowUserError("auth_failure", { group  => "editusers",
                                            action => "add",
                                            object => "users"});

    my $email = trim($params->{email})
        || ThrowCodeError('param_required', { param => 'email' });
    my $realname = trim($params->{full_name});
    my $password = trim($params->{password}) || '*';

    my $user = Bugzilla::User->create({
        login_name    => $email,
        realname      => $realname,
        cryptpassword => $password
    });

106
    return { id => $self->type('int', $user->id) };
107 108
}

109 110 111 112 113 114

# function to return user information by passing either user ids or 
# login names or both together:
# $call = $rpc->call( 'User.get', { ids => [1,2,3], 
#         names => ['testusera@redhat.com', 'testuserb@redhat.com'] });
sub get {
115
    my ($self, $params) = validate(@_, 'names', 'ids');
116

117 118
    Bugzilla->switch_to_shadow_db();

119 120 121 122 123
    defined($params->{names}) || defined($params->{ids})
        || defined($params->{match})
        || ThrowCodeError('params_required', 
               { function => 'User.get', params => ['ids', 'names', 'match'] });

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    my @user_objects;
    @user_objects = map { Bugzilla::User->check($_) } @{ $params->{names} }
                    if $params->{names};

    # start filtering to remove duplicate user ids
    my %unique_users = map { $_->id => $_ } @user_objects;
    @user_objects = values %unique_users;
      
    my @users;

    # If the user is not logged in: Return an error if they passed any user ids.
    # Otherwise, return a limited amount of information based on login names.
    if (!Bugzilla->user->id){
        if ($params->{ids}){
            ThrowUserError("user_access_by_id_denied");
        }
140 141 142
        if ($params->{match}) {
            ThrowUserError('user_access_by_match_denied');
        }
143 144
        my $in_group = $self->_filter_users_by_group(
            \@user_objects, $params);
145
        @users = map {filter $params, {
146 147 148
                     id        => $self->type('int', $_->id),
                     real_name => $self->type('string', $_->name), 
                     name      => $self->type('string', $_->login),
149
                 }} @$in_group;
150 151 152 153 154 155 156 157 158

        return { users => \@users };
    }

    my $obj_by_ids;
    $obj_by_ids = Bugzilla::User->new_from_list($params->{ids}) if $params->{ids};

    # obj_by_ids are only visible to the user if he can see 
    # the otheruser, for non visible otheruser throw an error
159
    foreach my $obj (@$obj_by_ids) {
160
        if (Bugzilla->user->can_see_user($obj)){
161 162 163 164
            if (!$unique_users{$obj->id}) {
                push (@user_objects, $obj);
                $unique_users{$obj->id} = $obj;
            }
165 166 167 168 169 170 171 172
        }
        else {
            ThrowUserError('auth_failure', {reason => "not_visible",
                                            action => "access",
                                            object => "user",
                                            userid => $obj->id});
        }
    }
173 174 175 176 177 178
    
    # User Matching
    my $limit;
    if ($params->{'maxusermatches'}) {
        $limit = $params->{'maxusermatches'} + 1;
    }
179
    my $exclude_disabled = $params->{'include_disabled'} ? 0 : 1;
180
    foreach my $match_string (@{ $params->{'match'} || [] }) {
181
        my $matched = Bugzilla::User::match($match_string, $limit, $exclude_disabled);
182 183 184 185 186 187 188
        foreach my $user (@$matched) {
            if (!$unique_users{$user->id}) {
                push(@user_objects, $user);
                $unique_users{$user->id} = $user;
            }
        }
    }
189 190 191
   
    my $in_group = $self->_filter_users_by_group(
        \@user_objects, $params); 
192
    if (Bugzilla->user->in_group('editusers')) {
193
        @users = 
194
            map {filter $params, {
195 196 197 198
                id        => $self->type('int', $_->id),
                real_name => $self->type('string', $_->name),
                name      => $self->type('string', $_->login),
                email     => $self->type('string', $_->email),
199
                can_login => $self->type('boolean', $_->is_enabled ? 1 : 0),
200
                groups    => $self->_filter_bless_groups($_->groups), 
201 202
                email_enabled     => $self->type('boolean', $_->email_enabled),
                login_denied_text => $self->type('string', $_->disabledtext),
203
            }} @$in_group;
204 205 206
    }    
    else {
        @users =
207
            map {filter $params, {
208 209 210 211
                id        => $self->type('int', $_->id),
                real_name => $self->type('string', $_->name),
                name      => $self->type('string', $_->login),
                email     => $self->type('string', $_->email),
212
                can_login => $self->type('boolean', $_->is_enabled ? 1 : 0),
213
                groups    => $self->_filter_bless_groups($_->groups),
214
            }} @$in_group;
215 216 217 218 219
    }

    return { users => \@users };
}

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
sub _filter_users_by_group {
    my ($self, $users, $params) = @_;
    my ($group_ids, $group_names) = @$params{qw(group_ids groups)};

    # If no groups are specified, we return all users.
    return $users if (!$group_ids and !$group_names);

    my @groups = map { Bugzilla::Group->check({ id => $_ }) } 
                     @{ $group_ids || [] };
    my @name_groups = map { Bugzilla::Group->check($_) } 
                          @{ $group_names || [] };
    push(@groups, @name_groups);
    

    my @in_group = grep { $self->_user_in_any_group($_, \@groups) }
                        @$users;
    return \@in_group;
}

sub _user_in_any_group {
    my ($self, $user, $groups) = @_;
    foreach my $group (@$groups) {
        return 1 if $user->in_group($group);
    }
    return 0;
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
sub _filter_bless_groups {
    my ($self, $groups) = @_;
    my $user = Bugzilla->user;

    my @filtered_groups;
    foreach my $group (@$groups) {
        next unless ($user->in_group('editusers') || $user->can_bless($group->id));
        push(@filtered_groups, $self->_group_to_hash($group));
    }

    return \@filtered_groups;
}

sub _group_to_hash {
    my ($self, $group) = @_;
    my $item = {
        id          => $self->type('int', $group->id), 
        name        => $self->type('string', $group->name), 
        description => $self->type('string', $group->description), 
    };
    return $item;
}

270
1;
271 272 273 274 275 276 277 278 279

__END__

=head1 NAME

Bugzilla::Webservice::User - The User Account and Login API

=head1 DESCRIPTION

280 281
This part of the Bugzilla API allows you to create User Accounts and
log in/out using an existing account.
282 283 284

=head1 METHODS

285 286
See L<Bugzilla::WebService> for a description of how parameters are passed,
and what B<STABLE>, B<UNSTABLE>, and B<EXPERIMENTAL> mean.
287

288
=head1 Logging In and Out
289

290
=head2 login
291 292

B<STABLE>
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

=over

=item B<Description>

Logging in, with a username and password, is required for many
Bugzilla installations, in order to search for bugs, post new bugs,
etc. This method logs in an user.

=item B<Params>

=over

=item C<login> (string) - The user's login name. 

=item C<password> (string) - The user's password.

=item C<remember> (bool) B<Optional> - if the cookies returned by the
call to login should expire with the session or not.  In order for
this option to have effect the Bugzilla server must be configured to
allow the user to set this option - the Bugzilla parameter
I<rememberlogin> must be set to "defaulton" or
"defaultoff". Addionally, the client application must implement
management of cookies across sessions.

=back

=item B<Returns>

On success, a hash containing one item, C<id>, the numeric id of the
user that was logged in.  A set of http cookies is also sent with the
response.  These cookies must be sent along with any future requests
to the webservice, for the duration of the session.

=item B<Errors>

=over

=item 300 (Invalid Username or Password)

The username does not exist, or the password is wrong.

=item 301 (Account Disabled)

The account has been disabled.  A reason may be specified with the
error.

340 341 342 343 344
=item 305 (New Password Required)

The current password is correct, but the user is asked to change
his password.

345 346 347 348
=item 50 (Param Required)

A login or password parameter was not provided.

349 350 351 352
=back

=back

353
=head2 logout
354 355

B<STABLE>
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

=over

=item B<Description>

Log out the user. Does nothing if there is no user logged in.

=item B<Params> (none)

=item B<Returns> (nothing)

=item B<Errors> (none)

=back

371
=head1 Account Creation
372

373
=head2 offer_account_by_email
374 375

B<STABLE>
376

377 378 379
=over

=item B<Description>
380

381 382 383
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.
384

385 386 387 388 389 390 391 392 393 394 395 396 397
This is the recommended way to create a Bugzilla account.

=item B<Param>

=over

=item C<email> (string) - the email to send the offer to.

=back

=item B<Returns> (nothing)

=item B<Errors>
398 399 400

=over

401
=item 500 (Account Already Exists)
402

403
An account with that email address already exists in Bugzilla.
404

405
=item 501 (Illegal Email Address)
406

407 408
This Bugzilla does not allow you to create accounts with the format of
email address you specified. Account creation may be entirely disabled.
409 410 411

=back

412 413
=back

414
=head2 create
415

416
B<STABLE>
417

418 419 420 421 422 423 424 425 426
=over

=item B<Description>

Creates a user account directly in Bugzilla, password and all.
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. This function does not check that.

427 428 429
You must be logged in and have the C<editusers> privilege in order to
call this function.

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
=item B<Params>

=over

=item C<email> (string) - The email address for the new user.

=item C<full_name> (string) B<Optional> - The user's full name. Will
be set to empty if not specified.

=item C<password> (string) B<Optional> - 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.

=back

=item B<Returns>

A hash containing one item, C<id>, the numeric id of the user that was
created.

=item B<Errors>

The same as L</offer_account_by_email>. If a password is specified,
the function may also throw:
457 458 459 460 461 462 463 464

=over

=item 502 (Password Too Short)

The password specified is too short. (Usually, this means the
password is under three characters.)

465 466 467 468 469
=back

=item B<History>

=over
470

471
=item Error 503 (Password Too Long) removed in Bugzilla B<3.6>.
472 473 474 475

=back

=back
476

477
=head1 User Info
478

479
=head2 get
480

481
B<STABLE>
482 483 484 485 486 487 488 489 490

=over

=item B<Description>

Gets information about user accounts in Bugzilla.

=item B<Params>

491 492 493 494
B<Note>: At least one of C<ids>, C<names>, or C<match> must be specified.

B<Note>: Users will not be returned more than once, so even if a user 
is matched by more than one argument, only one user will be returned.
495

496 497 498 499
In addition to the parameters below, this method also accepts the
standard L<include_fields|Bugzilla::WebService/include_fields> and
L<exclude_fields|Bugzilla::WebService/exclude_fields> arguments.

500 501
=over

502 503 504 505
=item C<ids> (array) 

An array of integers, representing user ids.

506
Logged-out users cannot pass this parameter to this function. If they try,
507 508
they will get an error. Logged-in users will get an error if they specify
the id of a user they cannot see.
509

510 511 512
=item C<names> (array)

An array of login names (strings).
513

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
=item C<match> (array)

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.

Some Bugzilla installations have user-matching turned off, in which
case you will only be returned exact matches.

Most installations have a limit on how many matches are returned for
each string, which defaults to 1000 but can be changed by the Bugzilla
administrator.

Logged-out users cannot use this argument, and an error will be thrown
529 530 531
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.)
532

533 534 535 536 537 538 539 540 541
=item C<group_ids> (array)

=item C<groups> (array)

C<group_ids> is an array of numeric ids for groups that a user can be in.
C<groups> is an array of names of groups that a user can be in.
If these are specified, they limit the return value to users who are
in I<any> of the groups specified.

542 543 544
=item C<include_disabled> (boolean)

By default, when using the C<match> parameter, disabled users are excluded
545 546 547 548
from the returned results unless their full username is identical to the
match string. Setting C<include_disabled> to C<true> will include disabled
users in the returned results even if their username doesn't fully match
the input string.
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
=back

=item B<Returns> 

A hash containing one item, C<users>, that is an array of
hashes. Each hash describes a user, and has the following items:

=over

=item id

C<int> The unique integer ID that Bugzilla uses to represent this user. 
Even if the user's login name changes, this will not change.

=item real_name

C<string> The actual name of the user. May be blank.

=item email

C<string> The email address of the user.

=item name

C<string> The login name of the user. Note that in some situations this is 
different than their email.

=item can_login

C<boolean> A boolean value to indicate if the user can login into bugzilla. 

=item email_enabled

C<boolean> A boolean value to indicate if bug-related mail will be sent
to the user or not.

=item login_denied_text

C<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.

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
=item groups

C<array> An array of group hashes the user is a member of. Each hash describes
the group and contains the following items:

=over

=item id

C<int> The group id

=item name

C<string> The name of the group

=item description

C<string> The description for the group

=back

613 614 615
B<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. If you are
logged in and not in editusers group, you will only be returned the C<id>, C<name>, 
616 617
C<real_name>, C<email>, and C<can_login> items. The groups returned are filtered
based on your permission to bless each group.
618 619 620 621 622 623 624

=back

=item B<Errors>

=over

625
=item 51 (Bad Login Name or Group Name)
626

627 628
You passed an invalid login name in the "names" array or a bad
group name/id in the C<groups>/C<group_ids> arguments.
629 630 631 632 633 634

=item 304 (Authorization Required)

You are logged in, but you are not authorized to see one of the users you
wanted to get information about by user id.

635
=item 505 (User Access By Id or User-Matching Denied)
636

637 638
Logged-out users cannot use the "ids" or "match" arguments to this 
function.
639 640 641

=back

642 643 644 645 646 647
=item B<History>

=over

=item Added in Bugzilla B<3.4>.

648
=item C<group_ids> and C<groups> were added in Bugzilla B<4.0>.
649

650 651 652
=item C<include_disabled> added in Bugzilla B<4.0>. Default behavior 
for C<match> has changed to only returning enabled accounts.

653 654
=item C<groups> Added in Bugzilla B<4.4>.

655 656
=back

657
=back