using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; //using System.Collections.Generic; using System.Data; using System.Web.UI.HtmlControls; using System.Text; using Microsoft.SharePoint.Administration; using System.Web.UI.WebControls;
namespace CoPAnnouncer { [Guid("0c6833bc-aa4a-4b77-b2c6-e6d81e263cec")] public class CoPAnnouncer : Microsoft.SharePoint.WebPartPages.WebPart { //private bool _error = false; //private string _myProperty = null; public DataTable tbl; int countDocList = 0; Label lblName = new Label(); HyperLink hlnk = new HyperLink();
public CoPAnnouncer() { this.ExportMode = WebPartExportMode.All; }
/// /// Create all your controls here for rendering. /// Try to avoid using the RenderWebPart() method. /// /// protected override void Render(HtmlTextWriter writer) { ///Creating the main table and adding step by step rows ///along with column and rendering the controls within the ///columns. writer.Write("
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();