use of com.ableneo.liferay.portal.setup.domain.PermissionAction in project liferay-db-setup-core by ableneo.
the class SetupPermissions method updatePermission.
public static void updatePermission(final String locationHint, final long companyId, final List<Object> elementIdList, final String className, final RolePermissions rolePermissions, final Map<String, List<String>> defaultPermissions) {
boolean useDefaultPermissions = false;
if (rolePermissions != null) {
List<String> actions = new ArrayList<>();
if (rolePermissions.isClearPermissions()) {
for (Object elementId : elementIdList) {
try {
SetupPermissions.removePermission(companyId, className, String.valueOf(elementId));
} catch (PortalException | SystemException e) {
LOG.error(String.format("Permissions for %1$s could not be cleared. (elId:%2$s)", locationHint, elementId), e);
}
}
}
// else {
List<RolePermissionType> rmRolePermissionList = rolePermissions.getRemoveRolePermission();
if (rmRolePermissionList != null) {
for (RolePermissionType rp : rmRolePermissionList) {
actions.clear();
String roleName = rp.getRoleName();
List<PermissionAction> roleActions = rp.getPermissionAction();
for (PermissionAction pa : roleActions) {
String actionName = pa.getActionName();
actions.add(actionName);
}
for (Object elementId : elementIdList) {
try {
// we need to have 'NO_XXX_RIGHT', and not MISSING XXX_RIGHT; and
// LR optimizes out remove-action-right when there is no permission row
// to substract from.. so let there be a row.
SetupPermissions.addPermission(roleName, className, String.valueOf(elementId), actions.toArray(new String[actions.size()]));
} catch (SystemException e) {
LOG.error("Permissions for " + roleName + " for " + locationHint + " " + "could not be removed, step add. ", e);
} catch (NullPointerException e) {
LOG.error("Permissions for " + roleName + " for " + locationHint + " " + "could not be removed, step add. " + "Probably role not found! ", e);
}
try {
SetupPermissions.removePermission(roleName, className, String.valueOf(elementId), actions.toArray(new String[actions.size()]));
} catch (SystemException e) {
LOG.error("Permissions for " + roleName + " for " + locationHint + " " + "could not be removed, step rm. ", e);
} catch (NullPointerException e) {
LOG.error("Permissions for " + roleName + " for " + locationHint + " " + "could not be removed, step rm. " + "Probably role not found! ", e);
}
}
}
}
// }
List<RolePermissionType> rolePermissionList = rolePermissions.getRolePermission();
if (rolePermissionList != null) {
for (RolePermissionType rp : rolePermissionList) {
actions.clear();
String roleName = rp.getRoleName();
List<PermissionAction> roleActions = rp.getPermissionAction();
for (PermissionAction pa : roleActions) {
String actionName = pa.getActionName();
actions.add(actionName);
}
for (Object elementId : elementIdList) {
try {
SetupPermissions.addPermission(roleName, className, String.valueOf(elementId), actions.toArray(new String[actions.size()]));
} catch (SystemException e) {
LOG.error("Permissions for " + roleName + " for " + locationHint + " " + "could not be set. ", e);
} catch (NullPointerException e) {
LOG.error("Permissions for " + roleName + " for " + locationHint + " " + "could not be set. " + "Probably role not found! ", e);
}
}
}
} else {
useDefaultPermissions = true;
}
} else {
useDefaultPermissions = true;
}
if (useDefaultPermissions) {
Set<String> roles = defaultPermissions.keySet();
List<String> actions;
for (String roleName : roles) {
actions = defaultPermissions.get(roleName);
for (Object elementId : elementIdList) {
try {
SetupPermissions.addPermission(roleName, className, String.valueOf(elementId), actions.toArray(new String[actions.size()]));
} catch (SystemException e) {
LOG.error("Permissions for " + roleName + " for " + locationHint + " " + "could not be defaulted. ", e);
} catch (NullPointerException e) {
LOG.error("Permissions for " + roleName + " for " + locationHint + " " + "could not be defaulted. " + "Probably role not found! ", e);
}
}
}
}
}
use of com.ableneo.liferay.portal.setup.domain.PermissionAction 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);
}
}
}
}
}
}
use of com.ableneo.liferay.portal.setup.domain.PermissionAction in project liferay-db-setup-core by ableneo.
the class RolePermissionBuilder method permissionActionFrom.
protected static PermissionAction permissionActionFrom(String action) {
PermissionAction pa = new PermissionAction();
pa.setActionName(action);
return pa;
}
Aggregations