NormalAuthStrategy.cs 5.18 KB
// ***********************************************************************
// <summary>
// 普通用户授权策略
// </summary>
// ***********************************************************************

using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.Service.Repository;
using System.Collections.Generic;
using System.Linq;


namespace Hh.Mes.Service.SystemAuth
{
    /// <summary>
    /// 普通用户授权策略
    /// </summary>
    public class NormalAuthStrategy : RepositorySqlSugar<SysUser>
    {

        protected SysUser _user;
        //用户角色
        private List<int> _userRoleIds;    

        public List<SysModuleView> Modules
        {
            get
            {
                //根据用户来获取模块ID
                var moduleIds = Context.Queryable<SysRelevance>()
                                       .Where(t => (t.FirstId == _user.Id && t.RelKey == Define.USERMODULE) ||
                                                   (t.RelKey == Define.ROLEMODULE && _userRoleIds.Contains(t.FirstId.Value)))
                                       .Select(t => t.SecondId)
                                       .ToList();
                //根据用户的模块ID来获取具体模块信息
                var moduleList = Context.Queryable<SysModule>().Where(t => moduleIds.Contains(t.Id)).ToList();
                //组合成页面展示需要的SysModuleView格式
                List<SysModuleView> modules = new List<SysModuleView>();
                foreach (var item in moduleList.OrderBy(u => u.SortNo))
                {
                    SysModuleView sysModuleView = new SysModuleView();
                    sysModuleView.Id = item.Id;
                    sysModuleView.Code = item.Code;
                    sysModuleView.Name = item.Name;
                    sysModuleView.CascadeId = item.CascadeId;     
                    sysModuleView.IconName = item.IconName;
                    sysModuleView.Url = item.Url;
                    sysModuleView.ParentId = item.ParentId;
                    sysModuleView.ParentName = item.ParentName;
                    sysModuleView.SortNo = item.SortNo;
                    sysModuleView.IsShow = item.IsShow.Value;

                    sysModuleView.CreateBy = item.CreateBy;
                    sysModuleView.CreateTime = item.CreateTime;
                    sysModuleView.UpdateBy = item.UpdateBy;
                    sysModuleView.UpdateTime = item.UpdateTime;

                    modules.Add(sysModuleView);
                }
                //根据用户来获取模块内元素ID
                var elementIds = Context.Queryable<SysRelevance>()
                                        .Where(t => (t.FirstId == _user.Id && t.RelKey == Define.USERELEMENT) ||
                                                   (t.RelKey == Define.ROLEELEMENT && _userRoleIds.Contains(t.FirstId.Value)))
                                        .Select(t => t.SecondId)
                                        .ToList();
                //根据模块内元素ID
                var usermoduleelements = Context.Queryable<SysModuleElement>().Where(t => elementIds.Contains(t.Id)).ToList();

                foreach (var module in modules)
                {
                    module.Elements = usermoduleelements.Where(u => u.ModuleId == module.Id).OrderBy(u => u.Sort).ToList();
                }

                return modules;
            }
        }

        public List<SysRole> Roles
        {
            get 
            { 
                return Context.Queryable<SysRole>().Where(t => _userRoleIds.Contains(t.Id)).ToList(); 
            }
        }

        public List<SysDept> Orgs
        {
            get
            {
                var orgids = Context.Queryable<SysRelevance>()
                                    .Where(u => (u.FirstId == _user.Id && u.RelKey == Define.USERORG) ||
                                                (u.RelKey == Define.ROLEORG && _userRoleIds.Contains(u.FirstId.Value)))
                                    .Select(u => u.SecondId)
                                    .ToList();
                var deptList = Context.Queryable<SysDept>().Where(t => true).ToList();
                //用组织
                var userDeptList = deptList.Where(t => orgids.Contains(t.Id)).ToList();
                // 用户所有模块,包含组织下属的所有组织
                var userAllDeptList = new List<SysDept>();
                foreach (var item in userDeptList)
                {
                    //找出当前组织的下属组织,并且不在userAllDeptList中的
                    var depts = deptList.Where(t => t.CascadeId.StartsWith(item.CascadeId) && !userAllDeptList.Contains(t));
                    userAllDeptList.AddRange(depts);
                }
                return userAllDeptList;
            }
        }

        public SysUser User
        {
            get 
            { 
                return _user; 
            }
            set
            {
                _user = value;
                _userRoleIds = Context.Queryable<SysRelevance>().Where(t => t.FirstId == _user.Id && t.RelKey == Define.USERROLE).Select(u => u.SecondId.Value).ToList();
            }
        }


    }
}