PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / api / user.php
vikappointments / site / helpers / libraries / api Last commit date
implementors 1 month ago plugins 1 month ago api.php 1 month ago autoload.php 1 month ago error.php 1 month ago event.php 1 month ago index.html 1 month ago response.php 1 month ago user.php 1 month ago
user.php
229 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * VikAppointments API base user.
16 * This class is used from the framework to connect the users.
17 *
18 * @see VAPApiEvent
19 *
20 * @since 1.7
21 */
22 abstract class VAPApiUser
23 {
24 /**
25 * The username of the user, required to login.
26 *
27 * @var string
28 */
29 private $username;
30
31 /**
32 * The password of the user, required to login.
33 *
34 * @var string
35 */
36 private $password;
37
38 /**
39 * The ID of the user, assigned after a successful login.
40 *
41 * @var integer
42 */
43 private $id = null;
44
45 /**
46 * The origin IP address from which the user is trying to connect.
47 *
48 * @var string
49 */
50 private $sourceIp;
51
52 /**
53 * A temporary array to maintain the provided credentials in case they don't match the requirements.
54 * This variable is useful to return always the details provided from the user,
55 * because in case of failure the credentials may be unset.
56 *
57 * @var array
58 */
59 private $failure = array();
60
61 /**
62 * Class constructor.
63 *
64 * @param string $username The username of the user for login.
65 * @param string $password The password of the user for login.
66 * @param string $ip The IP address from which the user is trying to login.
67 *
68 * @uses isUsernameAccepted() Validate if the provided username mets the structure requirements.
69 * @uses isPasswordAccepted() Validate if the provided password mets the structure requirements.
70 * @uses hashMask() Mask the password with the chosen hash algorithm.
71 */
72 public function __construct($username, $password, $ip = null)
73 {
74 // create a temporary credentials array
75 $this->failure = array('', '');
76
77 // check if the username can be accepted
78 if ($this->isUsernameAccepted($username))
79 {
80 // assign it to this class
81 $this->username = $username;
82 }
83 else
84 {
85 // otherwise push it into the temporary array
86 $this->failure[0] = $username;
87 }
88
89 // check if the password can be accepted
90 if ($this->isPasswordAccepted($password))
91 {
92 // mask the password and assign it to this class
93 $this->password = $this->hashMask($password);
94 }
95 else
96 {
97 // otherwise push it into the temporary array
98 $this->failure[1] = $password;
99 }
100
101 $this->sourceIp = $ip;
102 }
103
104 /**
105 * Get the username of the user.
106 * The username is not empty only if it is verified from the constructor.
107 *
108 * @return string The username of the user.
109 *
110 * @see getCredentials() Get always the provided credentials, also in failure cases.
111 */
112 public function getUsername()
113 {
114 return $this->username;
115 }
116
117 /**
118 * Get the password of the user.
119 * The password is not empty only if it is verified from the constructor.
120 *
121 * @return string The password of the user.
122 *
123 * @see getCredentials() Get always the provided credentials, also in failure cases.
124 */
125 public function getPassword()
126 {
127 return $this->password;
128 }
129
130 /**
131 * Get the credentials of the user, also in failure cases.
132 *
133 * @return object An object containing the credentials of the user.
134 */
135 public function getCredentials()
136 {
137 $credentials = new stdClass;
138
139 // if username is not empty (accepted) return it, otherwise return failure[0]
140 $credentials->username = (!empty($this->username) ? $this->username : $this->failure[0]);
141 // if password is not empty (accepted) return it, otherwise return failure[1]
142 $credentials->password = (!empty($this->password) ? $this->password : $this->failure[1]);
143
144 return $credentials;
145 }
146
147 /**
148 * Set the ID of the user after a successful login.
149 * By setting an ID through this method, the framework assumes that the user is currently connected.
150 *
151 * @return self This object to support chaining.
152 */
153 public function assign($id)
154 {
155 $this->id = $id;
156
157 return $this;
158 }
159
160 /**
161 * Get the ID of the user. Return NULL in case the user is not yet connected.
162 *
163 * @return integer The ID of the user or NULL.
164 */
165 public function id()
166 {
167 return $this->id;
168 }
169
170 /**
171 * Return true if the credentials provided match the strcture requirements.
172 * When true, it is possible to proceed with the login check.
173 *
174 * @return boolean True if the username and password are not empty (accepted).
175 */
176 public function isConnectable()
177 {
178 return strlen($this->username) && strlen($this->password);
179 }
180
181 /**
182 * Get the origin IP address from which the user is trying to connect.
183 *
184 * @return string The IP address if provided, otherwise NULL.
185 */
186 public function getSourceIp()
187 {
188 return $this->sourceIp;
189 }
190
191 /**
192 * Check if the user is able to perform the event provided.
193 *
194 * @param VAPApiEvent $event The event to authorise.
195 *
196 * @return boolean True if the event can be performed, otherwise false.
197 */
198 abstract public function authorise(VAPApiEvent $event);
199
200 /**
201 * Return true if the given username owns a valid structure.
202 * In this function it is possible to check minimum length, minimum digits and so on.
203 *
204 * @param string $username The username to check.
205 *
206 * @return boolean True in case the username is valid.
207 */
208 abstract protected function isUsernameAccepted($username);
209
210 /**
211 * Return true if the given password owns a valid structure.
212 * In this function it is possible to check minimum length, minimum digits and so on.
213 *
214 * @param string $password The password to check.
215 *
216 * @return boolean True in case the password is valid.
217 */
218 abstract protected function isPasswordAccepted($password);
219
220 /**
221 * Return the hash of the specified password to mask it.
222 *
223 * @param string $password The password to mask.
224 *
225 * @return string The hash of the password masked.
226 */
227 abstract protected function hashMask($password);
228 }
229