Search code examples
javapermissionsliferayportletliferay-6

Liferay 6.0.5 - Set Permissions in a new page programatically


I'm creating a new liferay page dynamically (Which is called layout in Liferay). But, it is created with default user permissions.

What I need is to set the permissions for the new page: The Power User has only the permission VIEW, but I want to add the permissions DELETE, UPDATE and PERMISSIONS.

One method that I checked in the internet is:

long userRoleId = RoleLocalServiceUtil.getRole(companyId,
                                RoleConstants.POWER_USER).getRoleId();

ResourcePermissionLocalServiceUtil.addResourcePermission (
CompanyID,
layout.getModelClassName ().
ResourceConstants.SCOPE_INDIVIDUAL,
String.valueOf (layout.getPrimaryKey ()) userRoleId,
ActionKeys.DELETE);

And other one is:

ResourcePermissionServiceUtil.setIndividualResourcePermissions(groupId,
                         companyId, layout.getName(Locale.FRANCE),
                         String.valueOf(layout.getPrimaryKey()), userRoleId,
                         new String[] { ActionKeys.DELETE });

Since my liferay version is 6.0.6, the layout object has no method layout.getModelClassName(), so I tried with the methods layout.getClass().getName() and layout.getName(Locale.FRANCE), and the result was:

NoSuchResourceException

So, I'd be very thankful if someone knows how to change the permission of a page programatically.

Here's the code I'm using to create new pages (without the code that add the permissions):

private void createPage(ThemeDisplay themeDisplay, String name)
                  throws Exception {
            _log.info("createPage:" + name);
            long userId = themeDisplay.getUserId();
            long groupId = themeDisplay.getScopeGroupId();
            long companyId = themeDisplay.getCompanyId();
            boolean privateLayout = false;
            long parentLayoutId = 0;
            String title = null;
            String description = null;
            String type = LayoutConstants.TYPE_PORTLET;
            boolean hidden = true;
            String friendlyURL = "/cngwallboard/" + name;
            ServiceContext serviceContext = new ServiceContext();
            serviceContext.setScopeGroupId(groupId);

            Layout layout = LayoutLocalServiceUtil.addLayout(userId, groupId,
                        privateLayout, parentLayoutId, name, title, description, type,
                        hidden, friendlyURL, serviceContext);
            LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
                        .getLayoutType();

            layoutTypePortlet.setLayoutTemplateId(userId, "3_pages2");

            _log.info("setThemeId");
            layout.setThemeId("wallboardconfiguration_WAR_wallboardconfigurationtheme");

            LayoutSetLocalServiceUtil.updateLookAndFeel(layout.getGroupId(), false,
                        "wallboardconfiguration_WAR_wallboardconfigurationtheme", "01",
                        "", false);

            PortletPreferences prefs = PortletPreferencesFactoryUtil
                        .getPortletSetup(layout, "118_INSTANCE_pg01", "");

            prefs.setValue("layout-template-id", "2_windows_model_1");
            prefs.setValue("portlet-setup-show-borders", "true");

            prefs.store();

            LayoutLocalServiceUtil.updateLayout(layout);

      }

Solution

  • You can use this code to assign permission

    Role userRole = RoleLocalServiceUtil.getRole(companyId, "POWER USER");
    
    ResourcePermissionServiceUtil.setIndividualResourcePermissions(themeDisplay.getScopeGroupId(),
    themeDisplay.getCompanyId(), Layout.class.getName(), "primKey",
    userRole.getRoleId(), new String[] { ActionKeys.VIEW });
    

    HTH