Esempio n. 1
0
        internal FileStatus GetFileStatus <T>(File <T> file, ref FileStatus currentStatus)
        {
            if (FileTracker.IsEditing(file.ID))
            {
                currentStatus |= FileStatus.IsEditing;
            }

            if (FileTracker.IsEditingAlone(file.ID))
            {
                currentStatus |= FileStatus.IsEditingAlone;
            }

            if (FileConverter.IsConverting(file))
            {
                currentStatus |= FileStatus.IsConverting;
            }

            return(currentStatus);
        }
Esempio n. 2
0
        public File <T> GetParams <T>(File <T> file, bool lastVersion, FileShare linkRight, bool rightToRename, bool rightToEdit, bool editPossible, bool tryEdit, bool tryCoauth, out Configuration <T> configuration)
        {
            if (file == null)
            {
                throw new FileNotFoundException(FilesCommonResource.ErrorMassage_FileNotFound);
            }
            if (!string.IsNullOrEmpty(file.Error))
            {
                throw new Exception(file.Error);
            }

            var rightToReview  = rightToEdit;
            var reviewPossible = editPossible;

            var rightToFillForms  = rightToEdit;
            var fillFormsPossible = editPossible;

            var rightToComment  = rightToEdit;
            var commentPossible = editPossible;

            var rightModifyFilter = rightToEdit;

            if (linkRight == FileShare.Restrict && UserManager.GetUsers(AuthContext.CurrentAccount.ID).IsVisitor(UserManager))
            {
                rightToEdit      = false;
                rightToReview    = false;
                rightToFillForms = false;
                rightToComment   = false;
            }

            var fileSecurity = FileSecurity;

            rightToEdit = rightToEdit &&
                          (linkRight == FileShare.ReadWrite || linkRight == FileShare.CustomFilter ||
                           fileSecurity.CanEdit(file) || fileSecurity.CanCustomFilterEdit(file));
            if (editPossible && !rightToEdit)
            {
                editPossible = false;
            }

            rightModifyFilter = rightModifyFilter &&
                                (linkRight == FileShare.ReadWrite ||
                                 fileSecurity.CanEdit(file));

            rightToRename = rightToRename && rightToEdit && fileSecurity.CanEdit(file);

            rightToReview = rightToReview &&
                            (linkRight == FileShare.Review || linkRight == FileShare.ReadWrite ||
                             fileSecurity.CanReview(file));
            if (reviewPossible && !rightToReview)
            {
                reviewPossible = false;
            }

            rightToFillForms = rightToFillForms &&
                               (linkRight == FileShare.FillForms || linkRight == FileShare.Review || linkRight == FileShare.ReadWrite ||
                                fileSecurity.CanFillForms(file));
            if (fillFormsPossible && !rightToFillForms)
            {
                fillFormsPossible = false;
            }

            rightToComment = rightToComment &&
                             (linkRight == FileShare.Comment || linkRight == FileShare.Review || linkRight == FileShare.ReadWrite ||
                              fileSecurity.CanComment(file));
            if (commentPossible && !rightToComment)
            {
                commentPossible = false;
            }

            if (linkRight == FileShare.Restrict &&
                !(editPossible || reviewPossible || fillFormsPossible || commentPossible) &&
                !fileSecurity.CanRead(file))
            {
                throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_ReadFile);
            }

            if (file.RootFolderType == FolderType.TRASH)
            {
                throw new Exception(FilesCommonResource.ErrorMassage_ViewTrashItem);
            }

            if (file.ContentLength > SetupInfo.AvailableFileSize)
            {
                throw new Exception(string.Format(FilesCommonResource.ErrorMassage_FileSizeEdit, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize)));
            }

            string strError = null;

            if ((editPossible || reviewPossible || fillFormsPossible || commentPossible) &&
                LockerManager.FileLockedForMe(file.ID))
            {
                if (tryEdit)
                {
                    strError = FilesCommonResource.ErrorMassage_LockedFile;
                }
                rightToRename    = false;
                rightToEdit      = editPossible = false;
                rightToReview    = reviewPossible = false;
                rightToFillForms = fillFormsPossible = false;
                rightToComment   = commentPossible = false;
            }

            if (editPossible &&
                !FileUtility.CanWebEdit(file.Title))
            {
                rightToEdit = editPossible = false;
            }

            if (file.Encrypted &&
                file.RootFolderType != FolderType.Privacy)
            {
                rightToEdit      = editPossible = false;
                rightToReview    = reviewPossible = false;
                rightToFillForms = fillFormsPossible = false;
                rightToComment   = commentPossible = false;
            }


            if (!editPossible && !FileUtility.CanWebView(file.Title))
            {
                throw new Exception(string.Format("{0} ({1})", FilesCommonResource.ErrorMassage_NotSupportedFormat, FileUtility.GetFileExtension(file.Title)));
            }

            if (reviewPossible &&
                !FileUtility.CanWebReview(file.Title))
            {
                rightToReview = reviewPossible = false;
            }

            if (fillFormsPossible &&
                !FileUtility.CanWebRestrictedEditing(file.Title))
            {
                rightToFillForms = fillFormsPossible = false;
            }

            if (commentPossible &&
                !FileUtility.CanWebComment(file.Title))
            {
                rightToComment = commentPossible = false;
            }

            var rightChangeHistory = rightToEdit && !file.Encrypted;

            if (FileTracker.IsEditing(file.ID))
            {
                rightChangeHistory = false;

                bool coauth;
                if ((editPossible || reviewPossible || fillFormsPossible || commentPossible) &&
                    tryCoauth &&
                    (!(coauth = FileUtility.CanCoAuhtoring(file.Title)) || FileTracker.IsEditingAlone(file.ID)))
                {
                    if (tryEdit)
                    {
                        var editingBy = FileTracker.GetEditingBy(file.ID).FirstOrDefault();
                        strError = string.Format(!coauth
                                                     ? FilesCommonResource.ErrorMassage_EditingCoauth
                                                     : FilesCommonResource.ErrorMassage_EditingMobile,
                                                 Global.GetUserName(editingBy, true));
                    }
                    rightToEdit = editPossible = reviewPossible = fillFormsPossible = commentPossible = false;
                }
            }

            var fileStable = file;

            if (lastVersion && file.Forcesave != ForcesaveType.None && tryEdit)
            {
                var fileDao = DaoFactory.GetFileDao <T>();
                fileStable = fileDao.GetFileStable(file.ID, file.Version);
            }

            var docKey    = GetDocKey(fileStable);
            var modeWrite = (editPossible || reviewPossible || fillFormsPossible || commentPossible) && tryEdit;

            configuration = new Configuration <T>(file, ServiceProvider)
            {
                Document =
                {
                    Key         = docKey,
                    Permissions =
                    {
                        Edit          = rightToEdit && lastVersion,
                        Rename        = rightToRename && lastVersion && !file.ProviderEntry,
                        Review        = rightToReview && lastVersion,
                        FillForms     = rightToFillForms && lastVersion,
                        Comment       = rightToComment && lastVersion,
                        ChangeHistory = rightChangeHistory,
                        ModifyFilter  = rightModifyFilter
                    }
                },
                EditorConfig =
                {
                    ModeWrite = modeWrite,
                },
                ErrorMessage = strError,
            };

            if (!lastVersion)
            {
                configuration.Document.Title += string.Format(" ({0})", file.CreateOnString);
            }

            return(file);
        }
Esempio n. 3
0
        public static File GetParams(File file, bool lastVersion, bool checkLink, bool itsNew, bool editPossible, bool rightToEdit, bool tryEdit, out DocumentServiceParams docServiceParams)
        {
            if (!TenantExtra.GetTenantQuota().DocsEdition)
            {
                throw new Exception(FilesCommonResource.ErrorMassage_PayTariffDocsEdition);
            }

            if (!checkLink && CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor())
            {
                rightToEdit = false;
            }

            if (file == null)
            {
                throw new FileNotFoundException(FilesCommonResource.ErrorMassage_FileNotFound);
            }

            if (!string.IsNullOrEmpty(file.Error))
            {
                throw new Exception(file.Error);
            }

            if (!checkLink)
            {
                rightToEdit = rightToEdit && Global.GetFilesSecurity().CanEdit(file);
                if (editPossible && !rightToEdit)
                {
                    editPossible = false;
                }

                if (!editPossible && !Global.GetFilesSecurity().CanRead(file))
                {
                    throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_ReadFile);
                }
            }

            if (file.RootFolderType == FolderType.TRASH)
            {
                throw new Exception(FilesCommonResource.ErrorMassage_ViewTrashItem);
            }

            rightToEdit = rightToEdit && !EntryManager.FileLockedForMe(file.ID);
            if (editPossible && !rightToEdit)
            {
                editPossible = false;
            }

            rightToEdit = rightToEdit && (!FileTracker.IsEditing(file.ID) ||
                                          FileUtility.CanCoAuhtoring(file.Title) && !FileTracker.IsEditingAlone(file.ID));
            if (editPossible && !rightToEdit)
            {
                editPossible = false;
            }

            rightToEdit = rightToEdit && FileUtility.CanWebEdit(file.Title);
            if (editPossible && !rightToEdit)
            {
                editPossible = false;
            }

            if (!editPossible && !FileUtility.CanWebView(file.Title))
            {
                throw new Exception(FilesCommonResource.ErrorMassage_NotSupportedFormat);
            }

            var versionForKey = file.Version;

            if (!FileTracker.FixedVersion(file.ID))
            {
                versionForKey++;
            }

            //CreateNewDoc
            if (itsNew && file.Version == 1 && file.ConvertedType != null && file.CreateOn == file.ModifiedOn)
            {
                versionForKey = 1;
            }

            var docKey    = GetDocKey(file.ID, versionForKey, file.ProviderEntry ? file.ModifiedOn : file.CreateOn);
            var modeWrite = editPossible && tryEdit;

            docServiceParams = new DocumentServiceParams
            {
                File      = file,
                Key       = docKey,
                CanEdit   = rightToEdit && lastVersion,
                ModeWrite = modeWrite,
            };

            return(file);
        }
        public static File GetParams(File file, bool lastVersion, FileShare linkRight, bool rightToRename, bool rightToEdit, bool editPossible, bool tryEdit, bool tryCoauth, out Configuration configuration)
        {
            if (file == null)
            {
                throw new FileNotFoundException(FilesCommonResource.ErrorMassage_FileNotFound);
            }
            if (!string.IsNullOrEmpty(file.Error))
            {
                throw new Exception(file.Error);
            }

            var rightToReview  = rightToEdit;
            var reviewPossible = editPossible;

            if (linkRight == FileShare.Restrict && CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor())
            {
                rightToEdit   = false;
                rightToReview = false;
            }

            var fileSecurity = Global.GetFilesSecurity();

            rightToEdit = rightToEdit &&
                          (linkRight == FileShare.ReadWrite ||
                           fileSecurity.CanEdit(file));
            if (editPossible && !rightToEdit)
            {
                editPossible = false;
            }

            rightToRename = rightToRename && rightToEdit && fileSecurity.CanEdit(file);

            rightToReview = rightToReview &&
                            (linkRight == FileShare.Review || linkRight == FileShare.ReadWrite ||
                             fileSecurity.CanReview(file));
            if (reviewPossible && !rightToReview)
            {
                reviewPossible = false;
            }

            if (linkRight == FileShare.Restrict &&
                !(editPossible || reviewPossible) &&
                !fileSecurity.CanRead(file))
            {
                throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_ReadFile);
            }


            if (file.RootFolderType == FolderType.TRASH)
            {
                throw new Exception(FilesCommonResource.ErrorMassage_ViewTrashItem);
            }

            if (file.ContentLength > SetupInfo.AvailableFileSize)
            {
                throw new Exception(string.Format(FilesCommonResource.ErrorMassage_FileSizeEdit, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize)));
            }

            string strError = null;

            if ((editPossible || reviewPossible) &&
                EntryManager.FileLockedForMe(file.ID))
            {
                strError       = FilesCommonResource.ErrorMassage_LockedFile;
                rightToRename  = false;
                rightToEdit    = false;
                editPossible   = false;
                reviewPossible = false;
            }

            if (editPossible &&
                !FileUtility.CanWebEdit(file.Title))
            {
                rightToEdit = editPossible = false;
            }

            if (!editPossible && !FileUtility.CanWebView(file.Title))
            {
                throw new Exception(string.Format("{0} ({1})", FilesCommonResource.ErrorMassage_NotSupportedFormat, FileUtility.GetFileExtension(file.Title)));
            }

            if (reviewPossible &&
                !FileUtility.CanWebReview(file.Title))
            {
                rightToReview = reviewPossible = false;
            }

            var rightChangeHistory = rightToEdit;

            if (FileTracker.IsEditing(file.ID))
            {
                rightChangeHistory = false;

                bool coauth;
                if ((editPossible || reviewPossible) &&
                    tryCoauth &&
                    (!(coauth = FileUtility.CanCoAuhtoring(file.Title)) || FileTracker.IsEditingAlone(file.ID)))
                {
                    if (tryEdit)
                    {
                        var editingBy = FileTracker.GetEditingBy(file.ID).FirstOrDefault();
                        strError = string.Format(!coauth
                                                     ? FilesCommonResource.ErrorMassage_EditingCoauth
                                                     : FilesCommonResource.ErrorMassage_EditingMobile,
                                                 Global.GetUserName(editingBy));
                    }
                    rightToEdit = editPossible = reviewPossible = false;
                }
            }

            var docKey    = GetDocKey(file);
            var modeWrite = (editPossible || reviewPossible) && tryEdit;

            configuration = new Configuration(file)
            {
                Document =
                {
                    Key         = docKey,
                    Permissions =
                    {
                        Edit          = rightToEdit && lastVersion,
                        Rename        = rightToRename && lastVersion && !file.ProviderEntry,
                        Review        = rightToReview && lastVersion,
                        ChangeHistory = rightChangeHistory,
                    }
                },
                EditorConfig =
                {
                    ModeWrite = modeWrite,
                },
                ErrorMessage = strError,
            };

            return(file);
        }
        private void PageLoad()
        {
            var editPossible = !RequestEmbedded;
            var isExtenral   = false;

            File file;
            var  fileUri = string.Empty;

            try
            {
                if (string.IsNullOrEmpty(RequestFileUrl))
                {
                    _fileNew = (Request["new"] ?? "") == "true";

                    var app = ThirdPartySelector.GetAppByFileId(RequestFileId);
                    if (app == null)
                    {
                        var ver = string.IsNullOrEmpty(Request[FilesLinkUtility.Version]) ? -1 : Convert.ToInt32(Request[FilesLinkUtility.Version]);

                        file = DocumentServiceHelper.GetParams(RequestFileId, ver, RequestShareLinkKey, _fileNew, editPossible, !RequestView, out _docParams);

                        _fileNew = _fileNew && file.Version == 1 && file.CreateOn == file.ModifiedOn;
                    }
                    else
                    {
                        isExtenral = true;

                        bool editable;
                        _thirdPartyApp = true;
                        file           = app.GetFile(RequestFileId, out editable);
                        file           = DocumentServiceHelper.GetParams(file, true, true, true, editable, editable, editable, editable, out _docParams);

                        _docParams.FileUri   = app.GetFileStreamUrl(file);
                        _docParams.FolderUrl = string.Empty;
                    }
                }
                else
                {
                    isExtenral = true;

                    fileUri = RequestFileUrl;
                    var fileTitle = Request[FilesLinkUtility.FileTitle];
                    if (string.IsNullOrEmpty(fileTitle))
                    {
                        fileTitle = Path.GetFileName(HttpUtility.UrlDecode(fileUri)) ?? "";
                    }

                    if (CoreContext.Configuration.Standalone)
                    {
                        try
                        {
                            var webRequest = (HttpWebRequest)WebRequest.Create(RequestFileUrl);

                            // hack. http://ubuntuforums.org/showthread.php?t=1841740
                            if (WorkContext.IsMono)
                            {
                                ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
                            }

                            using (var response = webRequest.GetResponse())
                                using (var responseStream = new ResponseStream(response))
                                {
                                    var externalFileKey = DocumentServiceConnector.GenerateRevisionId(RequestFileUrl);
                                    fileUri = DocumentServiceConnector.GetExternalUri(responseStream, MimeMapping.GetMimeMapping(fileTitle), externalFileKey);
                                }
                        }
                        catch (Exception error)
                        {
                            Global.Logger.Error("Cannot receive external url for \"" + RequestFileUrl + "\"", error);
                        }
                    }

                    file = new File
                    {
                        ID    = RequestFileUrl,
                        Title = Global.ReplaceInvalidCharsAndTruncate(fileTitle)
                    };

                    file = DocumentServiceHelper.GetParams(file, true, true, true, false, false, false, false, out _docParams);
                    _docParams.CanEdit   = editPossible && !CoreContext.Configuration.Standalone;
                    _docParams.CanReview = _docParams.CanEdit;
                    _editByUrl           = true;

                    _docParams.FileUri = fileUri;
                }
            }
            catch (Exception ex)
            {
                Global.Logger.Error("DocEditor", ex);
                _errorMessage = ex.Message;
                return;
            }

            if (_docParams.ModeWrite && FileConverter.MustConvert(file))
            {
                try
                {
                    file = FileConverter.ExecDuplicate(file, RequestShareLinkKey);
                }
                catch (Exception ex)
                {
                    _docParams = null;
                    Global.Logger.Error("DocEditor", ex);
                    _errorMessage = ex.Message;
                    return;
                }

                var comment = "#message/" + HttpUtility.UrlEncode(FilesCommonResource.CopyForEdit);

                Response.Redirect(FilesLinkUtility.GetFileWebEditorUrl(file.ID) + comment);
                return;
            }

            Title = file.Title;

            if (string.IsNullOrEmpty(_docParams.FolderUrl))
            {
                _docParams.FolderUrl = Request[FilesLinkUtility.FolderUrl] ?? "";
            }
            if (MobileDetector.IsRequestMatchesMobile(true))
            {
                _docParams.FolderUrl = string.Empty;
            }

            if (RequestEmbedded)
            {
                _docParams.Type = DocumentServiceParams.EditorType.Embedded;

                var shareLinkParam = "&" + FilesLinkUtility.DocShareKey + "=" + RequestShareLinkKey;
                _docParams.ViewerUrl   = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.FilesBaseAbsolutePath + FilesLinkUtility.EditorPage + "?" + FilesLinkUtility.Action + "=view" + shareLinkParam);
                _docParams.DownloadUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.FileHandlerPath + "?" + FilesLinkUtility.Action + "=download" + shareLinkParam);
                _docParams.EmbeddedUrl = CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.FilesBaseAbsolutePath + FilesLinkUtility.EditorPage + "?" + FilesLinkUtility.Action + "=embedded" + shareLinkParam);
            }
            else
            {
                _docParams.Type = IsMobile ? DocumentServiceParams.EditorType.Mobile : DocumentServiceParams.EditorType.Desktop;

                if (FileSharing.CanSetAccess(file))
                {
                    _docParams.SharingSettingsUrl = CommonLinkUtility.GetFullAbsolutePath(Share.Location + "?" + FilesLinkUtility.FileId + "=" + file.ID);
                }
            }

            if (!isExtenral)
            {
                _docKeyForTrack = DocumentServiceHelper.GetDocKey(file.ID, -1, DateTime.MinValue);

                FileMarker.RemoveMarkAsNew(file);
            }

            if (_docParams.ModeWrite)
            {
                _tabId        = FileTracker.Add(file.ID, _fileNew);
                _fixedVersion = FileTracker.FixedVersion(file.ID);
                if (SecurityContext.IsAuthenticated)
                {
                    _docParams.FileChoiceUrl  = CommonLinkUtility.GetFullAbsolutePath(FileChoice.Location) + "?" + FileChoice.ParamFilterExt + "=xlsx&" + FileChoice.MailMergeParam + "=true";
                    _docParams.MergeFolderUrl = CommonLinkUtility.GetFullAbsolutePath(MailMerge.GetUrl);
                }
            }
            else
            {
                if (!RequestView && FileTracker.IsEditingAlone(file.ID))
                {
                    var editingBy = FileTracker.GetEditingBy(file.ID).FirstOrDefault();
                    _errorMessage = string.Format(FilesCommonResource.ErrorMassage_EditingMobile, Global.GetUserName(editingBy));
                }

                _docParams.LinkToEdit = _editByUrl
                                            ? CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorExternalUrl(fileUri, file.Title))
                                            : CommonLinkUtility.GetFullAbsolutePath(FilesLinkUtility.GetFileWebEditorUrl(file.ID));

                if (FileConverter.MustConvert(_docParams.File))
                {
                    _editByUrl = true;
                }
            }
        }
Esempio n. 6
0
        public static File GetParams(File file, bool lastVersion, bool checkLink, bool itsNew, bool editPossible, bool rightToEdit, bool rightToReview, bool tryEdit, out DocumentServiceParams docServiceParams)
        {
            if (!checkLink && CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor())
            {
                rightToEdit   = false;
                rightToReview = false;
            }

            if (file == null)
            {
                throw new FileNotFoundException(FilesCommonResource.ErrorMassage_FileNotFound);
            }

            if (!string.IsNullOrEmpty(file.Error))
            {
                throw new Exception(file.Error);
            }

            var fileSecurity   = Global.GetFilesSecurity();
            var reviewPossible = editPossible;

            if (!checkLink)
            {
                rightToEdit = rightToEdit && fileSecurity.CanEdit(file);
                if (editPossible && !rightToEdit)
                {
                    editPossible = false;
                }

                rightToReview = rightToReview && fileSecurity.CanReview(file);
                if (reviewPossible && !rightToReview)
                {
                    reviewPossible = false;
                }

                if (!(editPossible || reviewPossible) && !fileSecurity.CanRead(file))
                {
                    throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_ReadFile);
                }
            }

            if (file.RootFolderType == FolderType.TRASH)
            {
                throw new Exception(FilesCommonResource.ErrorMassage_ViewTrashItem);
            }

            if (file.ContentLength > SetupInfo.AvailableFileSize)
            {
                throw new Exception(string.Format(FilesCommonResource.ErrorMassage_FileSizeEdit, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize)));
            }

            if ((editPossible || reviewPossible) &&
                EntryManager.FileLockedForMe(file.ID))
            {
                rightToEdit = editPossible = reviewPossible = false;
            }

            if ((editPossible || reviewPossible) &&
                FileTracker.IsEditing(file.ID) &&
                (!FileUtility.CanCoAuhtoring(file.Title) || FileTracker.IsEditingAlone(file.ID)))
            {
                rightToEdit = editPossible = reviewPossible = false;
            }

            if (editPossible &&
                !FileUtility.CanWebEdit(file.Title))
            {
                rightToEdit = editPossible = false;
            }

            if (!editPossible && !FileUtility.CanWebView(file.Title))
            {
                throw new Exception(FilesCommonResource.ErrorMassage_NotSupportedFormat);
            }

            rightToReview = rightToReview && reviewPossible && FileUtility.CanWebReview(file.Title);

            var versionForKey = file.Version;

            //CreateNewDoc
            if ((itsNew || FileTracker.FixedVersion(file.ID)) && file.Version == 1 && file.CreateOn == file.ModifiedOn)
            {
                versionForKey = 0;
            }

            var docKey    = GetDocKey(file.ID, versionForKey, file.ProviderEntry ? file.ModifiedOn : file.CreateOn);
            var modeWrite = (editPossible || reviewPossible) && tryEdit;

            docServiceParams = new DocumentServiceParams
            {
                File      = file,
                Key       = docKey,
                CanEdit   = rightToEdit && lastVersion,
                ModeWrite = modeWrite,
                CanReview = rightToReview && lastVersion,
            };

            return(file);
        }