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\Admin;
15 use Omines\DirectAdmin\Objects\Users\Reseller;
16 use Omines\DirectAdmin\Objects\Users\User;
17
18 /**
19 * Context for administrator functions.
20 *
21 * @author Niels Keurentjes <niels.keurentjes@omines.com>
22 */
23 class AdminContext extends ResellerContext
24 {
25 /**
26 * Creates a new Admin level account.
27 *
28 * @param string $username
29 * @param string $password
30 * @param string $email
31 * @return Admin The newly created Admin
32 */
33 public function createAdmin($username, $password, $email)
34 {
35 return $this->createAccount($username, $password, $email, [], 'ACCOUNT_ADMIN', Admin::class);
36 }
37
38 /**
39 * Creates a new Reseller level account.
40 *
41 * @param string $username
42 * @param string $password
43 * @param string $email
44 * @param string $domain
45 * @param string|array $package Either a package name or an array of options for custom
46 * @param string $ip shared, sharedreseller or assign. Defaults to 'shared'
47 * @return Reseller
48 * @url http://www.directadmin.com/api.html#create for options to use.
49 */
50 public function createReseller($username, $password, $email, $domain, $package = [], $ip = 'shared')
51 {
52 $options = array_merge(
53 ['ip' => $ip, 'domain' => $domain, 'serverip' => 'ON', 'dns' => 'OFF'],
54 is_array($package) ? $package : ['package' => $package]
55 );
56 return $this->createAccount($username, $password, $email, $options, 'ACCOUNT_RESELLER', Reseller::class);
57 }
58
59 /**
60 * Returns a list of known admins on the server.
61 *
62 * @return Admin[]
63 */
64 public function getAdmins()
65 {
66 return BaseObject::toObjectArray($this->invokeApiGet('SHOW_ADMINS'), Admin::class, $this);
67 }
68
69 /**
70 * Returns a full list of all accounts of any type on the server.
71 *
72 * @return User[]
73 */
74 public function getAllAccounts()
75 {
76 $accounts = array_merge($this->getAllUsers(), $this->getResellers(), $this->getAdmins());
77 ksort($accounts);
78 return $accounts;
79 }
80
81 /**
82 * Returns a full list of all users on the server, so no resellers or admins.
83 *
84 * @return User[]
85 */
86 public function getAllUsers()
87 {
88 return BaseObject::toObjectArray($this->invokeApiGet('SHOW_ALL_USERS'), User::class, $this);
89 }
90
91 /**
92 * Returns a specific reseller by name, or NULL if there is no reseller by this name.
93 *
94 * @param string $username
95 * @return null|Reseller
96 */
97 public function getReseller($username)
98 {
99 $resellers = $this->getResellers();
100 return isset($resellers[$username]) ? $resellers[$username] : null;
101 }
102
103 /**
104 * Returns the list of known resellers.
105 *
106 * @return Reseller[]
107 */
108 public function getResellers()
109 {
110 return BaseObject::toObjectArray($this->invokeApiGet('SHOW_RESELLERS'), Reseller::class, $this);
111 }
112
113 /**
114 * Returns a new AdminContext acting as the specified admin.
115 *
116 * @param string $username
117 * @param bool $validate Whether to check the admin exists and is an admin
118 * @return AdminContext
119 */
120 public function impersonateAdmin($username, $validate = false)
121 {
122 return new self($this->getConnection()->loginAs($username), $validate);
123 }
124
125 /**
126 * Returns a new ResellerContext acting as the specified reseller.
127 *
128 * @param string $username
129 * @param bool $validate Whether to check the reseller exists and is a reseller
130 * @return ResellerContext
131 */
132 public function impersonateReseller($username, $validate = false)
133 {
134 return new ResellerContext($this->getConnection()->loginAs($username), $validate);
135 }
136 }
137