I am using grails 2.2.0 to an application with login page and add users feature ,
Here is the authentication method in the contoller :
def auth()
{
def user = AppUsers.findByUsernameAndPassword (params?.username , params?.password.encodeAsSHA())
if(params.username==user?.username && params.password.encodeAsSHA()==user?.password/*.encodeAsSHA()*/)
{
flash.message =message(code:'user.log.suc',args:[params.username])
session.user=params.username
redirect (controller:'patients' , action:'list')
}
else
{
//render ('login failed in if')
//flash.message='login failed'
flash.message =message(code:'user.log.failed',args:[params.username])
redirect (action:'login')
}
}
and here is to check if the user is admin or not :
def beforeInterceptor = [action:this.&isUserAdmin , except :[/*'auth' ,*/ 'logout', 'login']]
def isUserAdmin ()
{
sherifUser=params.username
if (!params?.username?.empty)
{
// println("session userrr " +session.user)
def userRole =AppUsers.findByUsername(params.username)//(username :'${params.username}', role:'admin')
println("userrole " + userRole.role)
//session.setAttribute("${params.username}", userRole.role)
session["${params.username}"] = userRole.role
println("session role : " + session.getAttribute("${params.username}"))
return true
}
else
{
flash.message ="username or password empty"
render(action:'login')
}
}
and I'm using a filter to check if the user got admin role or not before entering to the add users page , here is the filter :
class AdminFilters
{
def filters =
{
adminOnly(controller:'appUsers', action: '(create|edit|show|list|update)')
{
before =
{
// def sessions = RequestContextHolder.currentRequestAttributes().getSession()
println("params username" + params.username) // this always returns back null !!
def userRole = AppUsers.findByUsername(params.username)
println("useeeeeee in fillter role " + session["params.username"])
if (userRole?.equalsIgnoreCase("user"))
{
flash.messsage= " this area for admins only"
redirect (controller:'appUsers' , action:'login')
return false
}
else
{
flash.messsage= " Welcome admin"
redirect (controller:'appUsers' , action:'list')
}
}
after =
{ Map model ->
}
afterView =
{ Exception e ->
}
}
}
}
what cause that always params.username is null in the filter , altough the auth() method is working perfect ?!!
Any clarifications ?
Thanks Sherif