0

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

4

1 回答 1

1

我的第一个猜测是您的过滤器定义并不完全正确。

adminOnly(controller:'appUsers', action: '(create|edit|show|list|update)')

应该..

adminOnly(controller:'appUsers', action: 'create|edit|show|list|update')

更新..

查看代码后的另一个疯狂猜测。当您的用户成功通过身份验证时,在 auth() 方法中,您将用户名放入会话中。之后不再设置 params.username !当您执行重定向(控制器:'患者',操作:'列表')之类的重定向时,您将在页面成功呈现后丢失所有参数。一个解决方案是在 adminOnly 过滤器中重新使用 session.user。

        println("username" + session?.user)
        def userRole = AppUsers.findByUsername(session?.user)
        println("useeeeeee in fillter role " + session.user)
于 2013-03-23T11:18:03.940 回答