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 106: Removing a Role throws an exception #113

Merged
merged 3 commits into from
Sep 7, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ classes
cobertura.ser
kindlegen
/plugin/src/main/templates/views
.java-version
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class RolesTab extends Module {
$("input", type: "checkbox", id: roleName).value(true)
}

void disableRole(String roleName) {
$("input", type: "checkbox", id: roleName).value(false)
}

boolean hasEnabledRole(String roleName) {
return hasEnabledRoles([roleName])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import page.user.UserCreatePage
import page.user.UserEditPage
import page.user.UserSearchPage
import spock.lang.IgnoreIf
import spock.lang.IgnoreRest
import spock.lang.Issue

@IgnoreIf({
Expand All @@ -18,7 +19,7 @@ import spock.lang.Issue
}
false
})
class UserSpec extends AbstractSecuritySpec {
class UserSimpleSpec extends AbstractSecuritySpec {

void testFindAll() {
when:
Expand Down Expand Up @@ -204,4 +205,61 @@ class UserSpec extends AbstractSecuritySpec {
assert rolesTab.hasEnabledRoles(['ROLE_USER', 'ROLE_ADMIN'])
assert rolesTab.totalRoles() == 12
}

@Issue("https://github.com/grails-plugins/grails-spring-security-ui/issues/106")
void testUserRoleAssociationsAreRemoved() {
when: "edit user 2"
go 'user/edit/2'

then:
at UserEditPage

when: "select Roles tab"
rolesTab.select()

then: "12 roles are listed and 1 is enabled"
assert rolesTab.totalRoles() == 12
assert rolesTab.totalEnabledRoles() == 1
assert rolesTab.hasEnabledRole('ROLE_USER')

when: "ROLE_ADMIN is enabled and the changes are saved"
rolesTab.enableRole "ROLE_ADMIN"
submit()
rolesTab.select()

then: "12 roles are listed and 2 are enabled"
assert rolesTab.totalEnabledRoles() == 2
assert rolesTab.hasEnabledRoles(['ROLE_USER', 'ROLE_ADMIN'])
assert rolesTab.totalRoles() == 12

when: "edit user 2"
go 'user/edit/2'

then:
at UserEditPage

when: "select Roles tab"
rolesTab.select()

then: "12 roles are listed and 2 are enabled"
assert rolesTab.totalRoles() == 12
assert rolesTab.totalEnabledRoles() == 2
assert rolesTab.hasEnabledRole('ROLE_USER')

when: "ROLE_ADMIN is disabled and the changes are saved"
rolesTab.disableRole "ROLE_ADMIN"
submit()
go 'user/edit/2'

then:
at UserEditPage

when: "select Roles tab"
rolesTab.select()

then: "12 roles are listed and 1 is enabled"
assert rolesTab.totalEnabledRoles() == 1
assert rolesTab.hasEnabledRoles(['ROLE_USER'])
assert rolesTab.totalRoles() == 12
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,20 @@ class SpringSecurityUiService implements AclStrategy, ErrorsStrategy, Persistent
}

try {
for (role in rolesToRemove) {
UserRole.remove user, role
rolesToRemove.each { role ->
removeUserRoleAndReturnBoolean(user, role)
}
}
catch (e) {
uiErrorsStrategy.handleException e, user, null, this, 'removeUserRoles', transactionStatus
}
}

@Transactional
boolean removeUserRoleAndReturnBoolean(def user, def role) {
UserRole.remove user, role
}

@Transactional
Number removeUserRole(def u, def r) {
UserRole.where { user == u && role == r }.deleteAll()
Expand Down