权限系统的实现原理

QQ61507633 2009-04-11 12:19:50
把你平时使用的权限系统的实现原理以及重要代码写下来,没写过,有的发过来看下啊
...全文
163 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
aaajedll 2009-04-18
  • 打赏
  • 举报
回复
權限比較簡單,我看過ERP和HCP的權限,都差不多,只不過前2者分得比較細
一般都只分到模組,但是HCP分到了模組上的按鈕,但是個人覺得沒必要那么做
以上屬于個人意見
蓝海D鱼 2009-04-18
  • 打赏
  • 举报
回复
好 谢谢分享
3tzjq 2009-04-18
  • 打赏
  • 举报
回复

#region License
/// <summary>
/// 用户是否获得当前程序编号的许可证。
/// </summary>
/// <param name="type">受权类型</param>
/// <returns></returns>
public bool this[LicenseType type]
{
get
{
return IsLicense(type);
}
}

/// <summary>
/// 用户是否获得当前程序编号的访问许可证。
/// 不提示未受权消息!
/// </summary>
/// <returns>return IsLicense(LicenseType.Access,true);</returns>
public bool IsLicense()
{
return IsLicense(LicenseType.Access,true);
}

/// <summary>
/// 用户是否获得当前程序编号的许可证,默认许可类型是:访问
/// </summary>
/// <param name="type">受权类型</param>
/// <returns>if (type == LicenseType.Access ) bol =true; return IsLicense(type,bol);</returns>
public bool IsLicense(LicenseType type)
{
bool bol=false;
if (type == LicenseType.Access ) bol =true;//显示未受权消息
return IsLicense(type,bol);
}

/// <summary>
/// 用户是否获得当前程序编号的指定许可证
/// </summary>
/// <param name="type">许可类型</param>
/// <param name="IsShowUnlicenseInfo">是否提示未授权消息</param>
/// <returns>当前权限类型是否生效(无效则消息提示,一般为“访问权限”)</returns>
public bool IsLicense(LicenseType type, bool IsShowUnlicenseInfo)
{
if (Common.Common.get_CIMSReadOnly(false))
{
if (type == LicenseType.Access) return true;
return false;
}

bool result = false;
switch (type)
{
case LicenseType.Access:
result = Provider.AllowAccess;
break;
case LicenseType.NewRow:
result = Provider.AllowNew;
break;
case LicenseType.ModifyRow:
result = Provider.AllowEdit;
break;
case LicenseType.DeleteRow:
result = Provider.AllowDel;
break;
case LicenseType.IO:
result = Provider.AllowIO;
break;
case LicenseType.Query:
result = Provider.AllowQuery;
break;
case LicenseType.Report:
result = Provider.AllowReport;
break;
case LicenseType.Audit:
result = Provider.AllowAudit;
break;
default:
throw new UnauthorizedAccessException("未指定的权限类型!");
break;
}

if (result==false && IsShowUnlicenseInfo == true) UnlicensedInfo(type, "当前");

return result;
}

public static void UnlicensedInfo(LicenseType type,string ProgramCode)
{
if (Common.Common.get_CIMSReadOnly(false)) return;
string formText = ProgramCodes.GetFormText(ProgramCode);
if (string.IsNullOrEmpty(formText)) formText = type.ToString();
Common.ShareMembers.UnlicensedInfo(Common.Common.GetMasks(formText) + " 的" + LicenseString(type));
}

internal static void LicenseingErrorInfo(LicenseType type,string ErrorMessage)
{
if (Common.Common.CIMSReadOnly) return;

Common.Common.OnInfo("您没有 " + LicenseString(type) + " 权限!" + Common.ShareMembers.ConstString.LinkAdmin,InfoBase.InfoTypeEnum.Error);
MessageBox.Show(Common.InfoProvider.LateInfo + "\n\n" + ErrorMessage, "权限错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
#endregion
3tzjq 2009-04-18
  • 打赏
  • 举报
回复
public class LicenseProvider
{
protected LicenseItem m_licenseItem = null;
#region Init
/// <summary>
/// 实例化权限管理类,CurrentUserName,并使用默认程序权限
/// </summary>
public LicenseProvider()
{
m_licenseItem = new LicenseItem();
}

/// <summary>
/// 实例化权限管理类
/// </summary>
/// <param name="ProgramCode">程序模块</param>
public LicenseProvider(string ProgramCode)
{
m_licenseItem = new LicenseItem(ProgramCode);
}

/// <summary>
/// 实例化权限管理类
/// </summary>
/// <param name="ProgramCode">程序模块</param>
public LicenseProvider(ProgramCodes.Codes ProgramCode)
{
m_licenseItem = new LicenseItem(ProgramCode);
}

#endregion

#region Properties

/// <summary>
/// 获取或设置受当前权限管理的程序编码
/// </summary>
public ProgramCodes.Codes LicenseID
{
get{return this.m_licenseItem.LicenseID;}
set { this.Provider.LicenseID = value; }
}
protected LicenseItem Provider { get { return m_licenseItem; } }
/// <summary>
/// 指定访问类型的字段
/// </summary>
/// <param name="type">受权类型</param>
/// <returns></returns>
public static string LicenseString(LicenseType type)
{
switch (type)
{
case LicenseType.Access :
return Utils.COL_ACCESS;

case LicenseType.NewRow:
return Utils.COL_ADD;

case LicenseType.ModifyRow :
return Utils.COL_CHANGE;

case LicenseType.DeleteRow :
return Utils.COL_DELETE;

case LicenseType.Query :
return Utils.COL_QUERY;

case LicenseType.IO :
return Utils.COL_IO;

case LicenseType.Report :
return Utils.COL_REPORT;

case LicenseType.Audit:
return Utils.COL_AUDITING;

default:
return Utils.COL_ACCESS;
}
}

#endregion
3tzjq 2009-04-18
  • 打赏
  • 举报
回复
仅供参考:
 public class LicenseItem
{
protected ProgramCodes.Codes m_id = ProgramCodes.Codes.Null;
protected bool m_allowOpen = true, m_allowNew = false, m_allowEdit = false, m_allowDel = false, m_allowIO = true, m_allowQuery = true, m_allowReport = true, m_allowAudit = false;

public LicenseItem() { }
public LicenseItem(ProgramCodes.Codes licenseID)
{
m_id = licenseID;

this.InitLicense();
}
public LicenseItem(string licenseID)
{
m_id = (ProgramCodes.Codes)System.Enum.Parse(typeof(ProgramCodes.Codes),licenseID);

this.InitLicense();
}
/// <summary>
/// 获取或设置 权限编号
/// </summary>
public ProgramCodes.Codes LicenseID
{
get { return m_id; }
set
{
if (m_id == value) return;
m_id = value;

this.InitLicense();
}
}
///// <summary>
///// 获取或设置 当未获得指定的权限时,是否弹出对话框提示"未授权"
///// </summary>
// public Common.Common.DefaultBoolean ShowUnlicensedInfo
// { get { return m_popupMSGBOX; }
// set { m_popupMSGBOX = value; }
// }

protected void InitLicense()
{
if (Common.Common.get_CIMSReadOnly(false)) return;
if (LicenseID == ProgramCodes.Codes.Null) return;

try
{
//string paramName = "@CurrentUser";
string cmdText = string.Format("Select * From UserLicense Where {0} = @{0} And {1} = @{1}", Utils.COL_USER, Utils.COL_PROGRAMID);
SqlCommand cmd = Common.SqlHelper.AccountCommandProvider.CreateCommand(cmdText);
cmd.Parameters.Add("@" + Utils.COL_USER, SqlDbType.VarChar, 20).Value = Common.Common.CurrentUserName;
cmd.Parameters.Add("@" + Utils.COL_PROGRAMID, SqlDbType.VarChar, 50).Value = this.LicenseID.ToString();

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

if (dr.Read())
{
this.m_allowOpen = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_ACCESS], true));
this.m_allowNew = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_ADD], false));
this.m_allowEdit = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_CHANGE], false));
this.m_allowDel = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_DELETE], false));
this.m_allowIO = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_IO], true));
this.m_allowQuery = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_QUERY], true));
this.m_allowReport = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_REPORT], true));
this.m_allowAudit = Convert.ToBoolean(Common.Common.IsNull(dr[Utils.COL_AUDITING], true));
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
Common.SqlHelper.AccountProvider.Close();
}
}
/// <summary>
/// 获取是否拥有该模块的 访问 权限
/// </summary>
public bool AllowAccess
{
get
{
// if (m_allowOpen == false && ShowUnlicensedInfo != CIMS.Common.Common.DefaultBoolean.False) LicenseProvider.UnlicensedInfo(LicenseType.Access, this.LicenseID.ToString());
return m_allowOpen;
}
}
/// <summary>
/// 获取是否拥有该模块的 新增 权限
/// </summary>
public bool AllowNew { get {// if (m_allowNew == false && ShowUnlicensedInfo == CIMS.Common.Common.DefaultBoolean.True) LicenseProvider.UnlicensedInfo(LicenseType.NewRow, this.LicenseID.ToString());
return m_allowNew; } }
/// <summary>
/// 获取是否拥有该模块的 编辑 权限
/// </summary>
public bool AllowEdit { get { return m_allowEdit; } }
/// <summary>
/// 获取是否拥有该模块的 删除 权限
/// </summary>
public bool AllowDel { get { return m_allowDel; } }
/// <summary>
/// 获取是否拥有该模块的 数据导入/导出 权限
/// </summary>
public bool AllowIO { get { return m_allowIO; } }
/// <summary>
/// 获取是否拥有该模块的 查询 权限
/// </summary>
public bool AllowQuery { get { return m_allowQuery; } }
/// <summary>
/// 获取是否拥有该模块的 报表 权限
/// </summary>
public bool AllowReport { get { return m_allowReport; } }
/// <summary>
/// 获取是否拥有该模块的 审核 权限
/// </summary>
public bool AllowAudit { get { return m_allowAudit; } }

public override string ToString()
{
return string.Format("{0}:{1},{2},{3},{4},{5},{6},{7},{8}", LicenseID, AllowAccess, AllowNew, AllowEdit, AllowDel, AllowIO, AllowQuery, AllowReport, AllowAudit);
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj == this) return true;
if (obj is LicenseItem && ((LicenseItem)obj).LicenseID == this.LicenseID) return true;

return base.Equals(obj);
}
}
一、在基于Laravel8.x实现API接口签名认证系统课程里: 我将带领大家基于laravel 8.x来开发用户认证系统与接口签名验证系统以解决API接口请求中的安全问题,我将带领同学们认识Laravel用户认证的两大核心要素,守卫者与数据提供者,并从源码层面分析用户认证中涉及到的核心概念,通过基于接口签名的认证逻辑,带领同学们实现自定义守卫者以及签名认证器,实现基于签名认证的用户登陆逻辑,并基于该守卫者实现一个接口签名认证中间件对接口请求进行拦截处理。 在实战过程中,涉及到的核心概念我们会在源码层面对其原理进行阐述,以帮助同学们更好地掌握这些知识。 在完成上述功能后,我们会带领大家将我们实现的基于接口签名认证的用户认证与接口认证逻辑封装成Laravel扩展包,从而使得我们的代码与Laravel核心框架解耦,以保证功能上的独立性和可复用性。 二、在基于Laravel 7.x的后台权限验证API课程里: 以后台权限验证API的开发为载体,带领大家使用Laravel 7.x进行权限扩展包的开发,你将学习到如下知识: 1、如何使用laravel编写Restful api接口 2、如何使用composer进行项目依赖管理,laravel常用扩展的安装与使用,如dingo/api 以及repository 3、如何使用jwt进行实现后台用户认证机制 4、学习使用laravel扩展包的形式进行后台权限验证API的开发 5、如何编写Seeders帮助我们在新系统里实现数据的初始化 6、理解和使用Laravel核心概念和面向接口的编程思想 三、这两套课程分别解决的是API接口请求的安全问题与接口权限问题

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧