博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Liferay 用权限控制视图
阅读量:7030 次
发布时间:2019-06-28

本文共 1327 字,大约阅读时间需要 4 分钟。

 

需求:

在Liferay中,我们可以用其特有的权限机制来控制视图,比如我们想要页面上某些元素只对某种权限的用户开放。

 

解决:

其实Liferay中有内置的标记和对象可以轻松的达到这些要求,但是我们首先必须在页面上引入正确的标记库:

 
  1. <liferay-theme:defineObjects/> 

 

然后我们在页面上,就可以使用user 内置对象来正确的取得其权限了,我们把它封装在一段scriplet中:

 
  1. <!-- charles:determine whether the current has the admin privilege --> 
  2.  
  3. <% 
  4.  
  5.                 boolean hasAdminPrivilege= false
  6.  
  7.                 List<Role> userRoles = user.getRoles(); 
  8.  
  9.                 for (Role role :userRoles){ 
  10.  
  11.                                 if("Administrator".equals( role.getName().trim()) ){ 
  12.  
  13.                                                 hasAdminPrivilege=true
  14.  
  15.                                                 break
  16.  
  17.                                 } 
  18.  
  19.                 } 
  20.  
  21.                 
  22.  
  23. %> 

比如这段代码我们就是用user内置对象获取它所有的权限,然后判断它是否包含"Administrator"权限,然后吧这个布尔变量保存下来。

 

最后我们就用刚才的boolean变量来正确的进行视图控制,可以配合c标记库一起使用,比如以下代码就实现了只有Admin用户才可以看到"delete'按钮。

 
  1. <!-- charles:make conclusion that only the Administrator can view the delete button  --> 
  2.  
  3.   
  4.  
  5. <c:if test="<%=hasAdminPrivilege %>"> 
  6.  
  7.   
  8.  
  9.                 <!--  the first time when adminstrator goes to the view mode, he can't see the delete button --> 
  10.  
  11.                 <!--  because now nothing uploaded ,how it can delete from web server--> 
  12.  
  13.                 <!--  but after uploaded (heml_url !=null) ,then the delete button is visible to administrator --> 
  14.  
  15.                 <c:if test="${html_url != null }"> 
  16.  
  17.                 
  18.  
  19.                 <form action="<portlet:actionURL name="deleteInstance"/>" method="post" name="<portlet:namespace />" class="rs-form">               
  20.  
  21.                                       <input type="submit" value="Delete" class="del"/> 
  22.  
  23.                 </form> 
  24.  
  25.                 
  26.  
  27.                 </c:if> 
  28.  
  29.                 
  30.  
  31. </c:if> 
本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/1004274,如需转载请自行联系原作者
你可能感兴趣的文章
【常用配置】Spring框架web.xml通用配置
查看>>
[leetcode 240]Search a 2D Matrix II
查看>>
域名指的是这一级目录
查看>>
[Angular] Creating an Observable Store with Rx
查看>>
[转]Porting to Oracle with Entity Framework NLog
查看>>
chmod更改文件的权限
查看>>
oracle 10g/11g RAC 启停归档模式
查看>>
poj3461 Oulipo
查看>>
OAuth2.0学习(1-12)开源的OAuth2.0项目和比较
查看>>
Gitlab,这也就O了???
查看>>
2014 百度之星 1003 题解 Xor Sum
查看>>
Linux中在主机上实现对备机上文件夹及文件的操作的C代码实现
查看>>
iOS 块的简单理解
查看>>
idea中如何配置git以及在idea中初始化git
查看>>
关于JQuery Class选择器的一点
查看>>
POJ3264 Balanced Lineup
查看>>
redis-cli 连接远程服务器
查看>>
emlog通过pjax实现无刷新加载网页--完美解决cnzz统计和javascript失效问题
查看>>
sublime 之 vitage/emmet
查看>>
代码管理(四)SVN和Git对比
查看>>