예제 #1
0
        public static bool SetAceObject(List <AceWrapper> aceWrappers, FileEntry entry, bool notify, string message)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(FilesCommonResource.ErrorMassage_BadRequest);
            }
            if (!CanSetAccess(entry))
            {
                throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException);
            }

            var fileSecurity = Global.GetFilesSecurity();

            var entryType         = entry.FileEntryType;
            var recipients        = new Dictionary <Guid, FileShare>();
            var usersWithoutRight = new List <Guid>();
            var changed           = false;

            foreach (var w in aceWrappers.OrderByDescending(ace => ace.SubjectGroup))
            {
                var subjects = fileSecurity.GetUserSubjects(w.SubjectId);

                var ownerId = entry.RootFolderType == FolderType.USER ? entry.RootFolderCreator : entry.CreateBy;
                if (entry.RootFolderType == FolderType.COMMON && subjects.Contains(Constants.GroupAdmin.ID) ||
                    ownerId == w.SubjectId)
                {
                    continue;
                }

                var share = w.Share;

                if (w.SubjectId == FileConstant.ShareLinkId)
                {
                    if (w.Share == FileShare.ReadWrite && CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor())
                    {
                        throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException);
                    }
                    share = w.Share == FileShare.Restrict ? FileShare.None : w.Share;
                }

                fileSecurity.Share(entry.ID, entryType, w.SubjectId, share);
                changed = true;

                if (w.SubjectId == FileConstant.ShareLinkId)
                {
                    continue;
                }

                entry.Access = share;

                var listUsersId = new List <Guid>();

                if (w.SubjectGroup)
                {
                    listUsersId = CoreContext.UserManager.GetUsersByGroup(w.SubjectId).Select(ui => ui.ID).ToList();
                }
                else
                {
                    listUsersId.Add(w.SubjectId);
                }
                listUsersId.Remove(SecurityContext.CurrentAccount.ID);

                if (entryType == FileEntryType.File)
                {
                    listUsersId.ForEach(uid => FileTracker.ChangeRight(entry.ID, uid, true));
                }

                var addRecipient = share == FileShare.Read ||
                                   share == FileShare.ReadWrite ||
                                   share == FileShare.Review ||
                                   share == FileShare.FillForms ||
                                   share == FileShare.Comment ||
                                   share == FileShare.None && entry.RootFolderType == FolderType.COMMON;
                var removeNew = share == FileShare.None && entry.RootFolderType == FolderType.USER ||
                                share == FileShare.Restrict;
                listUsersId.ForEach(id =>
                {
                    recipients.Remove(id);
                    if (addRecipient)
                    {
                        recipients.Add(id, share);
                    }
                    else if (removeNew)
                    {
                        usersWithoutRight.Add(id);
                    }
                });
            }

            if (entryType == FileEntryType.File)
            {
                DocumentServiceHelper.CheckUsersForDrop((File)entry);
            }

            if (recipients.Any())
            {
                if (entryType == FileEntryType.File ||
                    ((Folder)entry).TotalSubFolders + ((Folder)entry).TotalFiles > 0 ||
                    entry.ProviderEntry)
                {
                    FileMarker.MarkAsNew(entry, recipients.Keys.ToList());
                }

                if (entry.RootFolderType == FolderType.USER &&
                    notify)
                {
                    NotifyClient.SendShareNotice(entry, recipients, message);
                }
            }

            usersWithoutRight.ForEach(userId => FileMarker.RemoveMarkAsNew(entry, userId));

            return(changed);
        }
예제 #2
0
        public static File UpdateToVersionFile(object fileId, int version, bool checkRight = true)
        {
            using (var fileDao = Global.DaoFactory.GetFileDao())
            {
                if (version < 1)
                {
                    throw new ArgumentNullException("version");
                }

                var fromFile = fileDao.GetFile(fileId);
                if (fromFile == null)
                {
                    throw new FileNotFoundException(FilesCommonResource.ErrorMassage_FileNotFound);
                }

                if (fromFile.Version != version)
                {
                    fromFile = fileDao.GetFile(fromFile.ID, Math.Min(fromFile.Version, version));
                }

                if (fromFile == null)
                {
                    throw new FileNotFoundException(FilesCommonResource.ErrorMassage_FileNotFound);
                }
                if (checkRight && (!Global.GetFilesSecurity().CanEdit(fromFile) || CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor()))
                {
                    throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_EditFile);
                }
                if (FileLockedForMe(fromFile.ID))
                {
                    throw new Exception(FilesCommonResource.ErrorMassage_LockedFile);
                }
                if (checkRight && FileTracker.IsEditing(fromFile.ID))
                {
                    throw new Exception(FilesCommonResource.ErrorMassage_SecurityException_UpdateEditingFile);
                }
                if (fromFile.RootFolderType == FolderType.TRASH)
                {
                    throw new Exception(FilesCommonResource.ErrorMassage_ViewTrashItem);
                }

                var exists = cache.Get <string>(UPDATE_LIST + fileId.ToString()) != null;
                if (exists)
                {
                    throw new Exception(FilesCommonResource.ErrorMassage_UpdateEditingFile);
                }
                else
                {
                    cache.Insert(UPDATE_LIST + fileId.ToString(), fileId.ToString(), TimeSpan.FromMinutes(2));
                }

                try
                {
                    var currFile = fileDao.GetFile(fileId);
                    var newFile  = new File
                    {
                        ID            = fromFile.ID,
                        Version       = currFile.Version + 1,
                        VersionGroup  = currFile.VersionGroup,
                        Title         = fromFile.Title,
                        ContentLength = fromFile.ContentLength,
                        FileStatus    = fromFile.FileStatus,
                        FolderID      = fromFile.FolderID,
                        CreateBy      = fromFile.CreateBy,
                        CreateOn      = fromFile.CreateOn,
                        ModifiedBy    = fromFile.ModifiedBy,
                        ModifiedOn    = fromFile.ModifiedOn,
                        ConvertedType = fromFile.ConvertedType,
                        Comment       = string.Format(FilesCommonResource.CommentRevert, fromFile.ModifiedOnString),
                    };

                    using (var stream = fileDao.GetFileStream(fromFile))
                    {
                        newFile = fileDao.SaveFile(newFile, stream);
                    }

                    FileMarker.MarkAsNew(newFile);

                    SetFileStatus(newFile);

                    return(newFile);
                }
                catch (Exception e)
                {
                    Global.Logger.Error(string.Format("Error on update {0} to version {1}", fileId, version), e);
                    throw new Exception(e.Message, e);
                }
                finally
                {
                    cache.Remove(UPDATE_LIST + fromFile.ID);
                }
            }
        }
예제 #3
0
        private static File SaveConvertedFile(File file, string convertedFileUrl)
        {
            var fileSecurity = Global.GetFilesSecurity();

            using (var fileDao = Global.DaoFactory.GetFileDao())
                using (var folderDao = Global.DaoFactory.GetFolderDao())
                {
                    File newFile      = null;
                    var  newFileTitle = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetInternalExtension(file.Title));

                    if (!FilesSettings.StoreOriginalFiles && fileSecurity.CanEdit(file))
                    {
                        newFile = (File)file.Clone();
                        newFile.Version++;
                    }
                    else
                    {
                        var folderId = Global.FolderMy;

                        var parent = folderDao.GetFolder(file.FolderID);
                        if (parent != null &&
                            fileSecurity.CanCreate(parent))
                        {
                            folderId = parent.ID;
                        }

                        if (Equals(folderId, 0))
                        {
                            throw new SecurityException(FilesCommonResource.ErrorMassage_FolderNotFound);
                        }

                        if (FilesSettings.UpdateIfExist && (parent != null && folderId != parent.ID || !file.ProviderEntry))
                        {
                            newFile = fileDao.GetFile(folderId, newFileTitle);
                            if (newFile != null && fileSecurity.CanEdit(newFile) && !EntryManager.FileLockedForMe(newFile.ID) && !FileTracker.IsEditing(newFile.ID))
                            {
                                newFile.Version++;
                            }
                            else
                            {
                                newFile = null;
                            }
                        }

                        if (newFile == null)
                        {
                            newFile = new File {
                                FolderID = folderId
                            };
                        }
                    }

                    newFile.Title         = newFileTitle;
                    newFile.ConvertedType = null;
                    newFile.Comment       = string.Format(FilesCommonResource.CommentConvert, file.Title);

                    var req = (HttpWebRequest)WebRequest.Create(convertedFileUrl);

                    if (WorkContext.IsMono && ServicePointManager.ServerCertificateValidationCallback == null)
                    {
                        ServicePointManager.ServerCertificateValidationCallback += (s, c, n, p) => true; //HACK: http://ubuntuforums.org/showthread.php?t=1841740
                    }

                    try
                    {
                        using (var convertedFileStream = new ResponseStream(req.GetResponse()))
                        {
                            newFile.ContentLength = convertedFileStream.Length;
                            newFile = fileDao.SaveFile(newFile, convertedFileStream);
                        }
                    }
                    catch (WebException e)
                    {
                        using (var response = e.Response)
                        {
                            var httpResponse = (HttpWebResponse)response;
                            var errorString  = String.Format("WebException: {0}", httpResponse.StatusCode);

                            if (httpResponse.StatusCode != HttpStatusCode.NotFound)
                            {
                                using (var responseStream = response.GetResponseStream())
                                {
                                    if (responseStream != null)
                                    {
                                        using (var readStream = new StreamReader(responseStream))
                                        {
                                            var text = readStream.ReadToEnd();
                                            errorString += String.Format(" Error message: {0}", text);
                                        }
                                    }
                                }
                            }

                            throw new Exception(errorString);
                        }
                    }

                    FilesMessageService.Send(newFile, MessageInitiator.DocsService, MessageAction.FileConverted, newFile.Title);
                    FileMarker.MarkAsNew(newFile);

                    using (var tagDao = Global.DaoFactory.GetTagDao())
                    {
                        var tags = tagDao.GetTags(file.ID, FileEntryType.File, TagType.System).ToList();
                        if (tags.Any())
                        {
                            tags.ForEach(r => r.EntryId = newFile.ID);
                            tagDao.SaveTags(tags.ToArray());
                        }
                    }

                    return(newFile);
                }
        }
예제 #4
0
        public static File SaveEditing(String fileId, int version, Guid tabId, string fileType, string downloadUri, Stream stream, bool asNew, String shareLinkKey, string comment = null, bool checkRight = true)
        {
            var newType = string.IsNullOrEmpty(fileType)
                              ? FileUtility.GetFileExtension(downloadUri)
                              : fileType;

            var app = ThirdPartySelector.GetAppByFileId(fileId);

            if (app != null)
            {
                app.SaveFile(fileId, newType, downloadUri, stream);
                return(null);
            }

            File file;

            using (var fileDao = Global.DaoFactory.GetFileDao())
            {
                var editLink = FileShareLink.Check(shareLinkKey, false, fileDao, out file);
                if (file == null)
                {
                    file = fileDao.GetFile(fileId);
                }

                if (file == null)
                {
                    throw new FileNotFoundException(FilesCommonResource.ErrorMassage_FileNotFound);
                }
                if (checkRight && !editLink && (!Global.GetFilesSecurity().CanEdit(file) || CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor()))
                {
                    throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_EditFile);
                }
                if (checkRight && FileLockedForMe(file.ID))
                {
                    throw new Exception(FilesCommonResource.ErrorMassage_LockedFile);
                }
                if (file.RootFolderType == FolderType.TRASH)
                {
                    throw new Exception(FilesCommonResource.ErrorMassage_ViewTrashItem);
                }

                var currentType = file.ConvertedExtension;

                if ((file.Version <= version || !newType.Equals(currentType) || version < 1) &&
                    (file.Version > 1 || !asNew) &&
                    !FileTracker.FixedVersion(file.ID))
                {
                    file.Version++;
                    if (string.IsNullOrEmpty(comment))
                    {
                        comment = FilesCommonResource.CommentEdit;
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(comment))
                    {
                        comment = FilesCommonResource.CommentCreate;
                    }
                }

                file.ConvertedType = FileUtility.GetFileExtension(file.Title) != newType ? newType : null;

                if (file.ProviderEntry && !newType.Equals(currentType))
                {
                    if (FileUtility.ExtsConvertible.Keys.Contains(newType) &&
                        FileUtility.ExtsConvertible[newType].Contains(currentType))
                    {
                        var key = DocumentServiceConnector.GenerateRevisionId(downloadUri ?? Guid.NewGuid().ToString());
                        if (stream != null)
                        {
                            using (var tmpStream = new MemoryStream())
                            {
                                stream.CopyTo(tmpStream);
                                downloadUri = DocumentServiceConnector.GetExternalUri(tmpStream, newType, key);
                            }
                        }

                        DocumentServiceConnector.GetConvertedUri(downloadUri, newType, currentType, key, false, out downloadUri);

                        stream = null;
                    }
                    else
                    {
                        file.ID    = null;
                        file.Title = FileUtility.ReplaceFileExtension(file.Title, newType);
                    }

                    file.ConvertedType = null;
                }

                using (var tmpStream = new MemoryStream())
                {
                    if (stream != null)
                    {
                        stream.CopyTo(tmpStream);
                    }
                    else
                    {
                        // hack. http://ubuntuforums.org/showthread.php?t=1841740
                        if (WorkContext.IsMono)
                        {
                            ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
                        }

                        var req = (HttpWebRequest)WebRequest.Create(downloadUri);
                        using (var editedFileStream = new ResponseStream(req.GetResponse()))
                        {
                            editedFileStream.CopyTo(tmpStream);
                        }
                    }
                    tmpStream.Position = 0;

                    file.ContentLength = tmpStream.Length;
                    file.Comment       = string.IsNullOrEmpty(comment) ? null : comment;
                    file = fileDao.SaveFile(file, tmpStream);
                }
            }

            checkRight = FileTracker.ProlongEditing(file.ID, tabId, true, SecurityContext.CurrentAccount.ID);
            if (checkRight)
            {
                FileTracker.ChangeRight(file.ID, SecurityContext.CurrentAccount.ID, false);
            }

            FileMarker.MarkAsNew(file);
            FileMarker.RemoveMarkAsNew(file);
            return(file);
        }