http://www.liferaysavvy.com/2014/10/working-with-liferay-user-roles.html#
Q-
Q-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:
Role userRole = RoleLocalServiceUtil.getRole(companyId, "POWER USER");
ResourcePermissionServiceUtil.setIndividualResourcePermissions(themeDisplay.getScopeGroupId(),
themeDisplay.getCompanyId(), Layout.class.getName(), "primKey",
userRole.getRoleId(), new String[] { ActionKeys.VIEW });
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
Q-Example:-this is permission to display for user (VIEW PURPOSE) assetVocablary and assetCategory
public static void addPermission(long classPK, String ClassName) {
try {
// ResourcePermissionLocalServiceUtil.setResourcePermissions
// (companyId, name, scope, primKey, roleIdsToActionIds);
ResourcePermissionLocalServiceUtil.setResourcePermissions(
PortalUtil.getDefaultCompanyId(), ClassName,
ResourceConstants.SCOPE_INDIVIDUAL,
String.valueOf(classPK), getRoleId(RoleConstants.USER),
new String[] { ActionKeys.VIEW });
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
}
public static long getRoleId(String roleName) {
long roleId = 0l;
try {
roleId = RoleLocalServiceUtil.getRole(
PortalUtil.getDefaultCompanyId(), roleName).getRoleId();
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
return roleId;
}
for this assetVocablary and assetCategory
AssetVocabularyLocalServiceUtil.updateAssetVocabulary(assetVocabulary);
// Give the permission to vocabulary
AssetCategotyUtil.addPermission(assetVocabulary.getVocabularyId(), AssetVocabulary.class.getName());
} catch (PortalException e) {
_log.error(e.getMessage());
} catch (SystemException e) {
_log.error(e.getMessage());
}
return assetVocabulary.getVocabularyId();
AssetVocabularyLocalServiceUtil.updateAssetVocabulary(assetVocabulary);
// Give the permission to vocabulary
AssetCategotyUtil.addPermission(assetVocabulary.getVocabularyId(), AssetVocabulary.class.getName());
} catch (PortalException e) {
_log.error(e.getMessage());
} catch (SystemException e) {
_log.error(e.getMessage());
}
return assetVocabulary.getVocabularyId();
}
Second way
private void setPermissionsAndProperties(long companyId,
ExpandoColumn expandoColumn) {
HashMap<String, String[]> rolesMap = new HashMap<String, String[]>();
rolesMap.put(RoleConstants.GUEST, new String[] { ActionKeys.VIEW,
ActionKeys.UPDATE });
rolesMap.put(RoleConstants.USER, new String[] { ActionKeys.VIEW,
ActionKeys.UPDATE });
Iterator<String> itr = rolesMap.keySet().iterator();
while (itr.hasNext()) {
String roleName = itr.next();
Role role = null;
try {
role = RoleLocalServiceUtil.getRole(companyId, roleName);
} catch (PortalException e) {
_log.error(e);
} catch (SystemException e) {
_log.error(e);
}
try {
ResourcePermissionLocalServiceUtil.setResourcePermissions(
companyId, ExpandoColumn.class.getName(),
ResourceConstants.SCOPE_INDIVIDUAL,
String.valueOf(expandoColumn.getColumnId()),
role.getRoleId(), rolesMap.get(roleName));
} catch (PortalException e) {
_log.error(e);
} catch (SystemException e) {
_log.error(e);
}
}
Role role = RoleLocalServiceUtil.getRole(companyId, "Site Administrator"); long[] SiteroleIds = {role.getRoleId()}; UserGroupRoleLocalServiceUtil.addUserGroupRoles(userId, siteId, SiteroleIds);
RoleLocalServiceUtil.fetchRole(CompanyThreadLocal.getCompanyId(), "Role Name"); ResourcePermissionLocalServiceUtil.setResourcePermissions(CompanyThreadLocal.getCompanyId(), Role.class.getName(), ResourceConstants.SCOPE_GROUP_TEMPLATE, String.valueOf(role.getRoleId()), role.getRoleId(), new String[] {ActionKeys.VIEW, ActionKeys.UPDATE, ActionKeys.DELETE});
No comments:
Post a Comment