对于UserController进行控制,只允许管理员创建新用户
利用beforeInterceptor
如下:
注意:auth是方法,不是闭包,不会在URL中暴露给最终用户。只能被类中的其他闭包或方法调用。
beforeInterceptor中的&符号是方法指针,它是由Groovy提供的。这意味着,auth()将会在调用每个Action之前被调用。
except列表允许对login,logout和authenticate Action不进行控制。除了except,还有only,意思与except相反。
利用beforeInterceptor
如下:
class UserController {
def beforeInterceptor=[action:this.&auth,except:['login','logout','authenticate']]
def auth(){
if(!session.user){
redirect(cotroller:'user',action:'login')
return false
}
if(!session.user.admin){
flash.message="请用管理员登录"
redirect(controller:'race',atcion:'list')
return false
}
}
}
注意:auth是方法,不是闭包,不会在URL中暴露给最终用户。只能被类中的其他闭包或方法调用。
beforeInterceptor中的&符号是方法指针,它是由Groovy提供的。这意味着,auth()将会在调用每个Action之前被调用。
except列表允许对login,logout和authenticate Action不进行控制。除了except,还有only,意思与except相反。