Search code examples
liferayliferay-6.2

Liferay 6.2: change one role´s permission to view roles or assign roles to users


I´m coding a script to change several users´s permissions and, in one step, I need to check some boxes in the matrix located in Roles -> Permissions. In order words, I need to redefine which users will be able to view or assign some other roles and it´s kind of a massive change, so the manual approach should be as avoided as possible.

Do you know what function should I apply to do this? Maybe something similar to "ResourcePermissionLocalServiceUtil.setResourcePermissions" but I´m not sure there is a proper "action" here or if there is actually a "resource" in this case.

Thanks in advance.


Solution

  • I finally solved this. It was, indeed, using the function setResourcePermission. Just for helping the people around here, I´ll explain the arguments briefly, in order:

    • long companyId: the id of the portal instance.
    • String name: the resource´s name, in this case, "com.liferay.portal.model.Role".
    • int scope: it has to be with the joint the permission affects. 1 is the entire instance of the portal while 4 is the only role referenced in the...
    • String primKey: is the Id of the role that will be able to be "manipulated" by...
    • long roleId: the id of the Role that will have the permissions over the role referenced in the previous argument.
    • String[] actionIds: an array of keys determining the actions. They are KEYS, not Ids,. E.g. VIEW, ASSIGN_MEMBERS...

    So, for instance, a call like this:

    PermissionResourceLocalServiceUtil.setResourcePermissions(10000,"com.liferay.portal.model.Role",4,"20150",22000, {"VIEW", "ASSIGN_MEMBERS"});
    

    Will grant permissions to the role 22000 of View and a assignation of users to the role "20150" in the portal instance whose id is 10000.

    Edit: another version of the same function is setOwnerResourcePermissions, which is the same but gives you the possibility to specify the ownerId of the permission.