檢查所有使用者對所有documentlibrary的許可權

來源:互聯網
上載者:User
using System;using System.ComponentModel;using System.Web.UI.WebControls.WebParts;using Microsoft.SharePoint;namespace ZY.VisualWebPart1{    [ToolboxItemAttribute(false)]    public partial class VisualWebPart1 : WebPart    {        System.Web.HttpResponse Response = null;        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready        // for production. Because the SecurityPermission attribute bypasses the security check for callers of        // your constructor, it's not recommended for production purposes.        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]        public VisualWebPart1()        {            Response = System.Web.HttpContext.Current.Response;           }        protected override void OnInit(EventArgs e)        {                        base.OnInit(e);            InitializeControl();        }        protected void Page_Load(object sender, EventArgs e)        {            System.Text.StringBuilder sb = new System.Text.StringBuilder();            if (!SPContext.Current.Site.RootWeb.CurrentUser.IsSiteAdmin)            {                //Response.Write("Permission deny!");                sb.AppendLine("Permission deny!");                Label1.Text = sb.ToString();                return;            }                        SPSecurity.CatchAccessDeniedException = false;            SPWeb Web = SPContext.Current.Site.RootWeb;            SPUser currentUser = Web.CurrentUser;            sb.AppendLine("<b>CurrentUser:" + currentUser.Name + "</b><br />===================begin====================<br />");            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()            {                System.Collections.Generic.List<SPDocumentLibrary> documentList = new System.Collections.Generic.List<SPDocumentLibrary>();                 using (SPSite site1 = new SPSite("https://www.abc.net.cn:8899/"))                {                    SPListCollection collLists = site1.RootWeb.Lists;                    foreach (SPList oList in collLists)                    {                        if (oList.BaseType == SPBaseType.DocumentLibrary)                        {                            SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oList;                            //var permissions=oDocumentLibrary.Permissions.GetEnumerator();                            var roles = oDocumentLibrary.AllRolesForCurrentUser;                            SPUser user = Web.CurrentUser;                            //SPMember member = Web.AllUsers[userPrefix + "sk\\zy"];                            if (!oDocumentLibrary.IsCatalog && oList.BaseTemplate != SPListTemplateType.XMLForm)                            {                                documentList.Add(oDocumentLibrary);                                /*                                SPListItemCollection collListItems = oDocumentLibrary.Items;                                Response.Write("<b> Document Library Name:" +oDocumentLibrary.Title + "</b><br>");                                SPRoleDefinitionBindingCollection roles0 = oDocumentLibrary.AllRolesForCurrentUser;                                Response.Write("DocumentLibrary.AllRolesForCurrentUser.Count:" + roles0.Count + "<br>");                                foreach (SPRoleDefinition role in roles0)                                {                                    Response.Write("++++" + role.Name + "<br>");                                }                                Response.Write("-------------------<br />");                                Response.Write("SPRoleAssignment.Member.Name:<br />");                                try                                {                                    SPRoleAssignmentCollection roleAssigments = oDocumentLibrary.RoleAssignments;                                    foreach (SPRoleAssignment item in roleAssigments)                                    {                                        Response.Write(item.Member.Name + "<br>");                                    }                                }                                catch (Exception ex)                                {                                    Response.Write(ex.GetType().ToString());                                }                                Response.Write(currentUser.Name + "<br />==============<br/>");                                */                            }                        }                    }                }                sb.AppendLine("Permission list for all users:");                SPUserCollection users = Web.AllUsers;                foreach (SPUser u in users)                {                    sb.AppendLine("<br />" + u.Name + "<br />");                    foreach (SPGroup item in u.Groups)                    {                        sb.AppendLine("++" + item.Name);                        sb.AppendLine("<br />");                    }                    //u.UserToken                    //documentList[0].Items[0].Title;                    using (SPSite site = new SPSite("https://www.abc.net.cn:8899/", u.UserToken))                    {                                                foreach(SPDocumentLibrary docLib in documentList)                        {                            sb.AppendLine(docLib.Title);                            try                            {                                //var o = site.RootWeb.Lists[docLib.ID].GetView(site.RootWeb.Lists[docLib.ID].DefaultView.ID);                                if (site.RootWeb.Lists[docLib.ID].DoesUserHavePermissions(SPBasePermissions.ViewFormPages                                                                    | SPBasePermissions.ViewListItems                                                                    | SPBasePermissions.Open                                                                    | SPBasePermissions.ViewPages))                                {                                    //Response.Write("<font color='green'>" +site.RootWeb.Lists[docLib.ID].Items.Count.ToString() + "</font>");                                    sb.AppendLine(" <font color='green'>Read</font>");                                }                                else                                {                                    sb.AppendLine("<font color='blue'>Deny</font>");                                }                                if (site.RootWeb.Lists[docLib.ID].DoesUserHavePermissions(SPBasePermissions.ViewPages |                                                             SPBasePermissions.Open |                                                             SPBasePermissions.AddListItems |                                                             SPBasePermissions.EditListItems |                                                             SPBasePermissions.ViewListItems |                                                             SPBasePermissions.OpenItems |                                                             SPBasePermissions.ViewVersions |                                                             SPBasePermissions.CreateAlerts |                                                             SPBasePermissions.ViewFormPages |                                                             SPBasePermissions.UseClientIntegration |                                                             SPBasePermissions.ManagePersonalViews |                                                             SPBasePermissions.ManagePersonalViews |                                                             SPBasePermissions.UpdatePersonalWebParts |                                                                 SPBasePermissions.ViewPages))                                {                                    //Response.Write("<font color='green'>" +site.RootWeb.Lists[docLib.ID].Items.Count.ToString() + "</font>");                                    sb.AppendLine(" <font color='green'>Contribute</font>");                                }                                else                                {                                    sb.AppendLine("<font color='blue'>Contribute Deny</font>");                                }                            }                            catch (Exception ex)                            {                                //Response.Write("<font color='red'>" + ex.Message + "</font>");                                sb.AppendLine("<font color='red'>Deny</font>");                            }                            sb.AppendLine("<br />");                        }                    }                }                sb.AppendLine("===================end====================<br />");                //The groups that the user has management privilege                /*                Response.Write("<br />");                Response.Write("<b>SPUser.OwnedGroups</b>");                Response.Write("<br />");                SPGroupCollection groups_u = currentUser.OwnedGroups;                foreach (SPGroup item in groups_u)                {                    Response.Write(item.Name);                    Response.Write("<br />");                }                Response.Write("=======================================<br />");                */                /*                Response.Write("<br />");                Response.Write("<b>SPGroupCollection.Groups</b>");                Response.Write("<br />");                SPGroupCollection u_Groups = currentUser.Groups;                foreach (SPGroup item in u_Groups)                {                    Response.Write(item.Name);                    Response.Write("<br />");                }                Response.Write("=======================================<br />");                */            });            Label1.Text = sb.ToString();            //Response.End();        }//end of pageload    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.