1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* DirectAdmin API Client |
5
|
|
|
* (c) Omines Internetbureau B.V. - https://omines.nl/ |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Omines\DirectAdmin\Context; |
12
|
|
|
|
13
|
|
|
use Omines\DirectAdmin\Objects\BaseObject; |
14
|
|
|
use Omines\DirectAdmin\Objects\Users\User; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Context for reseller functions. |
18
|
|
|
* |
19
|
|
|
* @author Niels Keurentjes <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ResellerContext extends UserContext |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Creates a new user on the server. |
25
|
|
|
* |
26
|
|
|
* @param string $username Login for the new user |
27
|
|
|
* @param string $password Password for the new user |
28
|
|
|
* @param string $email Email for the new user |
29
|
|
|
* @param string $domain Default domain for the new user |
30
|
|
|
* @param string $ip IP for the user |
31
|
|
|
* @param string|array $package Either a package name or an array of options for custom |
32
|
|
|
* @return User Newly created user |
33
|
|
|
* @url http://www.directadmin.com/api.html#create for options to use. |
34
|
|
|
*/ |
35
|
|
|
public function createUser($username, $password, $email, $domain, $ip, $package = []) |
36
|
|
|
{ |
37
|
|
|
$options = array_merge( |
38
|
|
|
['ip' => $ip, 'domain' => $domain], |
39
|
|
|
is_array($package) ? $package : ['package' => $package] |
40
|
|
|
); |
41
|
|
|
return $this->createAccount($username, $password, $email, $options, 'ACCOUNT_USER', User::class); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Internal helper function for creating new accounts. |
46
|
|
|
* |
47
|
|
|
* @param string $username Login for the new user |
48
|
|
|
* @param string $password Password for the new user |
49
|
|
|
* @param string $email Email for the new user |
50
|
|
|
* @param array $options List of DA account options to apply |
51
|
|
|
* @param string $endpoint API endpoint to invoke |
52
|
|
|
* @param string $returnType Class name that should wrap the resulting account |
53
|
|
|
* @return object An instance of the type specified in $returnType |
54
|
|
|
*/ |
55
|
|
|
protected function createAccount($username, $password, $email, $options, $endpoint, $returnType) |
56
|
|
|
{ |
57
|
|
|
$this->invokePost($endpoint, array_merge($options, [ |
58
|
|
|
'action' => 'create', |
59
|
|
|
'add' => 'Submit', |
60
|
|
|
'email' => $email, |
61
|
|
|
'passwd' => $password, |
62
|
|
|
'passwd2' => $password, |
63
|
|
|
'username' => $username, |
64
|
|
|
])); |
65
|
|
|
return new $returnType($username, $this); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Deletes a single account. |
70
|
|
|
* |
71
|
|
|
* @param string $username Account to delete |
72
|
|
|
*/ |
73
|
|
|
public function deleteAccount($username) |
74
|
|
|
{ |
75
|
|
|
$this->deleteAccounts([$username]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Deletes multiple accounts. |
80
|
|
|
* |
81
|
|
|
* @param string[] $usernames Accounts to delete |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function deleteAccounts(array $usernames) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$options = ['confirmed' => 'Confirm', 'delete' => 'yes']; |
86
|
|
|
foreach (array_values($usernames) as $idx => $username) { |
87
|
|
|
$options["select{$idx}"] = $username; |
88
|
|
|
} |
89
|
|
|
$this->invokePost('SELECT_USERS', $options); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Suspends a single account |
94
|
|
|
* |
95
|
|
|
* @param string $username Account to delete |
96
|
|
|
*/ |
97
|
|
|
public function suspendAccount($username) |
98
|
|
|
{ |
99
|
|
|
$this->suspendAccounts([$username]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Unsuspends a single account |
104
|
|
|
* @param string $username Account to delete |
105
|
|
|
*/ |
106
|
|
|
public function unsuspendAccount($username) |
107
|
|
|
{ |
108
|
|
|
$this->suspendAccounts([$username], false); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Suspends (or unsuspends) multiple accounts. |
113
|
|
|
* |
114
|
|
|
* @param string[] $usernames Accounts to delete |
115
|
|
|
* @param bool $suspend (true - suspend, false - unsuspend) |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function suspendAccounts(array $usernames, $suspend = true) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$options = ['suspend' => $suspend ? 'Suspend' : 'Unsuspend']; |
120
|
|
|
foreach (array_values($usernames) as $idx => $username) { |
121
|
|
|
$options["select{$idx}"] = $username; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
$this->invokePost('SELECT_USERS', $options); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Unsuspends multiple accounts. |
128
|
|
|
* |
129
|
|
|
* @param string[] $usernames Accounts to delete |
130
|
|
|
*/ |
131
|
|
|
public function unsuspendAccounts(array $usernames) |
132
|
|
|
{ |
133
|
|
|
$this->suspendAccounts($usernames, false); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns all IPs available to this reseller. |
138
|
|
|
* |
139
|
|
|
* @return array List of IPs as strings |
140
|
|
|
*/ |
141
|
|
|
public function getIPs() |
142
|
|
|
{ |
143
|
|
|
return $this->invokeGet('SHOW_RESELLER_IPS'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns a single user by name. |
148
|
|
|
* |
149
|
|
|
* @param string $username |
150
|
|
|
* @return User|null |
151
|
|
|
*/ |
152
|
|
|
public function getUser($username) |
153
|
|
|
{ |
154
|
|
|
$resellers = $this->getUsers(); |
155
|
|
|
return isset($resellers[$username]) ? $resellers[$username] : null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns all users for this reseller. |
160
|
|
|
* |
161
|
|
|
* @return User[] Associative array of users |
162
|
|
|
*/ |
163
|
|
|
public function getUsers() |
164
|
|
|
{ |
165
|
|
|
return BaseObject::toObjectArray($this->invokeGet('SHOW_USERS'), User::class, $this); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Impersonates a user, allowing the reseller/admin to act on their behalf. |
170
|
|
|
* |
171
|
|
|
* @param string $username Login of the account to impersonate |
172
|
|
|
* @param bool $validate Whether to check the user exists and is a user |
173
|
|
|
* @return UserContext |
174
|
|
|
*/ |
175
|
|
|
public function impersonateUser($username, $validate = false) |
176
|
|
|
{ |
177
|
|
|
return new UserContext($this->getConnection()->loginAs($username), $validate); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.