Customizando o Spring Security
16/11/2012 14:07
class Usermodule implements Serializable {
transient springSecurityService;
transient UsermoduleService;
String name;
String login;
String username
String password;
String email;
String description;
Boolean valid = Boolean.TRUE;
static hasMany = [profiles: Profile, modulos: Module]
public String toString() {
return this.name
}
static constraints = {
name(blank:Boolean.FALSE, nullable: Boolean.FALSE)
login(blank:Boolean.FALSE, nullable: Boolean.FALSE, unique: Boolean.TRUE)
password(blank:Boolean.FALSE, nullable: Boolean.FALSE, password: Boolean.TRUE)
email(blank:Boolean.FALSE, nullable: Boolean.FALSE, unique: Boolean.TRUE)
description(blank:Boolean.TRUE, nullable: Boolean.TRUE)
valid(blank:Boolean.FALSE, nullable: Boolean.FALSE)
}
static mapping = {
password column: '`password`'
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
Set<Ruleprofile> getAuthorities() {
UsermoduleService.getUserProfileRules(this.id) as Set
}
}
class Ruleprofile implements Serializable {
String authority
String name
String description
static belongsTo = Profile
static hasMany = [profiles: Profile]
static constraints = {
name(nullable:false, blank:false, unique:true)
description(nullable:true, blank:true)
authority(nullable:false, blank:false, unique:true)
}
static mapping = {
cache true
}
@Override
public String toString() {
return authority;
}
}
class Profile implements Serializable{
String name
String description
Module module
static belongsTo = [Module, Usermodule]
static hasMany = [rules: Ruleprofile, users: Usermodule]
public String toString() {
return this.name
}
static constraints = { name(blank:false, nullable:false, unique:true)
description(blank:true, nullable:true)
module(blank:true, nullable:true)
}
}
class UsermoduleService {
public def getUserProfileRules(Long id) {
def usermoduleInstance = Usermodule.get(id)
Set<Ruleprofile> rules = new HashSet<Ruleprofile>();
if (usermoduleInstance) {
usermoduleInstance.profiles.each{ profile->
rules.addAll(profile.rules);
}
}
return rules;
}
}
grails.plugins.springsecurity.userLookup.userDomainClassName = 'br.com.ntc.moduloseguranca.Usermodule'
grails.plugins.springsecurity.authority.className = 'br.com.ntc.moduloseguranca.Ruleprofile'
@Secured(['ROLE_ADMIN'])
def create() {
[usermoduleInstance: new Usermodule(params)]
}
transient UsermoduleService;
def create() {
if (!UsermoduleService.getUserProfileRules(session.user.id).contains('ROLE_ADMIN')) {
redirect(action: "list");
}
[usermoduleInstance: new Usermodule(params)]
}
grails.plugins.springsecurity.securityConfigType = "Annotation"
grails.plugins.springsecurity.userLookup.authoritiesPropertyName = 'rules'
grails.plugins.springsecurity.authority.nameField = 'authority'
def springSecurityService;
def usermoduleService;
@Securedpor padrão e com isso para que a verificação do "ROLE_ADMIN" funcione, tente usar
@Secured("hasRole('ROLE_ADMIN')" )e por último garanta que o serviço esteja com o import correto:
import grails.plugins.springsecurity.Secured;
Para se registrar, clique aqui.