File Coverage

lib/API/DirectAdmin/User.pm
Criterion Covered Total %
statement 48 57 84.2
branch 8 18 44.4
condition n/a
subroutine 11 12 91.6
pod 0 9 0.0
total 67 96 69.7


line stmt bran cond sub pod time code
1             package API::DirectAdmin::User;
2              
3 1     1   3556 use Modern::Perl '2010';
  1         2  
  1         10  
4 1     1   173 use Carp;
  1         3  
  1         86  
5              
6 1     1   7 use base 'API::DirectAdmin::Component';
  1         3  
  1         1264  
7              
8             our $VERSION = 0.06;
9              
10             # Return list of users (only usernames)
11             sub list {
12 1     1 0 3 my ($self ) = @_;
13              
14 1         5 my $responce = $self->directadmin->query(
15             command => 'CMD_API_SHOW_ALL_USERS',
16             );
17              
18 1 50       7 return $responce->{list} if ref $responce eq 'HASH';
19 0         0 return [];
20             }
21              
22             # Create a New User
23             # params: username, domain, passwd, passwd2, package, ip, email
24             sub create {
25 2     2 0 4 my ($self, $params ) = @_;
26            
27 2         8 my %add_params = (
28             action => 'create',
29             add => 'submit',
30             notify => 'no',
31             );
32            
33 2         15 my %params = (%$params, %add_params);
34              
35 2         21 my $responce = $self->directadmin->query(
36             params => \%params,
37             command => 'CMD_API_ACCOUNT_USER',
38             allowed_fields =>
39             'action
40             add
41             notify
42             username
43             domain
44             passwd
45             passwd2
46             package
47             ip
48             email',
49             );
50              
51 2 50       8 carp "Creating account: $responce->{text}, $responce->{details}" if $self->{debug};
52 2         10 return $responce;
53             }
54              
55             # Suspend user
56             # params: select0
57             sub disable {
58 1     1 0 3 my ($self, $params ) = @_;
59            
60 1         5 my %add_params = (
61             suspend => 'Suspend',
62             location => 'CMD_SELECT_USERS',
63             );
64            
65 1         6 my %params = (%$params, %add_params);
66            
67 1         5 my $responce = $self->directadmin->query(
68             command => 'CMD_API_SELECT_USERS',
69             method => 'POST',
70             params => \%params,
71             allowed_fields => 'location
72             suspend
73             select0',
74             );
75              
76 1 50       5 carp "Suspend account: $responce->{text}, $responce->{details}" if $self->{debug};
77 1         5 return $responce;
78             }
79              
80             # Unsuspend user
81             # params: select0
82             sub enable {
83 1     1 0 2 my ($self, $params ) = @_;
84            
85 1         5 my %add_params = (
86             suspend => 'Unsuspend',
87             location => 'CMD_SELECT_USERS',
88             );
89            
90 1         6 my %params = (%$params, %add_params);
91            
92 1         4 my $responce = $self->directadmin->query(
93             command => 'CMD_API_SELECT_USERS',
94             method => 'POST',
95             params => \%params,
96             allowed_fields => 'location
97             suspend
98             select0',
99             );
100              
101 1 50       4 carp "Unsuspend account: $responce->{text}, $responce->{details}" if $self->{debug};
102 1         6 return $responce;
103            
104             }
105              
106             # Delete user
107             # params: select0
108             sub delete {
109 2     2 0 3 my ($self, $params ) = @_;
110            
111 2         8 my %add_params = (
112             confirmed => 'Confirm',
113             delete => 'yes',
114             );
115            
116 2         9 my %params = (%$params, %add_params);
117              
118 2         7 my $responce = $self->directadmin->query(
119             command => 'CMD_API_SELECT_USERS',
120             method => 'POST',
121             params => \%params,
122             allowed_fields => 'confirmed
123             delete
124             select0',
125             );
126              
127 2 50       7 carp "Delete account: $responce->{text}, $responce->{details}" if $self->{debug};
128 2         9 return $responce;
129             }
130              
131             # Change passwd
132             # params: username, passwd, passwd2
133             sub change_password {
134 1     1 0 2 my ($self, $params ) = @_;
135              
136 1         5 my $responce = $self->directadmin->query(
137             command => 'CMD_API_USER_PASSWD',
138             method => 'POST',
139             params => $params,
140             allowed_fields => 'passwd
141             passwd2
142             username',
143             );
144              
145 1 50       5 carp "Change passwd account: $responce->{text}, $responce->{details}" if $self->{debug};
146 1         4 return $responce;
147             }
148              
149             # Change package for user
150             # params: user, package
151             sub change_package {
152 1     1 0 3 my ($self, $params ) = @_;
153            
154 1         4 my $package = $params->{package};
155              
156 1 50       7 unless ( $self->{fake_answer} ) {
157 1 50       3 unless ( $package ~~ $self->show_packages() ) {
158 1         7 return {error => 1, text => "No such package $package on server"};
159             }
160             }
161            
162 0         0 my %add_params = (
163             action => 'package',
164             );
165            
166 0         0 my %params = (%$params, %add_params);
167            
168 0         0 my $responce = $self->directadmin->query(
169             command => 'CMD_API_MODIFY_USER',
170             method => 'POST',
171             params => \%params,
172             allowed_fields => 'action
173             package
174             user',
175             );
176            
177 0 0       0 carp "Change package: $responce->{text}, $responce->{details}" if $self->{debug};
178 0         0 return $responce;
179             }
180              
181             # Show a list of user packages
182             # no params
183             sub show_packages {
184 1     1 0 2 my ($self ) = @_;
185              
186 1         5 my $responce = $self->directadmin->query(
187             command => 'CMD_API_PACKAGES_USER',
188             )->{list};
189              
190 1         5 return $responce;
191             }
192              
193             # Show user config
194             # params: user
195             sub show_user_config {
196 0     0 0   my ( $self, $params ) = @_;
197              
198 0           my $responce = $self->directadmin->query(
199             command => 'CMD_API_SHOW_USER_CONFIG',
200             params => $params,
201             allowed_fields => 'user',
202             );
203              
204 0           return $responce;
205             }
206              
207             1;