Liferay7开发文档_3.6.2服务层的权限

前面介绍了资源的概念。资源是保存在实体中的数据,用于定义如何访问它们。例如,default.xml文件中的配置应用到实体,会在数据库中创建资源。然后将这些资源与Liferay Portal的权限系统一起使用,以确定谁可以对实体执行什么操作。

Liferay Portal提供了完整的API,以管理与Service Builder集成的资源。这个API会自动注入到你的实现类。管理资源,只需在服务的添加和删除方法中调用API。按以下步骤操作:

  1. In your guestbook-service module, open GuestbookLocalServiceImpl.java from the com.liferay.docs.guestbook.service.impl package。
  2. Just before the addGuestbook method’s return statement, add this code:
    resourceLocalService.addResources(user.getCompanyId(), groupId, userId,
        Guestbook.class.getName(), guestbookId, false, true, true);
    

    请注意,resourceLocalService对象已经存在,可使用。这是由Service Builder自动注入的几个实用程序之一。后面会看到其余的。

    此代码为Liferay Portal的数据库添加资源以与实体对应(请注意,该资源guestbookId已包含在调用中)。最后是三个布尔值。第一个,是否添加Portlet操作权限。如果权限是用于portlet资源的,那么为Ture。由于此权限是针对模型资源(实体)的,因此这里为false。另外两个是用于添加组和访客权限的设置。如果将这些设置为true,就会在前面的步骤中添加在权限配置文件(default.xml)中定义的默认权限。既然确定这样做,这些布尔值就会被设置为true。

  3. Next, go to the updateGuestbook method. Add a similar bit of code in between guestbookPersistence.update(guestbook); and the return statement:
    resourceLocalService.updateResources(serviceContext.getCompanyId(),
                    serviceContext.getScopeGroupId(), 
                    Guestbook.class.getName(), guestbookId,
                    serviceContext.getGroupPermissions(),
                    serviceContext.getGuestPermissions());
    
  4. Now you’ll do the same for deleteGuestbook. Add this code in between guestbook = deleteGuestbook(guestbook); and the return statement:
    resourceLocalService.deleteResource(serviceContext.getCompanyId(),
                    Guestbook.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL,
                    guestbookId);
    
  5. Hit [CTRL]+[SHIFT]+O to organize the imports and save the file。
  6. Now you’ll add resources for the Entry entity. Open EntryLocalServiceImpl.java from the same package. For addEntry, add a line of code that adds resources for this entity, just before the return statement:
    resourceLocalService.addResources(user.getCompanyId(), groupId, userId,
        Entry.class.getName(), entryId, false, true, true);
    
  7. For deleteEntry, add this code just before the return statement:
    resourceLocalService.deleteResource(
                   serviceContext.getCompanyId(), Entry.class.getName(),
                   ResourceConstants.SCOPE_INDIVIDUAL, entryId);
    
  8. Finally, find updateEntry and add its resource action, also just before the return statement:
    resourceLocalService.updateResources(
          user.getCompanyId(), serviceContext.getScopeGroupId(), 
          Entry.class.getName(), entryId, serviceContext.getGroupPermissions(),
          serviceContext.getGuestPermissions());
    

这就是添加权限资源所需的全部内容。添加到数据库中的未来实体是完全允许的。但是,请注意,已经在门户中添加到Guestbook应用程序中的任何实体都没有资源,因此不能被权限保护。您将在本节的末尾修复这个问题。

接下来,将创建helper类,以便轻松地检查权限。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值