C# 角色(包含权限)数据库

using CommonLib.DBUtils.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCSPro.Model;

namespace WCSPro.IDAL
{
    /// <summary>
    /// 角色数据库访问接口
    /// </summary>
    public interface IRoleRightsDAL : IBaseDAL<RoleRights>
    {
        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        List<Role> GetAllRoles();

        /// <summary>
        /// 获取所有权限(从数据库表中获取)
        /// </summary>
        /// <returns></returns>
        List<RightsItem> GetAllRightsItems();

        /// <summary>
        /// 获取所有固定权限源(用于Admin账号的权限)
        /// </summary>
        /// <returns></returns>
        List<RightsItem> GetAllRightsSource();

        /// <summary>
        /// 获取角色拥有的权限
        /// </summary>
        /// <param name="role">角色信息对象</param>
        /// <returns></returns>
        List<RightsItem> GetRoleRights(Role role);

        /// <summary>
        /// 更新角色权限
        /// </summary>
        /// <param name="role">角色</param>
        /// <param name="roleRights"></param>
        /// <returns></returns>
        bool UpdateRoleRights(Role role, List<RoleRights> roleRights);

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role">角色对象</param>
        /// <returns></returns>
        bool AddRole(Role role);

        /// <summary>
        /// 检查角色名称是否已存在
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <returns></returns>
        bool ExistsRoleName(string roleName);

        /// <summary>
        /// 检查角色主键是否存在
        /// </summary>
        /// <param name="gid">角色主键</param>
        /// <returns></returns>
        bool ExistsRole(string gid);

        /// <summary>
        /// 检查角色是否存在
        /// </summary>
        /// <param name="role">角色对象</param>
        /// <returns></returns>
        bool ExistsRole(Role role);

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="role">角色对象</param>
        /// <returns></returns>
        bool DeleteRole(Role role);

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="gid">角色主键</param>
        /// <returns></returns>
        bool DeleteRole(string gid);
    }
}

using CommonLib.DBUtils.SqlSugar;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using WCSPro.IDAL;
using WCSPro.Model;

namespace WCSPro.DAL
{
    /// <summary>
    /// 角色(包含权限)数据库访问接口
    /// </summary>
    public class RoleRightsDAL : BaseDAL<RoleRights>, IRoleRightsDAL
    {
        public RoleRightsDAL(ISqlSugarClient db) : base(db)
        {
            Context.DbMaintenance.CreateDatabase();
            Context.CodeFirst.SetStringDefaultLength(200).InitTables<Role>();
            Context.CodeFirst.SetStringDefaultLength(200).InitTables<RightsItem>();
            Context.CodeFirst.SetStringDefaultLength(200).InitTables<RoleRights>();
            InitRights();
        }

        public bool AddRole(Role role)
        {
            return Context.Insertable(role).ExecuteCommand() == 1;
        }

        public bool DeleteRole(Role role)
        {
            return Context.Deleteable(role).ExecuteCommand() == 1;
        }

        public bool DeleteRole(string gid)
        {
            return Context.Deleteable<Role>(gid).ExecuteCommand() == 1;
        }

        public bool ExistsRole(string gid)
        {
            return Context.Queryable<Role>().Any(t => t.GID == gid);
        }

        public bool ExistsRole(Role role)
        {
            return ExistsRole(role.GID);
        }

        public bool ExistsRoleName(string roleName)
        {
            return Context.Queryable<Role>().Any(t => t.Name.ToUpper() == roleName.ToUpper());
        }

        public List<RightsItem> GetAllRightsItems()
        {
            return Context.Queryable<RightsItem>().ToList();
        }

        public List<Role> GetAllRoles()
        {
            return Context.Queryable<Role>().ToList();
        }

        public List<RightsItem> GetRoleRights(Role role)
        {
            var rightQ = Context.Queryable<RoleRights>().Where(t => t.RoleGID == role.GID);
            return Context.Queryable<RightsItem>()
                .LeftJoin(rightQ, (ri, rr) => ri.RightsCode == rr.RightCode)
                .OrderBy((ri, rr) => ri.RightsCode)
                .Select((ri, rr) => new RightsItem()
                {
                    RightsCode = ri.RightsCode,
                    ParentCode = ri.ParentCode,
                    RightsDescription = ri.RightsDescription,
                    RightsName = ri.RightsName,
                    Authorized = !string.IsNullOrWhiteSpace(rr.RoleGID)
                }).ToList();

            //.LeftJoin<RoleRights>((ri, rr) => ri.RightsCode == rr.RightCode)
            //.OrderBy((ri, rr) => ri.RightsCode)
            //.Select((ri, rr) => new RightsItem()
            //{
            //    RightsCode = ri.RightsCode,
            //    ParentCode = ri.ParentCode,
            //    RightsDescription = ri.RightsDescription,
            //    RightsName = ri.RightsName,
            //    Authorized = !string.IsNullOrWhiteSpace(rr.RoleGID)
            //})
            //.ToList();
        }

        public bool UpdateRoleRights(Role role, List<RoleRights> roleRights)
        {
            try
            {
                Context.Ado.BeginTran();
                Context.Deleteable<RoleRights>().Where(t => t.RoleGID == role.GID).ExecuteCommand();
                Context.Insertable(roleRights).ExecuteCommand();
                Context.Ado.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                Context.Ado.RollbackTran();
                throw ex;
            }
        }

        /// <summary>
        /// 固定权限集合
        /// </summary>
        private List<RightsItem> ALLRights { get; } = new List<RightsItem>()
        {
            new RightsItem() { RightsCode="00", RightsName="MAIN", RightsDescription="MAIN"},
            new RightsItem() { RightsCode="00.001", ParentCode="00", RightsName="SCAN MODE", RightsDescription="MAIN -> SCAN MODE"},
            new RightsItem() { RightsCode="00.002", ParentCode="00", RightsName="RECIPE", RightsDescription="MAIN -> RECIPE"},
            new RightsItem() { RightsCode="00.003", ParentCode="00", RightsName="JOB", RightsDescription="MAIN -> JOB"},
            new RightsItem() { RightsCode="00.004", ParentCode="00", RightsName="System View", RightsDescription="MAIN -> System View"},
            new RightsItem() { RightsCode="00.005", ParentCode="00", RightsName="Safety", RightsDescription="MAIN -> Safety"},
            new RightsItem() { RightsCode="01", RightsName="MAINT", RightsDescription="MAINT"},
            new RightsItem() { RightsCode="01.001", ParentCode="01", RightsName="P&ID Main", RightsDescription="MAINT -> P&ID Main"},
            new RightsItem() { RightsCode="01.002", ParentCode="01", RightsName="P&ID Chem", RightsDescription="MAINT -> P&ID Chem"},
            new RightsItem() { RightsCode="01.003", ParentCode="01", RightsName="I/O List", RightsDescription="MAINT -> I/O List"},
            new RightsItem() { RightsCode="01.004", ParentCode="01", RightsName="PUMP", RightsDescription="MAINT -> PUMP"},
            new RightsItem() { RightsCode="01.005", ParentCode="01", RightsName="Load Port", RightsDescription="MAINT -> Load Port"},
            new RightsItem() { RightsCode="01.006", ParentCode="01", RightsName="Robot", RightsDescription="MAINT -> Robot"},
            new RightsItem() { RightsCode="01.007", ParentCode="01", RightsName="MOTION", RightsDescription="MAINT -> MOTION"},
            new RightsItem() { RightsCode="01.008", ParentCode="01", RightsName="MFC/LFC", RightsDescription="MAINT -> MFC/LFC"},
            new RightsItem() { RightsCode="01.009", ParentCode="01", RightsName="Run Test", RightsDescription="MAINT -> Run Test"},
            new RightsItem() { RightsCode="01.010", ParentCode="01", RightsName="DEV TEST", RightsDescription="MAINT -> DEV TEST"},
            new RightsItem() { RightsCode="01.011", ParentCode="01", RightsName="RFID", RightsDescription="MAINT -> RFID"},
            new RightsItem() { RightsCode="01.012", ParentCode="01", RightsName="Heater", RightsDescription="MAINT -> Heater"},
            new RightsItem() { RightsCode="02", RightsName="ICPMS", RightsDescription="ICPMS"},
            new RightsItem() { RightsCode="02.001", ParentCode="02", RightsName="ICP-Mass", RightsDescription="ICPMS -> ICP-Mass"},
            new RightsItem() { RightsCode="02.002", ParentCode="02", RightsName="Calibration", RightsDescription="ICPMS -> Calibration"},
            new RightsItem() { RightsCode="02.003", ParentCode="02", RightsName="Sol Blank", RightsDescription="ICPMS -> Sol Blank"},
            new RightsItem() { RightsCode="02.004", ParentCode="02", RightsName="Sensitivity", RightsDescription="ICPMS -> Sensitivity"},
            new RightsItem() { RightsCode="02.005", ParentCode="02", RightsName="SelfValidation", RightsDescription="ICPMS -> SelfValidation"},
            new RightsItem() { RightsCode="02.006", ParentCode="02", RightsName="Parameter", RightsDescription="ICPMS -> Parameter"},
            new RightsItem() { RightsCode="02.007", ParentCode="02", RightsName="Result", RightsDescription="ICPMS -> Result"},
            new RightsItem() { RightsCode="02.008", ParentCode="02", RightsName="Periodic Table", RightsDescription="ICPMS -> Periodic Table"},
            new RightsItem() { RightsCode="02.009", ParentCode="02", RightsName="PreAnalysis", RightsDescription="ICPMS -> PreAnalysis"},
            new RightsItem() { RightsCode="02.010", ParentCode="02", RightsName="Sample Result", RightsDescription="ICPMS -> Sample Result"},
            new RightsItem() { RightsCode="03", RightsName="PARAM", RightsDescription="PARAM"},
            new RightsItem() { RightsCode="04", RightsName="Data", RightsDescription="Data"},
            new RightsItem() { RightsCode="04.001", ParentCode="04", RightsName="USERS", RightsDescription="Data -> USERS"},
            new RightsItem() { RightsCode="04.002", ParentCode="04", RightsName="ROLES", RightsDescription="Data -> ROLES"},
            new RightsItem() { RightsCode="05", RightsName="LOG", RightsDescription="LOG"},
        };

        /// <summary>
        /// 初始化权限数据
        /// </summary>
        private void InitRights()
        {
            List<RightsItem> dbls = this.GetAllRightsItems();
            var diffls = this.ALLRights.Except(dbls, new RightsItemComparer());
            if (diffls != null && diffls.Count() > 0)
            {
                Context.Insertable(diffls.ToList()).ExecuteCommand();
            }
        }

        public List<RightsItem> GetAllRightsSource()
        {
            return this.ALLRights;
        }
    }

    /// <summary>
    /// 权限对象比较器
    /// </summary>
    public class RightsItemComparer : IEqualityComparer<RightsItem>
    {
        public bool Equals(RightsItem x, RightsItem y)
        {
            return x.RightsCode == y.RightsCode;
        }

        public int GetHashCode(RightsItem obj)
        {
            return obj.RightsCode.GetHashCode();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace WCSPro.Model
{
    /// <summary>
    /// 角色权限映射实体
    /// </summary>
    [SugarTable("RoleRightsMap", "角色权限映射表")]
    public class RoleRights
    {
        /// <summary>
        /// 角色GID
        /// </summary>
        [SugarColumn(IsPrimaryKey = true, ColumnDataType = "varchar(36)")]
        public string RoleGID { get; set; } = string.Empty;

        /// <summary>
        /// 权限编号(主键)
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(16)", IsPrimaryKey = true)]
        public string RightCode { get; set; } = string.Empty;
    }
}

using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using System.ComponentModel.DataAnnotations;
using System.Xml.Linq;

namespace WCSPro.Model
{
    /// <summary>
    /// 角色表
    /// </summary>
    [AddINotifyPropertyChangedInterface]
    public class Role
    {
        /// <summary>
        /// 数据库主键
        /// </summary>
        [SugarColumn(IsPrimaryKey = true, ColumnDataType = "varchar(36)")]
        public string GID { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 角色名称
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(32)")]
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }
}

using PropertyChanged;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WCSPro.Model
{
    /// <summary>
    /// 权限项
    /// </summary>
    [SugarTable("Rights", "权限表")]
    [AddINotifyPropertyChangedInterface]
    public class RightsItem
    {
        /// <summary>
        /// 权限编号(唯一)
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(16)", IsPrimaryKey = true)]
        public string RightsCode { get; set; } = string.Empty;

        /// <summary>
        /// 权限名称
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(32)")]
        public string RightsName { get; set; } = string.Empty;

        /// <summary>
        /// 权限描述
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(128)")]
        public string RightsDescription { get; set; } = string.Empty;

        /// <summary>
        /// 上级权限编号
        /// </summary>
        [SugarColumn(ColumnDataType = "varchar(8)")]
        public string ParentCode { get; set; } = string.Empty;

        /// <summary>
        /// 是否授权
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public bool Authorized { get; set; }
    }
}

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值