Permalink
Browse files

reworked the role/group code:

- added exists() method to all join table classes
- added custom validator to all join table classes
- changed custom validators to use exists(), and run in a new session to avoid flushing the current during validation
- added missing hashCode() method where needed
- changed the create() methods to return the validated instance and not null in case there are errors
- made method/field/arg names consistent (removed 'permission' names since this conflicts logically with ACL permissions)
- added null safety checks
  • Loading branch information...
1 parent 78f3e2c commit 2a5e160bb07d5b8b0cb5bfb6ed55a70a23fe1713 @burtbeckwith burtbeckwith committed Jan 9, 2014
@@ -4,10 +4,11 @@ package test
* @author <a href='mailto:th3morg@gmail.com'>Ryan Morgan</a>
*/
class TestRoleGroup {
+
String name
Set<TestRole> getRoles() {
- TestRoleGroupRoles.findAllByGroup(this).collect { it.role }
+ TestRoleGroupRoles.findAllByRoleGroup(this).collect { it.role }
}
static constraints = {
@@ -1,64 +1,93 @@
package test
+import org.apache.commons.lang.builder.HashCodeBuilder
+
/**
* @author <a href='mailto:th3morg@gmail.com'>Ryan Morgan</a>
*/
class TestRoleGroupRoles implements Serializable {
private static final long serialVersionUID = 1
- TestRoleGroup group
+ TestRoleGroup roleGroup
TestRole role
boolean equals(other) {
if (!(other instanceof TestRoleGroupRoles)) {
return false
}
- other.role?.id == role?.id && other.group?.id == group?.id
+ other.role?.id == role?.id && other.roleGroup?.id == roleGroup?.id
+ }
+
+ int hashCode() {
+ def builder = new HashCodeBuilder()
+ if (roleGroup) builder.append(roleGroup.id)
+ if (role) builder.append(role.id)
+ builder.toHashCode()
}
- static TestRoleGroupRoles get(long roleId, long permissionId) {
+ static TestRoleGroupRoles get(long roleGroupId, long roleId) {
TestRoleGroupRoles.where {
- group == TestRoleGroup.load(roleId) &&
- role == TestRole.load(permissionId)
+ roleGroup == TestRoleGroup.load(roleGroupId) &&
+ role == TestRole.load(roleId)
}.get()
}
- static TestRoleGroupRoles create(TestRoleGroup role, TestRole permission, boolean flush = false) {
- new TestRoleGroupRoles(group: role, role: permission).save(flush: flush, insert: true)
+ static boolean exists(long roleGroupId, long roleId) {
+ TestRoleGroupRoles.where {
+ roleGroup == TestRoleGroup.load(roleGroupId) &&
+ role == TestRole.load(roleId)
+ }.count() > 0
+ }
+
+ static TestRoleGroupRoles create(TestRoleGroup roleGroup, TestRole role, boolean flush = false) {
+ def instance = new TestRoleGroupRoles(roleGroup: roleGroup, role: role)
+ instance.save(flush: flush, insert: true)
+ instance
}
- static boolean remove(TestRoleGroup r, TestRole p) {
+ static boolean remove(TestRoleGroup rg, TestRole r, boolean flush = false) {
int rowCount = TestRoleGroupRoles.where {
- group == TestRoleGroup.load(r.id) && role == TestRole.load(p.id)
+ roleGroup == TestRoleGroup.load(rg.id) && role == TestRole.load(r.id)
}.deleteAll()
+ if (flush) { TestRoleGroupRoles.withSession { it.flush() } }
+
rowCount > 0
}
- static void removeAllByPermission(TestRole p) {
+ static void removeAll(TestRole r, boolean flush = false) {
TestRoleGroupRoles.where {
- role == TestRole.load(p.id)
+ role == TestRole.load(r.id)
}.deleteAll()
+
+ if (flush) { TestRoleGroupRoles.withSession { it.flush() } }
}
- static void removeAllByRole(TestRoleGroup r) {
+ static void removeAll(TestRoleGroup rg, boolean flush = false) {
TestRoleGroupRoles.where {
- group == TestRoleGroup.load(r.id)
+ roleGroup == TestRoleGroup.load(rg.id)
}.deleteAll()
+
+ if (flush) { TestRoleGroupRoles.withSession { it.flush() } }
}
static constraints = {
- role validator: { r, obj ->
- if (get(obj.group.id, r.id)) {
- return "rolePermission.exists"
+ role validator: { TestRole r, TestRoleGroupRoles rg ->
+ if (rg.roleGroup == null) return
+ boolean existing = false
+ TestRoleGroupRoles.withNewSession {
+ existing = TestRoleGroupRoles.exists(rg.roleGroup.id, r.id)
+ }
+ if (existing) {
+ return 'roleGroup.exists'
}
}
}
static mapping = {
- id composite: ['group', 'role']
+ id composite: ['roleGroup', 'role']
version false
}
}
@@ -19,8 +19,6 @@ package test
*/
class TestUser {
- static transients = ['roles', 'roleNames', 'groups']
-
String loginName
String passwrrd
boolean enabld
@@ -29,7 +27,7 @@ class TestUser {
boolean passwordExpired
Set<TestRole> getRoles() { TestUserRole.findAllByUser(this).collect { it.role } }
- Set<TestRoleGroup> getGroups() { TestUserRoleGroup.findAllByUser(this).collect { it.group } }
+ Set<TestRoleGroup> getGroups() { TestUserRoleGroup.findAllByUser(this).collect { it.roleGroup } }
Collection<String> getRoleNames() { roles*.auth }
static constraints = {
@@ -31,7 +31,7 @@ class TestUserRole implements Serializable {
return false
}
- other.user.id == user.id && other.role.id == role.id
+ other.user?.id == user?.id && other.role?.id == role?.id
}
int hashCode() {
@@ -48,30 +48,57 @@ class TestUserRole implements Serializable {
}.get()
}
+ static boolean exists(long userId, long roleId) {
+ TestUserRole.where {
+ user == TestUser.load(userId) &&
+ role == TestRole.load(roleId)
+ }.count() > 0
+ }
+
static TestUserRole create(TestUser user, TestRole role, boolean flush = false) {
- new TestUserRole(user: user, role: role).save(flush: flush, insert: true)
+ def instance = new TestUserRole(user: user, role: role)
+ instance.save(flush: flush, insert: true)
+ instance
}
static boolean remove(TestUser u, TestRole r, boolean flush = false) {
-
int rowCount = TestUserRole.where {
user == TestUser.load(u.id) &&
role == TestRole.load(r.id)
}.deleteAll()
+ if (flush) { TestUserRole.withSession { it.flush() } }
+
rowCount > 0
}
- static void removeAll(TestUser u) {
+ static void removeAll(TestUser u, boolean flush = false) {
TestUserRole.where {
user == TestUser.load(u.id)
}.deleteAll()
+
+ if (flush) { TestUserRole.withSession { it.flush() } }
}
- static void removeAll(TestRole r) {
+ static void removeAll(TestRole r, boolean flush = false) {
TestUserRole.where {
role == TestRole.load(r.id)
}.deleteAll()
+
+ if (flush) { TestUserRole.withSession { it.flush() } }
+ }
+
+ static constraints = {
+ role validator: { TestRole r, TestUserRole ur ->
+ if (ur.role == null) return
+ boolean existing = false
+ TestUserRole.withNewSession {
+ existing = TestUserRole.exists(ur.user.id, r.id)
+ }
+ if (existing) {
+ return 'userRole.exists'
+ }
+ }
}
static mapping = {
@@ -1,5 +1,7 @@
package test
+import org.apache.commons.lang.builder.HashCodeBuilder
+
/**
* @author <a href='mailto:th3morg@gmail.com'>Ryan Morgan</a>
*/
@@ -8,50 +10,85 @@ class TestUserRoleGroup implements Serializable {
private static final long serialVersionUID = 1
TestUser user
- TestRoleGroup group
+ TestRoleGroup roleGroup
boolean equals(other) {
if (!(other instanceof TestUserRoleGroup)) {
return false
}
- other.user?.id == user?.id && other.group?.id == group?.id
+ other.user?.id == user?.id && other.roleGroup?.id == roleGroup?.id
+ }
+
+ int hashCode() {
+ def builder = new HashCodeBuilder()
+ if (user) builder.append(user.id)
+ if (roleGroup) builder.append(roleGroup.id)
+ builder.toHashCode()
}
- static TestUserRoleGroup get(long userId, long roleId) {
+ static TestUserRoleGroup get(long userId, long roleGroupId) {
TestUserRoleGroup.where {
user == TestUser.load(userId) &&
- group == TestRoleGroup.load(roleId)
+ roleGroup == TestRoleGroup.load(roleGroupId)
}.get()
}
- static TestUserRoleGroup create(TestUser user, TestRoleGroup role, boolean flush = false) {
- new TestUserRoleGroup(user: user, group: role).save(flush: flush, insert: true)
+ static boolean exists(long userId, long roleGroupId) {
+ TestUserRoleGroup.where {
+ user == TestUser.load(userId) &&
+ roleGroup == TestRoleGroup.load(roleGroupId)
+ }.count() > 0
+ }
+
+ static TestUserRoleGroup create(TestUser user, TestRoleGroup roleGroup, boolean flush = false) {
+ def instance = new TestUserRoleGroup(user: user, roleGroup: roleGroup)
+ instance.save(flush: flush, insert: true)
+ instance
}
- static boolean remove(TestUser u, TestRoleGroup r) {
+ static boolean remove(TestUser u, TestRoleGroup rg, boolean flush = false) {
int rowCount = TestUserRoleGroup.where {
user == TestUser.load(u.id) &&
- group == TestRoleGroup.load(r.id)
+ roleGroup == TestRoleGroup.load(rg.id)
}.deleteAll()
+ if (flush) { TestUserRoleGroup.withSession { it.flush() } }
+
rowCount > 0
}
- static void removeAll(TestUser u) {
+ static void removeAll(TestUser u, boolean flush = false) {
TestUserRoleGroup.where {
user == TestUser.load(u.id)
}.deleteAll()
+
+ if (flush) { TestUserRoleGroup.withSession { it.flush() } }
}
- static void removeAll(TestRoleGroup r) {
+ static void removeAll(TestRoleGroup rg, boolean flush = false) {
TestUserRoleGroup.where {
- group == TestRoleGroup.load(r.id)
+ roleGroup == TestRoleGroup.load(rg.id)
}.deleteAll()
+
+ if (flush) { TestUserRoleGroup.withSession { it.flush() } }
+ }
+
+ static constraints = {
+ user validator: { TestUser u, TestUserRoleGroup ug ->
+ if (ug.roleGroup == null) return
+ boolean existing = false
+ TestUserRoleGroup.withNewSession {
+ existing = TestUserRoleGroup.exists(u.id, ug.roleGroup.id)
+ }
+ if (existing) {
+ return 'userGroup.exists'
+ }
+ }
}
static mapping = {
- id composite: ['group', 'user']
+ id composite: ['roleGroup', 'user']
version false
}
}
Oops, something went wrong.

1 comment on commit 2a5e160

Great changes and additions. Reviewing them was a good learning experience. Thank you for taking the time!

Please sign in to comment.