@@ -58,35 +58,35 @@ public function isAuthenticated(): bool
5858 }
5959
6060 // Check Bearer Token
61- if ($ this ->authenticateBearer ()) {
61+ if ($ this ->byBearerToken ()) {
6262 $ this ->method = 'bearer ' ;
6363 $ this ->status = true ;
6464 return $ this ->status ;
6565 }
6666
6767 // Check Basic Authentication
68- if ($ this ->authenticateBasic ()) {
68+ if ($ this ->byBasicAuth ()) {
6969 $ this ->method = 'basic ' ;
7070 $ this ->status = true ;
7171 return $ this ->status ;
7272 }
7373
7474 // Check Session Authentication
75- if ($ this ->authenticateSession ()) {
75+ if ($ this ->bySession ()) {
7676 $ this ->method = 'session ' ;
7777 $ this ->status = true ;
7878 return $ this ->status ;
7979 }
8080
8181 // Check Cookie Authentication
82- if ($ this ->authenticateCookie ()) {
82+ if ($ this ->byCookie ()) {
8383 $ this ->method = 'cookie ' ;
8484 $ this ->status = true ;
8585 return $ this ->status ;
8686 }
8787
8888 // Check Request Authentication
89- if ($ this ->authenticateRequest ()) {
89+ if ($ this ->byRequest ()) {
9090 $ this ->method = 'request ' ;
9191 $ this ->status = true ;
9292 return $ this ->status ;
@@ -125,25 +125,50 @@ protected function isResetting(): void
125125 return ;
126126 }
127127
128- // echo 'code: '; var_dump($REQUEST->getParams('REQUEST','code'));
129- // echo 'username: '; var_dump($REQUEST->getParams('REQUEST','username'));
130- // echo 'forgot: '; var_dump($REQUEST->getParams('REQUEST','forgot'));
131- // echo 'reset: '; var_dump($REQUEST->getParams('REQUEST','reset'));
132- // echo 'verify: '; var_dump($REQUEST->getParams('REQUEST','verify'));
133128 if (
134129 $ REQUEST ->getParams ('REQUEST ' ,'code ' ) &&
135130 $ REQUEST ->getParams ('REQUEST ' ,'username ' ) &&
136131 !is_null ($ REQUEST ->getParams ('REQUEST ' ,'reset ' )) &&
137132 is_null ($ REQUEST ->getParams ('REQUEST ' ,'forgot ' )) &&
138133 is_null ($ REQUEST ->getParams ('REQUEST ' ,'verify ' ))
139134 ) {
140- $ this ->verifyPin ($ REQUEST ->getParams ('REQUEST ' ,'username ' ), $ REQUEST ->getParams ('REQUEST ' ,'code ' ));
135+ $ this ->verifyPin ($ REQUEST ->getParams ('REQUEST ' ,'username ' ), $ REQUEST ->getParams ('REQUEST ' ,'code ' ),function (object $ user ){
136+
137+ // Reset the user's password
138+ $ password = $ user ->backend ()->reset ();
139+
140+ // Notify the user of the new password
141+ $ this ->requested = $ user ->backend ()->notify ($ user ,$ password );
142+ });
141143 } elseif (
142144 $ REQUEST ->getParams ('REQUEST ' ,'username ' ) &&
143145 !is_null ($ REQUEST ->getParams ('REQUEST ' ,'forgot ' )) &&
144146 !is_null ($ REQUEST ->getParams ('REQUEST ' ,'reset ' ))
145147 ) {
146- $ this ->setPin ($ REQUEST ->getParams ('REQUEST ' ,'username ' ));
148+ $ this ->setPin ($ REQUEST ->getParams ('REQUEST ' ,'username ' ), function (object $ user , string $ pin ){
149+
150+ // Import Global Variables
151+ global $ SMTP , $ REQUEST ;
152+
153+ // Write the email
154+ $ body = '' ;
155+ $ body .= '<p>Did you request a new password?</p> ' ;
156+ $ body .= '<p>Here is your verification code:</p> ' ;
157+ $ body .= '<pre style="background-color: #F5F5F5; font-weight: 700; font-size: 28px; text-align: center; letter-spacing: 16px; margin: 20px 20px; padding: 20px 0; font-family: Courier, monospace"> ' .($ pin ?? 'ERROR! ' ).'</pre> ' ;
158+ $ body .= '<p>Please follow the link below to reset your password.</p> ' ;
159+ $ body .= '<p style="text-align:center;margin-top: 40px;margin-bottom:40px;"> ' ;
160+ $ body .= '<a href=" ' .$ REQUEST ->getHostAddress ().'?forgot&verify= ' .$ pin .'&username= ' .$ user ->username .'" target="_blank" style="margin-left: 6px; margin-right: 6px; text-decoration:none; background-color: #528fb3;color: #fff;font-size: 24px;padding: 20px 40px;text-align: center;margin: 20px 20px;border-radius: 8px;">Reset</a> ' ;
161+ $ body .= '</p> ' ;
162+ $ body .= '<p>If you did not request this code, please contact your system administrator immediately.</p> ' ;
163+
164+ // Create a new message
165+ $ eml = $ SMTP ->message ()
166+ ->subject ('Reset your password ' )
167+ ->body ($ body );
168+
169+ // Return the message
170+ return $ eml ;
171+ });
147172 }
148173 }
149174
@@ -153,7 +178,7 @@ protected function isResetting(): void
153178 * @param string $username
154179 * @return bool
155180 */
156- protected function setPin (string $ username ): void
181+ protected function setPin (string $ username, callable $ fn ): void
157182 {
158183 // Retrieve User
159184 $ user = $ this ->user ($ username );
@@ -171,7 +196,7 @@ protected function setPin(string $username): void
171196 $ Pin ->save ($ user ->id ,$ pin );
172197
173198 // Send the pin to the user email
174- $ this ->requested = $ Pin ->notify ($ user ,$ pin );
199+ $ this ->requested = $ Pin ->notify ($ user ,$ pin, $ fn );
175200 }
176201 }
177202
@@ -182,7 +207,7 @@ protected function setPin(string $username): void
182207 * @param string $code
183208 * @return bool
184209 */
185- protected function verifyPin (string $ username , string $ code ): void
210+ protected function verifyPin (string $ username , string $ code, callable $ fn ): void
186211 {
187212 // Retrieve User
188213 $ user = $ this ->user ($ username );
@@ -196,11 +221,8 @@ protected function verifyPin(string $username, string $code): void
196221 // Verify the pin
197222 if ($ Pin ->verify ($ code )){
198223
199- // Reset the user's password
200- $ password = $ user ->backend ()->reset ();
201-
202- // Notify the user of the new password
203- $ this ->requested = $ user ->backend ()->notify ($ user ,$ password );
224+ // Execute the callable function
225+ $ fn ($ user );
204226 }
205227 }
206228 }
@@ -240,7 +262,7 @@ public function method(): string
240262 *
241263 * @return bool
242264 */
243- protected function authenticateBearer (): bool
265+ protected function byBearerToken (): bool
244266 {
245267 // Import Global Variables
246268 global $ REQUEST ;
@@ -303,7 +325,7 @@ protected function authenticateBearer(): bool
303325 *
304326 * @return bool
305327 */
306- protected function authenticateBasic (): bool
328+ protected function byBasicAuth (): bool
307329 {
308330 // Import Global Variables
309331 global $ REQUEST ;
@@ -337,7 +359,7 @@ protected function authenticateBasic(): bool
337359 *
338360 * @return bool
339361 */
340- protected function authenticateCookie (): bool
362+ protected function byCookie (): bool
341363 {
342364 // Import Global Variables
343365 global $ REQUEST ;
@@ -354,7 +376,7 @@ protected function authenticateCookie(): bool
354376 *
355377 * @return bool
356378 */
357- protected function authenticateSession (): bool
379+ protected function bySession (): bool
358380 {
359381 // Import Global Variables
360382 global $ REQUEST , $ UUID ;
@@ -387,7 +409,7 @@ protected function authenticateSession(): bool
387409 *
388410 * @return bool
389411 */
390- protected function authenticateRequest (): bool
412+ protected function byRequest (): bool
391413 {
392414 // Import Global Variables
393415 global $ REQUEST ;
@@ -438,12 +460,6 @@ protected function authenticate(mixed $user, ?string $password = null): bool
438460 // Check if the user is deleted
439461 $ status = !$ this ->user ->deleted ();
440462
441- // Check if the user is banned
442- $ status = ($ status && !$ this ->user ->banned ());
443-
444- // Check if the user is verified
445- $ status = ($ status && $ this ->user ->verified ());
446-
447463 // Check if the user's organization is active
448464 $ status = ($ status && $ this ->user ->organization ['isActive ' ] > 0 );
449465
@@ -456,6 +472,65 @@ protected function authenticate(mixed $user, ?string $password = null): bool
456472 // Set Session
457473 $ this ->user ->session ()->create ();
458474
475+ // Check if the user is verified
476+ if (!$ this ->user ->verified ()){
477+
478+ // Check if username is in request params
479+ if (is_null ($ REQUEST ->getParams ('REQUEST ' ,'username ' ))){
480+
481+ // Redirect to verification page (?username='.$user->username.')
482+ header ('Location: ?username= ' .$ user ->username );
483+ } else {
484+
485+ // Check if we should resend the verification pin
486+ if (is_null ($ user ->pin ['id ' ]) || !is_null ($ REQUEST ->getParams ('REQUEST ' ,'resend ' ))){
487+
488+ // Set a new pin
489+ $ this ->setPin ($ user ->username , function (object $ user , string $ pin ){
490+
491+ // Import Global Variables
492+ global $ SMTP , $ REQUEST ;
493+
494+ // Write the email
495+ $ body = '' ;
496+ $ body .= '<p>Here is your verification code:</p> ' ;
497+ $ body .= '<pre style="background-color: #F5F5F5; font-weight: 700; font-size: 28px; text-align: center; letter-spacing: 16px; margin: 20px 20px; padding: 20px 0; font-family: Courier, monospace"> ' .($ pin ?? 'ERROR! ' ).'</pre> ' ;
498+ $ body .= '<p>Please follow the link below to verify your acount.</p> ' ;
499+ $ body .= '<p style="text-align:center;margin-top: 40px;margin-bottom:40px;"> ' ;
500+ $ body .= '<a href=" ' .$ REQUEST ->getHostAddress ().'?username= ' .$ user ->username .'&verify= ' .$ pin .'" target="_blank" style="margin-left: 6px; margin-right: 6px; text-decoration:none; background-color: #528fb3;color: #fff;font-size: 24px;padding: 20px 40px;text-align: center;margin: 20px 20px;border-radius: 8px;">Verify</a> ' ;
501+ $ body .= '</p> ' ;
502+ $ body .= '<p>If you did not request this code, please contact your system administrator immediately.</p> ' ;
503+
504+ // Create a new message
505+ $ eml = $ SMTP ->message ()
506+ ->subject ('Account Verification ' )
507+ ->body ($ body );
508+
509+ // Return the message
510+ return $ eml ;
511+ });
512+ } else {
513+
514+ // Check if code is in request params
515+ if (!is_null ($ REQUEST ->getParams ('REQUEST ' ,'code ' ))){
516+
517+ // Verify the pin
518+ $ this ->verifyPin ($ user ->username , $ REQUEST ->getParams ('REQUEST ' ,'code ' ),function (object $ user ){
519+
520+ // Import Global Variables
521+ global $ REQUEST ;
522+
523+ // Verify the user
524+ $ user ->verify ();
525+
526+ // Redirect to original page
527+ header ('Location: ' .$ REQUEST ->getHostAddress () . $ REQUEST ->getUri ());
528+ });
529+ }
530+ }
531+ }
532+ }
533+
459534 return true ;
460535 }
461536 }
0 commit comments