//Retrieving the current user
User user = userWebAPI.getLoggedInUser(request);
boolean respectFrontendRoles = !userWebAPI.isLoggedToBackend(request);
PermissionAPI permissionAPI = APILocator.getPermissionAPI();
Permissionable asset = retrievePermissionable(assetId, language, user, respectFrontendRoles);
List<Permission> newSetOfPermissions = new ArrayList<Permission>();
for(Map<String, String> permission: permissions) {
String roleId = permission.get("roleId");
String individualPermission = permission.get("individualPermission");
if(individualPermission != null ) {
newSetOfPermissions.add(new Permission(asset.getPermissionId(), roleId, Integer.parseInt(individualPermission), true));
//If a structure we need to save permissions inheritable by children content
if(asset instanceof Structure) {
newSetOfPermissions.add(new Permission(Contentlet.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(individualPermission), true));
}
//If a category we need to save permissions inheritable by children categories
if(asset instanceof Category) {
newSetOfPermissions.add(new Permission(Category.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(individualPermission), true));
}
}
String containersPermission = permission.get("containersPermission");
if(containersPermission != null) {
newSetOfPermissions.add(new Permission(Container.class.getCanonicalName(), asset.getPermissionId(),
roleId, Integer.parseInt(containersPermission), true));
}
String filesPermission = permission.get("filesPermission");
if(filesPermission != null) {
newSetOfPermissions.add(new Permission(File.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(filesPermission), true));
}
String pagesPermission = permission.get("pagesPermission");
if(pagesPermission != null) {
newSetOfPermissions.add(new Permission(HTMLPage.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(pagesPermission), true));
}
String foldersPermission = permission.get("foldersPermission");
if(foldersPermission != null) {
newSetOfPermissions.add(new Permission(Folder.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(foldersPermission), true));
}
String contentPermission = permission.get("contentPermission");
if(contentPermission != null) {
newSetOfPermissions.add(new Permission(Contentlet.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(contentPermission), true));
}
String linksPermission = permission.get("linksPermission");
if(linksPermission != null) {
newSetOfPermissions.add(new Permission(Link.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(linksPermission), true));
}
String templatesPermission = permission.get("templatesPermission");
if(templatesPermission != null) {
newSetOfPermissions.add(new Permission(Template.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(templatesPermission), true));
}
String templateLayoutsPermission = permission.get("templateLayoutsPermission");
if(templateLayoutsPermission != null) {
newSetOfPermissions.add(new Permission(Template.TEMPLATE_LAYOUTS_CANONICAL_NAME, asset.getPermissionId(), roleId,
Integer.parseInt(templateLayoutsPermission), true));
}
String structurePermission = permission.get("structurePermission");
if(structurePermission != null) {
newSetOfPermissions.add(new Permission(Structure.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(structurePermission), true));
}
String categoriesPermissions = permission.get("categoriesPermissions");
if(categoriesPermissions != null) {
newSetOfPermissions.add(new Permission(Category.class.getCanonicalName(), asset.getPermissionId(), roleId,
Integer.parseInt(categoriesPermissions), true));
}
}
if(newSetOfPermissions.size() > 0) {
permissionAPI.assignPermissions(newSetOfPermissions, asset, user, respectFrontendRoles);
if(reset && asset.isParentPermissionable()) {
ResetPermissionsJob.triggerJobImmediately(asset);
}
} else {
permissionAPI.removePermissions(asset);
}