4

C# +SQL 存储过程 实现系统数据权限审查AOP效果 - hztech

 1 year ago
source link: https://www.cnblogs.com/hztech/p/17252527.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

C# +SQL 存储过程 实现系统数据权限审查AOP效果

  1、C/S系统架构

  2、前端 Extjs 

  3、后台C#

  4、数据库SQL

前端通过ajAx请求与后台通信。

前端应用页面统一继承入口类  BasePage

public partial class xxxxxxx :BasePage 
 {

    //y业务代码......
}

BasePage

public class BasePage : System.Web.UI.Page
{
     //在这里实现数据权限核查  

}

完成数据操作权限核查,并提醒前端

因为所有页面都继承了 BasePage 所以,现可以BasePage中加入代码。后台功能实现,因为要根据当前用户的ID 和所请求页面进行权限判断。

为提高效率可以存储过程中完成判断,并返回结果。

另外为便于管理和跟踪,判断结束后记录日志

 
-- =============================================
-- Author:        张立辉
-- Create date: 2023-3-24
-- Description:     系统权限控制 AOP
-- =============================================
create PROCEDURE [dbo].[Hztech_Aop] 
    @userID     as nvarchar(20),
    @URI as nvarchar(200),
    
    @result as nvarchar(20) output,
    @msg as nvarchar(200) output
    
     
AS
BEGIN
declare @roleid as int --角色ID
declare @location as int-- DataStore/ 的位置
declare @moduleAndPage as nvarchar(200)--模块/页面及请求参数
declare @module as nvarchar(50) --模块
declare @pageAndOptype as nvarchar(100)-- page
declare @page as nvarchar(100)-- page
declare @optype as nvarchar(50) --optype


declare @Edit   as bit
declare  @Del   as bit
declare    @Close  as bit
declare   @Lock   as bit
declare   @Unop as bit
declare   @Export as bit
declare @pageName as nvarchar(100)
declare @pass nvarchar(20)    
set @pass='refuse'

set @pageName='查询'
--1 通过用户id 获取角色 id
    select @roleid=isnull(roleid,0) from [xxxx_UserRoles] where UserID=@userID
    if(@roleid is null or @roleid=0)
    begin
        set @pass='refuse'
        set @msg='您未被授权进行任何操作。'
        set @result= 0
        goto logg
    end
--2 通过url 和 和角色ID 获取菜单权限
    --2.1 判断是否为 datastore请求
    set @location=charindex('xxxxxxxxxx/',@URI,0)
    if(@location<=0)
    begin
        set @msg='pass+log,非xxxxxxxxxx操作。'
        set @result= 1
        goto logg
    end
    --2.2 包含 DataStore/
    set   @moduleAndPage= SUBSTRING(@URI,@location+len('xxxxxxxxxxxx/'),len(@URI)-4)
    --解析URL 
    set @module=dbo.GetSplitOfIndex(@moduleAndPage,'/',1)--模块
    set @pageAndOptype=dbo.GetSplitOfIndex(@moduleAndPage,'/',2)--@pageAndOptype
    set @page=dbo.GetSplitOfIndex(@pageAndOptype,'?',1) --yyy.aspx?optype=xxxx
    set @optype=dbo.GetSplitOfIndex(@pageAndOptype,'?',2)-- optype=xxxx
    set @optype=dbo.GetSplitOfIndex(@optype,'=',2) -- xxxx   或者 xxxx&p=v
    if(CHARINDEX('&',@optype,0)>0) --包含多个参数
    begin
        set @optype=dbo.GetSplitOfIndex(@optype,'&',1) -- xxxx
    end
    
 
--3 判断是否需要控制权限
    /* optype  值范围
    
        *Submit --dialog  单表提交
        savePOBill  单据保存
      。。。。。。。

    */
    --3.1 查询操作
     if(@optype='getPobillAndDetail' or @optype='showfile' or charindex('Select',@optype,0)>0
     or @optype='WorkFlowApprovePobill' --审批提交
     )
        begin
            set @pass='log'
            set @msg='pass,非修改操作。'
            set @result= 1
            goto logg
        end
    --3.2 获取权限
      select  @Edit=[CanEdit]
       --其它权限省略
      from [permissionView] 
      where [Url] like '%/'+@module+'/%' and roleid=@roleid

    
--4 判断是否有 权限
    if    (charindex('Submit',@optype,0)>0--dialog  单表提交
        or @optype='savePOBill' --单据保存
         
        )
        begin---编辑权限
            if(@Edit=1)--可编辑
                begin
                    set @result= 1
             
                end
            else
            begin
                set @result=0
                 goto logg
            end
        end  
      ---其它权限核查 略
    。。。。。。。。。。
else set @result=0-- 其它情况 拒绝 refuse if(@result=1) set @pass='pass' --5 记录日志 logg: if(@result=0) set @msg='未授权进行当前操作'+@optype insert into sys_aoplog([code] ,[name] ,[uri] ,[verifyresult], module,[page],optype,remark) values (@userID,@pageName,@URI,@pass,@module,@page,@optype,@msg) --6 返回结果 return @result END

前端调用存储过程进行鉴权

  //判断权限
                   bool purCheck= dataOperate.ExeAopProc(aurl, currentUser.UserID.ToString(), out purMsg);
                   if (!purCheck)
                   {
                       string jsonlist1 = Common.ExtAjaxRequest.GetOperateRet("0", purCheck, purMsg);
                       Response.Write(jsonlist1);
                       Response.End();
                   }
3085698-20230324161839108-395451725.png

3085698-20230324162236248-547773609.png


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK