1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Microsoft.SharePoint;
5: using UserGroupCheck.localhost;
6: using System.Xml;
7: using System.IO;
8:
9: namespace UserGroupCheck
10: { 11: class Program
12: { 13: private static localhost.UserGroup objUserGroup = null;
14: private static TextWriter oTxtWriter = null;
15: static void Main(string[] args)
16: { 17: try
18: { 19: oTxtWriter = new StreamWriter("UserGroupsAndRolesReport.txt"); 20:
21: Console.WriteLine("*************************************************************************************************"); 22: Console.WriteLine("Enter your choice for generating a report for getting user's groups and related roles information :"); 23: Console.WriteLine("*************************************************************************************************"); 24: Console.WriteLine("1. Site Collection Level"); 25: Console.WriteLine("2. Specific website level (Eg: only from a subsite which has unique permission)"); 26: Console.WriteLine("3. For a specific user (In this case you enter the login name of that user)"); 27: string strChoice = Console.ReadLine();
28:
29: Console.WriteLine("Enter your site URL (eg: http://servername/sitename)"); 30: string strURL = Console.ReadLine();
31:
32: objUserGroup = new UserGroup();
33: objUserGroup.Credentials = System.Net.CredentialCache.DefaultCredentials;
34: objUserGroup.Url = strURL + "/_vti_bin/UserGroup.asmx";
35:
36: switch (strChoice)
37: { 38: case "1": DecideAboutUsers(true);
39: break;
40: case "2": DecideAboutUsers(false);
41: break;
42: case "3": Console.WriteLine("Please enter login name of the user"); 43: RetrieveGroupsAndRoles(Console.ReadLine());
44: break;
45: }
46:
47: oTxtWriter.Close();
48: Console.WriteLine(@"operation has been completed successfully...please check the report - \UserGroupCheck\bin\Debug\UserGroupsAndRolesReport.txt");
49:
50: }
51: catch (Exception ex)
52: { 53: Console.WriteLine(ex.Message);
54: }
55: Console.ReadLine();
56: }
57:
58: private static void DecideAboutUsers(bool IsSiteCollection)
59: { 60: XmlNode xUsers = null;
61: if(IsSiteCollection)
62: xUsers = objUserGroup.GetUserCollectionFromSite();
63: else
64: xUsers = objUserGroup.GetAllUserCollectionFromWeb();
65:
66: XmlNodeList xUsersList = xUsers.ChildNodes;
67: foreach (XmlNode xUser in xUsersList)
68: { 69: XmlNodeReader oUserReader = new XmlNodeReader(xUser);
70: while (oUserReader.Read())
71: { 72: if (oUserReader["LoginName"] != null)
73: { 74: string strLoginName = oUserReader["LoginName"].ToString();
75: if (strLoginName != "NT AUTHORITY\\local service")
76: { 77: if (strLoginName != "SHAREPOINT\\system")
78: RetrieveGroupsAndRoles(strLoginName);
79: }
80: }
81: }
82: oUserReader.Close();
83: }
84: }
85:
86: private static void RetrieveGroupsAndRoles(string strLoginName)
87: { 88: XmlNode xGroups = objUserGroup.GetGroupCollectionFromUser(strLoginName);
89: XmlNodeList xGroup = xGroups.ChildNodes;
90:
91: // Finding the groups for this user and then will find the group roles
92: foreach (XmlNode oNode in xGroup)
93: { 94: if (oNode.HasChildNodes)
95: { 96: oTxtWriter.WriteLine("*************************************************************************************************"); 97: oTxtWriter.WriteLine(strLoginName + " was added under group. Please see the group(s) and its roles below");
98: oTxtWriter.WriteLine("*************************************************************************************************"); 99: XmlNodeReader oGreoupReader = new XmlNodeReader(oNode);
100: while (oGreoupReader.Read())
101: { 102: if (oGreoupReader["Name"] != null)
103: { 104: string strGroup = oGreoupReader["Name"].ToString();
105: oTxtWriter.WriteLine("Group :" + strGroup); 106: oTxtWriter.WriteLine();
107:
108: // find the group roles
109: XmlNode xGroupRoles = objUserGroup.GetRoleCollectionFromGroup(strGroup);
110: XmlNodeList xGroupRoleList = xGroupRoles.ChildNodes;
111: foreach (XmlNode oGroupRole in xGroupRoleList)
112: { 113: XmlNodeReader oGroupRoleReader = new XmlNodeReader(oGroupRole);
114: while (oGroupRoleReader.Read())
115: { 116: if (oGroupRoleReader["Name"] != null)
117: { 118: oTxtWriter.WriteLine("Role : " + oGroupRoleReader["Name"].ToString()); 119: oTxtWriter.WriteLine("Description : " + oGroupRoleReader["Description"].ToString()); 120: oTxtWriter.WriteLine("Type : " + oGroupRoleReader["Type"].ToString()); 121: oTxtWriter.WriteLine("BasePermissions : " + oGroupRoleReader["BasePermissions"].ToString()); 122: oTxtWriter.WriteLine();
123: }
124: }
125: }
126: }
127: }
128: }
129:
130: // else part means this user doesn't have a group but he will be having some roles, so we can list out that roles
131: else
132: { 133: oTxtWriter.WriteLine("*************************************************************************************************"); 134: oTxtWriter.WriteLine(strLoginName + " was not added under group. His roles and corrusponding details are listed below");
135: oTxtWriter.WriteLine("*************************************************************************************************"); 136: XmlNode xUserRoles = objUserGroup.GetRoleCollectionFromUser(strLoginName);
137: XmlNodeList xUserRoleList = xUserRoles.ChildNodes;
138: foreach (XmlNode xUserRole in xUserRoleList)
139: { 140: XmlNodeReader oUserRoleReader = new XmlNodeReader(xUserRole);
141: while (oUserRoleReader.Read())
142: { 143: if (oUserRoleReader["Name"] != null)
144: { 145: oTxtWriter.WriteLine("Role : " + oUserRoleReader["Name"].ToString()); 146: oTxtWriter.WriteLine("Description : " + oUserRoleReader["Description"].ToString()); 147: oTxtWriter.WriteLine("Type : " + oUserRoleReader["Type"].ToString()); 148: oTxtWriter.WriteLine("BasePermissions : " + oUserRoleReader["BasePermissions"].ToString()); 149: oTxtWriter.WriteLine();
150: }
151: }
152: }
153: }
154:
155: }
156:
157: }
158: }
159: }