/// <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 Principal GetPrinciple(string username) { string user = Principal.GetLogin(username); Principal p = (Principal) GetCachedObject(user); if (p != null) return p; p = new Principal(user); AddCacheObject(user, p, TimeSpan.FromMinutes(60)); return p; }
public Principal GetPrinciple(HttpContext current) { string user = Principal.GetLogin(current.User.Identity.Name); user = user == string.Empty ? Principal.GetLogin(Environment.UserName) : user; Principal p = (Principal) GetCachedObject(user); if (p != null) return p; p = new Principal(user); AddCacheObject(user, p, TimeSpan.FromMinutes(60)); return p; }
public Principal GetPrinciple(FromType fromType) { string user; switch (fromType) { case FromType.Web: user = Principal.GetLogin(HttpContext.Current.User.Identity.Name); user = user == string.Empty ? Principal.GetLogin(Environment.UserName) : user; break; case FromType.WebDav: user = ((IIdentity) Thread.GetData(Thread.GetNamedDataSlot(WebDavServer.HttpUser))).Name; break; default: throw new Exception("No Enum Set"); } Principal p = (Principal) GetCachedObject(user); if (p != null) return p; p = new Principal(user); AddCacheObject(user, p, TimeSpan.FromMinutes(60)); return p; }
/// <summary> /// Gets all the files in the trash bin. /// </summary> /// <param name="context"></param> /// <param name="p"></param> /// <returns></returns> public static List<File> GetTrashBinFile(this OnlineFilesEntities context, Principal p) { List<File> found; bool createdContext = false; if (context == null) { createdContext = true; context = new OnlineFilesEntities(); } try { found = context.Files.Where(d => d.IsDeleted && d.OwnerId == p.UserProfile.SecurityObjectId) .ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } finally { if (createdContext) { context.Dispose(); context = null; } } return found; }
/// <summary> /// Retrieves all the Child Folders for the folder /// </summary> /// <param name="context"></param> /// <param name="Key"></param> /// <param name="p"></param> /// <param name="ReadOnly"></param> /// <returns></returns> public static List<Folder> GetChildFolders(this OnlineFilesEntities context, Guid? Key, Principal p, bool ReadOnly = false) { List<Folder> found = new List<Folder>(); bool createdContext = false; if (context == null) { createdContext = true; context = new OnlineFilesEntities(); } try { DbQuery<Folder> source = ReadOnly ? context.Folders.AsNoTracking() : context.Folders; if (Key == new Guid()) { found = source.Where(d => d.fk_ParentFolderId == Key && !(d.IsDeleted) && (d.OwnerId == p.UserProfile.SecurityObjectId)).ToList(); } else { found = source.Where(d => d.fk_ParentFolderId == Key && !(d.IsDeleted) && (d.OwnerId == p.UserProfile.SecurityObjectId || d.FolderSecurities.Any(x => x.canListObjects && p.UserProfile.mySecurityGroups.Contains(x.SecurityObjectId)) )).ToList(); } } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } finally { if (createdContext) { context.Dispose(); context = null; } } return found; }
/// <summary> /// Set the permission on a file for the target User. /// </summary> /// <param name="fileId"></param> /// <param name="user"></param> /// <param name="targetUser"></param> /// <param name="canRead"></param> /// <param name="canWrite"></param> /// <param name="canDelete"></param> /// <param name="context"></param> /// <param name="dbcxtransaction"></param> public static void SetPermissions(Guid fileId, Principal user, Principal targetUser, bool canRead, bool canWrite, bool canDelete, OnlineFilesEntities context = null, DbContextTransaction dbcxtransaction = null) { bool createdContext = false; bool createdTransaction = false; bool didRollback = false; if (context == null) { createdContext = true; context = new OnlineFilesEntities(); } if (dbcxtransaction == null) { dbcxtransaction = context.Database.BeginTransaction(); createdTransaction = true; } try { File targetfile = context.Files .Include(d => d.FileSecurities) .FirstOrDefault(d => d.pk_FileId == fileId); if (targetfile == null) throw new Exception("File does not exist."); Folder target = context.Folders.Include(d => d.FolderSecurities).FirstOrDefault(d => d.pk_FolderId == targetfile.fk_FolderId); if (target == null) throw new Exception("Parent Folder does not exist."); //Can the user Change Permissions if (target.FolderSecurities.Any(d => d.canChangePermissions && user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId))) { var secRecord = targetfile.FileSecurities.FirstOrDefault(d => d.SecurityObjectId == targetUser.UserProfile.SecurityObjectId); if (secRecord == null) { secRecord = new FileSecurity { fk_FileId = targetfile.pk_FileId, CanDelete = canDelete, canRead = canRead, canWrite = canWrite, SecurityObjectId = targetUser.UserProfile.SecurityObjectId }; targetfile.FileSecurities.Add(secRecord); } else { secRecord.canRead = canRead; secRecord.CanDelete = canDelete; secRecord.canWrite = canWrite; } context.SaveChanges(); } else { throw new SecurityException("Not Authorized."); } } catch (Exception) { if (!createdTransaction) throw; didRollback = true; dbcxtransaction.Rollback(); throw; } finally { if (createdTransaction) { if (!didRollback) dbcxtransaction.Commit(); dbcxtransaction.Dispose(); } if (createdContext) context.Dispose(); } }
/// <summary> /// Marks the File object as deleted, don't forget to commitchanges. /// </summary> /// <param name="deletedBy"></param> public void SetDeleted(Principal deletedBy) { using (var context = new OnlineFilesEntities()) if (!(context.FileSecurities.AsNoTracking().Any(x => deletedBy.UserProfile.mySecurityGroups.Contains(x.SecurityObjectId) && x.CanDelete))) throw new SecurityException("Not Authorized."); IsDeleted = true; DeletedDt = DateTime.Now; DeletedById = deletedBy.UserProfile.SecurityObjectId; }
/// <summary> /// Opens a WRITE stream to the file. /// </summary> /// <param name="user"></param> /// <param name="itemPath"></param> /// <param name="webDavSqlStoreDocumentFactoryInstance"></param> /// <returns></returns> public Stream OpenWriteStream(Principal user, string itemPath = null, object webDavSqlStoreDocumentFactoryInstance = null) { using (var context = new OnlineFilesEntities()) { if (!(context.FileSecurities.Where(d => d.fk_FileId == pk_FileId).ToList().Any(x => user.UserProfile.mySecurityGroups.Contains(x.SecurityObjectId) && x.canWrite))) throw new SecurityException("Not Authorized."); int revision = 0; FileData fd = context.FileDatas.Include(x => x.Catalog).Where(d => d.fk_FileId == pk_FileId).OrderByDescending(d => d.Revision).FirstOrDefault(); if (fd != null) revision = fd.Revision; revision++; Catalog catalog; if (fd == null || fd.Catalog.fk_CatalogStatusId != CatalogStatus.Open) { Folder f = context.Folders.FirstOrDefault(d => d.pk_FolderId == fk_FolderId); if (f == null) throw new Exception("Null ptr"); CatalogCollection t = context.CatalogCollections.Include(d => d.Catalogs).FirstOrDefault(d => d.pk_CatalogCollectionId == f.fk_CatalogCollectionId); if (t == null) throw new Exception("Cat col is null"); catalog = t.Catalogs.FirstOrDefault(d => d.fk_CatalogStatusId == CatalogStatus.Open); if (catalog == null) throw new Exception("No Catalog Available."); } else catalog = fd.Catalog; if (catalog == null) throw new Exception("No Catalog Available for file."); using (var ctx = new OnlineFiles_CatalogEntities(catalog.EntityConnectionString)) { FileCatalogEntry fce = new FileCatalogEntry { binaryData = _emptyBytes.ToArray() }; ctx.FileCatalogEntries.Add(fce); ctx.SaveChanges(); FileData filedata = new FileData { fk_FileId = pk_FileId, Revision = revision, Size = 0, CreateDt = DateTime.Now, fk_CatalogId = catalog.pk_CatalogId, fk_ContentId = fce.pk_FileCatalogEntryId }; context.FileDatas.Add(filedata); context.SaveChanges(); Stream stream = new SqlStoreFileStream { CatalogId = catalog.pk_CatalogId, FileCatalogEntryId = fce.pk_FileCatalogEntryId, Path = itemPath, FileDataId = filedata.pk_FileDataId, WebDavSqlStoreDocumentFactoryInstance = webDavSqlStoreDocumentFactoryInstance }; return stream; } } }
/// <summary> /// Opens a READ only stream to the file data. /// </summary> /// <param name="user"></param> /// <returns></returns> public Stream OpenReadStream(Principal user) { using (var context = new OnlineFilesEntities()) { var sec = context.FileSecurities.Where(d => d.fk_FileId == pk_FileId && user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId)); if (!sec.Any(d => d.canRead)) throw new SecurityException("Not Authorized."); FileData filedata = context.FileDatas.AsNoTracking() .Include(x => x.Catalog) .Where(d => d.fk_FileId == pk_FileId) .OrderByDescending(d => d.Revision) .FirstOrDefault(); if (filedata == null) return new MemoryStream(_emptyBytes.ToArray()) { Position = 0 }; using (var ctx = new OnlineFiles_CatalogEntities(filedata.Catalog.EntityConnectionString)) { FileCatalogEntry filecat = ctx.FileCatalogEntries.AsNoTracking() .FirstOrDefault(d => d.pk_FileCatalogEntryId == filedata.fk_ContentId); if (filecat == null) return new MemoryStream(_emptyBytes.ToArray()) { Position = 0 }; return new MemoryStream(filecat.binaryData) { Position = 0 }; } } }
/// <summary> /// Will set the permissions for the targetUser, use recursive if you want it to propagate to child file and folders. /// context & dbcxtransaction should be null. /// </summary> /// <param name="FolderId"></param> /// <param name="user"></param> /// <param name="targetUser"></param> /// <param name="canListObjects"></param> /// <param name="canCreateFiles"></param> /// <param name="canCreateFolders"></param> /// <param name="canDelete"></param> /// <param name="canChangePermissions"></param> /// <param name="recursive"></param> /// <param name="context"></param> /// <param name="dbcxtransaction"></param> public static void SetPermissions(Guid FolderId, Principal user, Principal targetUser, bool canListObjects, bool canCreateFiles, bool canCreateFolders, bool canDelete, bool canChangePermissions, bool recursive = false, OnlineFilesEntities context = null, DbContextTransaction dbcxtransaction = null) { bool createdContext = false; bool createdTransaction = false; bool didRollback = false; if (context == null) { createdContext = true; context = new OnlineFilesEntities(); } if (dbcxtransaction == null) { dbcxtransaction = context.Database.BeginTransaction(); createdTransaction = true; } try { Folder target = context.Folders .Include(d => d.FolderSecurities) .FirstOrDefault(d => d.pk_FolderId == FolderId); //Can the user Change Permissions if (target.FolderSecurities.Any(d => d.canChangePermissions && user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId))) { var secRecord = target.FolderSecurities.FirstOrDefault(d => d.SecurityObjectId == targetUser.UserProfile.SecurityObjectId); if (secRecord == null) { secRecord = new FolderSecurity() { SecurityObjectId = targetUser.UserProfile.SecurityObjectId, canChangePermissions = canChangePermissions, canListObjects = canListObjects, canCreateFolders = canCreateFolders, canCreateFiles = canCreateFiles, canDelete = canDelete, fk_FolderId = target.pk_FolderId }; target.FolderSecurities.Add(secRecord); } else { secRecord.canListObjects = canListObjects; secRecord.canChangePermissions = canChangePermissions; secRecord.canCreateFiles = canCreateFiles; secRecord.canCreateFolders = canCreateFolders; secRecord.canDelete = canDelete; } foreach (File source in context.Files.Where(d => d.fk_FolderId == target.pk_FolderId)) File.SetPermissions(source.pk_FileId, user, targetUser, canListObjects, canCreateFiles, canDelete, context, dbcxtransaction); context.SaveChanges(); if (recursive) foreach (Folder folder in context.Folders.Where(d => d.fk_ParentFolderId == target.pk_FolderId)) SetPermissions(folder.pk_FolderId, user, targetUser, canListObjects, canCreateFiles, canCreateFolders, canDelete, canChangePermissions, recursive, context, dbcxtransaction); } else { throw new SecurityException("Not Authorized."); } } catch (Exception) { if (createdTransaction) { didRollback = true; dbcxtransaction.Rollback(); } throw; } finally { if (createdTransaction) { if (!didRollback) dbcxtransaction.Commit(); dbcxtransaction.Dispose(); } if (createdContext) context.Dispose(); } }
/// <summary> /// Marks the Folder object as deleted, don't forget to commitchanges. /// </summary> /// <param name="deletedBy"></param> public void SetDeleted(Principal deletedBy) { List<FolderSecurity> perm; using (var context = new OnlineFilesEntities()) perm = context.FolderSecurities.AsNoTracking().Where(d => d.fk_FolderId == pk_FolderId).ToList(); if (perm.Any(d => d.canDelete && deletedBy.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId))) { IsDeleted = true; DeletedDt = DateTime.Now; DeletedById = deletedBy.UserProfile.SecurityObjectId; } else { throw new SecurityException("Not Authorized."); } }
/// <summary> /// Copies the fileId to this Folder. /// </summary> /// <param name="destination"></param> /// <param name="fileId"></param> /// <param name="destinationName"></param> /// <param name="user"></param> /// <param name="context"></param> /// <param name="dbcxtransaction"></param> public static void CopyFileHere(this Folder destination, Guid fileId, string destinationName, Principal user, OnlineFilesEntities context = null, DbContextTransaction dbcxtransaction = null) { bool createdContext = false; bool createdTransaction = false; bool didRollback = false; if (context == null) { createdContext = true; context = new OnlineFilesEntities(); } if (dbcxtransaction == null) { dbcxtransaction = context.Database.BeginTransaction(); createdTransaction = true; } try { var destSecurity = context.FolderSecurities.Where(d => d.fk_FolderId == destination.pk_FolderId); //Can the user create files at the destination location? if (destSecurity.Any(d => d.canCreateFiles && user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId))) { var file = context.Files .Include(x => x.FileSecurities) .Include(x => x.FileDatas) .FirstOrDefault(d => d.pk_FileId == fileId); //Can the user Read the file to be copied? if (file.FileSecurities.Any(f => user.UserProfile.mySecurityGroups.Contains(f.SecurityObjectId) && f.canRead)) { File newFile = File.Create(destination.pk_FolderId, file.Name, user.UserProfile, true); context.SaveChanges(); //Copy the file data using (Stream s = file.OpenReadStream(user)) using (Stream d = newFile.OpenWriteStream(user)) s.CopyTo(d); //Copy the file Security foreach (FileSecurity security in file.FileSecurities) { FileSecurity nsec = context.FileSecurities.FirstOrDefault(d => d.fk_FileId == newFile.pk_FileId && d.SecurityObjectId == security.SecurityObjectId); if (nsec == null) { nsec = new FileSecurity() { canRead = security.canRead, canWrite = security.canWrite, CanDelete = security.CanDelete, fk_FileId = newFile.pk_FileId, SecurityObjectId = security.SecurityObjectId }; context.FileSecurities.Add(nsec); } else { nsec.canRead = nsec.canRead || security.canRead; nsec.CanDelete = nsec.CanDelete || security.CanDelete; nsec.canWrite = nsec.canWrite || security.canWrite; } } context.SaveChanges(); } else { throw new SecurityException("Not Authorized."); } } else { throw new SecurityException("Not Authorized."); } } catch (Exception) { //If we got an error and we created the transaction, roll it back. if (createdTransaction) { dbcxtransaction.Rollback(); didRollback = true; } throw; } finally { if (createdTransaction) { //If we didn't roll back the transaction, commit it. if (!didRollback) dbcxtransaction.Commit(); dbcxtransaction.Dispose(); } if (createdContext) context.Dispose(); } }
/// <summary> /// Copies the folderId to this Folder /// </summary> /// <param name="destination"></param> /// <param name="folderId"></param> /// <param name="destinationName"></param> /// <param name="user"></param> /// <param name="context"></param> /// <param name="recursive"></param> /// <param name="dbcxtransaction"></param> public static void CopyFolderHere(this Folder destination, Guid folderId, string destinationName, Principal user, OnlineFilesEntities context = null, bool recursive = true, DbContextTransaction dbcxtransaction = null) { bool createdContext = false; bool createdTransaction = false; bool didRollback = false; if (context == null) { createdContext = true; context = new OnlineFilesEntities(); } if (dbcxtransaction == null) { dbcxtransaction = context.Database.BeginTransaction(); createdTransaction = true; } try { 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 read permission in the folder the item is comming from? if (parentToFolderToMove.FolderSecurities.Any(d => user.UserProfile.mySecurityGroups.Contains(d.SecurityObjectId) && d.canListObjects)) { Folder newFolder = Folder.Create(destinationName, destination.pk_FolderId, user.UserProfile, true); context.SaveChanges(); if (recursive) { //Get all the files that are in the folder being copied. var files = context.Files.Where(d => d.fk_FolderId == folderToMove.pk_FolderId); foreach (File file in files) newFolder.CopyFileHere(file.pk_FileId, file.Name, user, context, dbcxtransaction); //Get all the folders inside the folder we are moving. var folders = context.Folders.Where(d => d.fk_ParentFolderId == folderToMove.pk_FolderId); //Copy the folders to the new location foreach (var folder in folders) newFolder.CopyFolderHere(folder.pk_FolderId, folder.Name, user, context, recursive, dbcxtransaction); } context.SaveChanges(); } else { throw new SecurityException("Not Authorized."); } } else { throw new SecurityException("Not Authorized."); } } catch (Exception) { if (createdTransaction) { didRollback = true; dbcxtransaction.Rollback(); } throw; } finally { if (createdTransaction) { if (!didRollback) dbcxtransaction.Commit(); dbcxtransaction.Dispose(); } if (createdContext) context.Dispose(); } }
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."); } } }
/// <summary> /// Gets all the files in a folder. /// </summary> /// <param name="context"></param> /// <param name="folderkey"></param> /// <param name="p"></param> /// <param name="readOnly"></param> /// <returns></returns> public static List<File> GetChildFiles(this OnlineFilesEntities context, Guid? folderkey, Principal p, bool readOnly = false) { List<File> found; bool createdContext = false; if (context == null) { createdContext = true; context = new OnlineFilesEntities(); } try { var folder = context.Folders.Include(x => x.FolderSecurities).FirstOrDefault(d => d.pk_FolderId == folderkey); if (folder == null) throw new SecurityException("No Access"); if (!folder.FolderSecurities.Any(x => p.UserProfile.mySecurityGroups.Contains(x.SecurityObjectId) && x.canListObjects)) throw new SecurityException("No Access"); DbQuery<File> source = readOnly ? context.Files.AsNoTracking() : context.Files; found = source.Where(d => d.fk_FolderId == folderkey && !(d.IsDeleted) && (d.OwnerId == p.UserProfile.SecurityObjectId || d.FileSecurities.Any(x => x.canRead && p.UserProfile.mySecurityGroups.Contains(x.SecurityObjectId)))) .ToList(); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } finally { if (createdContext) context.Dispose(); } return found; }
/// <summary> /// Gets all the files in a folder /// </summary> /// <param name="context"></param> /// <param name="folder"></param> /// <param name="p"></param> /// <param name="readOnly"></param> /// <returns></returns> public static List<File> GetChildFiles(this OnlineFilesEntities context, Folder folder, Principal p, bool readOnly = false) { return GetChildFiles(context, folder.pk_FolderId, p, readOnly); }
/// <summary> /// Sets a bit flag to a file attribute. /// </summary> /// <param name="user"></param> /// <param name="attrib"></param> /// <param name="value"></param> public void SetWin32Attribute(Principal user, FileAttributes attrib, bool value) { using (var context = new OnlineFilesEntities()) if (!(context.FileSecurities.Where(d => d.fk_FileId == pk_FileId).ToList().Any(x => user.UserProfile.mySecurityGroups.Contains(x.SecurityObjectId) && x.canWrite))) throw new SecurityException("Not Authorized."); if (value) Win32FileAttribute = (int) FlagsHelper.Set(((FileAttributes) Win32FileAttribute), attrib); else Win32FileAttribute = (int) FlagsHelper.Unset(((FileAttributes) Win32FileAttribute), attrib); }