Monday, March 8, 2010

ContentMangerAdminRightsCanAddUsers

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using Microsoft.SharePoint;using Microsoft.SharePoint.WebControls;using Microsoft.SharePoint.WebPartPages;using System.Collections.Generic;using System.Text;using Microsoft.SharePoint.Administration;using System.Xml;using System.Data.SqlClient;using System.Collections;using System.DirectoryServices;
namespace Sharek{ public partial class ContentManagementAdmin : System.Web.UI.Page { string sLoginName = null;
protected void Page_Load(object sender, EventArgs e) { LblMsg.Text = ""; LblMsg.Text = "Login User " + sLoginName; }
protected override void OnPreInit(EventArgs e) { base.OnPreInit(e);
//if (!HttpContext.Current.User.Identity.IsAuthenticated) //{ // //re-direct the user to log-in page and authenticate // Response.Redirect("/_layouts/accessdenied.aspx"); //} //else { sLoginName = HttpContext.Current.User.Identity.Name; // Added NewLy to get only login username sLoginName = ExtractUserName(sLoginName); // Response.Write(sLoginName);
if (!IsExistInAD(sLoginName)) { Response.Write(" User has no access "); } //Check user against AD group name "HelpDesk" ArrayList UserList = new ArrayList(); UserList = GetADGroupUsers("CACADeDocShareKSiteCollectionAdmin"); // UserList = GetADGroupUsers("CACADeDocShareK"); // UserList = GetADGroupUsers("CACADeDocDivision"); // UserList = GetADGroupUsers("CA/CAD/e-Doc Division"); //UserList = GetADGroupUsers("HelpDesk");
for (int i = 0; i <>"); string value = UserList[i] as string; value.ToUpper().Trim(); sLoginName.ToUpper().Trim(); // Response.Write("
"); // Response.Write("value :- " + value + "
"); // Response.Write("sLoginName :- " + sLoginName); if (value.ToUpper() == sLoginName.ToUpper()) { // LblMsg.Text = "Login User " + sLoginName; return; } } //Response.Write("U DON'T HAVE ACCESS"); Response.Redirect("/_layouts/accessdenied.aspx"); } }
protected void Button1_Click(object sender, EventArgs e) { try { LblMsg.Text = ""; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(TextBox1.Text)) { using (SPWeb subWeb = site.OpenWeb())// .Rootweb { subWeb.AllowUnsafeUpdates = true; SPUser UserId = subWeb.EnsureUser((TextBox2.Text)); string strGroup = null; foreach (SPGroup mySPGroup in subWeb.Groups) { foreach (SPRole mySPRole in mySPGroup.Roles) { if (mySPRole.Name == "Content Manager") { strGroup = mySPGroup.ToString(); } } } if (strGroup != null) { SPGroup spGroup = subWeb.SiteGroups[strGroup]; if (!IsUserAuthorized(strGroup, UserId)) { spGroup.AddUser(UserId); //You need to break role inheritence if you want to assign unique permission to subsite if (!subWeb.HasUniqueRoleAssignments) subWeb.BreakRoleInheritance(true); SPRoleAssignment roleAssignment = new SPRoleAssignment(spGroup); roleAssignment.RoleDefinitionBindings.Add(subWeb.RoleDefinitions["Content Manager"]); subWeb.RoleAssignments.Add(roleAssignment); subWeb.Update(); subWeb.AllowUnsafeUpdates = false; LblMsg.Text = "User " + UserId + " Added Succesfully"; } else { LblMsg.Text = "User " + UserId + " Already available"; } } } } }); } catch (Exception err) { // throw err; LblMsg.Text = err.Message; } } protected void Button2_Click(object sender, EventArgs e) { try { LblMsg.Text = ""; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite sps = new SPSite(TextBox1.Text)) { using (SPWeb spWeb = sps.OpenWeb()) { SPUser UserId = spWeb.EnsureUser((TextBox2.Text)); spWeb.AllowUnsafeUpdates = true; string strGrpName = null; foreach (SPGroup mySPGroup in spWeb.Groups) { //spWeb.CurrentUser. foreach (SPRole mySPRole in mySPGroup.Roles) { if (mySPRole.Name == "Content Manager") { //Retrieving all the user groups in the site/web SPGroupCollection userGroups = UserId.Groups; strGrpName = mySPGroup.ToString(); if (IsUserAuthorized(strGrpName, UserId) == true) { //Loops through the grops and check if the user is part of given group or not. foreach (SPGroup group in userGroups) { //Checking the group if (group.Name.Contains(strGrpName)) { strGrpName = mySPGroup.ToString(); mySPRole.Groups[strGrpName].RemoveUser(UserId); LblMsg.Text = "User " + UserId + " Remove Succesfully"; } } } else { LblMsg.Text = "User " + UserId + " could not be found"; } } } } spWeb.AllowUnsafeUpdates = false; } } }); } catch (Exception err) { LblMsg.Text = err.Message; } }
protected void Button3_Click(object sender, EventArgs e) { TextBox2.Text = ""; TextBox1.Text = ""; }

///// ///// Establish identity (principal) and culture for a thread. ///// //public static void SetCultureAndIdentity() //{ // AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); // WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal; // WindowsIdentity identity = (WindowsIdentity)principal.Identity; // System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); //}
public bool IsUserAuthorized(string groupName, SPUser currentUser) { SPGroupCollection userGroups = currentUser.Groups; //Loops through the grops and check if the user is part of given group or not. foreach (SPGroup group in userGroups) { //Checking the group if (group.Name.Contains(groupName)) return true; } return false; }
ArrayList GetADGroupUsers(string groupName) { ///////////////////
////SetCultureAndIdentity();

////////////////
SearchResult result; DirectorySearcher search = new DirectorySearcher(); // search.Filter = String.Format("(cn=)", groupName); // search.Filter = String.Format("(cn={0})", groupName); // search.Filter = "(&objectClass=" + groupName + ")"; search.Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))"; // search.Filter = "(&(objectClass=group) (cn={0})" + groupName + "))"; search.PropertiesToLoad.Add("member"); result = search.FindOne(); ArrayList userNames = new ArrayList(); if (result != null) {
for (int counter = 0; counter < user =" (string)result.Properties[" usern =" user.Split(',');" user =" userN[0].Replace(" cn=", " user =" userN[2].Replace(" dc=", " cn=", "> --USER N AF SPLIT--
"); //Response.Write(userN); userNames.Add(user);
} } // Response.Write(result + " -:USRR Name not Found. COUNT IS " + userNames.Count + " grp name:- " + groupName); return userNames;
}
string ExtractUserName(string path) { string[] userPath = path.Split(new char[] { '\\' }); return userPath[userPath.Length - 1]; } bool IsExistInAD(string loginName) { string userName = ExtractUserName(loginName); DirectorySearcher search = new DirectorySearcher(); search.Filter = String.Format("(SAMAccountName={0})", userName); search.PropertiesToLoad.Add("cn"); SearchResult result = search.FindOne();
if (result == null) { return false; } else { return true; } }
}}

-=======================-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="ContentManagementAdmin.aspx.cs" Inherits="Sharek.ContentManagementAdmin" %>





Content Mangement Rights assign Application

































Header

*

*

     







No comments:

Post a Comment