Pages

Thursday, March 31, 2011

Roles And Permissions in C#

Tables:

Roles

Id      int
Name   Varchar(50)

AccessPermissions.cs file

using System;

using System.Collections.Generic;

using System.Text;
namespace Immigration

{

public class AccessPermission

{

public string UserID

{

get;

set;

}

public string LOGON_ID

{

get;

set;

}

public string PASSWORD

{

get;

set;

}

public string F_Name

{

get;

set;

}

public string EMAIL

{

get;

set;

}

public bool VIEWFLAG

{

get;

set;

}

public bool CREATEFLAG

{get;

set;

}public bool EDITFLAG

{

get;

set;

}

public bool DELETEFLAG

{

get;

set;

}

public string ROLEID

{

get;

set;

}

}

}

—————————–

AccessPermissionBL.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Xml.Linq;
using Immigration;
using DataAccess;
/// <summary>
/// Summary description for AgricultureProductionBL
/// </summary>
namespace Business
{
public class AccessPermissionBL
{
#region Private Varaiables
AccessPermission VAP = null;
AccessPermissionDA vAPDA = null;
#endregion
public AccessPermissionBL(AccessPermission VNewAP)
{
VAP = VNewAP;
vAPDA = new AccessPermissionDA(VNewAP);
}
public AccessPermissionBL()
{
vAPDA = new AccessPermissionDA();
}
public AccessPermission GetAccessLevelsOfUser()
{
return vAPDA.GetAccessLevelsOfUser();
}
}
}

——————————————————————————–

AccessPermissionDA.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DataAccess;
using System.Configuration;
using System.Data;
using Immigration;
using System.Data.SqlClient;
namespace DataAccess
{
public class AccessPermissionDA
{
#region Private Variables
#region privatevariables
SqlConnection con = new SqlConnection();
ConnectionDA objCon = new ConnectionDA();
Exception varEx = new Exception();
#endregion
AccessPermission VAP = null;
#endregion
public AccessPermissionDA(AccessPermission VNewAP)
{
con = objCon.GetConnection();
VAP = VNewAP;
}
public AccessPermissionDA()
{
con = objCon.GetConnection();
}
public AccessPermission GetAccessLevelsOfUser()
{
VAP.VIEWFLAG = false;
VAP.CREATEFLAG = false;
VAP.EDITFLAG = false;
VAP.DELETEFLAG = false;
DataTable dt_ListOfPerm = GetPermissionsOfUser(VAP.UserID);
if (dt_ListOfPerm != null)
{
foreach (DataRow dr in dt_ListOfPerm.Rows)
{
switch (dr["permission_id"].ToString())
{
case “1″:///************VIEW******///
VAP.VIEWFLAG = true;
break;
case “2″:///************Create New******///
VAP.CREATEFLAG = true;
break;
case “3″:///************Edit******///
VAP.EDITFLAG = true;
break;
case “4″:///************Delete Content ******///
VAP.DELETEFLAG = true;
break;
}
}
}
return VAP;
}
private DataTable GetPermissionsOfUser(string UserId)
{
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(“select R.* from Role_Permissions R,User_Master U where U.RoleId=R.RoleId and U.User_Id=@Id”, con))
{
da.SelectCommand.Parameters.AddWithValue(“@Id”, UserId);
da.Fill(dt);
}
return dt;
}
}
}

—————————————————

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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 System.Data.SqlClient;
using System.Globalization;
using System.IO;
using Immigration;
using Business;
public partial class Neweditprofile : System.Web.UI.Page
{
#region Private Variables
AccessPermission VAP = new AccessPermission();
AccessPermissionBL vAPBL = null;
string UserId;

MemberEditBL vMeditBL = null;
MemberEdit VarMedit = new MemberEdit();
MemberEdit VarMedit1 = new MemberEdit();
AccessLevel VarAL = new AccessLevel();
UserBL vUserBL = null;
User VarUser = new User();
ArrayList allowedroles = new ArrayList();
WorkLogBL vWorkLogbl;
UserLogin vlogin = new UserLogin();
WorkLog VarWorkLog = new WorkLog();
int count;
SSPSearchBL varSearch = new SSPSearchBL();
#endregion

#region Private Functions
private AccessPermission GetUserPermissions()
{
try
{
UserId = Request.Cookies["uid"].Value;
VAP.UserID = Request.Cookies["uid"].Value;
vAPBL = new AccessPermissionBL(VAP);
}
catch (Exception genEx)
{
logxml Error_xml = new logxml();
Error_xml.WriteError(genEx);
}
return vAPBL.GetAccessLevelsOfUser();
}

#endregion

protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["Logon"] != null)
{
if (Request.Cookies["uid"] != null)
{
VAP = GetUserPermissions();
if (!IsPostBack)
{
if ((VAP.DELETEFLAG) || (VAP.EDITFLAG) || (VAP.CREATEFLAG))
{
try
{
LoadStates();
}
catch (Exception genEx)
{logxml Error_xml = new logxml();
Error_xml.WriteError(genEx);
}
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), “Msg”, “<script>alert(‘You do not have permission to access this page’);window.location.href=’welcome.aspx’;</script>”, false);
}
}
}
Session["RedirectPage"] = null;
}
else
{
Session["RedirectPage"] = “Default.aspx”;
Response.Redirect(“~/Login.aspx”);
}
}

No comments:

Post a Comment