fix: [users:registration] Fixed and improved user registration

develop-unstable
Sami Mokaddem 2022-12-12 16:02:41 +01:00
parent e366da6171
commit e13dc152e7
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
6 changed files with 100 additions and 27 deletions

View File

@ -73,8 +73,28 @@ class RegistrationProcessor extends UserInboxProcessor implements GenericInboxPr
]),
'individual' => [-1 => __('-- New individual --')] + $this->Users->Individuals->find('list', [
'sort' => ['email' => 'asc']
])->toArray()
])->toArray(),
'organisation' => $this->Users->Organisations->find('list', [
'sort' => ['email' => 'asc']
])->toArray(),
];
$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();
}
$individualEntity = $this->Users->Individuals->newEntity([
'email' => !empty($request['data']['email']) ? $request['data']['email'] : '',
'first_name' => !empty($request['data']['first_name']) ? $request['data']['first_name'] : '',
@ -86,6 +106,7 @@ class RegistrationProcessor extends UserInboxProcessor implements GenericInboxPr
'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'] : '',
'org_id' => !empty($desiredOrg) ? $desiredOrg['id'] : '',
'email' => !empty($request['data']['email']) ? $request['data']['email'] : '',
'first_name' => !empty($request['data']['first_name']) ? $request['data']['first_name'] : '',
@ -95,7 +116,12 @@ class RegistrationProcessor extends UserInboxProcessor implements GenericInboxPr
return [
'dropdownData' => $dropdownData,
'userEntity' => $userEntity,
'individualEntity' => $individualEntity
'individualEntity' => $individualEntity,
'desiredOrganisation' => [
'org_name' => $request['data']['org_name'],
'org_uuid' => $request['data']['org_uuid'],
],
'defaultRole' => $defaultRole,
];
}
@ -120,6 +146,7 @@ class RegistrationProcessor extends UserInboxProcessor implements GenericInboxPr
'password' => '~PASSWORD_TO_BE_REPLACED~',
'role_id' => $requestData['role_id'],
'disabled' => $requestData['disabled'],
'organisation_id' => $requestData['org_id'],
]);
$user->set('password', $hashedPassword, ['setter' => false]); // ignore default password hashing as it has already been hashed
$user = $this->Users->save($user);

View File

@ -1,10 +1,23 @@
<?php
$infoAlert = $this->Bootstrap->alert([
'variant' => 'info',
'html' => sprintf('
<ul>
<li>%s: <strong>%s</strong></li>
<li>%s: <strong>%s</strong></li>
</ul>',
__('Requested Organisation name'), $desiredOrganisation['org_name'],
__('Requested Organisation UUID'), $desiredOrganisation['org_uuid']
),
'dismissible' => false,
]);
$combinedForm = $this->element('genericElements/Form/genericForm', [
'entity' => $userEntity,
'ajax' => false,
'raw' => true,
'data' => [
'description' => __('Create user account'),
'descriptionHtml' => __('Create user account') . sprintf('<div class="mt-2">%s</div>', $infoAlert),
'model' => 'User',
'fields' => [
[
@ -17,19 +30,26 @@ $combinedForm = $this->element('genericElements/Form/genericForm', [
'field' => 'username',
'autocomplete' => 'off',
],
[
'field' => 'org_id',
'type' => 'dropdown',
'label' => __('Associated organisation'),
'options' => $dropdownData['organisation'],
],
[
'field' => 'role_id',
'type' => 'dropdown',
'label' => __('Role'),
'options' => $dropdownData['role']
'options' => $dropdownData['role'],
'default' => $defaultRole,
],
[
'field' => 'disabled',
'type' => 'checkbox',
'label' => 'Disable'
],
sprintf('<div class="pb-2 fs-4">%s</div>', __('Create individual')),
'<div class="individual-container">',
sprintf('<div class="pb-2 fs-4">%s</div>', __('Create a new individual')),
[
'field' => 'email',
'autocomplete' => 'off'
@ -52,6 +72,7 @@ $combinedForm = $this->element('genericElements/Form/genericForm', [
'field' => 'position',
'autocomplete' => 'off'
],
'</div>',
],
'submit' => [
'action' => $this->request->getParam('action')
@ -86,15 +107,19 @@ echo $this->Bootstrap->modal([
}
$(document).ready(function() {
$('div.user-container #individual_id-field').change(function() {
if ($(this).val() == -1) {
$('div.individual-container').show()
} else {
$('div.individual-container').hide()
}
$('form #individual_id-field').change(function() {
toggleIndividualContainer($(this).val() == -1)
})
})
function toggleIndividualContainer(show) {
if (show) {
$('div.individual-container').show()
} else {
$('div.individual-container').hide()
}
}
function getFormData(form) {
return Object.values(form).reduce((obj, field) => {
if (field.type === 'checkbox') {
@ -105,11 +130,4 @@ echo $this->Bootstrap->modal([
return obj
}, {})
}
</script>
<style>
div.individual-container>div,
div.user-container>div {
font-size: 1.5rem;
}
</style>
</script>

View File

@ -68,7 +68,7 @@ class UsersController extends AppController
} else {
$validRoles = $this->Users->Roles->find('list')->order(['name' => 'asc'])->all()->toArray();
}
$defaultRole = $this->Users->Roles->find()->select(['id'])->first()->toArray();
$defaultRole = $this->Users->Roles->find()->select(['id'])->where(['is_default' => true])->first()->toArray();
$individuals = $this->Users->Individuals->find('list', $individuals_params)->toArray();
$this->CRUD->add([
'beforeMarshal' => function($data) {
@ -442,6 +442,8 @@ class UsersController extends AppController
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'password' => $data['password'],
'org_name' => $data['org_name'],
'org_uuid' => $data['org_uuid'],
],
];
$processorResult = $processor->create($data);

View File

@ -34,6 +34,20 @@ use Cake\Core\Configure;
echo '</div>';
echo '</div>';
echo '<div class="row g-1 mb-3">';
echo $this->Form->control('org_name', ['label' => __('Organisation Name'), 'class' => 'form-control']);
echo $this->Form->control('org_uuid', [
'label' => __('UUID'),
'class' => 'form-control form-control-sm mb-2',
'style' => 'font-size: 0.815rem;',
'div' => ['class' => 'test'],
'templates' => [
'inputContainer' => '<div class="input d-flex {{type}}{{required}}">{{content}}</div>',
'label' => '<label class="fw-light fs-7 me-1 align-self-center small-label-addon" {{attrs}}>{{text}}</label>',
],
]);
echo '</div>';
echo $this->Form->control('password', ['type' => 'password', 'label' => __('Password'), 'class' => 'form-control mb-4']);
echo $this->Form->control(__('Sign up'), ['type' => 'submit', 'class' => 'btn btn-primary']);
@ -42,4 +56,16 @@ use Cake\Core\Configure;
echo '</div>';
echo $this->Form->end();
?>
</div>
</div>
<style>
.text-input-addon-center {
display: flex;
flex: 0 0 0%;
align-items: center
}
.small-label-addon {
height: calc(1.5em + 0.5rem + 2px);
}
</style>

View File

@ -4,12 +4,12 @@
</h2>
<?= $formCreate ?>
<?= $ajaxFlashMessage ?>
<?php if (!empty($data['description'])) : ?>
<?php if (!empty($data['description']) || !empty($data['descriptionHtml'])) : ?>
<div class="pb-3 fw-light">
<?= h($data['description']) ?>
<?= !empty($data['descriptionHtml']) ? $data['descriptionHtml'] : h($data['description']) ?>
</div>
<?php endif; ?>
<div class="panel col-lg-8">
<div class="panel col-lg-12">
<?= $fieldsString ?>
</div>

View File

@ -1,6 +1,6 @@
<?php if (!empty($data['description'])) : ?>
<?php if (!empty($data['description']) || !empty($data['descriptionHtml'])) : ?>
<div class="pb-3 fw-light">
<?= h($data['description']) ?>
<?= !empty($data['descriptionHtml']) ? $data['descriptionHtml'] : h($data['description']) ?>
</div>
<?php endif; ?>
<?= $ajaxFlashMessage ?>