Ejemplo n.º 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            SPListItemId = Request["SPListItemId"];
            SPListURLDir = Request["SPListURLDir"];
            SPListId     = Request["SPListId"];
            SPSource     = Request["SPSource"];

            SPUserToken userToken;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        //check secret key
//==================================================================================
                        if (web.Properties["SharePointSecret"] == null)
                        {
                            var rnd      = new Random();
                            var spSecret = "";
                            for (var i = 0; i < 6; i++)
                            {
                                spSecret = spSecret + rnd.Next(1, 9).ToString();
                            }
                            web.AllowUnsafeUpdates = true;
                            web.Update();
                            web.Properties.Add("SharePointSecret", spSecret);
                            web.Properties.Update();
                            web.AllowUnsafeUpdates = true;
                            web.Update();
                        }
                        Secret = web.Properties["SharePointSecret"];

                        //read settings
//==================================================================================
                        if (web.Properties["DocumentServerHost"] != null)
                        {
                            DocumentSeverHost = web.Properties["DocumentServerHost"];
                        }
                        DocumentSeverHost += DocumentSeverHost.EndsWith("/") ? "" : "/";

                        // get current user ID and Name
//==================================================================================
                        userToken = web.AllUsers[0].UserToken;
                        SPSite s  = new SPSite(SPUrl, userToken);

                        var currentUserName = User.Identity.Name.Substring(User.Identity.Name.LastIndexOf("\\") + 1);
                        var users           = web.AllUsers;

                        for (var i = 0; i < users.Count; i++)
                        {
                            var userNameOfList = users[i].LoginName.Substring(users[i].LoginName.LastIndexOf("\\") + 1);
                            if (userNameOfList == currentUserName)
                            {
                                currentUser     = users[i];
                                CurrentUserId   = users[i].ID;
                                CurrentUserName = users[i].Name;
                                break;
                            }
                        }

                        //get language
//==================================================================================

                        var lcid           = (int)web.Language;
                        var defaultCulture = new CultureInfo(lcid);
                        lang = defaultCulture.IetfLanguageTag;

                        GoToBackText = LoadResource("GoToBack");


                        //get user/group roles
//==================================================================================
                        canEdit = CheckForEditing(SPUrl, SPListURLDir, currentUser);

                        //generate key and get file info for DocEditor
//==================================================================================
                        try
                        {
                            SPWeb w = s.OpenWeb();
                            //SPRoleAssignmentCollection ss = w.RoleAssignments;
                            SPList list     = w.GetList(SPListURLDir);
                            SPListItem item = list.GetItemById(Int32.Parse(SPListItemId));

                            SPFile file = item.File;

                            //SPBasePermissions bp =SPContext.Current.Web.GetUserEffectivePermissions(SPContext.Current.Web.CurrentUser.LoginName);

                            if (file != null)
                            {
                                Key = file.ETag;
                                Key = GenerateRevisionId(Key);

                                Folder   = Path.GetDirectoryName(file.ServerRelativeUrl);
                                Folder   = Folder.Replace("\\", "/");
                                GoToBack = SPUrl + Folder;

                                FileAuthor = file.Author.Name;

                                var tzi         = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id);
                                FileTimeCreated = TimeZoneInfo.ConvertTimeFromUtc(file.TimeCreated, tzi).ToString();

                                FileName = file.Name;

                                var tmp  = FileName.Split('.');
                                FileType = tmp[tmp.Length - 1];

                                //check document format
                                try
                                {
                                    if (FileUtility.CanViewTypes.Contains(FileType))
                                    {
                                        var canEditType = FileUtility.CanEditTypes.Contains(FileType);
                                        canEdit         = canEdit & canEditType;
                                        FileEditorMode  = canEdit == true ? "edit" : FileEditorMode;
                                        //documentType = FileUtility.docTypes[FileType];   DocType.GetDocType(FileName)
                                        documentType = FileUtility.GetDocType(FileType);
                                    }
                                    else
                                    {
                                        Response.Redirect(SPUrl);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //if a error - redirect to home page
                                    Log.LogError(ex.Message);
                                    Response.Redirect(SPUrl);
                                }
                            }
                            else
                            {
                                Response.Redirect(SPUrl);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.LogError(ex.Message);
                            Response.Redirect(SPUrl + "/_layouts/" + SPVersion + "error.aspx");
                        }
                    }
                }
            });

            //generate url hash
//==================================================================================
            urlDocDownload = Encryption.GetUrlHash(SPListItemId, Folder, SPListURLDir, "download", Secret);
            urlDocTrack    = Encryption.GetUrlHash(SPListItemId, Folder, SPListURLDir, "track", Secret);
        }
Ejemplo n.º 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            SPListItemId = Request["SPListItemId"];
            SPListURLDir = Request["SPListURLDir"];
            SPListId     = Request["SPListId"];
            SPSource     = Request["SPSource"];

            currentUser     = SPContext.Current.Web.CurrentUser;
            CurrentUserId   = currentUser.ID;
            CurrentUserName = currentUser.Name;

            if (currentUser == null)
            {
                Response.Redirect(SPUrl, true);
                return;
            }
            SPList     list = null;
            SPListItem item = null;

            try
            {
                list = SPContext.Current.Web.GetList(SPListURLDir);
                item = list.GetItemById(Int32.Parse(SPListItemId));
            }
            catch (NullReferenceException ex) { Log.LogError(ex.Message); }
            if (item == null)
            {
                Response.Redirect(SPUrl, true);
                return;
            }

            // Make sure user has permissions to view the item
            if (!item.DoesUserHavePermissions(currentUser, SPBasePermissions.ViewListItems))
            {
                Response.Redirect(SPUrl, true);
                return;
            }
            // To distinguish between commenters and viewers we have created a special reduced permission level - ViewOnly
            // It has no OpenItems base permissions. This way we distinguish between those who can comment and those who cannot.
            canComment = item.DoesUserHavePermissions(currentUser, SPBasePermissions.OpenItems);
            canEdit    = item.DoesUserHavePermissions(currentUser, SPBasePermissions.EditListItems);

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        //check secret key
                        //==================================================================================
                        if (web.Properties["SharePointSecret"] == null)
                        {
                            var rnd      = new Random();
                            var spSecret = "";
                            for (var i = 0; i < 6; i++)
                            {
                                spSecret = spSecret + rnd.Next(1, 9).ToString();
                            }
                            web.AllowUnsafeUpdates = true;
                            web.Update();
                            web.Properties.Add("SharePointSecret", spSecret);
                            web.Properties.Update();
                            web.AllowUnsafeUpdates = true;
                            web.Update();
                        }
                        Secret = web.Properties["SharePointSecret"];

                        //read settings
                        //==================================================================================
                        if (web.Properties["DocumentServerHost"] != null)
                        {
                            DocumentSeverHost = web.Properties["DocumentServerHost"];
                        }
                        DocumentSeverHost += DocumentSeverHost.EndsWith("/") ? "" : "/";

                        var lcid           = (int)web.Language;
                        var defaultCulture = new CultureInfo(lcid);
                        lang = defaultCulture.IetfLanguageTag;

                        GoToBackText = LoadResource("GoToBack");

                        //generate key and get file info for DocEditor
                        //==================================================================================
                        try
                        {
                            SPFile file = item.File;
                            if (file != null)
                            {
                                Key = file.ETag;
                                Key = GenerateRevisionId(Key);

                                Folder        = Path.GetDirectoryName(file.ServerRelativeUrl);
                                Folder        = Folder.Replace("\\", "/");
                                GoToBack      = host + Folder;
                                SPUser author = file.Author;
                                FileAuthor    = author.Name;

                                var tzi         = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id);
                                FileTimeCreated = TimeZoneInfo.ConvertTimeFromUtc(file.TimeCreated, tzi).ToString();

                                FileName = file.Name;

                                var tmp  = FileName.Split('.');
                                FileType = tmp[tmp.Length - 1];

                                //check document format
                                try
                                {
                                    if (FileUtility.CanViewTypes.Contains(FileType))
                                    {
                                        var canEditType = FileUtility.CanEditTypes.Contains(FileType);
                                        canEdit         = canEdit & canEditType;
                                        //FileEditorMode = canEdit == true ? "edit" : FileEditorMode;
                                        FileEditorMode = canComment == true ? "edit" : FileEditorMode;
                                        //documentType = FileUtility.docTypes[FileType];   DocType.GetDocType(FileName)
                                        documentType = FileUtility.GetDocType(FileType);
                                    }
                                    else
                                    {
                                        Response.Redirect(SPUrl);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //if a error - redirect to home page
                                    Log.LogError(ex.Message);
                                    Response.Redirect(SPUrl);
                                }
                            }
                            else
                            {
                                Response.Redirect(SPUrl);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.LogError(ex.Message);
                            Response.Redirect(SPUrl + "/_layouts/" + SPVersion + "error.aspx");
                        }
                    }
                }
            });

            //generate url hash
            //==================================================================================
            urlDocDownload = Encryption.GetUrlHash(SPListItemId, Folder, SPListURLDir, "download", Secret);
            urlDocTrack    = Encryption.GetUrlHash(SPListItemId, Folder, SPListURLDir, "track", Secret);
        }