Skip to content

Commit

Permalink
UPD: account activation
Browse files Browse the repository at this point in the history
  • Loading branch information
Itskiprotich committed Aug 19, 2024
1 parent 8790a50 commit 3272f14
Show file tree
Hide file tree
Showing 20 changed files with 16,093 additions and 272 deletions.
2 changes: 1 addition & 1 deletion config/app_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'formGroup' => '<div class="form-group"> {{label}}{{input}} </div>',
'label' => '<div class="col-sm-4 control-label"><label {{attrs}}>{{text}}</label></div>',
// Generic input element.
'input' => '<div class="col-sm-6"><input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}/></div>',
'input' => '<div class="col-sm-10"><input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}/></div>',
// Container element used by control() when a field has an error.
'inputContainerError' => '<div class="input {{type}}{{required}} has-error">{{content}}{{error}}</div>',
// Error message wrapper elements.
Expand Down
25 changes: 25 additions & 0 deletions config/captcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
return [
'Captcha' => [
'table' => 'captchas',
'type' => 'math', // Ensure this is set to 'math'
'theme' => 'default',
'width' => 120,
'height' => 40,
'length' => 4,
'session' => 'session_id',
'ip' => 'ip',
'created' => 'created',
'used' => 'used',
'math' => [
'operators' => ['+', '-'], // Specify the operators you want to use
'minNumber' => 1,
'maxNumber' => 10,
],
]
];




?>
15,229 changes: 15,228 additions & 1 deletion doses.sql

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function bootstrap(): void
$this->addPlugin('Queue', ['routes' => false]);

Check failure on line 54 in src/Application.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to an undefined method App\Application::addPlugin().
$this->addPlugin('Migrations');

Check failure on line 55 in src/Application.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to an undefined method App\Application::addPlugin().
$this->addPlugin('Acl');

Check failure on line 56 in src/Application.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to an undefined method App\Application::addPlugin().

$this->addPlugin('Captcha');

Check failure on line 57 in src/Application.php

View workflow job for this annotation

GitHub Actions / Coding Standard & Static Analysis

Call to an undefined method App\Application::addPlugin().

if (PHP_SAPI === 'cli') {
$this->bootstrapCli();
Expand All @@ -73,8 +73,7 @@ public function bootstrap(): void
$this->addPlugin('DebugKit');
}

// Load more plugins here
$this->addPlugin('Captcha');
// Load more plugins here
$this->addPlugin('Josegonzalez/Upload');
$this->addPlugin('CakePdf');
}
Expand Down
6 changes: 6 additions & 0 deletions src/Controller/Admin/MeddrasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
*/
class MeddrasController extends AppController
{


public function autosync()
{
return $this->redirect($this->referer());
}
/**
* Index method
*
Expand Down
7 changes: 2 additions & 5 deletions src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ public function initialize(): void
$this->loadComponent('Flash');
$this->Notifications = $this->loadModel('Notifications');
$this->Messages = $this->loadModel('Messages');
$this->QueuedJobs = $this->loadModel('Queue.QueuedJobs');

// $this->loadHelper('Tools.Captcha', [
// 'type' => 'active'
// ]);
$this->QueuedJobs = $this->loadModel('Queue.QueuedJobs');

/*
* Enable the following component for recommended CakePHP form protection settings.
* see https://book.cakephp.org/4/en/controllers/components/form-protection.html
Expand Down
33 changes: 33 additions & 0 deletions src/Controller/AuthoritiesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace App\Controller;

use Cake\Event\EventInterface;

/**
* Authorities Controller
*
Expand All @@ -11,6 +13,37 @@
*/
class AuthoritiesController extends AppController
{
public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);
$this->Auth->allow('autocomplete');
}


public function autocomplete($query = null) {

$term = $this->request->getQuery('term');
$type = is_numeric($term) ? 'N' : 'A';

// $coders = $this->Authorities->finder($term, $type); // Assuming `finder` is a custom method

$coders = $this->Authorities->find('byTerm', ['term' => $term, 'type' => $type])->toArray();
$codes = [];
foreach ($coders as $value) {
$codes[] = [
'value' => $value->mah_company_email,
'label' => $value->mah_name,
'code' => $value->master_mah,
'addr' => $value->mah_company_address,
'phone' => $value->mah_company_telephone
];
}

$this->set([
'codes' => $codes,
'_serialize' => ['codes']
]);
}
/**
* Index method
*
Expand Down
17 changes: 0 additions & 17 deletions src/Controller/FacilityCodesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function wards()
'codes' => $codes,
'_serialize' => ['codes']
]);

}
public function autocomplete()
{
Expand All @@ -70,22 +69,6 @@ public function autocomplete()
'codes' => $codes,
'_serialize' => ['codes']
]);

// $this->RequestHandler->setContent('json', 'application/json');
// if (is_numeric($this->request->query['term'])) {
// $coders = $this->FacilityCode->finder($this->request->query['term'], 'N');
// } else {
// $coders = $this->FacilityCode->finder($this->request->query['term'], 'A');
// }
// $codes = array();
// foreach ($coders as $key => $value) {
// $codes[] = array(
// 'value' => $value['FacilityCode']['facility_code'], 'label' => $value['FacilityCode']['facility_name'], 'sub_county' => $value['FacilityCode']['district'],
// 'desc' => $value['FacilityCode']['county'], 'addr' => $value['FacilityCode']['official_address'], 'phone' => $value['FacilityCode']['official_mobile']
// );
// }
// $this->set('codes', $codes);
// $this->set('_serialize', 'codes');
}
/**
* Index method
Expand Down
73 changes: 46 additions & 27 deletions src/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function initialize(): void
parent::initialize();
$this->loadComponent('Paginator');
$this->Auth->allow('logout', 'activate', 'admin');
$this->loadComponent('Captcha.Captcha', [
'captchaType' => 'math', // or 'image'
]);
}
public function admin()
{
Expand Down Expand Up @@ -98,9 +101,14 @@ public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);
$this->Auth->allow([
'register', 'login', 'logout', 'activate',
'register',
'login',
'logout',
'activateAccount',
// 'admin',
'forgotPassword', 'resetPassword', 'view'
'forgotPassword',
'resetPassword',
'view'
]);
}

Expand All @@ -122,9 +130,7 @@ public function dashboard()
}
}

public function guest()
{
}
public function guest() {}
public function login()
{
if ($this->request->is('post')) {
Expand All @@ -144,7 +150,7 @@ public function login()
}
// Attempt to identify the user
$user = $this->Auth->identify();

if ($user) {
$this->Auth->setUser($user);

Expand All @@ -158,7 +164,7 @@ public function login()
}

// User is authenticated, handle redirect based on user group

switch ($user['role_id']) {
case '1':
return $this->redirect(['controller' => 'Users', 'action' => 'dashboard', 'prefix' => 'Admin']);
Expand Down Expand Up @@ -202,7 +208,8 @@ public function changePassword()
$user = $this->Auth->User();
$user = $this->Users->find('all', array(
'contain' => array(
'Designations', 'Counties'
'Designations',
'Counties'
),
'conditions' => array(
'Users.id' => $this->Auth->user('id')
Expand Down Expand Up @@ -231,14 +238,17 @@ public function reverseXOR($xor_id)

public function register()
{
// $this->Users->addBehavior('Captcha.Captcha');

$this->Users->addBehavior('Captcha.Captcha');

if ($this->Auth->user()) {
return $this->redirect($this->Auth->redirectUrl());
}

$user = $this->Users->newEmptyEntity();
if ($this->request->is('post')) {


// dd($this->request->getData());
$user = $this->Users->patchEntity($user, $this->request->getData());

Expand Down Expand Up @@ -281,17 +291,18 @@ public function register()

$html = new HtmlHelper(new \Cake\View\View());


$referenceLink = Router::url([
'controller' => 'users',
'action' => 'activate_account',
$user->id
], true);
$variables = array(
'name' => $user['name'],
'username' => $user['username'],
'email' => $user['email'],
'reference_link' => $html->link(
'Activate',
array(
'controller' => 'users',
'action' => 'activate_account', $user->id, $user->id, 'full_base' => true
),
$referenceLink,
array('escape' => false)
),
);
Expand All @@ -307,7 +318,7 @@ public function register()
'subject' => $subject,
'message' => $message
);

$this->QueuedJobs->createJob('GenericNotification', $datum);
$this->QueuedJobs->createJob('GenericEmail', $datum);
//
Expand All @@ -325,7 +336,10 @@ public function register()
'reference_link' => $html->link(
'Activate',
array(
'controller' => 'users', 'action' => 'activate_account', $user->id, $user->id,
'controller' => 'users',
'action' => 'activate_account',
$user->id,
$user->id,
'full_base' => true
),
array('escape' => false)
Expand All @@ -343,7 +357,7 @@ public function register()

$this->QueuedJobs->createJob('GenericNotification', $datum);
$this->QueuedJobs->createJob('GenericEmail', $datum);
}
}
return $this->redirect(['controller' => 'Pages', 'action' => 'home']);
} else {
$errorMessages = [];
Expand All @@ -365,21 +379,20 @@ public function register()

//TODO: Add forgot password functionality

public function activate($id = null)
public function activateAccount($id = null)
{
if ($id) {
$user = $this->Users->findByActivationKey($id)->first();
$user = $this->Users->get($id);

if ($user) {
$query = $this->Users->query();
$query->update()
->set(['is_active' => 1])
->where(['id' => $user->id])
->where(['id' => $id])
->execute();

$this->Flash->success(__('You have successfully activated your account.'));
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
$this->Flash->success(__('You have successfully activated your account. Please login to proceed'));
return $this->redirect('/');
} else {
$this->Flash->error(__('Invalid activation token.'));
$this->redirect('/');
Expand All @@ -401,7 +414,7 @@ public function forgotPassword()
$user = $this->Users->findByEmail($this->request->getData('email'))->first();
if ($user) {

$new_pass = date('sYmdHi', strtotime(Time::now()));
$new_pass = date('sYmdHi');
$hasher = new DefaultPasswordHasher();
$password = $hasher->hash($new_pass);
$query = $this->Users->query();
Expand All @@ -413,16 +426,22 @@ public function forgotPassword()
//Send registration confirm email
$this->loadModel('Queue.QueuedJobs');
$data = [
'email_address' => $user->email, 'user_id' => $user->id, 'type' => 'forgot_password_email', 'model' => 'Users',
'foreign_key' => $user->id, 'vars' => $user->toArray()
'email_address' => $user->email,
'user_id' => $user->id,
'type' => 'forgot_password_email',
'model' => 'Users',
'foreign_key' => $user->id,
'vars' => $user->toArray()
];
$pass = $this->Util->generateXOR($user->id);
$html = new HtmlHelper(new \Cake\View\View());
$data['vars']['name'] = (isset($user->name)) ? $user->name : 'Sir/Madam';
$data['vars']['new_password'] = $new_pass;
$data['vars']['pv_site'] = $html->link('MCAZ PV website', ['controller' => 'Pages', 'action' => 'home', '_full' => true]);
$data['vars']['reset_password_link'] = $html->link('Reset Password', [
'controller' => 'Users', 'action' => 'resetPassword', $pass,
'controller' => 'Users',
'action' => 'resetPassword',
$pass,
'_full' => true
]);
$this->QueuedJobs->createJob('GenericEmail', $data);
Expand Down
16 changes: 14 additions & 2 deletions src/Model/Table/AuthoritiesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Cake\Validation\Validator;

/**
* Authorities Model
* Authorities Model
*
* @method \App\Model\Entity\Authority newEmptyEntity()
* @method \App\Model\Entity\Authority newEntity(array $data, array $options = [])
Expand Down Expand Up @@ -38,10 +38,22 @@ public function initialize(array $config): void
parent::initialize($config);

$this->setTable('authorities');
$this->setDisplayField('id');
$this->setDisplayField('mah_name');
$this->setPrimaryKey('id');
}
public function findByTerm(Query $query, array $options): Query
{
$term = $options['term'] ?? '';
$type = $options['type'] ?? '';

if ($type === 'N') {
// Example: searching by numeric term
return $query->where(['mah_company_email LIKE' => "%$term%"]);
} else {
// Example: searching by alphanumeric term
return $query->where(['mah_company_email LIKE' => "%$term%"]);
}
}
/**
* Default validation rules.
*
Expand Down
Loading

0 comments on commit 3272f14

Please sign in to comment.