1

每当我将角色添加到我现有的角色时,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
}
}
4

0 回答 0