use of com.ableneo.liferay.portal.setup.domain.DefinePermissions in project liferay-db-setup-core by ableneo.
the class SetupRoles method addRolePermissions.
private static void addRolePermissions(com.ableneo.liferay.portal.setup.domain.Role role) {
if (role.getDefinePermissions() != null) {
String siteName = role.getSite();
if (siteName != null && !siteName.equals("")) {
LOG.warn(String.format("Note, refering a site inside a role definition makes no sense and will be ignored! This is:%1$s; When doing so, it is necessary to refer a site!", "attribute is intended to be used for refering assigning a site role to an Liferay object, such as a user!", siteName));
}
DefinePermissions permissions = role.getDefinePermissions();
if (permissions.getDefinePermission() != null && !permissions.getDefinePermission().isEmpty()) {
for (DefinePermission permission : permissions.getDefinePermission()) {
String permissionName = permission.getDefinePermissionName();
String resourcePrimKey = "0";
long companyId = SetupConfigurationThreadLocal.getRunInCompanyId();
if (permission.getElementPrimaryKey() != null) {
long groupId = SetupConfigurationThreadLocal.getRunInGroupId();
resourcePrimKey = ResolverUtil.lookupAll(groupId, companyId, permission.getElementPrimaryKey(), String.format("Role %1$s permission name %2$s", role.getName(), permissionName));
}
String type = role.getType();
int scope = ResourceConstants.SCOPE_COMPANY;
if (type != null && type.equalsIgnoreCase(SCOPE_PORTAL)) {
scope = ResourceConstants.SCOPE_COMPANY;
}
if (type != null && type.equalsIgnoreCase(SCOPE_SITE)) {
scope = ResourceConstants.SCOPE_GROUP_TEMPLATE;
}
if (type != null && type.equalsIgnoreCase("organization")) {
scope = ResourceConstants.SCOPE_GROUP_TEMPLATE;
}
if (scope == ResourceConstants.SCOPE_COMPANY && !resourcePrimKey.equals("0")) {
// if we have a regular role which is set for a particular site, set the scope to group
scope = ResourceConstants.SCOPE_GROUP;
}
if (scope == ResourceConstants.SCOPE_COMPANY && resourcePrimKey.equals("0")) {
// if we have a regular role which does not have any resource key set, set the company id as
// resource key
resourcePrimKey = Long.toString(companyId);
}
// if defined override the define permission scope
if (permission.getScope() != null && !permission.getScope().equals("")) {
if (permission.getScope().equalsIgnoreCase(SCOPE_PORTAL)) {
scope = ResourceConstants.SCOPE_COMPANY;
} else if (permission.getScope().equalsIgnoreCase(SCOPE_SITE_TEMPLATE)) {
scope = ResourceConstants.SCOPE_GROUP_TEMPLATE;
} else if (permission.getScope().equalsIgnoreCase(SCOPE_SITE)) {
scope = ResourceConstants.SCOPE_GROUP;
} else if (permission.getScope().equalsIgnoreCase(SCOPE_INDIVIDUAL)) {
scope = ResourceConstants.SCOPE_INDIVIDUAL;
}
}
if (permission.getPermissionAction() != null && !permission.getPermissionAction().isEmpty()) {
ArrayList<String> listOfActions = new ArrayList<>();
for (PermissionAction pa : permission.getPermissionAction()) {
String actionname = pa.getActionName();
listOfActions.add(actionname);
}
String[] loa = new String[listOfActions.size()];
loa = listOfActions.toArray(loa);
try {
SetupPermissions.addPermission(role.getName(), permissionName, resourcePrimKey, scope, loa);
} catch (SystemException e) {
LOG.error(String.format("Error when defining permission %1$s for role %2$s", permissionName, role.getName()), e);
}
}
}
}
}
}
Aggregations