Exemplo n.º 1
0
        public static List <AceWrapper> GetSharedInfo(FileEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(FilesCommonResource.ErrorMassage_BadRequest);
            }
            if (!CanSetAccess(entry))
            {
                LogManager.GetLogger("ASC.Web.Files").ErrorFormat("User {0} can't get shared info for {1} {2}", SecurityContext.CurrentAccount.ID, (entry is File ? "file" : "folder"), entry.ID);
                throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException);
            }

            var linkAccess = FileShare.Restrict;
            var result     = new List <AceWrapper>();

            var fileSecurity = Global.GetFilesSecurity();

            var records = fileSecurity
                          .GetShares(entry)
                          .GroupBy(r => r.Subject)
                          .Select(g => g.OrderBy(r => r.Level)
                                  .ThenBy(r => r.Level)
                                  .ThenByDescending(r => r.Share).FirstOrDefault());

            foreach (var r in records)
            {
                if (r.Subject == FileConstant.ShareLinkId)
                {
                    linkAccess = r.Share;
                    continue;
                }

                var u       = CoreContext.UserManager.GetUsers(r.Subject);
                var isgroup = false;
                var title   = u.DisplayUserName(false);

                if (u.ID == Constants.LostUser.ID)
                {
                    var g = CoreContext.GroupManager.GetGroupInfo(r.Subject);
                    isgroup = true;
                    title   = g.Name;

                    if (g.ID == Constants.GroupAdmin.ID)
                    {
                        title = FilesCommonResource.Admin;
                    }
                    if (g.ID == Constants.GroupEveryone.ID)
                    {
                        title = FilesCommonResource.Everyone;
                    }

                    if (g.ID == Constants.LostGroupInfo.ID)
                    {
                        fileSecurity.RemoveSubject(r.Subject);
                        continue;
                    }
                }

                var w = new AceWrapper
                {
                    SubjectId    = r.Subject,
                    SubjectName  = title,
                    SubjectGroup = isgroup,
                    Share        = r.Share,
                    Owner        =
                        entry.RootFolderType == FolderType.USER
                                ? entry.RootFolderCreator == r.Subject
                                : entry.CreateBy == r.Subject,
                    LockedRights = r.Subject == SecurityContext.CurrentAccount.ID
                };
                result.Add(w);
            }

            if (entry is File && result.All(w => w.SubjectId != FileConstant.ShareLinkId))
            {
                var w = new AceWrapper
                {
                    SubjectId    = FileConstant.ShareLinkId,
                    SubjectName  = FileShareLink.GetLink((File)entry),
                    SubjectGroup = true,
                    Share        = linkAccess,
                    Owner        = false
                };
                result.Add(w);
            }

            if (!result.Any(w => w.Owner))
            {
                var ownerId = entry.RootFolderType == FolderType.USER ? entry.RootFolderCreator : entry.CreateBy;
                var w       = new AceWrapper
                {
                    SubjectId    = ownerId,
                    SubjectName  = Global.GetUserName(ownerId),
                    SubjectGroup = false,
                    Share        = FileShare.ReadWrite,
                    Owner        = true
                };
                result.Add(w);
            }

            if (result.Any(w => w.SubjectId == SecurityContext.CurrentAccount.ID))
            {
                result.Single(w => w.SubjectId == SecurityContext.CurrentAccount.ID).LockedRights =
                    true;
            }

            if (entry.RootFolderType == FolderType.COMMON)
            {
                if (result.All(w => w.SubjectId != Constants.GroupAdmin.ID))
                {
                    var w = new AceWrapper
                    {
                        SubjectId    = Constants.GroupAdmin.ID,
                        SubjectName  = FilesCommonResource.Admin,
                        SubjectGroup = true,
                        Share        = FileShare.ReadWrite,
                        Owner        = false,
                        LockedRights = true,
                    };
                    result.Add(w);
                }
                if (result.All(w => w.SubjectId != Constants.GroupEveryone.ID))
                {
                    var w = new AceWrapper
                    {
                        SubjectId     = Constants.GroupEveryone.ID,
                        SubjectName   = FilesCommonResource.Everyone,
                        SubjectGroup  = true,
                        Share         = fileSecurity.DefaultCommonShare,
                        Owner         = false,
                        DisableRemove = true
                    };
                    result.Add(w);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public static List <AceWrapper> GetSharedInfo(FileEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(FilesCommonResource.ErrorMassage_BadRequest);
            }
            if (!CanSetAccess(entry))
            {
                Global.Logger.ErrorFormat("User {0} can't get shared info for {1} {2}", SecurityContext.CurrentAccount.ID, (entry.FileEntryType == FileEntryType.File ? "file" : "folder"), entry.ID);
                throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException);
            }

            var linkAccess = FileShare.Restrict;
            var result     = new List <AceWrapper>();

            var fileSecurity = Global.GetFilesSecurity();

            var records = fileSecurity
                          .GetShares(entry)
                          .GroupBy(r => r.Subject)
                          .Select(g => g.OrderBy(r => r.Level)
                                  .ThenBy(r => r.Level)
                                  .ThenByDescending(r => r.Share, new FileShareRecord.ShareComparer()).FirstOrDefault());

            foreach (var r in records)
            {
                if (r.Subject == FileConstant.ShareLinkId)
                {
                    linkAccess = r.Share;
                    continue;
                }

                if (r.Subject == FileConstant.DenyDownloadId || r.Subject == FileConstant.DenySharingId)
                {
                    continue;
                }

                var u       = CoreContext.UserManager.GetUsers(r.Subject);
                var isgroup = false;
                var title   = u.DisplayUserName(false);
                var share   = r.Share;

                if (u.ID == Constants.LostUser.ID)
                {
                    var g = CoreContext.UserManager.GetGroupInfo(r.Subject);
                    isgroup = true;
                    title   = g.Name;

                    if (g.ID == Constants.GroupAdmin.ID)
                    {
                        title = FilesCommonResource.Admin;
                    }
                    if (g.ID == Constants.GroupEveryone.ID)
                    {
                        title = FilesCommonResource.Everyone;
                    }

                    if (g.ID == Constants.LostGroupInfo.ID)
                    {
                        fileSecurity.RemoveSubject(r.Subject);
                        continue;
                    }
                }
                else if (u.IsVisitor() &&
                         new FileShareRecord.ShareComparer().Compare(FileShare.Read, share) > 0)
                {
                    share = FileShare.Read;
                }

                var w = new AceWrapper
                {
                    SubjectId    = r.Subject,
                    SubjectName  = title,
                    SubjectGroup = isgroup,
                    Share        = share,
                    Owner        =
                        entry.RootFolderType == FolderType.USER
                                ? entry.RootFolderCreator == r.Subject
                                : entry.CreateBy == r.Subject,
                    LockedRights = r.Subject == SecurityContext.CurrentAccount.ID
                };
                result.Add(w);
            }

            if (entry.FileEntryType == FileEntryType.File &&
                !((File)entry).Encrypted &&
                result.All(w => w.SubjectId != FileConstant.ShareLinkId) &&
                (linkAccess != FileShare.Restrict || CoreContext.Configuration.Standalone || !TenantExtra.GetTenantQuota().Trial || FileUtility.CanWebView(entry.Title)))
            {
                var w = new AceWrapper
                {
                    SubjectId    = FileConstant.ShareLinkId,
                    Link         = FilesSettings.ExternalShare ? FileShareLink.GetLink((File)entry) : string.Empty,
                    SubjectGroup = true,
                    Share        = linkAccess,
                    Owner        = false
                };
                result.Add(w);
            }

            if (!result.Any(w => w.Owner))
            {
                var ownerId = entry.RootFolderType == FolderType.USER ? entry.RootFolderCreator : entry.CreateBy;
                var w       = new AceWrapper
                {
                    SubjectId    = ownerId,
                    SubjectName  = Global.GetUserName(ownerId),
                    SubjectGroup = false,
                    Share        = FileShare.ReadWrite,
                    Owner        = true
                };
                result.Add(w);
            }

            if (result.Any(w => w.SubjectId == SecurityContext.CurrentAccount.ID))
            {
                result.Single(w => w.SubjectId == SecurityContext.CurrentAccount.ID).LockedRights = true;
            }

            if (entry.RootFolderType == FolderType.COMMON)
            {
                if (result.All(w => w.SubjectId != Constants.GroupAdmin.ID))
                {
                    var w = new AceWrapper
                    {
                        SubjectId    = Constants.GroupAdmin.ID,
                        SubjectName  = FilesCommonResource.Admin,
                        SubjectGroup = true,
                        Share        = FileShare.ReadWrite,
                        Owner        = false,
                        LockedRights = true,
                    };
                    result.Add(w);
                }
                var index = result.FindIndex(w => w.SubjectId == Constants.GroupEveryone.ID);
                if (index == -1)
                {
                    var w = new AceWrapper
                    {
                        SubjectId     = Constants.GroupEveryone.ID,
                        SubjectName   = FilesCommonResource.Everyone,
                        SubjectGroup  = true,
                        Share         = fileSecurity.DefaultCommonShare,
                        Owner         = false,
                        DisableRemove = true
                    };
                    result.Add(w);
                }
                else
                {
                    result[index].DisableRemove = true;
                }
            }

            return(result);
        }