Search in sources :

Example 1 with Role

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

the class SetupSites method assignUserMemberRoles.

private static void assignUserMemberRoles(List<Role> membershipRoles, long companyId, Group liferayGroup, User liferayUser) {
    if (Objects.isNull(membershipRoles) || membershipRoles.isEmpty()) {
        return;
    }
    for (Role membershipRole : membershipRoles) {
        try {
            com.liferay.portal.kernel.model.Role liferayRole = RoleLocalServiceUtil.getRole(companyId, membershipRole.getName());
            UserGroupRoleLocalServiceUtil.addUserGroupRoles(liferayUser.getUserId(), liferayGroup.getGroupId(), new long[] { liferayRole.getRoleId() });
            StringBuilder sb = new StringBuilder("Role ").append(liferayRole.getDescriptiveName()).append(" assigned to User ").append(liferayUser.getScreenName()).append(" for site ").append(liferayGroup.getDescriptiveName());
            LOG.info(sb.toString());
        } catch (PortalException e) {
            LOG.error(String.format("Can not add role with name%1$s does not exists. Will not be assigned.", membershipRole.getName()));
        }
    }
}
Also used : Role(com.ableneo.liferay.portal.setup.domain.Role) PortalException(com.liferay.portal.kernel.exception.PortalException)

Example 2 with Role

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

the class SetupSites method assignMemberRoles.

private static void assignMemberRoles(List<Role> membershipRoles, long companyId, long groupId) {
    if (Objects.isNull(membershipRoles) || membershipRoles.isEmpty()) {
        return;
    }
    Set<String> roles = new HashSet<String>();
    for (Role membershipRole : membershipRoles) {
        try {
            com.liferay.portal.kernel.model.Role liferayRole = RoleLocalServiceUtil.getRole(companyId, membershipRole.getName());
            roles.add(String.valueOf(liferayRole.getRoleId()));
        } catch (PortalException e) {
            LOG.error(String.format("Can not get role with name%1$s does not exists. Will not be assigned.", membershipRole.getName()));
        }
    }
    if (roles.isEmpty()) {
        LOG.info("NO new roles to be assigned to site " + groupId);
        return;
    }
    try {
        Group liferayGroup = GroupLocalServiceUtil.getGroup(groupId);
        UnicodeProperties siteGroupProps = liferayGroup.getTypeSettingsProperties();
        if (siteGroupProps == null) {
            siteGroupProps = new UnicodeProperties();
        }
        String roleIds = siteGroupProps.getProperty(UNIPARAM_DEFAULT_SITE_ROLE_IDS);
        if (roleIds != null && roleIds.length() > 1) {
            String[] roleIdArr = roleIds.split("[,;]+");
            roles.addAll(Arrays.asList(roleIdArr));
        }
        roleIds = StringUtil.merge(roles, ",");
        siteGroupProps.setProperty(UNIPARAM_DEFAULT_SITE_ROLE_IDS, roleIds);
        liferayGroup.setTypeSettingsProperties(siteGroupProps);
        GroupLocalServiceUtil.updateGroup(liferayGroup);
        StringBuilder sb = new StringBuilder("Roles ").append(roleIds).append(" assigned to site-group ").append(liferayGroup.getDescriptiveName());
        LOG.info(sb.toString());
    } catch (Exception e) {
        LOG.error("Can not set roles to site-group (" + roles + ")=>" + groupId, e);
    }
}
Also used : Group(com.liferay.portal.kernel.model.Group) UserGroup(com.liferay.portal.kernel.model.UserGroup) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) Role(com.ableneo.liferay.portal.setup.domain.Role) UnicodeProperties(com.liferay.portal.kernel.util.UnicodeProperties) PortalException(com.liferay.portal.kernel.exception.PortalException) HashSet(java.util.HashSet)

Example 3 with Role

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

the class SetupUserGroups method addRolesToUserGroup.

private static void addRolesToUserGroup(final UserGroup userGroup, final com.liferay.portal.kernel.model.UserGroup liferayUserGroup) {
    try {
        for (Role role : userGroup.getRole()) {
            long companyId = SetupConfigurationThreadLocal.getRunInCompanyId();
            com.liferay.portal.kernel.model.Role liferayRole = RoleLocalServiceUtil.getRole(companyId, role.getName());
            String roleType = role.getType();
            switch(roleType) {
                case RoleConstants.TYPE_REGULAR_LABEL:
                case "portal":
                    GroupLocalServiceUtil.addRoleGroup(liferayRole.getRoleId(), liferayUserGroup.getGroupId());
                    LOG.info(String.format("Adding role %1$s to userGroup %2$s", liferayRole.getDescriptiveName(), liferayUserGroup.getName()));
                    break;
                case "site":
                case "organization":
                    LOG.error("Adding site or organization roles to UserGroup is not supported. Bind userGroups to Site Roles within the Site elemennt.");
                    break;
                default:
                    LOG.error(String.format("unknown role type %1$s", roleType));
                    break;
            }
        }
    } catch (PortalException | SystemException e) {
        LOG.error(String.format("Error in adding roles to userGroup %1$s", userGroup.getName()), e);
    }
}
Also used : Role(com.ableneo.liferay.portal.setup.domain.Role) SystemException(com.liferay.portal.kernel.exception.SystemException) PortalException(com.liferay.portal.kernel.exception.PortalException)

Example 4 with Role

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

the class SetupSites method assignGroupMemberRoles.

private static void assignGroupMemberRoles(List<Role> membershipRoles, long companyId, Group liferayGroup, UserGroup liferayUserGroup) {
    if (Objects.isNull(membershipRoles) || membershipRoles.isEmpty()) {
        return;
    }
    for (Role membershipRole : membershipRoles) {
        try {
            com.liferay.portal.kernel.model.Role liferayRole = RoleLocalServiceUtil.getRole(companyId, membershipRole.getName());
            UserGroupGroupRoleLocalServiceUtil.addUserGroupGroupRoles(liferayUserGroup.getUserGroupId(), liferayGroup.getGroupId(), new long[] { liferayRole.getRoleId() });
            StringBuilder sb = new StringBuilder("Role[").append(liferayRole.getTypeLabel()).append(// 
            "] ").append(// 
            liferayRole.getDescriptiveName()).append(" assigned to UserGroup ").append(// 
            liferayUserGroup.getName()).append(" for site ").append(// 
            liferayGroup.getDescriptiveName());
            LOG.info(sb.toString());
        } catch (PortalException e) {
            LOG.error(String.format("Can not add role with name%1$s does not exists. Will not be assigned.", membershipRole.getName()));
        }
    }
}
Also used : Role(com.ableneo.liferay.portal.setup.domain.Role) PortalException(com.liferay.portal.kernel.exception.PortalException)

Example 5 with Role

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

the class SetupPermissions method getActionsPerRole.

/**
 * @param resource
 * @return mapping of role name to action ids for the resource
 */
private static Map<String, Set<String>> getActionsPerRole(ResourcePermissions.Resource resource) {
    Map<String, Set<String>> result = new HashMap<>();
    for (ResourcePermissions.Resource.ActionId actionId : resource.getActionId()) {
        for (Role role : actionId.getRole()) {
            final String roleName = role.getName();
            result.computeIfAbsent(roleName, s -> new HashSet<>()).add(actionId.getName());
        }
    }
    return result;
}
Also used : Role(com.ableneo.liferay.portal.setup.domain.Role) Arrays(java.util.Arrays) RoleLocalServiceUtil(com.liferay.portal.kernel.service.RoleLocalServiceUtil) HashMap(java.util.HashMap) ResourceConstants(com.liferay.portal.kernel.model.ResourceConstants) Log(com.liferay.portal.kernel.log.Log) Resource(com.liferay.portal.kernel.model.Resource) ArrayList(java.util.ArrayList) PortalException(com.liferay.portal.kernel.exception.PortalException) HashSet(java.util.HashSet) SystemException(com.liferay.portal.kernel.exception.SystemException) ResourceLocalServiceUtil(com.liferay.portal.kernel.service.ResourceLocalServiceUtil) Map(java.util.Map) Role(com.ableneo.liferay.portal.setup.domain.Role) ResourcePermission(com.liferay.portal.kernel.model.ResourcePermission) ActionKeys(com.liferay.portal.kernel.security.permission.ActionKeys) LogFactoryUtil(com.liferay.portal.kernel.log.LogFactoryUtil) RolePermissionType(com.ableneo.liferay.portal.setup.domain.RolePermissionType) Set(java.util.Set) ResourcePermissionLocalServiceUtil(com.liferay.portal.kernel.service.ResourcePermissionLocalServiceUtil) NestableException(com.liferay.portal.kernel.exception.NestableException) Layout(com.liferay.portal.kernel.model.Layout) RolePermissions(com.ableneo.liferay.portal.setup.domain.RolePermissions) List(java.util.List) ResourcePermissions(com.ableneo.liferay.portal.setup.domain.ResourcePermissions) SetupConfigurationThreadLocal(com.ableneo.liferay.portal.setup.SetupConfigurationThreadLocal) PermissionAction(com.ableneo.liferay.portal.setup.domain.PermissionAction) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Resource(com.liferay.portal.kernel.model.Resource) HashSet(java.util.HashSet)

Aggregations

Role (com.ableneo.liferay.portal.setup.domain.Role)5 PortalException (com.liferay.portal.kernel.exception.PortalException)5 SystemException (com.liferay.portal.kernel.exception.SystemException)3 HashSet (java.util.HashSet)2 SetupConfigurationThreadLocal (com.ableneo.liferay.portal.setup.SetupConfigurationThreadLocal)1 PermissionAction (com.ableneo.liferay.portal.setup.domain.PermissionAction)1 ResourcePermissions (com.ableneo.liferay.portal.setup.domain.ResourcePermissions)1 RolePermissionType (com.ableneo.liferay.portal.setup.domain.RolePermissionType)1 RolePermissions (com.ableneo.liferay.portal.setup.domain.RolePermissions)1 NestableException (com.liferay.portal.kernel.exception.NestableException)1 Log (com.liferay.portal.kernel.log.Log)1 LogFactoryUtil (com.liferay.portal.kernel.log.LogFactoryUtil)1 Group (com.liferay.portal.kernel.model.Group)1 Layout (com.liferay.portal.kernel.model.Layout)1 Resource (com.liferay.portal.kernel.model.Resource)1 ResourceConstants (com.liferay.portal.kernel.model.ResourceConstants)1 ResourcePermission (com.liferay.portal.kernel.model.ResourcePermission)1 UserGroup (com.liferay.portal.kernel.model.UserGroup)1 ActionKeys (com.liferay.portal.kernel.security.permission.ActionKeys)1 ResourceLocalServiceUtil (com.liferay.portal.kernel.service.ResourceLocalServiceUtil)1