/// <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();
            }



        }
Exemplo n.º 2
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>
        /// 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;
            }



        }
Exemplo n.º 4
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);
            }
        }