diff --git a/app/sprinkles/admin/assets/userfrosting/js/widgets/users.js b/app/sprinkles/admin/assets/userfrosting/js/widgets/users.js index 9f57d7c5c..84d07c19d 100644 --- a/app/sprinkles/admin/assets/userfrosting/js/widgets/users.js +++ b/app/sprinkles/admin/assets/userfrosting/js/widgets/users.js @@ -24,11 +24,49 @@ function attachUserForm() { // Reload page on success window.location.reload(); }); + + toggleSetPasswordMode(modal, 'link'); + + // On submission, submit either the PUT request, or POST for a password reset, depending on the toggle state + modal.find("input[name='change_password_mode']").click(function() { + var changePasswordMode = $(this).val(); + toggleSetPasswordMode(modal, changePasswordMode); + }); }); } /** * Enable/disable password fields when switch is toggled + * Applies to 'creating' a user + */ +function toggleSetPasswordMode(el, changePasswordMode) { + var form = el.find("form"); + if (changePasswordMode == 'link') { + $(".controls-password").find("input[type='password']").prop('disabled', true); + // Form submits password reset request + + var validator = form.validate(); + if (validator) { + //Iterate through named elements inside of the form, and mark them as error free + el.find("input[type='password']").each(function() { + validator.successList.push(this); //mark as error free + }); + validator.resetForm(); //remove error class on name elements and clear history + validator.reset(); //remove all error and success data + } + el.find("input[type='password']").closest('.form-group') + .removeClass('has-error has-success'); + el.find('.form-control-feedback').each(function() { + $(this).remove(); + }); + } else { + $(".controls-password").find("input[type='password']").prop('disabled', false); + } +} + +/** + * Enable/disable password fields when switch is toggled + * Applies to 'reseting' a users password */ function toggleChangePasswordMode(el, userName, changePasswordMode) { var form = el.find("form"); diff --git a/app/sprinkles/admin/schema/requests/user/create.yaml b/app/sprinkles/admin/schema/requests/user/create.yaml index 7e575bc69..821482001 100644 --- a/app/sprinkles/admin/schema/requests/user/create.yaml +++ b/app/sprinkles/admin/schema/requests/user/create.yaml @@ -70,3 +70,32 @@ group_id: label: "&GROUP" domain: server message: VALIDATE.INTEGER +password: + validators: + required: + domain: client + label: "&PASSWORD" + message: VALIDATE.REQUIRED + length: + domain: client + label: "&PASSWORD" + min: 12 + max: 100 + message: VALIDATE.LENGTH_RANGE +passwordc: + validators: + required: + domain: client + label: "&PASSWORD.CONFIRM" + message: VALIDATE.REQUIRED + matches: + domain: client + field: value + label: "&PASSWORD.CONFIRM" + message: VALIDATE.PASSWORD_MISMATCH + length: + domain: client + label: "&PASSWORD.CONFIRM" + min: 12 + max: 100 + message: VALIDATE.LENGTH_RANGE diff --git a/app/sprinkles/admin/src/Controller/UserController.php b/app/sprinkles/admin/src/Controller/UserController.php index ac8adccf5..4c6cfa4a9 100644 --- a/app/sprinkles/admin/src/Controller/UserController.php +++ b/app/sprinkles/admin/src/Controller/UserController.php @@ -133,8 +133,12 @@ public function create(Request $request, Response $response, $args) } $data['flag_verified'] = 1; - // Set password as empty on initial creation. We will then send email so new user can set it themselves via a verification token - $data['password'] = ''; + if (!isset($data['password'])) { + // Set password as empty on initial creation. We will then send email so new user can set it themselves via a verification token + $data['password'] = ''; + } else { + $data['password'] = Password::hash($data['password']); + } // All checks passed! log events/activities, create user, and send verification email (if required) // Begin transaction - DB will be rolled back if an exception occurs @@ -162,10 +166,12 @@ public function create(Request $request, Response $response, $args) // Try to generate a new password request $passwordRequest = $this->ci->repoPasswordReset->create($user, $config['password_reset.timeouts.create']); - // Create and send welcome email with password set link - $message = new TwigMailMessage($this->ci->view, 'mail/password-create.html.twig'); + // If the password_mode is manual, do not send an email to set it. Else, send the email. + if (!isset($data['value'])) { + // Create and send welcome email with password set link + $message = new TwigMailMessage($this->ci->view, 'mail/password-create.html.twig'); - $message->from($config['address_book.admin']) + $message->from($config['address_book.admin']) ->addEmailRecipient(new EmailRecipient($user->email, $user->full_name)) ->addParams([ 'user' => $user, @@ -173,7 +179,8 @@ public function create(Request $request, Response $response, $args) 'token' => $passwordRequest->getToken(), ]); - $this->ci->mailer->send($message); + $this->ci->mailer->send($message); + } $ms->addMessageTranslated('success', 'USER.CREATED', $data); }); @@ -694,7 +701,7 @@ public function getModalEdit(Request $request, Response $response, $args) // Generate form $fields = [ - 'hidden' => ['theme'], + 'hidden' => ['theme', 'password'], 'disabled' => ['user_name'], ]; diff --git a/app/sprinkles/admin/templates/forms/partials/user-set-password.html.twig b/app/sprinkles/admin/templates/forms/partials/user-set-password.html.twig new file mode 100644 index 000000000..cb6c1502b --- /dev/null +++ b/app/sprinkles/admin/templates/forms/partials/user-set-password.html.twig @@ -0,0 +1,37 @@ + + + +
+
+ +
+
+
+
+ +
+
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+
+
diff --git a/app/sprinkles/admin/templates/forms/user-set-password.html.twig b/app/sprinkles/admin/templates/forms/user-set-password.html.twig new file mode 100644 index 000000000..0299f32d7 --- /dev/null +++ b/app/sprinkles/admin/templates/forms/user-set-password.html.twig @@ -0,0 +1,21 @@ +
+ {% include "forms/csrf.html.twig" %} +
+
+
+ {% include "forms/partials/user-set-password.html.twig" %} +
+
+
+
+ +
+
+ +
+
+
+ + \ No newline at end of file diff --git a/app/sprinkles/admin/templates/forms/user.html.twig b/app/sprinkles/admin/templates/forms/user.html.twig index 8a55a955d..0ad38258e 100644 --- a/app/sprinkles/admin/templates/forms/user.html.twig +++ b/app/sprinkles/admin/templates/forms/user.html.twig @@ -110,6 +110,9 @@ {% endif %} + {% if 'password' not in form.fields.hidden %} + {% include "forms/partials/user-set-password.html.twig" %} + {% endif %} {% endblock %}
diff --git a/app/sprinkles/admin/templates/modals/user-set-password.html.twig b/app/sprinkles/admin/templates/modals/user-set-password.html.twig index 9f7854874..fcd7b137b 100644 --- a/app/sprinkles/admin/templates/modals/user-set-password.html.twig +++ b/app/sprinkles/admin/templates/modals/user-set-password.html.twig @@ -3,61 +3,5 @@ {% block modal_title %}{{translate("USER.ADMIN.CHANGE_PASSWORD")}}{% endblock %} {% block modal_body %} -
- {% include "forms/csrf.html.twig" %} - - - -
-
-
-
-
- -
-
-
-
- -
-
-
-
- -
- - - -
-
-
- -
- - -
-
-
-
-
-

-
-
- -
-
- -
-
-
- - + {% include "forms/user-set-password.html.twig" %} {% endblock %}