File Coverage

blib/lib/Net/Duo/Admin/User.pm
Criterion Covered Total %
statement 62 66 93.9
branch 6 10 60.0
condition 2 3 66.6
subroutine 18 18 100.0
pod 8 8 100.0
total 96 105 91.4


line stmt bran cond sub pod time code
1             # Representation of a single Duo user for the Admin API.
2             #
3             # This class wraps the Duo representation of a single Duo user, as returned by
4             # (for example) the Admin /users REST endpoint.
5             #
6             # SPDX-License-Identifier: MIT
7              
8             package Net::Duo::Admin::User 1.02;
9              
10 6     6   97 use 5.014;
  6         20  
11 6     6   32 use strict;
  6         9  
  6         120  
12 6     6   35 use warnings;
  6         30  
  6         191  
13              
14 6     6   36 use parent qw(Net::Duo::Object);
  6         12  
  6         34  
15              
16 6     6   2476 use Net::Duo;
  6         20  
  6         214  
17 6     6   443 use Net::Duo::Admin::Group;
  6         13  
  6         147  
18 6     6   513 use Net::Duo::Admin::Phone;
  6         10  
  6         211  
19 6     6   2639 use Net::Duo::Admin::Token;
  6         15  
  6         158  
20 6     6   39 use Net::Duo::Exception;
  6         13  
  6         4360  
21              
22             # Data specification for converting JSON into our object representation. See
23             # the Net::Duo::Object documentation for syntax information.
24             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
25             sub _fields {
26             return {
27 15     15   156 user_id => 'simple',
28             username => ['simple', 'set'],
29             realname => ['simple', 'set'],
30             email => ['simple', 'set'],
31             status => ['simple', 'set'],
32             groups => 'Net::Duo::Admin::Group',
33             last_login => 'simple',
34             notes => ['simple', 'set'],
35             phones => 'Net::Duo::Admin::Phone',
36             tokens => 'Net::Duo::Admin::Token',
37             };
38             }
39             ## use critic
40              
41             # Install our accessors.
42             Net::Duo::Admin::User->install_accessors;
43              
44             # Override the create method to add the appropriate URI.
45             #
46             # $class - Class of object to create
47             # $duo - Net::Duo object to use to create the object
48             # $data_ref - Data for new object as a reference to a hash
49             #
50             # Returns: Newly-created object
51             # Throws: Net::Duo::Exception on any problem creating the object
52             sub create {
53 1     1 1 318 my ($class, $duo, $data_ref) = @_;
54 1         10 return $class->SUPER::create($duo, '/admin/v1/users', $data_ref);
55             }
56              
57             # Add a phone to this user. All existing phones will be left unchanged.
58             #
59             # $self - The Net::Duo::Admin::User object to modify
60             # $phone - The Net::Duo::Admin::Phone object to add
61             #
62             # Returns: undef
63             # Throws: Net::Duo::Exception on any problem adding the phone
64             ## no critic (ErrorHandling::RequireCarping)
65             sub add_phone {
66 1     1 1 309 my ($self, $phone) = @_;
67 1 50       6 if (!$phone->isa('Net::Duo::Admin::Phone')) {
68 0         0 die Net::Duo::Exception->internal('invalid argument to add_phone');
69             }
70 1         6 my $uri = "/admin/v1/users/$self->{user_id}/phones";
71 1         6 $self->{_duo}->call_json('POST', $uri, { phone_id => $phone->phone_id });
72 1         3 return;
73             }
74             ## use critic
75              
76             # Add a token to this user. All existing tokens will be left unchanged.
77             #
78             # $self - The Net::Duo::Admin::User object to modify
79             # $token - The Net::Duo::Admin::Token object to add
80             #
81             # Returns: undef
82             # Throws: Net::Duo::Exception on any problem adding the phone
83             ## no critic (ErrorHandling::RequireCarping)
84             sub add_token {
85 1     1 1 317 my ($self, $token) = @_;
86 1 50       6 if (!$token->isa('Net::Duo::Admin::Token')) {
87 0         0 die Net::Duo::Exception->internal('invalid argument to add_token');
88             }
89 1         6 my $uri = "/admin/v1/users/$self->{user_id}/tokens";
90 1         4 $self->{_duo}->call_json('POST', $uri, { token_id => $token->token_id });
91 1         4 return;
92             }
93             ## use critic
94              
95             # Get one or more bypass codes for a user.
96             #
97             # $self - The Net::Duo::Admin::User object to modify
98             # $data_ref - Data for the bypass code request as a hash reference
99             # count - The number of codes to generate
100             # codes - Set this array of codes as bypass codes
101             # reuse_count - The number of times each bypass code can be reused
102             # valid_secs - Seconds for which these codes are valid
103             #
104             # Returns: A reference to an array of bypass codes
105             # Throws: Net::Duo::Exception on any problem getting the codes
106             sub bypass_codes {
107 2     2 1 630 my ($self, $data_ref) = @_;
108 2         10 my $uri = "/admin/v1/users/$self->{user_id}/bypass_codes";
109              
110             # Canonicalize the arguments.
111 2         5 my %data = %{$data_ref};
  2         7  
112 2 100 66     14 if ($data{codes} && ref($data{codes})) {
113 1         4 $data{codes} = join(q{,}, @{ $data{codes} });
  1         5  
114             }
115              
116             # Make the call and return the results.
117 2         9 return $self->{_duo}->call_json('POST', $uri, \%data);
118             }
119              
120             # Commit any changed data and refresh the object from Duo.
121             #
122             # $self - The Net::Duo::Admin::User object to commit changes for
123             #
124             # Returns: undef
125             # Throws: Net::Duo::Exception on any problem updating the object
126             sub commit {
127 2     2 1 513 my ($self) = @_;
128 2         21 return $self->SUPER::commit("/admin/v1/users/$self->{user_id}");
129             }
130              
131             # Delete the user from Duo. After this call, the object should be treated as
132             # read-only since it can no longer be usefully updated.
133             #
134             # $self - The Net::Duo::Admin::User object to delete
135             #
136             # Returns: undef
137             # Throws: Net::Duo::Exception on any problem deleting the object
138             ## no critic (Subroutines::ProhibitBuiltinHomonyms)
139             sub delete {
140 1     1 1 313 my ($self) = @_;
141 1         8 $self->{_duo}->call_json('DELETE', "/admin/v1/users/$self->{user_id}");
142 1         3 return;
143             }
144             ## use critic
145              
146             # Remove a phone from this user. Other phones will be left unchanged.
147             #
148             # $self - The Net::Duo::Admin::User object to modify
149             # $phone - The Net::Duo::Admin::Phone object to remove
150             #
151             # Returns: undef
152             # Throws: Net::Duo::Exception on any problem adding the phone
153             ## no critic (ErrorHandling::RequireCarping)
154             sub remove_phone {
155 1     1 1 337 my ($self, $phone) = @_;
156 1 50       7 if (!$phone->isa('Net::Duo::Admin::Phone')) {
157 0         0 die Net::Duo::Exception->internal('invalid argument to remove_phone');
158             }
159 1         6 my $uri = "/admin/v1/users/$self->{user_id}/phones/" . $phone->phone_id;
160 1         6 $self->{_duo}->call_json('DELETE', $uri);
161 1         3 return;
162             }
163             ## use critic
164              
165             # Remove a token from this user. Other tokens will be left unchanged.
166             #
167             # $self - The Net::Duo::Admin::User object to modify
168             # $token - The Net::Duo::Admin::Token object to remove
169             #
170             # Returns: undef
171             # Throws: Net::Duo::Exception on any problem adding the phone
172             ## no critic (ErrorHandling::RequireCarping)
173             sub remove_token {
174 1     1 1 341 my ($self, $token) = @_;
175 1 50       7 if (!$token->isa('Net::Duo::Admin::Token')) {
176 0         0 die Net::Duo::Exception->internal('invalid argument to remove_token');
177             }
178 1         7 my $uri = "/admin/v1/users/$self->{user_id}/tokens/" . $token->token_id;
179 1         6 $self->{_duo}->call_json('DELETE', $uri);
180 1         3 return;
181             }
182             ## use critic
183              
184             1;
185             __END__