List<Permission> permissionablePermissions = loadPermissions(permissionable);
if (isHost) {
//Templates
Permission inheritablePermission = filterInheritablePermission(allPermissions, permissionsPermissionable
.getPermissionId(), Template.class.getCanonicalName(), role.getId());
//Assigning inheritable permissions to the permissionable if needed
List<Permission> permissionableTemplatePermissions = filterOnlyInheritablePermissions(permissionablePermissions, permissionable.getPermissionId(),
Template.class.getCanonicalName());
if(permissionableTemplatePermissions.size() > 0) {
Permission permissionToUpdate = filterInheritablePermission(permissionablePermissions, permissionsPermissionable.getPermissionId(),
Template.class.getCanonicalName(), role.getId());
if(permissionToUpdate == null) {
permissionToUpdate = new Permission(Template.class.getCanonicalName(), permissionable.getPermissionId(), role.getId(), 0, true);
}
if(inheritablePermission != null)
permissionToUpdate.setPermission(inheritablePermission.getPermission());
savePermission(permissionToUpdate, permissionable);
}
//Looking for children templates overriding inheritance to also apply the cascade changes
dc.setSQL(selectChildrenTemplateWithIndividualPermissionsSQL);
dc.addParam(host.getPermissionId());
List<Map<String, String>> idsToUpdate = dc.loadResults();
TemplateAPI templateAPI = APILocator.getTemplateAPI();
int permission = 0;
if (inheritablePermission != null) {
permission = inheritablePermission.getPermission();
}
for (Map<String, String> idMap : idsToUpdate) {
String id = idMap.get("id");
Permissionable childPermissionable;
try {
childPermissionable = templateAPI.findWorkingTemplate(id, systemUser, false);
} catch (DotSecurityException e) {
Logger.error(PermissionBitFactoryImpl.class, e.getMessage(), e);
throw new DotRuntimeException(e.getMessage(), e);
}
savePermission(new Permission(id, role.getId(), permission, true), childPermissionable);
}
//Containers
inheritablePermission = filterInheritablePermission(allPermissions, permissionsPermissionable.getPermissionId(),
Container.class.getCanonicalName(), role.getId());
//Assigning inheritable permissions to the permissionable if needed
List<Permission> permissionableContainerPermissions = filterOnlyInheritablePermissions(permissionablePermissions, permissionable.getPermissionId(),
Container.class.getCanonicalName());
if(permissionableContainerPermissions.size() > 0) {
Permission permissionToUpdate = filterInheritablePermission(permissionablePermissions, permissionsPermissionable.getPermissionId(),
Container.class.getCanonicalName(), role.getId());
if(permissionToUpdate == null) {
permissionToUpdate = new Permission(Container.class.getCanonicalName(), permissionable.getPermissionId(), role.getId(), 0, true);
}
if(inheritablePermission != null)
permissionToUpdate.setPermission(inheritablePermission.getPermission());
savePermission(permissionToUpdate, permissionable);
}
//Looking for children containers overriding inheritance to also apply the cascade changes
dc.setSQL(selectChildrenContainerWithIndividualPermissionsSQL);
dc.addParam(host.getPermissionId());
idsToUpdate = dc.loadResults();
ContainerAPI containerAPI = APILocator.getContainerAPI();
permission = 0;
if (inheritablePermission != null) {
permission = inheritablePermission.getPermission();
}
for (Map<String, String> idMap : idsToUpdate) {
String id = idMap.get("id");
Permissionable childPermissionable;
try {
childPermissionable = containerAPI.getWorkingContainerById(id, systemUser, false);
} catch (DotSecurityException e) {
Logger.error(PermissionBitFactoryImpl.class, e.getMessage(), e);
throw new DotRuntimeException(e.getMessage(), e);
}
savePermission(new Permission(id, role.getId(), permission, true), childPermissionable);
}
}
//Folders
Permission inheritablePermission = filterInheritablePermission(allPermissions,
permissionsPermissionable.getPermissionId(), Folder.class.getCanonicalName(), role.getId());
//Assigning inheritable permissions to the permissionable if needed
List<Permission> permissionableFolderPermissions = filterOnlyInheritablePermissions(permissionablePermissions, permissionable.getPermissionId(),
Folder.class.getCanonicalName());
if(permissionableFolderPermissions.size() > 0) {
Permission permissionToUpdate = filterInheritablePermission(permissionablePermissions, permissionsPermissionable.getPermissionId(),
Folder.class.getCanonicalName(), role.getId());
if(permissionToUpdate == null) {
permissionToUpdate = new Permission(Folder.class.getCanonicalName(), permissionable.getPermissionId(), role.getId(), 0, true);
}
if(inheritablePermission != null)
permissionToUpdate.setPermission(inheritablePermission.getPermission());
savePermission(permissionToUpdate, permissionable);
}
// Selecting folders which are children and need individual permission
// changes
dc.setSQL(selectChildrenFolderWithDirectPermissionsSQL);
dc.addParam(host.getPermissionId());
dc.addParam(isHost ? "%" : folderPath + "%");
dc.addParam(isHost ? " " : folderPath + "");
List<Map<String, String>> idsToUpdate = dc.loadResults();
FolderAPI folderAPI = APILocator.getFolderAPI();
int permission = 0;
if (inheritablePermission != null) {
permission = inheritablePermission.getPermission();
}
for (Map<String, String> idMap : idsToUpdate) {
String id = idMap.get("inode");
Permissionable childPermissionable;
try {
childPermissionable = folderAPI.find(id, systemUser, false);
savePermission(new Permission(id, role.getId(), permission, true), childPermissionable);
} catch (DotSecurityException e) {
Logger.error(this.getClass(), "Should not be getting a Permission Error with system user", e);
}
}
//HTML pages
inheritablePermission = filterInheritablePermission(allPermissions, permissionsPermissionable.getPermissionId(),
HTMLPage.class.getCanonicalName(), role.getId());
//Assigning inheritable permissions to the permissionable if needed
List<Permission> permissionablePagesPermissions = filterOnlyInheritablePermissions(permissionablePermissions, permissionable.getPermissionId(),
HTMLPage.class.getCanonicalName());
if(permissionablePagesPermissions.size() > 0) {
Permission permissionToUpdate = filterInheritablePermission(permissionablePermissions, permissionsPermissionable.getPermissionId(),
HTMLPage.class.getCanonicalName(), role.getId());
if(permissionToUpdate == null) {
permissionToUpdate = new Permission(HTMLPage.class.getCanonicalName(), permissionable.getPermissionId(), role.getId(), 0, true);
}
if(inheritablePermission != null)
permissionToUpdate.setPermission(inheritablePermission.getPermission());
savePermission(permissionToUpdate, permissionable);
}
// Selecting html pages which are children and need individual
// permission changes
dc.setSQL(selectChildrenHTMLPageWithIndividualPermissionsSQL);
dc.addParam(host.getPermissionId());
dc.addParam(isHost ? "%" : folderPath + "%");
idsToUpdate = dc.loadResults();
HTMLPageAPI pageAPI = APILocator.getHTMLPageAPI();
permission = 0;
if (inheritablePermission != null) {
permission = inheritablePermission.getPermission();
}
for (Map<String, String> idMap : idsToUpdate) {
String id = idMap.get("id");
Permissionable childPermissionable;
try {
childPermissionable = pageAPI.loadWorkingPageById(id, systemUser, false);
} catch (DotSecurityException e) {
Logger.error(PermissionBitFactoryImpl.class, e.getMessage(), e);
throw new DotRuntimeException(e.getMessage(), e);
}
savePermission(new Permission(id, role.getId(), permission, true), childPermissionable);
}
// File
inheritablePermission = filterInheritablePermission(allPermissions, permissionsPermissionable.getPermissionId(),
File.class.getCanonicalName(), role.getId());
//Assigning inheritable permissions to the permissionable if needed
List<Permission> permissionableFilesPermissions = filterOnlyInheritablePermissions(permissionablePermissions, permissionable.getPermissionId(),
File.class.getCanonicalName());
if(permissionableFilesPermissions.size() > 0) {
Permission permissionToUpdate = filterInheritablePermission(permissionablePermissions, permissionsPermissionable.getPermissionId(),
File.class.getCanonicalName(), role.getId());
if(permissionToUpdate == null) {
permissionToUpdate = new Permission(File.class.getCanonicalName(), permissionable.getPermissionId(), role.getId(), 0, true);
}
if(inheritablePermission != null)
permissionToUpdate.setPermission(inheritablePermission.getPermission());
savePermission(permissionToUpdate, permissionable);
}
// Selecting files which are children and need individual permission
// changes
dc.setSQL(selectChildrenFileWithIndividualPermissionsSQL);
dc.addParam(host.getPermissionId());
dc.addParam(isHost ? "%" : folderPath + "%");
idsToUpdate = dc.loadResults();
FileAPI fileAPI = APILocator.getFileAPI();
permission = 0;
if (inheritablePermission != null) {
permission = inheritablePermission.getPermission();
}
for (Map<String, String> idMap : idsToUpdate) {
String id = idMap.get("id");
Permissionable childPermissionable;
try {
childPermissionable = fileAPI.getWorkingFileById(id, systemUser, false);
} catch (DotSecurityException e) {
Logger.error(PermissionBitFactoryImpl.class, e.getMessage(), e);
throw new DotRuntimeException(e.getMessage(), e);
}
savePermission(new Permission(id, role.getId(), permission, true), childPermissionable);
}
// Links
inheritablePermission = filterInheritablePermission(allPermissions, permissionsPermissionable.getPermissionId(),
Link.class.getCanonicalName(), role.getId());
//Assigning inheritable permissions to the permissionable if needed
List<Permission> permissionableLinksPermissions = filterOnlyInheritablePermissions(permissionablePermissions, permissionable.getPermissionId(),
Link.class.getCanonicalName());
if(permissionableLinksPermissions.size() > 0) {
Permission permissionToUpdate = filterInheritablePermission(permissionablePermissions, permissionsPermissionable.getPermissionId(),
Link.class.getCanonicalName(), role.getId());
if(permissionToUpdate == null) {
permissionToUpdate = new Permission(Link.class.getCanonicalName(), permissionable.getPermissionId(), role.getId(), 0, true);
}
if(inheritablePermission != null)
permissionToUpdate.setPermission(inheritablePermission.getPermission());
savePermission(permissionToUpdate, permissionable);
}
// Selecting links which are children and need individual permission
// changes
dc.setSQL(selectChildrenLinkWithIndividualPermissionsSQL);
dc.addParam(host.getPermissionId());
dc.addParam(isHost ? "%" : folderPath + "%");
idsToUpdate = dc.loadResults();
MenuLinkAPI linkAPI = APILocator.getMenuLinkAPI();
permission = 0;
if (inheritablePermission != null) {
permission = inheritablePermission.getPermission();
}
for (Map<String, String> idMap : idsToUpdate) {
String id = idMap.get("id");
Permissionable childPermissionable;
try {
childPermissionable = linkAPI.findWorkingLinkById(id, systemUser, false);
} catch (DotSecurityException e) {
Logger.error(PermissionBitFactoryImpl.class, e.getMessage(), e);
throw new DotRuntimeException(e.getMessage(), e);
}
savePermission(new Permission(id, role.getId(), permission, true), childPermissionable);
}
// Contentlets
inheritablePermission = filterInheritablePermission(allPermissions, permissionsPermissionable.getPermissionId(),
Contentlet.class.getCanonicalName(), role.getId());
//Assigning inheritable permissions to the permissionable if needed
List<Permission> permissionableContentPermissions = filterOnlyInheritablePermissions(permissionablePermissions, permissionable.getPermissionId(),
Contentlet.class.getCanonicalName());
if(permissionableContentPermissions.size() > 0) {
Permission permissionToUpdate = filterInheritablePermission(permissionablePermissions, permissionsPermissionable.getPermissionId(),
Contentlet.class.getCanonicalName(), role.getId());
if(permissionToUpdate == null) {
permissionToUpdate = new Permission(Contentlet.class.getCanonicalName(), permissionable.getPermissionId(), role.getId(), 0, true);
}
if(inheritablePermission != null)
permissionToUpdate.setPermission(inheritablePermission.getPermission());
savePermission(permissionToUpdate, permissionable);
}
// Selecting content which are children and need individual permission
// changes
dc.setSQL(selectChildrenContentWithIndividualPermissionsByPathSQL);
dc.addParam(host.getPermissionId());
dc.addParam(isHost ? "%" : folderPath + "%");
idsToUpdate = dc.loadResults();
ContentletAPI contentAPI = APILocator.getContentletAPI();
permission = 0;
if (inheritablePermission != null) {
permission = inheritablePermission.getPermission();
}
for (Map<String, String> idMap : idsToUpdate) {
String id = idMap.get("id");
//Search contentlets by identifier (all languages) and set permissions
String luceneQuery = "+identifier:"+id+" +working:true";
try {
for(Permissionable childPermissionable: contentAPI.search(luceneQuery,1,0,null,systemUser, false)) {
savePermission(new Permission(id, role.getId(), permission, true), childPermissionable);
break;
}
} catch (DotSecurityException e) {
Logger.error(PermissionBitFactoryImpl.class, e.getMessage(), e);
throw new DotRuntimeException(e.getMessage(), e);
}
}
// Structures
inheritablePermission = filterInheritablePermission(allPermissions, permissionsPermissionable.getPermissionId(),
Structure.class.getCanonicalName(), role.getId());
//Assigning inheritable permissions to the permissionable if needed
List<Permission> permissionableStructurePermissions = filterOnlyInheritablePermissions(permissionablePermissions, permissionable.getPermissionId(),
Structure.class.getCanonicalName());
if(permissionableStructurePermissions.size() > 0) {
Permission permissionToUpdate = filterInheritablePermission(permissionablePermissions, permissionsPermissionable.getPermissionId(),
Structure.class.getCanonicalName(), role.getId());
if(permissionToUpdate == null) {
permissionToUpdate = new Permission(Structure.class.getCanonicalName(), permissionable.getPermissionId(), role.getId(), 0, true);
}
if(inheritablePermission != null)
permissionToUpdate.setPermission(inheritablePermission.getPermission());
savePermission(permissionToUpdate, permissionable);
}
// Selecting structures which are children and need individual permission
// changes
dc.setSQL(selectChildrenStructureWithIndividualPermissionsByPathSQL);
dc.addParam(isHost ? "%" : folderPath + "%");
dc.addParam(host.getPermissionId());
dc.addParam(host.getPermissionId());
idsToUpdate = dc.loadResults();
permission = 0;
if (inheritablePermission != null) {
permission = inheritablePermission.getPermission();
}
for (Map<String, String> idMap : idsToUpdate) {
String id = idMap.get("inode");
Permissionable childPermissionable = StructureCache.getStructureByInode(id);
savePermission(new Permission(id, role.getId(), permission, true), childPermissionable);
//http://jira.dotmarketing.net/browse/DOTCMS-6090
//If a structure we need to save permissions inheritable by children content
savePermission(new Permission(Contentlet.class.getCanonicalName(), id, role.getId(), permission, true),childPermissionable);
}
}