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;
|
|
|
|
|
|
|
|
class UsersController extends AppController
|
|
|
|
{
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$this->CRUD->index([
|
|
|
|
'contain' => ['Individuals', 'Roles'],
|
|
|
|
'filters' => ['Users.email', 'uuid']
|
|
|
|
]);
|
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
return $this->restResponsePayload;
|
|
|
|
}
|
|
|
|
$this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add()
|
|
|
|
{
|
|
|
|
$this->CRUD->add();
|
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']
|
|
|
|
])
|
|
|
|
];
|
|
|
|
$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, [
|
|
|
|
'contain' => ['Individuals' => ['Alignments' => 'Organisations'], 'Roles']
|
|
|
|
]);
|
|
|
|
if ($this->ParamHandler->isRest()) {
|
|
|
|
return $this->restResponsePayload;
|
|
|
|
}
|
|
|
|
$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
|
|
|
{
|
2020-06-21 21:27:11 +02:00
|
|
|
if (empty($id) || empty($this->ACL->getUser()['role']['perm_admin'])) {
|
|
|
|
$id = $this->ACL->getUser()['id'];
|
|
|
|
}
|
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';
|
|
|
|
}
|
|
|
|
$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']
|
|
|
|
])
|
|
|
|
];
|
|
|
|
$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.
|
|
|
|
if ($result->isValid()) {
|
|
|
|
$target = $this->Authentication->getLoginRedirect() ?? '/instance/home';
|
|
|
|
return $this->redirect($target);
|
|
|
|
}
|
|
|
|
if ($this->request->is('post') && !$result->isValid()) {
|
|
|
|
$this->Flash->error(__('Invalid username or password'));
|
|
|
|
}
|
|
|
|
$this->viewBuilder()->setLayout('login');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
$result = $this->Authentication->getResult();
|
|
|
|
if ($result->isValid()) {
|
|
|
|
$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
|
|
|
|
|
|
|
public function register()
|
|
|
|
{
|
2021-03-18 08:51:11 +01:00
|
|
|
$this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor');
|
|
|
|
$processor = $this->requestProcessor->getProcessor('User', 'Registration');
|
2021-03-15 22:47:13 +01:00
|
|
|
$data = [
|
|
|
|
'origin' => '127.0.0.1',
|
|
|
|
'comment' => 'Hi there!, please create an account',
|
|
|
|
'data' => [
|
|
|
|
'username' => 'foobar',
|
|
|
|
'email' => 'foobar@admin.test',
|
|
|
|
'first_name' => 'foo',
|
|
|
|
'last_name' => 'bar',
|
|
|
|
],
|
|
|
|
];
|
2021-03-18 14:01:14 +01:00
|
|
|
$processorResult = $processor->create($data);
|
2021-03-19 11:54:43 +01:00
|
|
|
return $processor->genHTTPReply($this, $processorResult, ['controller' => 'Inbox', 'action' => 'index']);
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
2020-06-19 00:42:10 +02:00
|
|
|
}
|