コード例 #1
0
        /// <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.");
                }
            }
        }
コード例 #2
0
        public static File Rename(Guid?fileId, Guid?folderId, string name, SecurityObject createdBy)
        {
            using (var context = new OnlineFilesEntities())
            {
                if (folderId == null)
                {
                    throw new Exception("Bad Guid.");
                }

                var folder = context.Folders
                             .Include(x => x.FolderSecurities)
                             .FirstOrDefault(d => d.pk_FolderId == folderId);
                if (folder == null)
                {
                    throw new Exception("Folder Not Found.");
                }

                if (!folder.FolderSecurities.Any(x => createdBy.mySecurityGroups.Contains(x.SecurityObjectId) && x.canCreateFiles))
                {
                    throw new SecurityException("No Access.");
                }

                var filechk = context.Files
                              .Include(x => x.FileSecurities)
                              .FirstOrDefault(d => d.Name == name);
                if (filechk != null)
                {
                    throw new Exception("File Name already used.");
                }
                var file = context.Files
                           .Include(x => x.FileSecurities)
                           .FirstOrDefault(d => d.pk_FileId == fileId);
                if (file != null)
                {
                    file.Name = name;
                }

                context.Files.AddOrUpdate(file);
                context.SaveChanges();

                return(file);
            }
        }
コード例 #3
0
        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.");
                }
            }
        }
コード例 #4
0
        /// <summary>
        ///     Creates a new File Object.
        /// </summary>
        /// <param name="folderId"></param>
        /// <param name="name"></param>
        /// <param name="createdBy"></param>
        /// <param name="inheritSecurity"></param>
        /// <returns></returns>
        public static File Create(Guid?folderId, string name, SecurityObject createdBy, bool inheritSecurity = true)
        {
            using (var context = new OnlineFilesEntities())
            {
                if (folderId == null)
                {
                    throw new Exception("Bad Guid.");
                }

                var folder = context.Folders
                             .Include(x => x.FolderSecurities)
                             .FirstOrDefault(d => d.pk_FolderId == folderId);
                if (folder == null)
                {
                    throw new Exception("Folder Not Found.");
                }

                if (!folder.FolderSecurities.Any(x => createdBy.mySecurityGroups.Contains(x.SecurityObjectId) && x.canCreateFiles))
                {
                    throw new SecurityException("No Access.");
                }


                var file = new File
                {
                    fk_FolderId  = (Guid)folderId,
                    IsDeleted    = false,
                    isRevisioned = true,
                    Name         = name,
                    MimeType     = MimeMapping.GetMimeMapping(name),
                    CreatedById  = createdBy.SecurityObjectId,
                    CreateDt     = DateTime.Now,
                    OwnerId      = createdBy.SecurityObjectId
                };
                context.Files.Add(file);
                context.SaveChanges();

                FileSecurity fileSecurity = new FileSecurity
                {
                    CanDelete        = true,
                    canRead          = true,
                    canWrite         = true,
                    fk_FileId        = file.pk_FileId,
                    SecurityObjectId = createdBy.SecurityObjectId
                };
                context.FileSecurities.Add(fileSecurity);
                context.SaveChanges();

                foreach (FolderSecurity security in folder.FolderSecurities)
                {
                    fileSecurity = context.FileSecurities.FirstOrDefault(d => d.SecurityObjectId == security.SecurityObjectId && d.fk_FileId == file.pk_FileId);
                    if (fileSecurity == null)
                    {
                        fileSecurity = new FileSecurity
                        {
                            CanDelete        = security.canDelete,
                            canRead          = security.canListObjects,
                            canWrite         = security.canCreateFiles,
                            fk_FileId        = file.pk_FileId,
                            SecurityObjectId = security.SecurityObjectId
                        };
                        context.FileSecurities.Add(fileSecurity);
                    }
                    else
                    {
                        fileSecurity.CanDelete = security.canDelete;
                        fileSecurity.canRead   = security.canListObjects;
                        fileSecurity.canWrite  = security.canCreateFiles;
                    }
                }


                context.SaveChanges();
                return(file);
            }
        }
コード例 #5
0
        /// <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);
                }
            }
        }
コード例 #6
0
        /// <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);
            }
        }