在SecUser grails的许多角色中匹配Sec角色 [英] Match one Sec role among many roles of SecUser grails

查看:112
本文介绍了在SecUser grails的许多角色中匹配Sec角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下五个角色[ROLE_ADMIN,ROLESWITCHUSER,ROLE_DOCTOR,ROLE_USER]
和一些普通用户的管理员用户,只有一个角色即ROLE_USER,现在我的问题是我如何获得只有普通用户从我的secuser表我尝试了somne​​迭代

  def roleId = SecRole.findByAuthority(ROLE_USER)
userInstance = SecUserSecRole.findAllBySecRole(roleId).secUser

这里我得到了所有用户的userInstance以及adminuser现在我试图从我的userInstance中删除adminuser,并将其保存在selectUserMap中,但是在某个时候得到结果并且有时会给所有用户。我认为不排序userinstansce角色的sort()函数请帮助我



$ p $ for(int i = 0; i< userInstance。 size(); i ++)
{
println(am in loop+ i + userInstance [i] .username +roles+ userInstance [i] .getAuthorities())
def x =(userInstance [i] .getAuthorities()。sort())
$ b $ for(a x)
{// println(a.getAuthority())
if (a.getAuthority()== use))
abc = true
else
abc = false
if((a.getAuthority()== adm))
{
println(break)
break;
}
abc =(abc&(a.getAuthority()== use))
if(abc)
{
println(am in如果)
selectUserMap.add(j,userInstance [i])
j = j + 1
}
else
{
println(am in else)
}

}

}
println(==============所有用户+ selectUserMap)


解决方案

使用分层角色 - 请参阅 http: //grails-plugins.github.com/grails-spring-security-core/docs/manual/ - 然后您不会向任何人授予 ROLE_USER 但是真正的用户。如果你像这样定义你的层次结构:

  grails.plugins.springsecurity.roleHierarchy ='''
ROLE_ADMIN> ROLE_USER
ROLE_DOCTOR> ROLE_USER
ROLE_ADMIN> ROLE_DOCTOR
'''

那么您不需要明确地授予<$ c $将数据库中的c> ROLE_USER 添加到医生或管理员,但角色将被推断为已授予。然后您的原始查询将工作:

  def userRole = SecRole.findByAuthority(ROLE_USER)
def usersWithUserRole = SecUserSecRole .findAllBySecRole(userRole).secUser

如果您不能或不想这样做,那么你应该使用适当的数据库查询。从数据库加载每个用户和每个用户的角色并在应用程序中将其过滤出来是非常昂贵且不必要的。使用此HQL查询:

  def userRole = SecRole.findByAuthority(ROLE_USER)
def users = SecUserSecRole.executeQuery (
')从SecUser中选择你,其中'+
'(从SecUserSecRole中选择count(ur.user),其中ur.user = u)= 1和'+
'(:r in(从SecUserSecRole中选择ur.role,其中ur.user = u))',
[r:userRole])


I have admin user with following five roles[ROLE_ADMIN,ROLESWITCHUSER,ROLE_DOCTOR,ROLE_USER] and some normal users with only one role i.e ROLE_USER ,now my question is how can i get only normal users from my secuser table i tried with somne iterations

def roleId=SecRole.findByAuthority("ROLE_USER")
userInstance = SecUserSecRole.findAllBySecRole(roleId).secUser

here i got userInstance with all users along with adminuser now i tried to elminate adminuser from my userInstance and saved it in selectUserMap but am getting result for sometime and sometimes its giving all users. I think the sort() function not sorting the userinstansce roles please help me

for(int i=0;i<userInstance.size();i++) 
{
    println( "am in loop "+i+userInstance[i].username+"roles"+userInstance[i].getAuthorities())
    def x=(userInstance[i].getAuthorities().sort())

    for(a in x )
    {   //println(a.getAuthority()) 
        if((a.getAuthority() == use))
            abc=true
        else
            abc=false
        if((a.getAuthority() == adm))
        {
            println("break")
            break;
        }
        abc=(abc && (a.getAuthority() == use))
        if(abc)
        {   
            println("am in true if  ")
            selectUserMap.add(j,userInstance[i])
            j=j+1
        }
        else  
        {           
            println("am in else")
        }

    }

}
println("==============all users "+selectUserMap)

解决方案

One thing that would help is to use hierarchical roles - see section "14 Hierarchical Roles" at http://grails-plugins.github.com/grails-spring-security-core/docs/manual/ - and then you wouldn't grant ROLE_USER to anyone but "real" users. If you define your hierarchy like this:

grails.plugins.springsecurity.roleHierarchy = '''
   ROLE_ADMIN > ROLE_USER
   ROLE_DOCTOR > ROLE_USER
   ROLE_ADMIN > ROLE_DOCTOR
'''

then you don't need to explicitly grant ROLE_USER in the database to either a doctor or an admin, but the role will be inferred as granted. Then your original query will work:

def userRole = SecRole.findByAuthority("ROLE_USER")
def usersWithUserRole = SecUserSecRole.findAllBySecRole(userRole).secUser

If you can't or don't want to do this, then you should use a proper database query. It's extremely and unnecessarily expensive to load every user and every user's roles from the database and filter them out in your application. Use this HQL query:

def userRole = SecRole.findByAuthority("ROLE_USER")
def users = SecUserSecRole.executeQuery(
   'select u from SecUser u where ' +
   '(select count(ur.user) from SecUserSecRole ur where ur.user=u)=1 and ' +
   '(:r in (select ur.role from SecUserSecRole ur where ur.user=u))',
   [r: userRole])

这篇关于在SecUser grails的许多角色中匹配Sec角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆