国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

分享: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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通江县| 普定县| 平阴县| 塔河县| 保康县| 河间市| 达拉特旗| 高邑县| 郓城县| 怀集县| 东方市| 北辰区| 楚雄市| 修水县| 桐城市| 咸阳市| 博客| 马山县| 丰县| 略阳县| 道真| 岢岚县| 青岛市| 弥渡县| 邢台市| 延寿县| 蛟河市| 察隅县| 玛纳斯县| 抚远县| 新昌县| 策勒县| 永州市| 汝州市| 秀山| 进贤县| 镇远县| 香格里拉县| 丹寨县| 泌阳县| 缙云县|