Skip to content

Commit 6fda9ba

Browse files
committed
Admin Ability to Set pass for new user
When creating a user you can now set the password manually or force the user to create one. Previously an email was the only option and hindered the creation of a user if the email settings were not configured at all or incorrectly.
1 parent ccda2a9 commit 6fda9ba

4 files changed

Lines changed: 95 additions & 18 deletions

File tree

app/sprinkles/admin/assets/userfrosting/js/widgets/users.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,51 @@ function attachUserForm() {
2020
validators: page.validators
2121
}).on("submitSuccess.ufForm", function() {
2222
// Reload page on success
23-
window.location.reload();
23+
//window.location.reload();
24+
});
25+
26+
toggleSetPasswordMode(modal, 'link');
27+
28+
// On submission, submit either the PUT request, or POST for a password reset, depending on the toggle state
29+
modal.find("input[name='change_password_mode']").click(function() {
30+
var changePasswordMode = $(this).val();
31+
toggleSetPasswordMode(modal, changePasswordMode);
2432
});
2533
});
2634
}
2735

2836
/**
2937
* Enable/disable password fields when switch is toggled
38+
* Applies to 'creating' a user
39+
*/
40+
function toggleSetPasswordMode(el, changePasswordMode) {
41+
var form = el.find("form");
42+
if (changePasswordMode == 'link') {
43+
$(".controls-password").find("input[type='password']").prop('disabled', true);
44+
// Form submits password reset request
45+
46+
var validator = form.validate();
47+
if (validator) {
48+
//Iterate through named elements inside of the form, and mark them as error free
49+
el.find("input[type='password']").each(function() {
50+
validator.successList.push(this); //mark as error free
51+
});
52+
validator.resetForm();//remove error class on name elements and clear history
53+
validator.reset();//remove all error and success data
54+
}
55+
el.find("input[type='password']").closest('.form-group')
56+
.removeClass('has-error has-success');
57+
el.find('.form-control-feedback').each(function () {
58+
$(this).remove();
59+
});
60+
} else {
61+
$(".controls-password").find("input[type='password']").prop('disabled', false);
62+
}
63+
}
64+
65+
/**
66+
* Enable/disable password fields when switch is toggled
67+
* Applies to 'reseting' a users password
3068
*/
3169
function toggleChangePasswordMode(el, userName, changePasswordMode) {
3270
var form = el.find("form");

app/sprinkles/admin/schema/requests/user/create.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,32 @@ group_id:
7070
label: "&GROUP"
7171
domain: server
7272
message: VALIDATE.INTEGER
73+
value:
74+
validators:
75+
required:
76+
domain: client
77+
label: "&PASSWORD"
78+
message: VALIDATE.REQUIRED
79+
length:
80+
domain: client
81+
label: "&PASSWORD"
82+
min: 12
83+
max: 100
84+
message: VALIDATE.LENGTH_RANGE
85+
passwordc:
86+
validators:
87+
required:
88+
domain: client
89+
label: "&PASSWORD.CONFIRM"
90+
message: VALIDATE.REQUIRED
91+
matches:
92+
domain: client
93+
field: value
94+
label: "&PASSWORD.CONFIRM"
95+
message: VALIDATE.PASSWORD_MISMATCH
96+
length:
97+
domain: client
98+
label: "&PASSWORD.CONFIRM"
99+
min: 12
100+
max: 100
101+
message: VALIDATE.LENGTH_RANGE

app/sprinkles/admin/src/Controller/UserController.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,13 @@ public function create($request, $response, $args)
118118
}
119119

120120
$data['flag_verified'] = 1;
121-
// Set password as empty on initial creation. We will then send email so new user can set it themselves via a verification token
122-
$data['password'] = '';
123-
121+
if(!isset($data['value'])){
122+
// Set password as empty on initial creation. We will then send email so new user can set it themselves via a verification token
123+
$data['password'] = '';
124+
}else{
125+
$data['password'] = Password::hash($data['value']);
126+
}
127+
124128
// All checks passed! log events/activities, create user, and send verification email (if required)
125129
// Begin transaction - DB will be rolled back if an exception occurs
126130
Capsule::transaction( function() use ($classMapper, $data, $ms, $config, $currentUser) {
@@ -135,7 +139,7 @@ public function create($request, $response, $args)
135139
'type' => 'account_create',
136140
'user_id' => $currentUser->id
137141
]);
138-
142+
139143
// Load default roles
140144
$defaultRoleSlugs = $classMapper->staticMethod('role', 'getDefaultSlugs');
141145
$defaultRoles = $classMapper->staticMethod('role', 'whereIn', 'slug', $defaultRoleSlugs)->get();
@@ -147,19 +151,22 @@ public function create($request, $response, $args)
147151
// Try to generate a new password request
148152
$passwordRequest = $this->ci->repoPasswordReset->create($user, $config['password_reset.timeouts.create']);
149153

150-
// Create and send welcome email with password set link
151-
$message = new TwigMailMessage($this->ci->view, 'mail/password-create.html.twig');
152-
153-
$message->from($config['address_book.admin'])
154-
->addEmailRecipient(new EmailRecipient($user->email, $user->full_name))
155-
->addParams([
156-
'user' => $user,
157-
'create_password_expiration' => $config['password_reset.timeouts.create'] / 3600 . ' hours',
158-
'token' => $passwordRequest->getToken()
159-
]);
160-
161-
$this->ci->mailer->send($message);
162-
154+
// If the password_mode is manual, do not send an email to set it. Else, send the email.
155+
if(!isset($data['value'])){
156+
// Create and send welcome email with password set link
157+
$message = new TwigMailMessage($this->ci->view, 'mail/password-create.html.twig');
158+
159+
$message->from($config['address_book.admin'])
160+
->addEmailRecipient(new EmailRecipient($user->email, $user->full_name))
161+
->addParams([
162+
'user' => $user,
163+
'create_password_expiration' => $config['password_reset.timeouts.create'] / 3600 . ' hours',
164+
'token' => $passwordRequest->getToken()
165+
]);
166+
167+
$this->ci->mailer->send($message);
168+
}
169+
163170
$ms->addMessageTranslated('success', 'USER.CREATED', $data);
164171
});
165172

app/sprinkles/admin/templates/forms/user.html.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@
108108
</div>
109109
</div>
110110
{% endif %}
111+
{% if 'password' not in form.fields.hidden %}
112+
{% include "forms/partials/user-set-password.html.twig" %}
113+
{% endif %}
111114
{% endblock %}
112115
</div><br>
113116
<div class="row">

0 commit comments

Comments
 (0)