2021-03-15 22:47:13 +01:00
|
|
|
<?php
|
|
|
|
use Cake\ORM\TableRegistry;
|
2021-10-20 22:29:23 +02:00
|
|
|
use Authentication\PasswordHasher\DefaultPasswordHasher;
|
2021-03-15 22:47:13 +01:00
|
|
|
|
2021-06-18 11:09:19 +02:00
|
|
|
require_once(ROOT . DS . 'libraries' . DS . 'default' . DS . 'InboxProcessors' . DS . 'GenericInboxProcessor.php');
|
2021-03-15 22:47:13 +01:00
|
|
|
|
2021-06-18 11:09:19 +02:00
|
|
|
class UserInboxProcessor extends GenericInboxProcessor
|
2021-03-15 22:47:13 +01:00
|
|
|
{
|
|
|
|
protected $scope = 'User';
|
2021-03-18 08:51:11 +01:00
|
|
|
protected $action = 'not-specified'; //overriden when extending
|
|
|
|
protected $description = ''; // overriden when extending
|
2021-03-15 22:47:13 +01:00
|
|
|
protected $registeredActions = [
|
|
|
|
'Registration'
|
|
|
|
];
|
2021-03-22 16:17:56 +01:00
|
|
|
protected $Users;
|
2021-03-15 22:47:13 +01:00
|
|
|
|
|
|
|
public function __construct($loadFromAction=false) {
|
|
|
|
parent::__construct($loadFromAction);
|
2021-03-19 11:22:04 +01:00
|
|
|
$this->Users = TableRegistry::getTableLocator()->get('Users');
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function create($requestData)
|
|
|
|
{
|
2021-03-18 14:01:14 +01:00
|
|
|
return parent::create($requestData);
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:30:32 +02:00
|
|
|
class RegistrationProcessor extends UserInboxProcessor implements GenericInboxProcessorActionI {
|
2021-03-18 08:51:11 +01:00
|
|
|
public $action = 'Registration';
|
2021-03-15 22:47:13 +01:00
|
|
|
protected $description;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2022-11-15 11:14:25 +01:00
|
|
|
$this->severity = $this->Inbox::SEVERITY_WARNING;
|
2021-03-15 22:47:13 +01:00
|
|
|
$this->description = __('Handle user account for this cerebrate instance');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addValidatorRules($validator)
|
|
|
|
{
|
|
|
|
return $validator
|
2022-03-01 11:40:18 +01:00
|
|
|
->notEmptyString('username', 'A username must be provided.')
|
2021-03-15 22:47:13 +01:00
|
|
|
->add('email', 'validFormat', [
|
|
|
|
'rule' => 'email',
|
|
|
|
'message' => 'E-mail must be valid'
|
|
|
|
])
|
2022-03-01 11:40:18 +01:00
|
|
|
->notEmptyString('first_name', 'A first name must be provided')
|
|
|
|
->notEmptyString('last_name', 'A last name must be provided')
|
2021-10-20 22:29:23 +02:00
|
|
|
->add('password', 'password_complexity', [
|
|
|
|
'rule' => function($value, $context) {
|
|
|
|
if (!preg_match('/^((?=.*\d)|(?=.*\W+))(?![\n])(?=.*[A-Z])(?=.*[a-z]).*$|.{16,}/s', $value) || strlen($value) < 12) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
'message' => __('Invalid password. Passwords have to be either 16 character long or 12 character long with 3/4 special groups.')
|
|
|
|
]);
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function create($requestData) {
|
|
|
|
$this->validateRequestData($requestData);
|
2021-10-20 22:29:23 +02:00
|
|
|
$requestData['data']['password'] = (new DefaultPasswordHasher())->hash($requestData['data']['password']);
|
2021-03-15 22:47:13 +01:00
|
|
|
$requestData['title'] = __('User account creation requested for {0}', $requestData['data']['email']);
|
2021-10-20 22:29:23 +02:00
|
|
|
$creationResponse = parent::create($requestData);
|
|
|
|
$creationResponse['message'] = __('User account creation requested. Please wait for an admin to approve your account.');
|
|
|
|
return $creationResponse;
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 16:17:56 +01:00
|
|
|
public function getViewVariables($request)
|
2021-03-15 22:47:13 +01:00
|
|
|
{
|
|
|
|
$dropdownData = [
|
|
|
|
'role' => $this->Users->Roles->find('list', [
|
|
|
|
'sort' => ['name' => 'asc']
|
|
|
|
]),
|
|
|
|
'individual' => [-1 => __('-- New individual --')] + $this->Users->Individuals->find('list', [
|
|
|
|
'sort' => ['email' => 'asc']
|
2022-12-12 16:02:41 +01:00
|
|
|
])->toArray(),
|
|
|
|
'organisation' => $this->Users->Organisations->find('list', [
|
|
|
|
'sort' => ['email' => 'asc']
|
|
|
|
])->toArray(),
|
2021-03-15 22:47:13 +01:00
|
|
|
];
|
2022-12-12 16:02:41 +01:00
|
|
|
|
|
|
|
$defaultRole = $this->Users->Roles->find()->select(['id'])->where(['is_default' => true])->first()->toArray();
|
|
|
|
|
|
|
|
$conditions = [];
|
|
|
|
if (!empty($request['data']['org_uuid'])) {
|
|
|
|
$conditions['uuid'] = $request['data']['org_uuid'];
|
|
|
|
} else if (!empty($request['data']['org_name'])) {
|
|
|
|
$conditions['name'] = $request['data']['org_name'];
|
|
|
|
}
|
|
|
|
$desiredOrg = null;
|
|
|
|
if (!empty($conditions)) {
|
|
|
|
$desiredOrg = $this->Users->Organisations->find()
|
|
|
|
->where($conditions)
|
|
|
|
->first()
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2021-03-15 22:47:13 +01:00
|
|
|
$individualEntity = $this->Users->Individuals->newEntity([
|
|
|
|
'email' => !empty($request['data']['email']) ? $request['data']['email'] : '',
|
|
|
|
'first_name' => !empty($request['data']['first_name']) ? $request['data']['first_name'] : '',
|
|
|
|
'last_name' => !empty($request['data']['last_name']) ? $request['data']['last_name'] : '',
|
|
|
|
'position' => !empty($request['data']['position']) ? $request['data']['position'] : '',
|
|
|
|
]);
|
|
|
|
$userEntity = $this->Users->newEntity([
|
|
|
|
'individual_id' => -1,
|
|
|
|
'username' => !empty($request['data']['username']) ? $request['data']['username'] : '',
|
|
|
|
'role_id' => !empty($request['data']['role_id']) ? $request['data']['role_id'] : '',
|
|
|
|
'disabled' => !empty($request['data']['disabled']) ? $request['data']['disabled'] : '',
|
2022-12-12 16:02:41 +01:00
|
|
|
'org_id' => !empty($desiredOrg) ? $desiredOrg['id'] : '',
|
2021-10-20 22:29:23 +02:00
|
|
|
|
|
|
|
'email' => !empty($request['data']['email']) ? $request['data']['email'] : '',
|
|
|
|
'first_name' => !empty($request['data']['first_name']) ? $request['data']['first_name'] : '',
|
|
|
|
'last_name' => !empty($request['data']['last_name']) ? $request['data']['last_name'] : '',
|
|
|
|
'position' => !empty($request['data']['position']) ? $request['data']['position'] : '',
|
2021-03-15 22:47:13 +01:00
|
|
|
]);
|
2021-03-22 16:17:56 +01:00
|
|
|
return [
|
|
|
|
'dropdownData' => $dropdownData,
|
|
|
|
'userEntity' => $userEntity,
|
2022-12-12 16:02:41 +01:00
|
|
|
'individualEntity' => $individualEntity,
|
|
|
|
'desiredOrganisation' => [
|
|
|
|
'org_name' => $request['data']['org_name'],
|
|
|
|
'org_uuid' => $request['data']['org_uuid'],
|
|
|
|
],
|
|
|
|
'defaultRole' => $defaultRole,
|
2021-03-22 16:17:56 +01:00
|
|
|
];
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 13:25:49 +02:00
|
|
|
public function process($id, $requestData, $inboxRequest)
|
2021-03-15 22:47:13 +01:00
|
|
|
{
|
2021-10-20 22:29:23 +02:00
|
|
|
$hashedPassword = $inboxRequest['data']['password'];
|
2021-03-18 14:01:14 +01:00
|
|
|
if ($requestData['individual_id'] == -1) {
|
2021-03-15 22:47:13 +01:00
|
|
|
$individual = $this->Users->Individuals->newEntity([
|
2021-03-18 14:01:14 +01:00
|
|
|
'uuid' => $requestData['uuid'],
|
|
|
|
'email' => $requestData['email'],
|
|
|
|
'first_name' => $requestData['first_name'],
|
|
|
|
'last_name' => $requestData['last_name'],
|
|
|
|
'position' => $requestData['position'],
|
2021-03-15 22:47:13 +01:00
|
|
|
]);
|
|
|
|
$individual = $this->Users->Individuals->save($individual);
|
|
|
|
} else {
|
2021-03-18 14:01:14 +01:00
|
|
|
$individual = $this->Users->Individuals->get($requestData['individual_id']);
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
|
|
|
$user = $this->Users->newEntity([
|
|
|
|
'individual_id' => $individual->id,
|
2021-03-18 14:01:14 +01:00
|
|
|
'username' => $requestData['username'],
|
2021-03-15 22:47:13 +01:00
|
|
|
'password' => '~PASSWORD_TO_BE_REPLACED~',
|
2021-03-18 14:01:14 +01:00
|
|
|
'role_id' => $requestData['role_id'],
|
|
|
|
'disabled' => $requestData['disabled'],
|
2022-12-12 16:02:41 +01:00
|
|
|
'organisation_id' => $requestData['org_id'],
|
2021-03-15 22:47:13 +01:00
|
|
|
]);
|
2021-10-20 22:29:23 +02:00
|
|
|
$user->set('password', $hashedPassword, ['setter' => false]); // ignore default password hashing as it has already been hashed
|
2021-03-15 22:47:13 +01:00
|
|
|
$user = $this->Users->save($user);
|
2021-03-18 14:01:14 +01:00
|
|
|
|
|
|
|
if ($user !== false) {
|
|
|
|
$this->discard($id, $requestData);
|
|
|
|
}
|
|
|
|
return $this->genActionResult(
|
|
|
|
$user,
|
|
|
|
$user !== false,
|
|
|
|
$user !== false ? __('User `{0}` created', $user->username) : __('Could not create user `{0}`.', $user->username),
|
|
|
|
$user->getErrors()
|
|
|
|
);
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 14:01:14 +01:00
|
|
|
public function discard($id, $requestData)
|
2021-03-15 22:47:13 +01:00
|
|
|
{
|
2021-03-18 14:01:14 +01:00
|
|
|
return parent::discard($id, $requestData);
|
2021-03-15 22:47:13 +01:00
|
|
|
}
|
|
|
|
}
|