Skip to content

Commit d1fb173

Browse files
authored
Merge pull request #88 from LaswitchTech/dev
General: Version bumped to v0.0.88
2 parents 31357cb + 44789da commit d1fb173

6 files changed

Lines changed: 131 additions & 50 deletions

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.0.87
1+
v0.0.88

View/330.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<h2 class="h4 mb-1"><?= $this->Locale->get('Enter your verification code') ?></h2>
2323
<p class="opacity-50 mb-0"><?= $this->Locale->get('We sent you a code to your email') ?></p>
2424
</div>
25-
<form method="POST" action="<?= $this->Request->getHostAddress() . '/' . $this->Request->getUri() ?>" class="needs-validation" autocomplete="off" novalidate>
25+
<form method="POST" action="<?= $this->Request->getHostAddress() . $this->Request->getUri() ?>" class="needs-validation" autocomplete="off" novalidate>
2626
<?= $this->CSRF->field(); ?>
2727
<input type="email" class="d-none" name="username" id="username" autocomplete="off" value="<?=$this->Request->getParams('POST','username')?>">
2828
<div class="mb-3">

View/432.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
<h2 class="h4 mb-1"><?= $this->Locale->get('Enter your verification code') ?></h2>
55
<p class="opacity-50 mb-0"><?= $this->Locale->get('We sent you a code to your email') ?></p>
66
</div>
7-
<form method="POST" action="<?= $this->Request->getHostAddress() . '/' . $this->Request->getUri() ?>" class="needs-validation" autocomplete="off" novalidate>
7+
<form method="POST" action="<?= $this->Request->getHostAddress() . $this->Request->getUri() . '?' . $this->Request->getQueryString() ?>" class="needs-validation" autocomplete="off" novalidate>
88
<?= $this->CSRF->field(); ?>
99
<div class="mb-3">
1010
<label for="code" class="form-label"><?= $this->Locale->get('Code') ?></label>
1111
<input type="text" class="form-control" name="code" id="code" autocomplete="off" placeholder="000000" value="<?=$this->Request->getParams('GET','verify')?>" required>
1212
<div class="invalid-feedback"><?= $this->Locale->get('Please enter a valid pin.') ?></div>
1313
</div>
14+
<div class="d-flex justify-content-end align-items-center mb-4">
15+
<a href="<?= $this->Request->getHostAddress() . $this->Request->getUri() . '?' . $this->Request->getQueryString() ?>&resend"><?= $this->Locale->get('Request a new code') ?><i class="bi bi-arrow-counterclockwise ms-1"></i></a>
16+
</div>
1417
<div class="d-flex justify-content-between align-items-center gap-3 mt-4 mb-2">
1518
<button type="button" class="btn btn-outline-secondary" onclick="window.history.back();"><i class="bi bi-arrow-left me-1"></i><?= $this->Locale->get('Go Back') ?></button>
1619
<button type="submit" class="btn btn-primary flex-grow-1" name="reset"><?= $this->Locale->get('Continue') ?></button>

src/Auth.php

Lines changed: 106 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Objects/Pin.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function verify(string $code): bool
143143
* @param string $pin
144144
* @return bool
145145
*/
146-
public function notify(object $user, string $pin): bool
146+
public function notify(object $user, string $pin, callable $fn): bool
147147
{
148148
// Import Global Variables
149149
global $SMTP, $CONFIG, $REQUEST;
@@ -163,23 +163,12 @@ public function notify(object $user, string $pin): bool
163163
// Check if the SMTP Server is authenticated
164164
if($SMTP->isAuthenticated()){
165165

166-
// Write the email
167-
$body = '';
168-
$body .= '<p>Did you request a new password?</p>';
169-
$body .= '<p>Here is your verification code:</p>';
170-
$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>';
171-
$body .= '<p>Please follow the link below to reset your password.</p>';
172-
$body .= '<p style="text-align:center;margin-top: 40px;margin-bottom:40px;">';
173-
$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>';
174-
$body .= '</p>';
175-
$body .= '<p>If you did not request this code, please contact your system administrator immediately.</p>';
176-
177166
// Create a new message
178-
$eml = $SMTP->message()
179-
->to($user->username)
167+
$eml = $fn($user,$pin);
168+
169+
// Configure the message
170+
$eml->to($user->username)
180171
->from($user->organization()->email ?? $CONFIG->get('smtp','username'))
181-
->subject('Reset your password')
182-
->body($body)
183172
->var('logo', 'data:'.mime_content_type($CONFIG->root() . '/webroot' . $this->logo()).';base64,' . base64_encode(file_get_contents($CONFIG->root() . '/webroot' . $this->logo())))
184173
->var('brand', $CONFIG->get('application','name'))
185174
->var('greetings', "Sincerely,<br>".$user->organization()->name."'s Team");

src/Objects/User.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,18 @@ public function token(): string
333333
{
334334
return $this->token;
335335
}
336+
337+
/**
338+
* Verify the User
339+
*
340+
* @return bool
341+
*/
342+
public function verify(): bool
343+
{
344+
$query = $this->Database->query();
345+
$query->table('users')
346+
->update(['isVerified' => 1])
347+
->where('id', $this->user['id']);
348+
return $query->execute() > 0;
349+
}
336350
}

0 commit comments

Comments
 (0)