Skip to content

Commit 3280986

Browse files
author
Sudhir Nimavat
committed
Closes #13 - Add configuration option to whether to allow previously used passwords
1 parent e2677b8 commit 3280986

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

grails-app/conf/DefaultNimbleConfig.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ nimble {
6767
symbols = true
6868
}
6969
minlength = 8
70+
allowreuse = false
7071
}
7172

7273
messaging {

grails-app/controllers/grails/plugin/nimble/core/AdminsController.groovy

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,32 @@ class AdminsController {
100100

101101
log.debug("Performing search for users matching $q")
102102

103-
def users = UserBase.findAllByUsernameIlike(q)
104-
def profiles = ProfileBase.findAllByFullNameIlikeOrEmailIlike(q, q)
103+
def users = UserBase.findAllByUsernameIlike(q, [max:50])
104+
def profiles = ProfileBase.findAllByFullNameIlikeOrEmailIlike(q, q, [max:50, sort:"fullName"])
105105
def nonAdmins = []
106106

107107
users.each {
108108
boolean admin = false
109109
it.roles.each { role ->
110-
if(role.name == AdminsService.ADMIN_ROLE)
110+
if(role.name == AdminsService.ADMIN_ROLE) {
111111
admin = true
112+
}
112113
}
113-
if(!admin)
114+
if(!admin) {
114115
nonAdmins.add(it)
116+
}
115117
}
116118

117119
profiles.each {
118120
boolean admin = false
119121
it.owner.roles.each { role ->
120-
if(role.name == AdminsService.ADMIN_ROLE)
122+
if(role.name == AdminsService.ADMIN_ROLE) {
121123
admin = true
124+
}
122125
}
123-
if(!admin && !nonAdmins.contains(it.owner))
126+
if(!admin && !nonAdmins.contains(it.owner)) {
124127
nonAdmins.add(it.owner)
128+
}
125129
}
126130

127131
log.info("Search for new administrators complete, returning $nonAdmins.size records")

grails-app/services/grails/plugin/nimble/core/UserService.groovy

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ class UserService {
3232

3333
public static final String USER_ROLE = "USER"
3434

35-
def grailsApplication
3635
def permissionService
37-
36+
def grailsApplication
3837
def events = [:]
3938

4039
/**
@@ -284,6 +283,7 @@ class UserService {
284283
user.addToPasswdHistory(crypt)
285284

286285
user.save()
286+
287287
if (user.hasErrors()) {
288288
log.error("Unable to assign random password to user [$user.id]$user.username")
289289
user.errors.each { log.error(it) }
@@ -305,13 +305,10 @@ class UserService {
305305
}
306306

307307
log.debug("Creating new record for user [$user.id]$user.username login")
308-
308+
309309
def record = new LoginRecord()
310-
311-
String ip = request.getRemoteAddr()
312-
//it will be different then ip if request was forwarded by a proxy
313-
String resolvedIp = getClientIpAddress(request)
314-
310+
String resolvedIp = getClientIpAddress(request)
311+
315312
record.remoteAddr = resolvedIp
316313
record.remoteHost = request.getRemoteHost()
317314
record.userAgent = request.getHeader("User-Agent")
@@ -329,7 +326,7 @@ class UserService {
329326

330327
user.save()
331328

332-
if (record.hasErrors()) {
329+
if (user.hasErrors()) {
333330
log.error("Unable to update user [$user.id]$user.username with new login record")
334331
user.errors.each { log.error(it) }
335332

@@ -384,15 +381,14 @@ class UserService {
384381
boolean validatePass(UserBase user, boolean checkOnly) {
385382
log.debug("Validating user entered password")
386383

387-
if (user.pass == null || user.pass.length() < grailsApplication.config.nimble.passwords.minlength) {
384+
if (user.pass == null || user.pass.length() < nimbleConfig.passwords.minlength) {
388385
log.debug("Password to short")
389386
user.errors.rejectValue('pass', 'nimble.user.password.required')
390387
return false
391388
}
392389

393-
if (user.passConfirm == null || user.passConfirm.length() < grailsApplication.config.nimble.passwords.minlength) {
390+
if (user.passConfirm == null || user.passConfirm.length() < nimbleConfig.passwords.minlength) {
394391
log.debug("Confirmation password to short")
395-
396392
user.errors.rejectValue('passConfirm', 'nimble.user.passconfirm.required')
397393
return false
398394
}
@@ -403,25 +399,25 @@ class UserService {
403399
return false
404400
}
405401

406-
if (grailsApplication.config.nimble.passwords.mustcontain.lowercase && !(user.pass =~ /^.*[a-z].*$/)) {
402+
if (nimbleConfig.passwords.mustcontain.lowercase && !(user.pass =~ /^.*[a-z].*$/)) {
407403
log.debug("Password does not contain lower case letters")
408404
user.errors.rejectValue('pass', 'nimble.user.password.no.lowercase')
409405
return false
410406
}
411407

412-
if (grailsApplication.config.nimble.passwords.mustcontain.uppercase && !(user.pass =~ /^.*[A-Z].*$/)) {
408+
if (nimbleConfig.passwords.mustcontain.uppercase && !(user.pass =~ /^.*[A-Z].*$/)) {
413409
log.debug("Password does not contain uppercase letters")
414410
user.errors.rejectValue('pass', 'nimble.user.password.no.uppercase')
415411
return false
416412
}
417413

418-
if (grailsApplication.config.nimble.passwords.mustcontain.numbers && !(user.pass =~ /^.*[0-9].*$/)) {
414+
if (nimbleConfig.passwords.mustcontain.numbers && !(user.pass =~ /^.*[0-9].*$/)) {
419415
log.debug("Password does not contain numbers")
420416
user.errors.rejectValue('pass', 'nimble.user.password.no.numbers')
421417
return false
422418
}
423419

424-
if (grailsApplication.config.nimble.passwords.mustcontain.symbols && !(user.pass =~ /^.*\W.*$/)) {
420+
if (nimbleConfig.passwords.mustcontain.symbols && !(user.pass =~ /^.*\W.*$/)) {
425421
log.debug("Password does not contain symbols")
426422
user.errors.rejectValue('pass', 'nimble.user.password.no.symbols')
427423
return false
@@ -430,10 +426,12 @@ class UserService {
430426
def pwEnc = new Sha256Hash(user.pass)
431427
def crypt = pwEnc.toHex()
432428

433-
if (user.passwdHistory != null && user.passwdHistory.contains(crypt)) {
434-
log.debug("Password was previously utilized")
435-
user.errors.rejectValue('pass', 'nimble.user.password.duplicate')
436-
return false
429+
if(!nimbleConfig.passwords.allowreuse) {
430+
if (user.passwdHistory != null && user.passwdHistory.contains(crypt)) {
431+
log.debug("Password was previously utilized")
432+
user.errors.rejectValue('pass', 'nimble.user.password.duplicate')
433+
return false
434+
}
437435
}
438436

439437
if (!user.hasErrors() && !checkOnly) {
@@ -443,4 +441,9 @@ class UserService {
443441

444442
return true
445443
}
444+
445+
private getNimbleConfig() {
446+
grailsApplication.config.nimble
447+
}
448+
446449
}

0 commit comments

Comments
 (0)