示例#1
0
        /// <summary>
        /// 登录,返回用户的基本信息和token
        /// </summary>
        /// <param name="loginDto">登录dto</param>
        /// <returns>用户的基本信息和token对象</returns>
        public virtual LoginResult Login(LoginDto loginDto)
        {
            IUser user;

            if (_permissionStore.HasTenant(out string tenantId))
            {
                if (string.IsNullOrEmpty(loginDto.TenantId))
                {
                    throw new BusinessException("多租户系统,必须传入租户id");
                }
                user = _permissionStore.GetAllUser().FirstOrDefault(a => a.GetAccount().Equals(loginDto.Account, StringComparison.OrdinalIgnoreCase) && ((ITenant <string>)a).TenantId == loginDto.TenantId);
            }
            else
            {
                user = _permissionStore.GetAllUser().FirstOrDefault(a => a.GetAccount().Equals(loginDto.Account, StringComparison.OrdinalIgnoreCase));
            }
            if (
                user != null &&
                (
                    loginDto.IgnorePwd
                    ||
                    HashPwd(loginDto.Pwd).Equals(user.GetPassword(), StringComparison.OrdinalIgnoreCase)
                )
                )
            {
                var roleKeys  = _permissionStore.GetAllUserRole().Where(a => a.GetUserKey() == user.GetKey()).Select(a => a.GetRoleKey()) ?? new List <string>();
                var roleNames = _permissionStore.GetAllRole().Where(a => roleKeys.Contains(a.GetKey())).Select(a => a.GetName()) ?? new List <string>();
                var userInfo  = new UserInfo
                {
                    Account   = user.GetAccount(),
                    RoleKeys  = roleKeys.ToList(),
                    RoleNames = roleNames.ToList(),
                    UserKey   = user.GetKey(),
                    UserName  = user.GetName(),
                };
                if (user is ITenant <string> userTenant)
                {
                    userInfo.TenantId = userTenant.TenantId; // todo 让string为泛型
                }
                var claims   = GetClaims(userInfo);
                var tokenStr = GenerateTokenStr(claims);
                return(new LoginResult
                {
                    Token = tokenStr,
                    UserInfo = userInfo
                });
            }
            else
            {
                throw new BusinessException($"用户名或密码错误");
            }
        }
示例#2
0
 public List <PermissionUserInfo> GetAllUserInfo()
 {
     return(_permissionStore.GetAllUser().AsQueryable().Where(GetTenantAndSoftDeletedPred <IUser>(true)).Select(a => new PermissionUserInfo
     {
         Id = a.GetKey(),
         Account = a.GetAccount(),
         Name = a.GetName(),
     }).ToList());
 }