///*************************************************************************/
///*
///* 文件名 :SystemAuthentication.cs
///* 程序说明 : 系统授权全局公共类
///* 原创作者 :孙中吕
///*
///* Copyright 2006-2021 C/S框架网 www.csframework.com
///*
///**************************************************************************/
using System;
using System.Data;
using System.Windows.Forms;
namespace CSFrameworkV5.Core.SystemSecurity
{
///
/// 系统授权全局公共类
///
public class SystemAuthentication
{
///
/// 当前用户的权限数据
///
private static DataTable _UserAuthorities = null;
///
/// 当前用户的权限数据
///
public static DataTable UserAuthorities
{
get => _UserAuthorities;
set => _UserAuthorities = value;
}
///
/// 系统登录策略
///
private static ILoginAuthorization _Current = null;
///
/// 系统预设的授权模式
///
public static ILoginAuthorization Current
{
get => _Current;
set => _Current = value;
}
///
/// 当前登录的用户
///
public static Loginer User => Loginer.CurrentUser;
///
/// 检查当前用户对指定菜单(窗体)有授权使用
///
/// 菜单名
///
public static bool Authorized(string menuName)
{
var rows =
UserAuthorities.Select(
string.Format("MenuName='{0}'", menuName));
var authority = rows != null && rows.Length > 0 ? rows[0] : null;
return authority != null;
}
///
/// 设置窗体的权限
///
/// 支持权限控制的窗体
/// 菜单名
public static void SetFormAuthority(IPurviewControllable form,
string menuName)
{
var rows =
UserAuthorities.Select(
string.Format("MenuName='{0}'", menuName));
var authority = rows != null && rows.Length > 0 ? rows[0] : null;
if (authority != null)
{
var actions = 0;
int.TryParse(authority["Actions"].ToStringEx(), out actions);
(form as IPurviewControllable).FormAuthorities = actions;
}
else
{
(form as IPurviewControllable).FormAuthorities = 0;
}
}
///
/// 检查窗体的权限是否拥有参数authorityValue的权限
///
/// 窗体的权限
/// 权限值(功能点的权限值)
///
public static bool ButtonAuthorized(int formAuthorities,
int authorityValue)
{
if (authorityValue <= 0) return true;
if (Loginer.CurrentUser.IsAdmin()) return true; //超级用户
//逻辑运算(与and运算)
return (authorityValue & formAuthorities) == authorityValue;
}
///
/// 设置主菜单的权限。取当前用户拥有的权限,
/// 枚举所有菜单,没有权限的菜单隐藏。
///
///
public static void SetMenuAuthority(MenuStrip menuStrip)
{
foreach (ToolStripItem item in menuStrip.Items)
{
if (item.Tag != null && item.Tag.ToStringEx() == "IsSystemMenu")
continue;
if (item is ToolStripMenuItem)
{
var menu = item as ToolStripMenuItem;
if (menu.DropDown.Items.Count > 0)
SetMenuItemSecurity(menu);
}
}
}
///
/// 递归设置菜单的权限
///
/// 父级菜单
private static void SetMenuItemSecurity(ToolStripMenuItem parent)
{
if (!Loginer.CurrentUser.IsAdmin() && UserAuthorities == null)
throw new Exception("User does't login!");
//The parent menuitem object will disabled if all child menuitem is disabled.
var hasShow = false;
foreach (ToolStripItem sub in parent.DropDown.Items)
if (sub is ToolStripMenuItem)
{
var rows =
UserAuthorities.Select(string.Format("MenuName='{0}' ",
sub.Name));
var menu = sub as ToolStripMenuItem;
//窗体
if (menu.DropDown.Items.Count == 0)
{
sub.Enabled = Loginer.CurrentUser.IsAdmin() ||
(rows != null && rows.Length > 0);
sub.Visible = sub.Enabled; //No right,No Menu
if (sub.Enabled) hasShow = true;
}
if (menu.DropDown.Items.Count > 0)
SetMenuItemSecurityChild(menu, ref hasShow);
}
//Process the parent menuitem
parent.Enabled = hasShow;
parent.Visible = hasShow;
}
///
/// 递归设置菜单的权限
///
/// 父级菜单
/// /// 如果子菜单都被隐藏,是否要显示父级菜单
private static void SetMenuItemSecurityChild(ToolStripMenuItem parent,
ref bool hideParentIfAllChildInvisible)
{
if (!Loginer.CurrentUser.IsAdmin() && UserAuthorities == null)
throw new Exception("User does't login!");
//The parent menuitem object will disabled if all child menuitem is disabled.
var hasShow = false;
foreach (ToolStripItem sub in parent.DropDown.Items)
if (sub is ToolStripMenuItem)
{
var rows =
UserAuthorities.Select(string.Format("MenuName='{0}' ",
sub.Name));
var menu = sub as ToolStripMenuItem;
if (menu.DropDown.Items.Count == 0)
{
sub.Enabled = Loginer.CurrentUser.IsAdmin() ||
(rows != null && rows.Length > 0);
sub.Visible = sub.Enabled; //No right,No Menu
if (sub.Enabled) hasShow = true;
}
if (menu.DropDown.Items.Count > 0)
SetMenuItemSecurityChild(menu, ref hasShow);
}
//Process the parent menuitem
parent.Enabled = hasShow;
parent.Visible = hasShow;
if (!hideParentIfAllChildInvisible)
hideParentIfAllChildInvisible = hasShow;
}
}
}