Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Adding Roles With Selected Permission In Custom Add Role ASpx Page

  Asked By: Landon    Date: Apr 08    Category: Sharepoint    Views: 3027

I have made a custom aspx form that lists down all the available
permission rights for all groups in a Grid, and provides a text field
for Site Group Name and a text area for its description. The purpose
of this form is to add new sitegroup in existing sitegroups
collection.

i am providing the complete code below. I have no issues in
populating existing permission rights to the datagrid but i will like
anyone to verify is that the right way to do this kind of job.
Secondly my issue is in retrieval of selected permissionrights as
SPRights objects obj1..n i.e obj1|obj2...objn. Although i
Successfully retrive selected rights but its in string format and no
cast is available for SPRights. i need a solution for retrieving
permission rights in the form of ( SPRights objects obj1..n i.e
obj1|obj2...objn )

button add the roles to existing roles collection fine when ever i
hard code the permissionMask parameter.

But I want to base permission mask on selection of particular rights
any suggestions for dynamically creating permission masks on the
basis of selected rights??

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;


namespace testcurrentUsersRoles
{
/// <summary>
/// Summary description for AddSiteGroup.
/// </summary>
public class AddSiteGroup : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox TextBox1;

private DataTable dt = null;
private DataRow dr = null;
protected System.Web.UI.WebControls.DataGrid
GroupGrid;
protected System.Web.UI.WebControls.Label Label1;
private Impersonator i = null;
private ArrayList lstPermissions = null;
protected System.Web.UI.WebControls.Label Label2;


private void Page_Load(object sender,
System.EventArgs e)
{
try
{

i = new Impersonator
("AdminUser","MYDomain","password");
i.Impersonate();



if(!Page.IsPostBack)
{
populateGrid();
}
}
catch(Exception ex)
{
Label1.Text = ex.Message;
}

}


private void populateGrid()
{
try
{
SPWeb myweb = SPControl.GetContextWeb
(Context);
SPRoleCollection roles = myweb.Roles;
//ArrayList lstPermissions =
GetPermissions(roles);
DataTable dtTemp = GetAllPermissions
();
dt = new DataTable();
dt.Columns.Add("d_PermissionName");
dt.Columns.Add("d_Description");
dt.Columns.Add("d_Value");
for(int j=0;j<dtTemp.Rows.Count;++j)
{
dr = dt.NewRow();
dr["d_PermissionName"] =
dtTemp.Rows[j]["Permission"];
dr["d_Description"] =
dtTemp.Rows[j]["Description"].ToString();
dr["d_Value"] =
Int32.Parse(dtTemp.Rows[j]["Value"].ToString());
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
GroupGrid.DataSource = dv;
GroupGrid.DataBind();

}
catch(Exception ex)
{
Label1.Text = ex.Message;
}
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the
ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not
modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler
(this.Button1_Click);
this.Load += new System.EventHandler
(this.Page_Load);

}
#endregion

private void Button1_Click(object sender,
System.EventArgs e)
{
try
{
ArrayList permissions =
GetSelectedPermissions();
SPWeb myweb = SPControl.GetContextWeb
(Context);
SPRoleCollection roles = myweb.Roles;

//SPRights rights = permissions;
roles.Add
("testRole","test...........",SPRights.ViewPages);
//myweb.Roles
["testRoles"].PermissionMask = SPRights.ManageRoles;

//UserGroups.UserGroup userGroups =
new UserGroups.UserGroup();
//userGroups.Credentials =
System.Net.CredentialCache.DefaultCredentials;
//ArrayList lstValues =
GetSelectedPermissionsValues();
//Label1.Text =
lstValues.Count.ToString();
//userGroups.AddRole
("testrole","test ......", Int32.Parse(lstValues[0].ToString()));
}
catch(Exception ex)
{
Label2.Text = ex.Message;
}
}



private DataTable GetAllPermissions()
{
try
{
DataTable dtPermissions = new
DataTable();
dtPermissions.Columns.Add
("Permission");
dtPermissions.Columns.Add
("Description");
dtPermissions.Columns.Add("Value");

//Permission One
DataRow drPermission =
dtPermissions.NewRow();
Label1.Text += "1";
drPermission["Permission"] =
SPRights.AddAndCustomizePages;
drPermission["Description"] = "Add,
change, or delete ASPX pages, HTML pages, or Web Part Pages, and edit
the Web site using a Windows SharePoint Services-compatible editor";
drPermission["Value"] = 0x00040000;
dtPermissions.Rows.Add(drPermission);

//Permission Two
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.AddDelPrivateWebParts;
drPermission["Description"] = "Add or
remove Web Parts on a personalized Web Part Page.";
drPermission["Value"] = 0x10000000;
dtPermissions.Rows.Add(drPermission);

//Permission Three
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.AddListItems;
drPermission["Description"] = "Add
items to lists, add documents to document libraries, and add Web
discussion comments.";
drPermission["Value"] = 0x00000002;
dtPermissions.Rows.Add(drPermission);

//Permission Four
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ApplyStyleSheets;
drPermission["Description"] = "Apply
a style sheet (.CSS file) to the Web site.";
drPermission["Value"] = 0x00100000;
dtPermissions.Rows.Add(drPermission);

//Permission Five
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ApplyThemeAndBorder;
drPermission["Description"] = "Apply
a theme or borders to the entire Web site.";
drPermission["Value"] = 0x00080000;
dtPermissions.Rows.Add(drPermission);

//Permission Six
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.BrowseDirectories;
drPermission["Description"] = "Browse
directories in a Web site.";
drPermission["Value"] = 0x04000000;
dtPermissions.Rows.Add(drPermission);


//Permission Seven
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.BrowseUserInfo;
drPermission["Description"] = "View
information about users. This right is not available through the user
interface.";
drPermission["Value"] = 0x08000000;
dtPermissions.Rows.Add(drPermission);


//Permission Eight
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.CancelCheckout;
drPermission["Description"] = "Check
in a document without saving the current changes.";
drPermission["Value"] = 0x00000100;
dtPermissions.Rows.Add(drPermission);


//Permission Nine
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.CreatePersonalGroups;
drPermission["Description"]
= "Create, change, and delete site groups, including adding users to
the site groups and specifying which rights are assigned to a site
group.";
drPermission["Value"] = 0x01000000;
dtPermissions.Rows.Add(drPermission);


//Permission Ten
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.CreateSSCSite;
drPermission["Description"] = "Create
a Web site using Self-Service Site Creation.";
drPermission["Value"] = 0x00400000;
dtPermissions.Rows.Add(drPermission);


//Permission Eleven
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.DeleteListItems;
drPermission["Description"] = "Delete
items from a list, documents from a document library, and Web
discussion comments in documents.";
drPermission["Value"] = 0x00000008;
dtPermissions.Rows.Add(drPermission);

//Permission Twelve
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.EditListItems;
drPermission["Description"] = "Edit
items in lists, edit documents in document libraries, edit Web
discussion comments in documents, and customize Web Part Pages in
document libraries.";
drPermission["Value"] = 0x00000004;
dtPermissions.Rows.Add(drPermission);


//Permission Thirteen
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.EmptyMask;
drPermission["Description"] = " Has
no permissions on the Web site. Not available through the user
interface. ";
drPermission["Value"] = 0x00000000;
dtPermissions.Rows.Add(drPermission);


//Permission Fourteen
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.FullMask;
drPermission["Description"] = "";
drPermission["Value"] = -1;
dtPermissions.Rows.Add(drPermission);


//Permission Fifteen
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ManageListPermissions;
drPermission["Description"] = "Has all permissions on
the Web site. Not available through the user interface.";
drPermission["Value"] = 0x00000400;
dtPermissions.Rows.Add(drPermission);


//Permission Sixteen
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ManageLists;
drPermission["Description"]
= "Approve content in lists, add or remove columns in a list, and add
or remove public views of a list.";
drPermission["Value"] = 0x00000800;
dtPermissions.Rows.Add(drPermission);


//Permission Seventeen
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ManagePersonalViews;
drPermission["Description"]
= "Create, change, and delete personal views of lists.";
drPermission["Value"] = 0x00000200;
dtPermissions.Rows.Add(drPermission);

//Permission Eighteen
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ManageRoles;
drPermission["Description"]
= "Create, change, and delete site groups, including adding users to
the site groups and specifying which rights are assigned to a site
group.";
drPermission["Value"] = 0x02000000;
dtPermissions.Rows.Add(drPermission);


//Permission Nineteen
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ManageSubwebs;
drPermission["Description"] = "Manage
or create subsites.";
drPermission["Value"] = 0x00800000;
dtPermissions.Rows.Add(drPermission);


//Permission Twenty
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ManageWeb;
drPermission["Description"] = "
Manage a site, including the ability to perform all administration
tasks for the site and manage contents and permissions ";
drPermission["Value"] = 0x40000000;
dtPermissions.Rows.Add(drPermission);


//Permission TwentyOne
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.OpenWeb;
drPermission["Description"] = "Open
the SharePoint Web site and get metadata related to the site, as well
as see the underlying navigation structure (not exposed in the user
interface). ";
drPermission["Value"] = 0x00010000;
dtPermissions.Rows.Add(drPermission);


//Permission TwentyTwo
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.UpdatePersonalWebParts;
drPermission["Description"] = "Update
Web Parts to display personalized information.";
drPermission["Value"] = 0x20000000;
dtPermissions.Rows.Add(drPermission);


//Permission TwentyThree
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ViewListItems;
drPermission["Description"] = "View
items in lists, documents in document libraries, view Web discussion
comments, and set up e-mail alerts for lists.";
drPermission["Value"] = 0x00000001;
dtPermissions.Rows.Add(drPermission);


//Permission TwentyFour
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ViewPages;
drPermission["Description"] = "View
pages in a Web site.";
drPermission["Value"] = 0x00020000;
dtPermissions.Rows.Add(drPermission);

//Permission TwentyFive
drPermission = dtPermissions.NewRow();
drPermission["Permission"] =
SPRights.ViewUsageData;
drPermission["Description"] = "View
reports on Web site usage.";
drPermission["Value"] = 0x00200000;
dtPermissions.Rows.Add(drPermission);

return dtPermissions;
}
catch(Exception ex)
{
Label1.Text = ex.Message;
return null;
}

}


private ArrayList GetSelectedPermissions()
{

ArrayList lstPermissionsValues = new ArrayList
();
if(GroupGrid != null)
{
foreach(DataGridItem dgi in
GroupGrid.Items)
{
CheckBox chkValues =
(CheckBox)dgi.Cells[0].FindControl("chkItem");
if(chkValues.Checked)
{

lstPermissionsValues.Add(dgi.Cells[1].Text);

}
}
}


return lstPermissionsValues;
}




}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 




Tagged: