分享
 
 
 

分享:Project级别的权限控制

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

在项目中常常要定义不同的Project级别的用户和权限,仿照windows的Role/User/Access Right的控制,我的实现如下:

1、在数据库中建立5个表:tSvRole, tSvUser, tSvObject, tSvRoleUser和tSvRoleObject,分别存储Role、User、Object、Role-User对应关系以及Role-Object对应关系。建表的tsql如下:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tSvObject]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[tSvObject]

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tSvRole]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[tSvRole]

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tSvRoleObject]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[tSvRoleObject]

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tSvRoleUser]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[tSvRoleUser]

GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tSvUser]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[tSvUser]

GO

CREATE TABLE [dbo].[tSvObject] (

[fObjectId] [varchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[fObjectName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL

) ON [PRIMARY]

GO

CREATE TABLE [dbo].[tSvRole] (

[fRoleId] [varchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[fRoleName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL

) ON [PRIMARY]

GO

CREATE TABLE [dbo].[tSvRoleObject] (

[fRoleId] [varchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[fObjectId] [varchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[fVisible] [bit] NOT NULL ,

[fEnable] [bit] NOT NULL ,

[fExecutable] [bit] NOT NULL

) ON [PRIMARY]

GO

CREATE TABLE [dbo].[tSvRoleUser] (

[fRoleId] [varchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[fUserId] [varchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL

) ON [PRIMARY]

GO

CREATE TABLE [dbo].[tSvUser] (

[fUserId] [varchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[fUserName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[fUserPwd] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,

[fUserEmail] [varchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tSvObject] WITH NOCHECK ADD

CONSTRAINT [PK_tSvObject] PRIMARY KEY CLUSTERED

(

[fObjectId]

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tSvRole] WITH NOCHECK ADD

CONSTRAINT [PK_tSvPrjRole] PRIMARY KEY CLUSTERED

(

[fRoleId]

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tSvRoleObject] WITH NOCHECK ADD

CONSTRAINT [DF_tSvRoleObject_fVisible] DEFAULT (0) FOR [fVisible],

CONSTRAINT [DF_tSvRoleObject_fEnabled] DEFAULT (0) FOR [fEnable],

CONSTRAINT [DF_tSvRoleObject_fExecutable] DEFAULT (0) FOR [fExecutable],

CONSTRAINT [PK_tSvRoleObject] PRIMARY KEY CLUSTERED

(

[fRoleId],

[fObjectId]

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tSvRoleUser] WITH NOCHECK ADD

CONSTRAINT [PK_tSvRoleUser] PRIMARY KEY CLUSTERED

(

[fRoleId],

[fUserId]

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tSvUser] WITH NOCHECK ADD

CONSTRAINT [PK_tSvPrjUser] PRIMARY KEY CLUSTERED

(

[fUserId]

) ON [PRIMARY]

GO

2、在程序中读取数据,函数是:

static public DataSet GetAdminData(String strDatabaseConnectionString)

{

DataSet ds;

SqlConnection sqlConnection = new SqlConnection();

SqlCommand sqlCommand = new SqlCommand();

sqlConnection.ConnectionString = strDatabaseConnectionString;

sqlCommand.CommandText = '[spSvAdminData]';

sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

sqlCommand.Connection = sqlConnection;

ds = new DataSet();

sqlConnection.Open();

SqlDataAdapter adap = new SqlDataAdapter(sqlCommand);

adap.Fill(ds);

sqlConnection.Close();

ds.Tables[0].TableName = 'tRole';

ds.Tables[1].TableName = 'tUser';

ds.Tables[2].TableName = 'tObject';

ds.Tables[3].TableName = 'tRoleUser';

ds.Tables[4].TableName = 'tRoleObject';

return ds;

}

其中调用的stored procedure是:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spSvAdminData]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[spSvAdminData]

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_NULLS OFF

GO

CREATE PROCEDURE dbo.spSvAdminData AS

SELECT fRoleId, fRoleName

FROM tSvRole

ORDER BY fRoleId

SELECT fUserId, fUserName, fUserEmail

FROM tSvUser

ORDER BY fUserId

SELECT fObjectId, fObjectName

FROM tSvObject

ORDER BY fObjectId

SELECT fRoleId, fUserId

FROM tSvRoleUser

ORDER BY fRoleId, fUserId

SELECT fRoleId, fObjectId, fVisible, fEnable, fExecutable

FROM tSvRoleObject

ORDER BY fRoleId, fObjectId

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

3、读取权限,判断某User是否可以访问某Object的函数是:

static public bool GetAccessRight(DataSet dsAdmin,

String tablenameRole, String tablenameUser, String tablenameObject,

String tablenameRoleUser, String tablenameRoleObject,

String fieldnameRole, String fieldnameUser, String fieldnameObject, String fieldnameAccessRight,

String strUserId, String strObjectId)

{

int i;

DataRow[] datarowObjectRoleList;

datarowObjectRoleList = dsAdmin.Tables[tablenameRoleObject].Select(fieldnameObject+'=''+strObjectId+''');

if(datarowObjectRoleList.GetLength(0) == 0)

return true;

for(i=0;i<datarowObjectRoleList.GetLength(0);i++)

{

DataRow datarowObjectRole;

datarowObjectRole = datarowObjectRoleList[i];

bool boolObjectRoleAccessRight = Convert.ToBoolean(datarowObjectRole[fieldnameAccessRight].ToString());

if(boolObjectRoleAccessRight == true)

{

String strRoleId = datarowObjectRole[fieldnameRole].ToString();

DataRow[] datarowObjectRoleUa;

datarowObjectRoleUa = dsAdmin.Tables[tablenameRoleUser].Select(fieldnameRole + '='' + strRoleId + '' AND ' + fieldnameUser + '='' + strUserId + ''');

if(datarowObjectRoleUa.GetLength(0)>0)

return true;

}

}

return false;

}

这里的规则是:

A、如果此Object没有在Role-Object表中注册,则返回允许;

B、如果此User的任意一个Role在Role-Object表中注册了可访问此Object,则此User可访问此Object

C、否则禁止。

4、使用举例

在User管理页面,使用DataGrid列出User,使用DataGrid的Footer行作为添加User的地方,程序设定只有有“添加User权限”的人才会看到Footer行。如下:

UserGrid.ShowFooter = clsCommon.GetAccessRight(

dsAdmin, 'tRole', 'tUser', 'tObject', 'tRoleUser', 'tRoleObject',

'fRoleId', 'fUserId', 'fObjectId', 'fVisible',

Session['UserId'].ToString().Trim(), 'ObjUserGridFooter');

小结:本方法使用Database与程序结合的方式,实现了Project级别User/Object访问权限的控制。

本文原发表于http://community.csdn.net/Expert/topic/3143/3143459.xml

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有