Search in sources :

Example 1 with Resource

use of com.liferay.portal.kernel.model.Resource in project liferay-imex by jpdacunha.

the class AdtImporter method doImportAdt.

private DDMTemplate doImportAdt(Bundle bundle, ServiceContext serviceContext, boolean debug, Group group, User user, Locale locale, File adtDir, Properties config) {
    DDMTemplate template = null;
    String adtfileBegin = FileNames.getAdtFileNameBegin();
    ServiceContext serviceContextTem = (ServiceContext) serviceContext.clone();
    String groupName = GroupUtil.getGroupName(group, locale);
    File[] adtFiles = FileUtil.listFiles(adtDir, adtfileBegin);
    if (adtFiles != null || (adtFiles != null && adtFiles.length == 0)) {
        for (File matchingAdtFile : Arrays.asList(adtFiles)) {
            String templateFileName = matchingAdtFile.getName();
            File adtFile = new File(adtDir, templateFileName);
            ImexOperationEnum operation = ImexOperationEnum.UPDATE;
            try {
                ImExAdt imexAdt = (ImExAdt) processor.read(ImExAdt.class, adtFile);
                long classNameId = classNameLocalService.getClassNameId(imexAdt.getClassName());
                // Liferay trap here :-) : the ressourceClassNameId is the name of the portlet. Potential problem here if the class is moved in future Liferay versions.
                long resourceClassNameId = classNameLocalService.getClassNameId(RESOURCE_CLASSNAME);
                Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(imexAdt.getName());
                Map<Locale, String> descriptionMap = LocalizationUtil.getLocalizationMap(imexAdt.getDescription());
                long userId = user.getUserId();
                String type = imexAdt.getTemplateType();
                String mode = null;
                String language = imexAdt.getLangType();
                String script = imexAdt.getData();
                boolean cacheable = imexAdt.isCacheable();
                long classPK = 0;
                serviceContextTem.setUuid(imexAdt.getUuid());
                serviceContextTem.setAddGroupPermissions(false);
                serviceContextTem.setAddGuestPermissions(false);
                try {
                    // Searching for existing template
                    template = dDMTemplateLocalService.getDDMTemplateByUuidAndGroupId(imexAdt.getUuid(), group.getGroupId());
                    long templateId = template.getTemplateId();
                    dDMTemplateLocalService.updateTemplate(userId, templateId, classPK, nameMap, descriptionMap, type, mode, language, script, cacheable, serviceContextTem);
                } catch (NoSuchTemplateException e) {
                    long groupId = group.getGroupId();
                    String templateKey = "imex-" + imexAdt.getKey() + "-" + counterLocalService.increment();
                    boolean smallImage = false;
                    String smallImageURL = null;
                    File smallImageFile = null;
                    template = dDMTemplateLocalService.addTemplate(userId, groupId, classNameId, classPK, resourceClassNameId, templateKey, nameMap, descriptionMap, type, mode, language, script, cacheable, smallImage, smallImageURL, smallImageFile, serviceContextTem);
                    operation = ImexOperationEnum.CREATE;
                }
                // Setting ADT permissions
                Resource resource = resourceLocalService.getResource(template.getCompanyId(), DDMTemplate.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, template.getTemplateId() + "");
                permissionSetter.setPermissions(config, bundle, resource);
                reportService.getOK(_log, groupName, "ADT : " + template.getName(locale), adtFile, operation);
            } catch (Exception e) {
                reportService.getError(_log, adtFile.getName(), e);
                if (debug) {
                    _log.error(e, e);
                }
            }
        }
    } else {
        reportService.getMessage(_log, "Missing templates", "No files found matching [" + adtfileBegin + "]");
    }
    return template;
}
Also used : Locale(java.util.Locale) ServiceContext(com.liferay.portal.kernel.service.ServiceContext) Resource(com.liferay.portal.kernel.model.Resource) DDMTemplate(com.liferay.dynamic.data.mapping.model.DDMTemplate) NoSuchTemplateException(com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException) ImExAdt(com.liferay.imex.adt.model.ImExAdt) NoSuchTemplateException(com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException) ImexOperationEnum(com.liferay.imex.core.api.report.model.ImexOperationEnum) File(java.io.File)

Example 2 with Resource

use of com.liferay.portal.kernel.model.Resource in project liferay-imex by jpdacunha.

the class ImexModelPermissionSetterImpl method setPermissions.

/**
 * Traitement de positionnement des permissions
 * @param batch
 * @param logger
 * @throws PortalException
 * @throws SystemException
 */
public void setPermissions(ModelPermissionBatch batch) throws PortalException, SystemException {
    if (ModelPermissionBatch.validate(batch)) {
        reportService.getStartMessage(_log, "Setting permissions");
        Resource resource = batch.getResource();
        List<ModelRolePermissionBatch> roleActions = batch.getBatchs();
        long companyId = resource.getCompanyId();
        String resourceName = resource.getName();
        String resourcePrimKey = resource.getPrimKey();
        boolean reInitOnSet = batch.isReInitOnSet();
        reportService.getPropertyMessage(_log, "Selected batch", batch.getBatchId());
        reportService.getPropertyMessage(_log, "Permissions reinitialisation", reInitOnSet + "");
        reportService.getPropertyMessage(_log, "Resource Name", resourceName);
        reportService.getPropertyMessage(_log, "Resource Primary Key", resourcePrimKey);
        if (roleActions != null && roleActions.size() > 0) {
            // Deleting previous setted permissions
            if (reInitOnSet) {
                resetPermissions(companyId, resource);
            }
            // Setting permissions on resource
            for (ModelRolePermissionBatch roleBatch : roleActions) {
                setPermissionsRole(roleBatch, resource);
            }
        } else {
            reportService.getMessage(_log, "No permissions to set");
        }
        reportService.getEndMessage(_log, "Setting permissions");
    } else {
        reportService.getError(_log, "Invalid parameter", "Unable to execute invalid batch");
    }
}
Also used : ModelRolePermissionBatch(com.liferay.imex.core.api.permission.model.ModelRolePermissionBatch) Resource(com.liferay.portal.kernel.model.Resource)

Example 3 with Resource

use of com.liferay.portal.kernel.model.Resource 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

Resource (com.liferay.portal.kernel.model.Resource)3 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 Role (com.ableneo.liferay.portal.setup.domain.Role)1 RolePermissionType (com.ableneo.liferay.portal.setup.domain.RolePermissionType)1 RolePermissions (com.ableneo.liferay.portal.setup.domain.RolePermissions)1 NoSuchTemplateException (com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException)1 DDMTemplate (com.liferay.dynamic.data.mapping.model.DDMTemplate)1 ImExAdt (com.liferay.imex.adt.model.ImExAdt)1 ModelRolePermissionBatch (com.liferay.imex.core.api.permission.model.ModelRolePermissionBatch)1 ImexOperationEnum (com.liferay.imex.core.api.report.model.ImexOperationEnum)1 NestableException (com.liferay.portal.kernel.exception.NestableException)1 PortalException (com.liferay.portal.kernel.exception.PortalException)1 SystemException (com.liferay.portal.kernel.exception.SystemException)1 Log (com.liferay.portal.kernel.log.Log)1 LogFactoryUtil (com.liferay.portal.kernel.log.LogFactoryUtil)1 Layout (com.liferay.portal.kernel.model.Layout)1 ResourceConstants (com.liferay.portal.kernel.model.ResourceConstants)1 ResourcePermission (com.liferay.portal.kernel.model.ResourcePermission)1