Duo/Admin/User.pm

filter: Statementstmt 94.1% 64 / 68 Branchbran 60.0% 6 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 18 / 18 Podpod 100% 8 / 8 totaltotal 90.8% 99 / 109 CC 7 SCAR 4.7
CC
7
Cov
89%
bypass_codes
3.0
add_phone
2.1
add_token
2.1
CRAP 1.6
« Duo/Auth/Async.pm ↑ Summary Duo/Admin/Token.pm »
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 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use parent qw(Net::Duo::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 7 ▶use Net::Duo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Net::Duo::Admin::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Net::Duo::Admin::Phone;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Net::Duo::Admin::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Net::Duo::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 16 ▶sub _fields {
Subroutine _fields: called CC=1 SCAR=0.0
26 return {
27 16 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 7 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 1 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
53 1 my ($class, $duo, $data_ref) = @_;
54 1 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 1 ▶sub add_phone {
Subroutine add_phone: called CC=2 SCAR=7.2
Pod documented
66 1 my ($self, $phone) = @_;
67 1 ▶ if (!$phone->isa('Net::Duo::Admin::Phone')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($phone->isa("Net::Duo::Admin::Phone")) 0 1 1
68 0 die Net::Duo::Exception->internal('invalid argument to add_phone');
69 }
70 1 my $uri = "/admin/v1/users/$self->{user_id}/phones";
71 1 $self->{_duo}->call_json('POST', $uri, { phone_id => $phone->phone_id });
72 1 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 1 ▶sub add_token {
Subroutine add_token: called CC=2 SCAR=7.2
Pod documented
85 1 my ($self, $token) = @_;
86 1 ▶ if (!$token->isa('Net::Duo::Admin::Token')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token->isa("Net::Duo::Admin::Token")) 0 1 1
87 0 die Net::Duo::Exception->internal('invalid argument to add_token');
88 }
89 1 my $uri = "/admin/v1/users/$self->{user_id}/tokens";
90 1 $self->{_duo}->call_json('POST', $uri, { token_id => $token->token_id });
91 1 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 2 ▶sub bypass_codes {
Subroutine bypass_codes: called CC=3 SCAR=11.0
Pod documented
107 2 my ($self, $data_ref) = @_;
108 2 my $uri = "/admin/v1/users/$self->{user_id}/bypass_codes";
109  
110 # Canonicalize the arguments.
111 2 my %data = %{$data_ref};
112 2 ▶ if ($data{codes} && ref($data{codes})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{"codes"} and ref $data{"codes"}) 1 1 2
Condition66%2/3
$data{"codes"} and ref $data{"codes"}
!ll&&!rl&&r
101
MC/DC50%1/2
$data{"codes"} and ref $data{"codes"}
$data{"codes"} ref $data{"codes"}
Truth table66%2/3
A and B
A= $data{"codes"}B= ref $data{"codes"}
ABresult
0X0
100
111
113 1 $data{codes} = join(q{,}, @{ $data{codes} });
114 }
115  
116 # Make the call and return the results.
117 2 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 2 ▶sub commit {
Subroutine commit: called CC=1 SCAR=0.0
Pod documented
127 2 my ($self) = @_;
128 2 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 1 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
140 1 my ($self) = @_;
141 1 $self->{_duo}->call_json('DELETE', "/admin/v1/users/$self->{user_id}");
142 1 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 1 ▶sub remove_phone {
Subroutine remove_phone: called CC=2 SCAR=7.2
Pod documented
155 1 my ($self, $phone) = @_;
156 1 ▶ if (!$phone->isa('Net::Duo::Admin::Phone')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($phone->isa("Net::Duo::Admin::Phone")) 0 1 1
157 0 die Net::Duo::Exception->internal('invalid argument to remove_phone');
158 }
159 1 my $uri = "/admin/v1/users/$self->{user_id}/phones/" . $phone->phone_id;
160 1 $self->{_duo}->call_json('DELETE', $uri);
161 1 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 1 ▶sub remove_token {
Subroutine remove_token: called CC=2 SCAR=7.2
Pod documented
174 1 my ($self, $token) = @_;
175 1 ▶ if (!$token->isa('Net::Duo::Admin::Token')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token->isa("Net::Duo::Admin::Token")) 0 1 1
176 0 die Net::Duo::Exception->internal('invalid argument to remove_token');
177 }
178 1 my $uri = "/admin/v1/users/$self->{user_id}/tokens/" . $token->token_id;
179 1 $self->{_duo}->call_json('DELETE', $uri);
180 1 return;
181 }
182 ## use critic
183  
184 7 1;
185 __END__
« Duo/Auth/Async.pm ↑ Summary Duo/Admin/Token.pm »