SystemAuthStrategy.cs 3.24 KB
// ***********************************************************************
// <summary>
// 超级管理员授权策略
// </summary>
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 领域服务
    /// <para>超级管理员权限</para>
    /// <para>超级管理员使用guid.empty为ID,可以根据需要修改</para>
    /// </summary>
    public class SystemAuthStrategy : WebApp<SysUser>, IAuthStrategy
    {
        protected SysUser _user;

        public SystemAuthStrategy(IUnitWork unitWork, IRepository<SysUser> repository) : base(unitWork, repository)
        {
            _user = unitWork.FindSingle<SysUser>(u => u.Account.Equals("System"));
        }

        public List<SysModuleView> Modules
        {
            get
            {
                List<SysModuleView> modules = null;
                List<SysModuleElement> elements = null;

                if (SystemCache.GetInstance.SysModuleViewCache.Count > 0)
                {
                    modules = SystemCache.GetInstance.SysModuleViewCache;
                }
                else
                {
                    modules = (from module in UnitWork.Find<SysModule>(null)
                               select new SysModuleView
                               {
                                   Name = module.Name,
                                   Id = module.Id.Value,
                                   CascadeId = module.CascadeId,
                                   Code = module.Code,
                                   IconName = module.IconName,
                                   Url = module.Url,
                                   ParentId = module.ParentId,
                                   ParentName = module.ParentName,
                                   SortNo = module.SortNo.Value,
                                   IsShow = module.IsShow.Value
                               }).OrderBy(u => u.SortNo).ToList();
                    SystemCache.GetInstance.SysModuleViewCache = modules;
                }

                if (SystemCache.GetInstance.SysModuleElementCache.Count > 0)
                {
                    elements = SystemCache.GetInstance.SysModuleElementCache;
                }
                else
                {
                    elements = UnitWork.Find<SysModuleElement>(null).ToList();
                    SystemCache.GetInstance.SysModuleElementCache = elements;
                }

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

        public List<SysRole> Roles
        {
            get { return UnitWork.Find<SysRole>(null).ToList(); }
        }

        public List<SysDept> Orgs
        {
            get { return UnitWork.Find<SysDept>(null).ToList(); }
        }

        public SysUser User
        {
            get { return _user; }
            set   //禁止外部设置
            {
                throw new Exception("超级管理员,禁止设置用户");
            }  
        }
    }
}