【发布时间】:2016-05-19 06:40:57
【问题描述】:
我正在尝试使用 Spring Security 3 插件配置我的 Grails 3 应用程序,以防止没有特定权限的用户访问一组特定功能。
我使用了默认设置,带有User、Role 和UserRole 域类(后者包括在下面),目的是广泛使用@Secure 或@PreAuthorize 注释。
后来出现了一个新要求:另一个域类是Project(基本上你可以被授予访问一个或多个项目的权限),并且用户可能拥有特定于项目的权限。
例子:
- 有A、B、C为
Roles - User1 具有 A 和 C 角色
- 对于 ProjectAlpha,我想“覆盖”User1 的“默认”角色,以便他拥有 A 和 B 特权。
因此,User1 对 ProjectAlpha 的“真实”权限必须在带有 Project 引用的 UserRole 条目中找到。
首先我要为UserRole 类添加一个新的Project project 属性,project 在拦截器中预先计算。但后来我想不出这个任务的最佳方法(甚至可行性)。
欢迎任何建议,提前谢谢!
package myApp
import grails.gorm.DetachedCriteria
import groovy.transform.ToString
import org.apache.commons.lang.builder.HashCodeBuilder
@ToString(cache=true, includeNames=true, includePackage=false)
class UserRole implements Serializable {
private static final long serialVersionUID = 1
static constraints = {
role validator: { Role r, UserRole ur ->
if(ur.user == null || ur.user.id == null) return
boolean existing = false
UserRole.withNewSession {
existing = UserRole.exists(ur.user.id, r.id)
}
if(existing) {
return 'userRole.exists'
}
}
}
static mapping = {
id composite: ['user', 'role']
version false
}
User user
Role role
/*UserRole(User u, Role r) {
this()
user = u
role = r
}*/
@Override
boolean equals(other) {
if(!(other instanceof UserRole)) {
return false
}
other.user?.id == user?.id && other.role?.id == role?.id
}
@Override
int hashCode() {
def builder = new HashCodeBuilder()
if(user) builder.append(user.id)
if(role) builder.append(role.id)
builder.toHashCode()
}
static UserRole get(long userId, long roleId) {
criteriaFor(userId, roleId).get()
}
static boolean exists(long userId, long roleId) {
criteriaFor(userId, roleId).count()
}
private static DetachedCriteria criteriaFor(long userId, long roleId) {
UserRole.where {
user == User.load(userId) &&
role == Role.load(roleId)
}
}
static UserRole create(User user, Role role, boolean flush = false) {
def instance = new UserRole(user: user, role: role)
instance.save(flush: flush, insert: true)
instance
}
static boolean remove(User u, Role r, boolean flush = false) {
if(u == null || r == null) return false
int rowCount = UserRole.where { user == u && role == r }.deleteAll()
if(flush) { UserRole.withSession { it.flush() } }
rowCount
}
static void removeAll(User u, boolean flush = false) {
if(u == null) return
UserRole.where { user == u }.deleteAll()
if(flush) { UserRole.withSession { it.flush() } }
}
static void removeAll(Role r, boolean flush = false) {
if(r == null) return
UserRole.where { role == r }.deleteAll()
if(flush) { UserRole.withSession { it.flush() } }
}
}
【问题讨论】: