2013-05-09 143 views
1

我想在我的主题Velocity模板来管理用户角色:管理角色的用户在速度

#set ($foundUser = $cmsuser.getUserByUserId($session.getAttribute("user_id"))) 

#if($foundUser) 
    #if($cmsuser.isUserRole($foundUser, "user_admin")) 

     <a href="/group/xxx/xxx" ></a> 

    #else 
     <a href="/group/xxx/yyy" ></a> 
    #end 

但它不工作!!!!

+0

什么不工作?你有什么错误吗?你想检查用户是否有这个角色或权限,并相应地显示链接?什么是'用户角色'?如果你可以详细说明 – 2013-05-09 07:27:51

+0

我希望具有一个角色的用户转到页面A和页面B中的其他人,这将有所帮助。 – user2273574 2013-05-09 09:28:00

回答

2

假设有2个角色(RoleU1RoleU2),所以现在如果我理解正确的话,如果用户有RoleU1他有一个链接转到一个页面,说Welcome Role U1 pageRoleU2用户将有一个链接到网页,Welcome to Role U2 page,要做到这一点,你可以做到以下几点:

  1. 获取角色RoleU1RoleU2或获取只是它们的ID。
  2. 取取登录用户。
  3. 获取登录用户的所有角色或获取用户的所有角色ID。
  4. 检查用户的角色,然后相应地向用户显示链接。

这里是上述步骤的代码:

#* Fetch the RoleLocalService to fetch the roles, this is similar to using RoleLocalServiceUtil in our custom code in portlets *# 
#set($roleLocalService = $serviceLocator.findService("com.liferay.portal.service.RoleLocalService")) 

#* fetch the RoleU1 *# 
#set($role_u1 = $roleLocalService.getRole($company_id, "RoleU1")) 
#set($role_u1_id = $role_u1.getRoleId()) 

#* fetch the RoleU2 *# 
#set($role_u2 = $roleLocalService.getRole($company_id, "RoleU2")) 
#set($role_u2_id = $role_u2.getRoleId()) 

#* current logged-in User is already defined in the theme as $user, so fetch roles for this user *# 
#set ($user_role_ids = $user.getRoleIds()) 

#* check by looping through the user roles *# 
#set ($has_role_u1 = false) 
#set ($has_role_u2 = false) 

#foreach($user_role_id in $user_role_ids) 

    #if($user_role_id == $role_u1_id) 
     #set ($has_role_u1 = true) 
    #end 

    #if($user_role_id == $role_u2_id) 
     #set ($has_role_u2 = true) 
    #end 

#end 

#if($has_role_u1) 
    <a href="/group/xxx/xxx" >Welcome to Role U1 page</a> 
#else if($has_role_u2) 
    <a href="/group/xxx/yyy" >Welcome to Role U2 page</a> 
#end 

希望这是你需要什么,或者至少会给出一个提示。

+0

这也适用于常规角色吗? – ajc 2015-02-24 19:19:55

+1

它应该工作,没有任何问题。 – 2015-02-26 05:07:47

相关问题