Exemple #1
0
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId">用户Id</param>
        public void DeleteUser(long userId)
        {
            User user   = userRepository.GetUser(userId);
            var  result = userRepository.DeleteCache(user);

            if (result)
            {
                UserIdToUserNameDictionary.RemoveUserId(userId);
                UserIdToUserNameDictionary.RemoveUserName(user.UserName);
            }
        }
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.HttpContext.User.Identity.Name == null)
            {
                context.Result = new RedirectToRouteResult("default", new { controller = "System", action = "ManageLogin" });
                base.OnActionExecuting(context);
                return;
            }
            var userId = UserIdToUserNameDictionary.GetUserId(context.HttpContext.User.Identity.Name);
            var user   = context.HttpContext.Session.Get <User>(userId.ToString());

            if (!user.IsAllowEntryManager())
            {
                context.Result = new RedirectToRouteResult("Error", new { controller = "Error", action = "SystemError", errorMsg = "您目前没有权限登录管理后台" });
            }
            base.OnActionExecuting(context);
        }
 /// <summary>
 /// 获取自身实例
 /// </summary>
 /// <returns></returns>
 private static UserIdToUserNameDictionary Instance()
 {
     if (_defaultInstance == null)
     {
         lock (lockObject)
         {
             if (_defaultInstance == null)
             {
                 _defaultInstance = DIContainer.Resolve <UserIdToUserNameDictionary>();
                 if (_defaultInstance == null)
                 {
                     throw new Exception("未在DIContainer注册UserIdToUserNameDictionary的具体实现类");
                 }
             }
         }
     }
     return(_defaultInstance);
 }
Exemple #4
0
        /// <summary>
        /// 根用户Id集合删除用户
        /// </summary>
        /// <param name="userIds">用户Id集合</param>
        public void DeleteUsers(IEnumerable <long> userIds)
        {
            List <User> users = new List <User>();

            foreach (var userId in userIds)
            {
                User user = userRepository.GetUser(userId);
                if (user == null)
                {
                    continue;
                }
                var result = userRepository.DeleteCache(user);

                if (result)
                {
                    UserIdToUserNameDictionary.RemoveUserId(userId);
                    UserIdToUserNameDictionary.RemoveUserName(user.UserName);
                }
            }
        }
        /// <summary>
        /// 验证用户名和密码是否匹配
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <returns>登录状态枚举</returns>
        public UserLoginStatus ValidateUser(string userName, string passWord)
        {
            long userId = UserIdToUserNameDictionary.GetUserId(userName);
            User user   = userRepository.GetUser(userId);

            if (user == null)
            {
                return(UserLoginStatus.InvalidCredentials);
            }
            if (!userName.Equals(user.UserName, StringComparison.CurrentCulture))
            {
                return(UserLoginStatus.InvalidCredentials);
            }
            if (!UserPasswordHelper.CheckPassword(passWord, user.Password, (UserPasswordFormat)user.PasswordFormat))
            {
                return(UserLoginStatus.InvalidCredentials);
            }
            if (!user.IsActivated)
            {
                return(UserLoginStatus.NotActivated);
            }
            if (user.IsBanned)
            {
                if (user.BanDeadLine >= DateTime.UtcNow)
                {
                    return(UserLoginStatus.Banned);
                }
                else
                {
                    user.IsBanned    = false;
                    user.BanDeadLine = DateTime.UtcNow;
                    userRepository.Update(user);
                }
            }

            return(UserLoginStatus.Success);
        }
        /// <summary>
        /// 根据用户Id查询器获取用户实体
        /// </summary>
        /// <param name="userService"></param>
        /// <param name="userId">用户ID</param>
        public static User GetFullUser(this IUserService userService, long userId)
        {
            string userName = UserIdToUserNameDictionary.GetUserName(userId);

            return(userService.GetUserRepository().GetUser(userId));
        }