【问题标题】:Checking permissions using Webservices in Liferay 6.2在 Liferay 6.2 中使用 Web 服务检查权限
【发布时间】:2015-05-26 13:13:46
【问题描述】:

我正在尝试使用 liferay 网络服务。 我想检查用户是否拥有资源的权限(添加/更新/删除),但我没有找到任何方法。

我发现 liferay 在 web 服务方法中实现了权限检查。 通过这种方式,当用户尝试执行他没有权限的操作时,我可以显示错误消息。

但我认为最好不要让他而不是显示错误消息。

例如: 用户无权添加文档。 当他尝试添加文档时,Webservice 调用将引发异常。 在我看来,隐藏添加按钮比显示错误要好。

【问题讨论】:

  • 您可以创建自己的自定义网络服务来检查用户是否拥有任何资源的权限。

标签: web-services liferay liferay-6


【解决方案1】:

你可以这样使用:

    PermissionChecker permissionChecker = getPermissionChecker();

    if(!permissionChecker.hasPermission(groupId, permissionModelKey, groupId, permissionKey))
            throw new PortalException("You don't have the required permissions!");

其中permissionModelKey 是您的资源的型号名称(例如:com.your.namespace.model.YourClass),permissionKey 是一个操作键,可以在您的resource-actions/default.xml 中定义。

将此作为YourClassServiceImpl 服务方法的第一行,如果用户没有正确的权限(不要用管理员用户测试),它将抛出PortalException。或者,如果您愿意,您可以优雅地退出。这仅取决于您要处理该用例的方式。

希望这会有所帮助。

【讨论】:

  • 它不能满足我的要求。您能否再次阅读问题中提到的示例。
【解决方案2】:

我没有时间详细说明,但您可以使用ResourcePermissionLocalServiceUtil 中的hasResourcePermission 来解决您的问题:

public static boolean hasResourcePermission(long companyId,
                                            String name,
                                            int scope,
                                            String primKey,
                                            long roleId,
                                            String actionId)
                                     throws PortalException,
                                            SystemException

如果角色在范围内有权对该类型的资源执行操作,则返回 true。

根据作用域的不同,primKey 的值会有不同的含义。有关详细信息,请参阅 ResourcePermissionImpl。

Parameters:
companyId - the primary key of the company
name - the resource's name, which can be either a class name or a portlet ID
scope - the scope
primKey - the primary key
roleId - the primary key of the role
actionId - the action ID
Returns:
true if the role has permission to perform the action on the resource; false otherwise
Throws:
PortalException - if a role with the primary key or a resource action with the name and action ID could not be found
SystemException - if a system exception occurred

javadocs 链接:hasResourcePermission

【讨论】:

  • 我创建了一个新的 Web 服务方法并从 CustomEntityServiceImpl 类中调用了 hasResourcePermission。
猜你喜欢
  • 2014-12-10
  • 1970-01-01
  • 2020-09-01
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多