假設有2個角色(RoleU1
和RoleU2
),所以現在如果我理解正確的話,如果用戶有RoleU1
他有一個鏈接轉到一個頁面,說Welcome Role U1 page
與RoleU2
用戶將有一個鏈接到網頁,Welcome to Role U2 page
,要做到這一點,你可以做到以下幾點:
- 獲取角色
RoleU1
和RoleU2
或獲取只是它們的ID。
- 取取登錄用戶。
- 獲取登錄用戶的所有角色或獲取用戶的所有角色ID。
- 檢查用戶的角色,然後相應地向用戶顯示鏈接。
這裏是上述步驟的代碼:
#* 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
希望這是你需要什麼,或者至少會給出一個提示。
什麼不工作?你有什麼錯誤嗎?你想檢查用戶是否有這個角色或權限,並相應地顯示鏈接?什麼是'用戶角色'?如果你可以詳細說明 – 2013-05-09 07:27:51
我希望具有一個角色的用戶轉到頁面A和頁面B中的其他人,這將有所幫助。 – user2273574 2013-05-09 09:28:00