检查所有用户对所有documentlibrary的权限

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

    }

}

 

你可能感兴趣的:(document)