Search in sources :

Example 1 with RolePermissionType

use of com.ableneo.liferay.portal.setup.domain.RolePermissionType 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);
                }
            }
        }
    }
}
Also used : PermissionAction(com.ableneo.liferay.portal.setup.domain.PermissionAction) ArrayList(java.util.ArrayList) SystemException(com.liferay.portal.kernel.exception.SystemException) RolePermissionType(com.ableneo.liferay.portal.setup.domain.RolePermissionType) PortalException(com.liferay.portal.kernel.exception.PortalException)

Example 2 with RolePermissionType

use of com.ableneo.liferay.portal.setup.domain.RolePermissionType in project liferay-db-setup-core by ableneo.

the class RolePermissionBuilder method rolePermissionFrom.

private RolePermissionType rolePermissionFrom(String key, Set<String> value) {
    RolePermissionType rp = new RolePermissionType();
    rp.setRoleName(key);
    for (String action : value) {
        rp.getPermissionAction().add(permissionActionFrom(action));
    }
    return rp;
}
Also used : RolePermissionType(com.ableneo.liferay.portal.setup.domain.RolePermissionType)

Example 3 with RolePermissionType

use of com.ableneo.liferay.portal.setup.domain.RolePermissionType in project liferay-db-setup-core by ableneo.

the class SetupCustomFields method setCustomFieldPermission.

private static void setCustomFieldPermission(final List<RolePermissionType> rolePermissions, final ExpandoBridge bridge, final String fieldName) {
    LOG.info("Set read permissions on  field " + fieldName + " for " + rolePermissions.size() + " rolePermissions");
    long companyId = SetupConfigurationThreadLocal.getRunInCompanyId();
    ExpandoColumn column = ExpandoColumnLocalServiceUtil.getColumn(companyId, bridge.getClassName(), ExpandoTableConstants.DEFAULT_TABLE_NAME, fieldName);
    for (RolePermissionType rolePermission : rolePermissions) {
        String roleName = rolePermission.getRoleName();
        String permission = rolePermission.getPermission();
        switch(permission) {
            case "update":
                SetupPermissions.addReadWrightRight(roleName, ExpandoColumn.class.getName(), String.valueOf(column.getColumnId()));
                LOG.info(String.format("Added update permission on field %1$s for role %2$s", fieldName, roleName));
                break;
            case "view":
                SetupPermissions.addReadRight(roleName, ExpandoColumn.class.getName(), String.valueOf(column.getColumnId()));
                LOG.info(String.format("Added read permission on field %1$s for role %2$s", fieldName, roleName));
                break;
            default:
                LOG.info("Unknown permission:" + permission + ". No permission added on " + "field " + fieldName + " for role " + roleName);
                break;
        }
    }
}
Also used : RolePermissionType(com.ableneo.liferay.portal.setup.domain.RolePermissionType)

Aggregations

RolePermissionType (com.ableneo.liferay.portal.setup.domain.RolePermissionType)3 PermissionAction (com.ableneo.liferay.portal.setup.domain.PermissionAction)1 PortalException (com.liferay.portal.kernel.exception.PortalException)1 SystemException (com.liferay.portal.kernel.exception.SystemException)1 ArrayList (java.util.ArrayList)1