Exemple #1
0
        /// <summary>
        /// 清除缓存(本地缓存+Redis)
        /// </summary>
        /// <param name="adminId"></param>
        public void RemoveCache(int adminId)
        {
            string key = $"SYSTEMADMIN:{adminId}";

            MemoryUtils.Remove(key);
            AdminCaching.Instance().RemoveCache(adminId);
        }
Exemple #2
0
 /// <summary>
 /// 是否包含权限
 /// </summary>
 /// <param name="adminId">管理员ID</param>
 /// <param name="permission">权限</param>
 /// <returns></returns>
 public bool IsPermission(int?adminId, string permission)
 {
     if (!adminId.HasValue || adminId == 0)
     {
         return(false);
     }
     return(AdminCaching.Instance().IsPermission(adminId.Value, permission));
 }
Exemple #3
0
 /// <summary>
 /// 退出登录
 /// </summary>
 /// <param name="adminId"></param>
 public void Logout(int adminId)
 {
     if (adminId == 0)
     {
         return;
     }
     AdminCaching.Instance().Logout(adminId);
     this.RemoveCache(adminId);
 }
Exemple #4
0
        /// <summary>
        /// 获取管理员信息(缓存中读取)
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns></returns>
        public SystemAdmin GetAdminInfo(int adminId)
        {
            SystemAdmin admin = AdminCaching.Instance().GetAdminInfo(adminId);

            if (admin == null)
            {
                admin = this.ReadDB.ReadInfo <SystemAdmin>(t => t.ID == adminId);
                if (admin != null)
                {
                    AdminCaching.Instance().SaveAdminInfo(admin);
                }
            }
            return(admin);
        }
Exemple #5
0
        /// <summary>
        /// 系统管理员登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="code"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool Login(string userName, string password, string code, out string token)
        {
            token = null;
            SystemAdmin admin = this.ReadDB.ReadInfo <SystemAdmin>(t => t.UserName == userName);

            if (admin == null)
            {
                if (userName == "admin" && !this.ReadDB.Exists <SystemAdmin>(t => t.ID != 0))
                {
                    this.CreateDefaultAdmin();
                    return(this.FaildMessage("已创建默认管理员账号,请重新登录"));
                }
                return(this.FaildMessage("用户名不存在"));
            }
            if (admin.Status != SystemAdmin.AdminStatus.Normal)
            {
                ((IAccount)admin).Log(LogType.Login, "当前账号被禁止登录");
                return(this.FaildMessage("当前账号被禁止登录"));
            }
            if (admin.SecretKey != Guid.Empty && !GoogleAuthenticator.Validate(admin.SecretKey.ToString("N"), code))
            {
                ((IAccount)admin).Log(LogType.Login, "验证码错误");
                return(this.FaildMessage("验证码错误"));
            }

            if (admin.Password != Encryption.SHA1WithMD5(password))
            {
                ((IAccount)admin).Log(LogType.Login, "密码错误");
                return(this.FaildMessage("密码错误"));
            }

            using (DbExecutor db = NewExecutor())
            {
                admin.LoginAt = DateTime.Now;
                admin.LoginIP = IPAgent.IP;
                admin.Update(db, t => t.LoginAt, t => t.LoginIP);
            }

            token = $"{admin.UserName}:{AdminCaching.Instance().SaveToken(admin.ID).ToString("N")}".ToBasicAuth();
            AdminCaching.Instance().SavePermission(admin.ID, admin.Permission.Split(','));
            this.RemoveCache(admin.ID);
            return(((IAccount)admin).Log(LogType.Login, "登录成功"));
        }
Exemple #6
0
        /// <summary>
        /// 获取当前登录的管理员信息(本地缓存10分钟)
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public SystemAdmin GetAdminInfo(string input)
        {
            if (!Guid.TryParse(input, out Guid token))
            {
                return(null);
            }
            int adminId = AdminCaching.Instance().GetAdminID(token);

            if (adminId == 0)
            {
                return(null);
            }
            string key = $"SYSTEMADMIN:{adminId}";

            return(MemoryUtils.Get(key, TimeSpan.FromMinutes(10), () =>
            {
                return this.GetAdminInfo(adminId);
            }));
        }
Exemple #7
0
        /// <summary>
        /// 获取管理员有权限的菜单列表
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns></returns>
        public AdminMenu GetAdminMenu(int adminId)
        {
            XElement root = XElement.Parse(Resources.Permission);

            return(new AdminMenu(root, true, AdminCaching.Instance().GetPermission(adminId).ToArray()));
        }