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\DirectAdmin;
14 use Omines\DirectAdmin\DirectAdminException;
15 use Omines\DirectAdmin\Objects\Domain;
16 use Omines\DirectAdmin\Objects\Users\User;
17
18 /**
19 * Context for user functions.
20 *
21 * @author Niels Keurentjes <niels.keurentjes@omines.com>
22 */
23 class UserContext extends BaseContext
24 {
25 /** @var User */
26 private $user;
27
28 /**
29 * Constructs the object.
30 *
31 * @param DirectAdmin $connection A prepared connection
32 * @param bool $validate Whether to check if the connection matches the context
33 */
34 public function __construct(DirectAdmin $connection, $validate = false)
35 {
36 parent::__construct($connection);
37 if ($validate) {
38 $classMap = [
39 DirectAdmin::ACCOUNT_TYPE_ADMIN => AdminContext::class,
40 DirectAdmin::ACCOUNT_TYPE_RESELLER => ResellerContext::class,
41 DirectAdmin::ACCOUNT_TYPE_USER => self::class,
42 ];
43 if ($classMap[$this->getType()] != get_class($this)) {
44 /* @codeCoverageIgnoreStart */
45 throw new DirectAdminException('Validation mismatch on context construction');
46 /* @codeCoverageIgnoreEnd */
47 }
48 }
49 }
50
51 /**
52 * Returns the type of the account (user/reseller/admin).
53 *
54 * @return string One of the DirectAdmin::ACCOUNT_TYPE_ constants describing the type of underlying account
55 */
56 public function getType()
57 {
58 return $this->getContextUser()->getType();
59 }
60
61 /**
62 * Returns the actual user object behind the context.
63 *
64 * @return User The user object behind the context
65 */
66 public function getContextUser()
67 {
68 if (!isset($this->user)) {
69 $this->user = User::fromConfig($this->invokeApiGet('SHOW_USER_CONFIG'), $this);
70 }
71 return $this->user;
72 }
73
74 /**
75 * Returns a domain managed by the current user.
76 *
77 * @param string $domainName The requested domain name
78 * @return null|Domain The domain if found, or NULL if it does not exist
79 */
80 public function getDomain($domainName)
81 {
82 return $this->getContextUser()->getDomain($domainName);
83 }
84
85 /**
86 * Returns a full list of the domains managed by the current user.
87 *
88 * @return Domain[]
89 */
90 public function getDomains()
91 {
92 return $this->getContextUser()->getDomains();
93 }
94
95 /**
96 * Returns the username of the current context.
97 *
98 * @return string Username for the current context
99 */
100 public function getUsername()
101 {
102 return $this->getConnection()->getUsername();
103 }
104 }
105