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