/// <summary> /// Moves the folderId to this Folder /// folderId is the folder to move. /// </summary> /// <param name="destination"></param> /// <param name="folderId"></param> /// <param name="destinationName"></param> /// <param name="user"></param> public static void MoveFolderHere(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 folderToMove = context.Folders.Include(d => d.FolderSecurities).FirstOrDefault(d => d.pk_FolderId == folderId); if (folderToMove == null) throw new Exception("Cannot move a non existant folder"); Folder parentToFolderToMove = context.Folders.Include(x => x.FolderSecurities).FirstOrDefault(d => d.pk_FolderId == folderToMove.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)) { folderToMove.Name = destination.CheckFolderName(destinationName, context); folderToMove.fk_ParentFolderId = destination.pk_FolderId; context.SaveChanges(); } else { throw new SecurityException("Not Authorized."); } } else { throw new SecurityException("Not Authorized."); } } }
public static void RestoreFileHere(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 fileToRestore = context.Files.FirstOrDefault(d => d.pk_FileId == fileId); if (fileToRestore == null) throw new Exception("Cannot move a non existant file"); Folder parentToFileToMove = context.Folders.Include(x => x.FolderSecurities).FirstOrDefault(d => d.pk_FolderId == fileToRestore.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."); } fileToRestore.RestoreDeleted(PrincipleFactory.Instance.GetPrinciple().UserProfile); fileToRestore.Name = destination.CheckFolderName(destinationName, context); fileToRestore.fk_FolderId = destination.pk_FolderId; context.SaveChanges(); } else { throw new SecurityException("Not Authorized."); } } }