2020-06-19 00:42:10 +02:00
|
|
|
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Controller\AppController;
|
|
|
|
use Cake\Utility\Hash;
|
|
|
|
use Cake\Utility\Text;
|
2021-03-15 22:47:13 +01:00
|
|
|
use Cake\ORM\TableRegistry;
|
2020-06-19 00:42:10 +02:00
|
|
|
use \Cake\Database\Expression\QueryExpression;
|
2021-10-20 22:29:23 +02:00
|
|
|
use Cake\Http\Exception\UnauthorizedException;
|
|
|
|
use Cake\Core\Configure;
|
2020-06-19 00:42:10 +02:00
|
|
|
|
|
|
|
class UsersController extends AppController
|
|
|
|
{
|
2021-11-24 01:32:05 +01:00
|
|
|
public $filterFields = ['Individuals.uuid', 'username', 'Individuals.email', 'Individuals.first_name', 'Individuals.last_name', 'Organisations.name'];
|
2021-09-10 11:55:54 +02:00
|
|
|
public $quickFilterFields = ['Individuals.uuid', ['username' => true], ['Individuals.first_name' => true], ['Individuals.last_name' => true], 'Individuals.email'];
|
2021-11-24 01:32:05 +01:00
|
|
|
public $containFields = ['Individuals', 'Roles', 'UserSettings', 'Organisations'];
|
2021-09-10 11:55:54 +02:00
|
|
|
|
2020-06-19 00:42:10 +02:00
|
|
|
public function index()
|
|
|
|
{
|
2021-11-24 01:32:05 +01:00
|
|
|
$currentUser = $this->ACL->getUser();
|
|
|
|
$conditions = [];
|
|
|
|
if (empty($currentUser['role']['perm_admin'])) {
|
|
|
|
$conditions['organisation_id'] = $currentUser['organisation_id'];
|
|
|
|
}
|
2020-06-19 00:42:10 +02:00
|
|
|
$this->CRUD->index([
|
2021-09-10 11:55:54 +02:00
|
|
|
'contain' => $this->containFields,
|
|
|
|
'filters' => $this->filterFields,
|
|
|
|
'quickFilters' => $this->quickFilterFields,
|
2021-11-24 01:32:05 +01:00
|
|
|
'conditions' => $conditions
|
2020-06-19 00:42:10 +02:00
|
|
|
]);
|
2021-06-29 16:15:05 +02:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|
|
|
|
$this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add()
|
|
|
|
{
|
2021-11-24 01:32:05 +01:00
|
|
|
$currentUser = $this->ACL->getUser();
|
2021-10-01 13:19:26 +02:00
|
|
|
$this->CRUD->add([
|
2021-11-24 01:32:05 +01:00
|
|
|
'beforeSave' => function($data) use ($currentUser) {
|
|
|
|
if (!$currentUser['role']['perm_admin']) {
|
|
|
|
$data['organisation_id'] = $currentUser['organisation_id'];
|
|
|
|
}
|
2021-10-01 13:19:26 +02:00
|
|
|
$this->Users->enrollUserRouter($data);
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
]);
|
2021-01-11 12:48:58 +01:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|
2021-11-24 01:32:05 +01:00
|
|
|
/*
|
|
|
|
$alignments = $this->Users->Individuals->Alignments->find('list', [
|
|
|
|
//'keyField' => 'id',
|
|
|
|
'valueField' => 'organisation_id',
|
|
|
|
'groupField' => 'individual_id'
|
|
|
|
])->toArray();
|
|
|
|
$alignments = array_map(function($value) { return array_values($value); }, $alignments);
|
|
|
|
*/
|
|
|
|
$org_conditions = [];
|
|
|
|
if (empty($currentUser['role']['perm_admin'])) {
|
|
|
|
$org_conditions = ['id' => $currentUser['organisation_id']];
|
|
|
|
}
|
2020-06-19 00:42:10 +02:00
|
|
|
$dropdownData = [
|
|
|
|
'role' => $this->Users->Roles->find('list', [
|
|
|
|
'sort' => ['name' => 'asc']
|
|
|
|
]),
|
|
|
|
'individual' => $this->Users->Individuals->find('list', [
|
|
|
|
'sort' => ['email' => 'asc']
|
2021-11-24 01:32:05 +01:00
|
|
|
]),
|
|
|
|
'organisation' => $this->Users->Organisations->find('list', [
|
|
|
|
'sort' => ['name' => 'asc'],
|
|
|
|
'conditions' => $org_conditions
|
2020-06-19 00:42:10 +02:00
|
|
|
])
|
|
|
|
];
|
|
|
|
$this->set(compact('dropdownData'));
|
|
|
|
$this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate');
|
|
|
|
}
|
|
|
|
|
2020-06-21 21:27:11 +02:00
|
|
|
public function view($id = false)
|
2020-06-19 00:42:10 +02:00
|
|
|
{
|
2020-06-21 21:27:11 +02:00
|
|
|
if (empty($id) || empty($this->ACL->getUser()['role']['perm_admin'])) {
|
|
|
|
$id = $this->ACL->getUser()['id'];
|
|
|
|
}
|
2020-06-19 00:42:10 +02:00
|
|
|
$this->CRUD->view($id, [
|
2021-11-24 01:32:05 +01:00
|
|
|
'contain' => ['Individuals' => ['Alignments' => 'Organisations'], 'Roles', 'Organisations']
|
2020-06-19 00:42:10 +02:00
|
|
|
]);
|
2021-06-29 16:15:05 +02:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|
|
|
|
$this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate');
|
|
|
|
}
|
|
|
|
|
2020-06-21 21:27:11 +02:00
|
|
|
public function edit($id = false)
|
2020-06-19 00:42:10 +02:00
|
|
|
{
|
2021-11-24 01:32:05 +01:00
|
|
|
$currentUser = $this->ACL->getUser();
|
|
|
|
if (empty($id) || (empty($currentUser['role']['perm_org_admin']) && empty($currentUser['role']['perm_site_admin']))) {
|
|
|
|
$id = $currentUser['id'];
|
2020-06-21 21:27:11 +02:00
|
|
|
}
|
2021-11-24 01:32:05 +01:00
|
|
|
|
2020-11-05 10:17:42 +01:00
|
|
|
$params = [
|
2020-06-21 21:27:11 +02:00
|
|
|
'get' => [
|
2020-11-05 10:17:42 +01:00
|
|
|
'fields' => [
|
|
|
|
'id', 'individual_id', 'role_id', 'username', 'disabled'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'removeEmpty' => [
|
|
|
|
'password'
|
|
|
|
],
|
|
|
|
'fields' => [
|
|
|
|
'id', 'individual_id', 'username', 'disabled', 'password', 'confirm_password'
|
2020-06-21 21:27:11 +02:00
|
|
|
]
|
2020-11-05 10:17:42 +01:00
|
|
|
];
|
|
|
|
if (!empty($this->ACL->getUser()['role']['perm_admin'])) {
|
|
|
|
$params['fields'][] = 'role_id';
|
2021-11-24 01:32:05 +01:00
|
|
|
$params['fields'][] = 'organisation_id';
|
2020-11-05 10:17:42 +01:00
|
|
|
}
|
|
|
|
$this->CRUD->edit($id, $params);
|
2021-01-11 12:48:58 +01:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|
|
|
|
$dropdownData = [
|
|
|
|
'role' => $this->Users->Roles->find('list', [
|
|
|
|
'sort' => ['name' => 'asc']
|
|
|
|
]),
|
|
|
|
'individual' => $this->Users->Individuals->find('list', [
|
|
|
|
'sort' => ['email' => 'asc']
|
2021-11-24 01:32:05 +01:00
|
|
|
]),
|
|
|
|
'organisation' => $this->Users->Organisations->find('list', [
|
|
|
|
'sort' => ['name' => 'asc']
|
2020-06-19 00:42:10 +02:00
|
|
|
])
|
|
|
|
];
|
|
|
|
$this->set(compact('dropdownData'));
|
|
|
|
$this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate');
|
|
|
|
$this->render('add');
|
|
|
|
}
|
|
|
|
|
2021-01-11 12:48:58 +01:00
|
|
|
public function toggle($id, $fieldName = 'disabled')
|
|
|
|
{
|
|
|
|
$this->CRUD->toggle($id, $fieldName);
|
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 00:42:10 +02:00
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
$this->CRUD->delete($id);
|
2021-01-11 12:48:58 +01:00
|
|
|
$responsePayload = $this->CRUD->getResponsePayload();
|
|
|
|
if (!empty($responsePayload)) {
|
|
|
|
return $responsePayload;
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|
|
|
|
$this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate');
|
|
|
|
}
|
2020-06-21 21:27:11 +02:00
|
|
|
|
|
|
|
public function login()
|
|
|
|
{
|
|
|
|
$result = $this->Authentication->getResult();
|
|
|
|
// If the user is logged in send them away.
|
2021-11-17 15:49:28 +01:00
|
|
|
$logModel = $this->Users->auditLogs();
|
2020-06-21 21:27:11 +02:00
|
|
|
if ($result->isValid()) {
|
2021-11-17 15:49:28 +01:00
|
|
|
$user = $logModel->userInfo();
|
|
|
|
$logModel->insert([
|
|
|
|
'request_action' => 'login',
|
|
|
|
'model' => 'Users',
|
|
|
|
'model_id' => $user['id'],
|
|
|
|
'model_title' => $user['name'],
|
2021-11-25 00:57:31 +01:00
|
|
|
'changed' => []
|
2021-11-17 15:49:28 +01:00
|
|
|
]);
|
2020-06-21 21:27:11 +02:00
|
|
|
$target = $this->Authentication->getLoginRedirect() ?? '/instance/home';
|
|
|
|
return $this->redirect($target);
|
|
|
|
}
|
|
|
|
if ($this->request->is('post') && !$result->isValid()) {
|
2021-11-17 15:49:28 +01:00
|
|
|
$logModel->insert([
|
|
|
|
'request_action' => 'login_fail',
|
|
|
|
'model' => 'Users',
|
|
|
|
'model_id' => 0,
|
|
|
|
'model_title' => 'unknown_user',
|
2021-11-25 00:57:31 +01:00
|
|
|
'changed' => []
|
2021-11-17 15:49:28 +01:00
|
|
|
]);
|
2020-06-21 21:27:11 +02:00
|
|
|
$this->Flash->error(__('Invalid username or password'));
|
|
|
|
}
|
|
|
|
$this->viewBuilder()->setLayout('login');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
$result = $this->Authentication->getResult();
|
|
|
|
if ($result->isValid()) {
|
2021-11-17 15:49:28 +01:00
|
|
|
$logModel = $this->Users->auditLogs();
|
|
|
|
$user = $logModel->userInfo();
|
|
|
|
$logModel->insert([
|
|
|
|
'request_action' => 'logout',
|
|
|
|
'model' => 'Users',
|
|
|
|
'model_id' => $user['id'],
|
|
|
|
'model_title' => $user['name'],
|
2021-11-25 00:57:31 +01:00
|
|
|
'changed' => []
|
2021-11-17 15:49:28 +01:00
|
|
|
]);
|
2020-06-21 21:27:11 +02:00
|
|
|
$this->Authentication->logout();
|
|
|
|
$this->Flash->success(__('Goodbye.'));
|
2021-04-30 23:59:53 +02:00
|
|
|
return $this->redirect(\Cake\Routing\Router::url('/users/login'));
|
2020-06-21 21:27:11 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 22:47:13 +01:00
|
|
|
|
2021-10-12 10:16:36 +02:00
|
|
|
public function settings()
|
|
|
|
{
|
|
|
|
$this->set('user', $this->ACL->getUser());
|
2021-10-18 13:28:26 +02:00
|
|
|
$all = $this->Users->UserSettings->getSettingsFromProviderForUser($this->ACL->getUser()['id'], true);
|
|
|
|
$this->set('settingsProvider', $all['settingsProvider']);
|
|
|
|
$this->set('settings', $all['settings']);
|
|
|
|
$this->set('settingsFlattened', $all['settingsFlattened']);
|
|
|
|
$this->set('notices', $all['notices']);
|
2021-10-12 10:16:36 +02:00
|
|
|
}
|
|
|
|
|
2021-03-15 22:47:13 +01:00
|
|
|
public function register()
|
|
|
|
{
|
2021-10-21 13:44:49 +02:00
|
|
|
if (empty(Configure::read('security.registration.self-registration'))) {
|
2021-10-20 22:29:23 +02:00
|
|
|
throw new UnauthorizedException(__('User self-registration is not open.'));
|
|
|
|
}
|
|
|
|
if ($this->request->is('post')) {
|
|
|
|
$data = $this->request->getData();
|
|
|
|
$this->InboxProcessors = TableRegistry::getTableLocator()->get('InboxProcessors');
|
|
|
|
$processor = $this->InboxProcessors->getProcessor('User', 'Registration');
|
|
|
|
$data = [
|
|
|
|
'origin' => $this->request->clientIp(),
|
|
|
|
'comment' => '-no comment-',
|
|
|
|
'data' => [
|
|
|
|
'username' => $data['username'],
|
|
|
|
'email' => $data['email'],
|
|
|
|
'first_name' => $data['first_name'],
|
|
|
|
'last_name' => $data['last_name'],
|
|
|
|
'password' => $data['password'],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$processorResult = $processor->create($data);
|
|
|
|
return $processor->genHTTPReply($this, $processorResult, ['controller' => 'Inbox', 'action' => 'index']);
|
|
|
|
}
|
|
|
|
$this->viewBuilder()->setLayout('login');
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|