cerebrate/src/Controller/UsersController.php

157 lines
5.1 KiB
PHP
Raw Normal View History

2020-06-19 00:42:10 +02:00
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Utility\Hash;
use Cake\Utility\Text;
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();
$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'];
}
$params = [
2020-06-21 21:27:11 +02:00
'get' => [
'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
]
];
if (!empty($this->ACL->getUser()['role']['perm_admin'])) {
$params['fields'][] = 'role_id';
}
$this->CRUD->edit($id, $params);
$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');
}
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);
$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
}
}
public function register()
{
$this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor');
$processor = $this->requestProcessor->getProcessor('User', 'Registration');
$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',
],
];
$processorResult = $processor->create($data);
return $processor->genHTTPReply($this, $processorResult, ['controller' => 'Inbox', 'action' => 'index']);
}
2020-06-19 00:42:10 +02:00
}