/// <summary> /// Moves the fileId to this Folder /// </summary> /// <param name="destination"></param> /// <param name="fileId"></param> /// <param name="destinationName"></param> /// <param name="user"></param> public static void MoveFileHere(this Folder destination, Guid fileId, string destinationName, Principal user) { using (var context = new OnlineFilesEntities()) { var destSecurity = context.FolderSecurities.Where(d => d.fk_FolderId == destination.pk_FolderId); //Can the user create files in the destinatioin folder? if (destSecurity.Any(d => d.canCreateFiles && user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId))) { File fileToMove = context.Files.FirstOrDefault(d => d.pk_FileId == fileId); if (fileToMove == null) { throw new Exception("Cannot move a non existant file"); } Folder parentToFileToMove = context.Folders.Include(x => x.FolderSecurities).FirstOrDefault(d => d.pk_FolderId == fileToMove.fk_FolderId); if (parentToFileToMove == null) { throw new Exception("No parent to folder being moved."); } //Can the user delete from the folder that the file starts in? if (parentToFileToMove.FolderSecurities.Any(d => user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId) && d.canDelete)) { var locks = context.ObjectLockInfoes.Where(d => d.ObjectGuid == fileId); if (locks.Count() > 0) { if (locks.Any(d => d.OwnerId == user.UserProfile.SecurityObjectId)) { context.ObjectLockInfoes.RemoveRange(context.ObjectLockInfoes.Where(d => d.ObjectGuid == fileId)); context.SaveChanges(); } else { throw new SecurityException("File is Locked by another user."); } } } else { throw new SecurityException("Not Authorized."); } fileToMove.Name = destination.CheckFolderName(destinationName, context); fileToMove.fk_FolderId = destination.pk_FolderId; context.SaveChanges(); } else { throw new SecurityException("Not Authorized."); } } }
public static void RestoreFolderHere(this Folder destination, Guid folderId, string destinationName, Principal user) { using (var context = new OnlineFilesEntities()) { var destSecurity = context.FolderSecurities.Where(d => d.fk_FolderId == destination.pk_FolderId); //Can the user create folders at the destination location? if (destSecurity.Any(d => d.canCreateFolders && user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId))) { Folder folderToRestore = context.Folders.Include(d => d.FolderSecurities).FirstOrDefault(d => d.pk_FolderId == folderId); if (folderToRestore == null) { throw new Exception("Cannot restore a non existant folder"); } Folder parentToFolderToMove = context.Folders.Include(x => x.FolderSecurities).FirstOrDefault(d => d.pk_FolderId == folderToRestore.fk_ParentFolderId); if (parentToFolderToMove == null) { throw new Exception("No parent to folder being moved."); } //Does the user have delete permission in the folder the item is comming from? if (parentToFolderToMove.FolderSecurities.Any(d => user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId) && d.canDelete)) { folderToRestore.RestoreDeleted(PrincipleFactory.Instance.GetPrinciple().UserProfile); folderToRestore.Name = destination.CheckFolderName(destinationName, context); folderToRestore.fk_ParentFolderId = destination.pk_FolderId; context.SaveChanges(); } else { throw new SecurityException("Not Authorized."); } } else { throw new SecurityException("Not Authorized."); } } }
/// <summary> /// Creates a new Folder object. /// </summary> /// <param name="name"></param> /// <param name="parentFolderGuid"></param> /// <param name="createdBy"></param> /// <param name="inheritSecurity"></param> /// <param name="isSystemCreate"></param> /// <returns></returns> public static Folder Create(string name, Guid?parentFolderGuid, SecurityObject createdBy, bool inheritSecurity = true, bool isSystemCreate = false) { using (var context = new OnlineFilesEntities()) { //Get Folder object for parent folder Folder parentFolder = context.Folders .Include(x => x.CatalogCollection) .Include(x => x.FolderSecurities) .FirstOrDefault(d => d.pk_FolderId == parentFolderGuid); if (!isSystemCreate) { if (!(parentFolder.FolderSecurities.Any(x => createdBy.mySecurityGroups.Contains(x.SecurityObjectId) && x.canCreateFolders))) { throw new SecurityException("Not Authorized."); } } //Check Name name = parentFolder.CheckFolderName(name); //If Parent Folder is null, throw exception if (parentFolder == null) { throw new Exception("Folder is null"); } //Create Folder Folder f = new Folder { fk_ParentFolderId = parentFolderGuid, Name = name, CreateDt = DateTime.Now, fk_CatalogCollectionId = parentFolder.fk_CatalogCollectionId, Win32FileAttribute = (int)FileAttributes.Directory, IsDeleted = false, DeletedDt = null, DeletedBy = null, OwnerId = createdBy.SecurityObjectId, CreatedById = createdBy.SecurityObjectId }; try { context.Folders.Add(f); context.SaveChanges(); var pfs = new FolderSecurity() { canDelete = true, canListObjects = true, canCreateFiles = true, canCreateFolders = true, canChangePermissions = true, fk_FolderId = f.pk_FolderId, SecurityObjectId = createdBy.SecurityObjectId }; context.SaveChanges(); if (inheritSecurity) { foreach (FolderSecurity fs in parentFolder.FolderSecurities) { FolderSecurity folderSecurity = new FolderSecurity() { SecurityObjectId = fs.SecurityObjectId, fk_FolderId = f.pk_FolderId, canCreateFolders = fs.canCreateFolders, canDelete = fs.canDelete, canChangePermissions = fs.canChangePermissions, canCreateFiles = fs.canCreateFiles, canListObjects = fs.canListObjects }; context.FolderSecurities.Add(folderSecurity); } context.SaveChanges(); FolderSecurity us = context.FolderSecurities.FirstOrDefault(d => d.fk_FolderId == f.pk_FolderId && d.SecurityObjectId == createdBy.SecurityObjectId); if (us == null) { us = new FolderSecurity() { canListObjects = true, canChangePermissions = true, canCreateFiles = true, canCreateFolders = true, canDelete = true, fk_FolderId = f.pk_FolderId, SecurityObjectId = createdBy.SecurityObjectId }; context.FolderSecurities.Add(us); } else { us.canListObjects = true; us.canChangePermissions = true; us.canCreateFolders = true; us.canCreateFiles = true; us.canDelete = true; } context.SaveChanges(); } } catch (Exception ex) { throw new Exception("Unable to create new folder.", ex); } return(f); } }