Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #79: Refactor UserController as demonstrated in PR 13 #80

Merged
merged 3 commits into from
Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package grails.plugin.springsecurity.ui

import grails.plugin.springsecurity.ui.strategy.UserStrategy
import groovy.transform.CompileStatic

/**
* @author <a href='mailto:[email protected]'>Burt Beckwith</a>
Expand Down Expand Up @@ -77,19 +78,27 @@ class UserController extends AbstractS2UiDomainController {
protected Map buildUserModel(user) {

Set userRoleNames = user[authoritiesPropertyName].collect { it[authorityNameField] }
def granted = [:]
def notGranted = [:]
for (role in sortedRoles()) {
Map roleMap = buildRoleMap(userRoleNames)

[roleMap: roleMap, tabData: tabData, user: user]
}

@CompileStatic
protected Map buildRoleMap(Set userRoleNames, List sortedRoles) {
if (!userRoleNames) {
return [:]
}
Map granted = [:]
Map notGranted = [:]
for (role in sortedRoles) {
String authority = role[authorityNameField]
if (userRoleNames.contains(authority)) {
granted[(role)] = userRoleNames.contains(authority)
}
else {
notGranted[(role)] = userRoleNames.contains(authority)
granted[(role)] = true
} else {
notGranted[(role)] = false
}
}

[roleMap: granted + notGranted, tabData: tabData, user: user]
granted + notGranted
}

protected List sortedRoles() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package grails.plugin.springsecurity.ui

import grails.testing.web.controllers.ControllerUnitTest
import spock.lang.Specification
import spock.lang.Unroll

@Unroll
class UserControllerSpec extends Specification implements ControllerUnitTest<UserController> {
static final Map ADMIN_ROLE = [authority: "ROLE_ADMIN"]
static final Map SUPER_ADMIN_ROLE = [authority: "ROLE_SUPER_ADMIN"]
static final Map USER_ROLE = [authority: "ROLE_USER"]

void "verify proper construction of roleMap for user with roles #rolesAssignedToUser"() {
given: "the authority name field has been set to the default name of 'authority'"
controller.authorityNameField = "authority"

and: "we mock the returning of all Role instances within the database"
List sortedRoles = [ADMIN_ROLE, SUPER_ADMIN_ROLE, USER_ROLE]

when: "we call buildRoleMap with the role names associated to the user"
Map results = controller.buildRoleMap(rolesAssignedToUser, sortedRoles)

then: "the user is only granted access to roles with which they are associated"
results == expectedResults
results instanceof LinkedHashMap

where:
rolesAssignedToUser | expectedResults
[ADMIN_ROLE.authority, USER_ROLE.authority] as Set | [(ADMIN_ROLE): true, (SUPER_ADMIN_ROLE): false, (USER_ROLE): true]
[] as Set | [:]
null | [:]
}
}