summaryrefslogtreecommitdiffstats
path: root/extensions/BzAPI/lib/Resources/User.pm
blob: 550a61d2825d705b8caaead8e3e93d4cf601e826 (plain)
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
# 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::Extension::BzAPI::Resources::User;

use 5.10.1;
use strict;
use warnings;

use Bugzilla::Extension::BzAPI::Util;

sub rest_handlers {
    my $rest_handlers = [
        qr{/user$}, {
            GET  => {
                response => \&get_users,
            },
        },
        qr{/user/([^/]+)$}, {
            GET => {
                response => \&get_user,
            },
        }
    ];
    return $rest_handlers;
}

sub get_users {
    my ($result) = @_;
    my $rpc    = Bugzilla->request_cache->{bzapi_rpc};
    my $params = Bugzilla->input_params;

    return if !exists $$result->{users};

    my @users;
    foreach my $user (@{$$result->{users}}) {
        my $object = Bugzilla::User->new(
            { id => $user->{id}, cache => 1 });

        $user = fix_user($user, $object);

        # Use userid instead of email for 'ref' for /user calls
        $user->{'ref'} = $rpc->type('string', ref_urlbase . "/user/" . $object->id);

        # Emails are not filtered even if user is not logged in
        $user->{name} = $rpc->type('string', $object->login);

        push(@users, filter($params, $user));
    }

    $$result->{users} = \@users;
}

sub get_user {
    my ($result) = @_;
    my $rpc    = Bugzilla->request_cache->{bzapi_rpc};
    my $params = Bugzilla->input_params;

    return if !exists $$result->{users};
    my $user = $$result->{users}->[0] || return;
    my $object = Bugzilla::User->new({ id => $user->{id}, cache => 1 });

    $user = fix_user($user, $object);

    # Use userid instead of email for 'ref' for /user calls
    $user->{'ref'} = $rpc->type('string', ref_urlbase . "/user/" . $object->id);

    # Emails are not filtered even if user is not logged in
    $user->{name} = $rpc->type('string', $object->login);

    $user = filter($params, $user);

    $$result = $user;
}

1;