Skip to content

Commit dbb02e6

Browse files
authored
fix #337 reset user password (#340)
1 parent 11ebb10 commit dbb02e6

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

pegr/grails-app/controllers/pegr/admin/UserAdminController.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import pegr.UserException
99
import grails.converters.*
1010
import grails.util.Holders
1111
import groovy.json.*
12+
import pegr.PasswordRegistrationCommand
1213

1314
class UserAdminController {
1415
public static AdminCategory category = AdminCategory.OTHER
@@ -255,6 +256,35 @@ class UserAdminController {
255256
outs.close()
256257

257258
}
259+
260+
def updateUserPassword(Long userId) {
261+
def user = User.get(userId)
262+
render(view: "updateUserPassword", model: [user: user])
263+
}
264+
265+
def saveUserPassword() {
266+
if(request.method=='POST') {
267+
withForm {
268+
def userId = params.userId as Long
269+
def user = User.get(userId)
270+
271+
if (user == null) {
272+
flash.message = "User not found!"
273+
redirect(controller: "userAdmin", action: "index")
274+
}
275+
276+
try {
277+
def urc = new PasswordRegistrationCommand(password: params.password, passwordRepeat: params.passwordRepeat)
278+
userService.updatePassword(user, urc)
279+
flash.message = "The password for ${user.username} has been changed."
280+
redirect(controller: "userAdmin", action: "index")
281+
} catch (Exception e) {
282+
request.message = e.message
283+
render(view: "updateUserPassword", model: [user: user])
284+
}
285+
}
286+
}
287+
}
258288
}
259289

260290
class CreateUserCommand implements grails.validation.Validateable {

pegr/grails-app/services/pegr/UserService.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class UserService {
186186
def updatePassword(User user, PasswordRegistrationCommand urc) {
187187
urc.validate()
188188
if (urc.hasErrors()) {
189-
throw new UserException(message: "Invalid input!")
189+
throw new UserException(message: "Invalid password!")
190190
} else {
191191
user.password = springSecurityService.encodePassword(urc.password)
192192
user.save()

pegr/grails-app/views/userAdmin/index.gsp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<th>Group</th>
6767
<th>Status</th>
6868
<th>Edit</th>
69+
<th>Reset password</th>
6970
</tr>
7071
</thead>
7172
<tbody>
@@ -85,6 +86,7 @@
8586
</g:else>
8687
</td>
8788
<td><g:link action="edit" params="[userId:it.id]">Edit</g:link></td>
89+
<td><g:link action="updateUserPassword" params="[userId:it.id]">Reset password</g:link></td>
8890
</tr>
8991
</g:each>
9092
</tbody>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<html>
2+
<head>
3+
<title>PEGR Admin - User</title>
4+
<meta name="layout" content="admin"/>
5+
</head>
6+
<body>
7+
<div class="container-fluid">
8+
<g:if test="${request.message}">
9+
<div class="alert alert-danger">${request.message}</div>
10+
</g:if>
11+
<h3>Reset password for username ${user.username}</h3>
12+
<p>The password should have</p>
13+
<ol>
14+
<li>minimum eight characters</li>
15+
<li>at least one upper case English letter</li>
16+
<li>at least one lower case English letter</li>
17+
<li>at least one digit</li>
18+
<li>at least one special character, such as #?!@$%^&amp;*-_</li>
19+
</ol>
20+
<g:form controller="userAdmin" action="saveUserPassword" class="fields" useToken="true">
21+
<input type="hidden" name="userId" value="${user.id}">
22+
<div>
23+
<label>New Password</label>
24+
<g:passwordField name="password"></g:passwordField>
25+
</div>
26+
<div>
27+
<label>Re-enter Password</label>
28+
<g:passwordField name="passwordRepeat"></g:passwordField>
29+
</div>
30+
<g:submitButton name="submit" value="Reset" class="btn btn-primary"></g:submitButton>
31+
<g:link controller="userAdmin" action="index" class="btn btn-default">Cancel</g:link>
32+
</g:form>
33+
</div>
34+
</body>
35+
</html>

0 commit comments

Comments
 (0)