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;
}
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");
}
}
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;
}
Aggregations