添加角色给现有用户时未知的grails行为

时间:2013-10-28 10:21:28

标签: grails spring-security

每当我将角色添加到我现有的User时,它就无法正常运行。

日志中没有错误,我也通过try catch访问我的控制器,但没有运气。

这是我的addRole封口:

def addRole = {
     def role = Role.get(params.addRoleAutoComplete_id)
     def user = User.get(params.id)
     if (!role) {
         flash.message = "Select role from list"
     } else {
     try{
        UserRole.create( user,  role)
     }catch(Exception e){e.printStackTrace()}
    }
 redirect  action:'edit', id:user.id
}

使用Grails 2.3,grails spring安全插件和STS 3.2

这是我的UserRole.Groovy:

class UserRole implements Serializable {

User user
Role role

boolean equals(other) {
    if (!(other instanceof UserRole)) {
        return false
    }

    other.user?.id == user?.id &&
        other.role?.id == role?.id
}

int hashCode() {
    def builder = new HashCodeBuilder()
    if (user) builder.append(user.id)
    if (role) builder.append(role.id)
    builder.toHashCode()
}


static UserRole create(User user, Role role, boolean flush = false) {
    new UserRole(user: user, role: role).save(flush: flush, insert: true)
}

static boolean remove(User user, Role role, boolean flush = false) {
    UserRole instance = UserRole.findByUserAndRole(user, role)
    if (!instance) {
        return false
    }

    instance.delete(flush: true)
    true
}


static mapping = {
    id composite: ['role', 'user']
    version false
}
}

0 个答案:

没有答案