首页 > 开发 > 综合 > 正文

分享:Project级别的权限控制

2024-07-21 02:16:01
字体:
来源:转载
供稿:网友


在项目中常常要定义不同的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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表