2013-05-09 138 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

相關問題