public async Task <IDictionary <string, object> > Login(LoginInput input) { await ValidCaptcha(input); var userInfo = await _userRepository.SingleOrDefaultAsync(p => p.UserName == input.UserName || p.Phone == input.UserName || p.Email == input.UserName); if (userInfo == null) { throw new BusinessException($"不存在账号为{input.UserName}的用户"); } if (userInfo.Status == Status.Invalid) { throw new BusinessException("账号为被激活,请先激活该账号"); } if (!_passwordHelper.EncryptPassword(userInfo.UserName, input.Password).Equals(userInfo.Password)) { throw new BusinessException("密码不正确"); } var payload = new Dictionary <string, object> { { ClaimTypes.UserId, userInfo.Id }, { ClaimTypes.UserName, userInfo.UserName }, { ClaimTypes.OrgId, userInfo.OrgId } }; return(payload); }
public IActionResult UserRegister(UserRegisterModel register) { var encryptedPW = _pwencrypt.EncryptPassword(register.Password); try { _userService.UserRegistration(register.Username, register.Email, encryptedPW); } catch (Npgsql.PostgresException) { ModelState.AddModelError("Email", "Email Already in Use."); return(View()); } return(Redirect("Login")); }
public async Task <IDictionary <string, object> > Login(string userName, string password) { var userInfo = await _userRepository.SingleOrDefaultAsync(p => p.UserName == userName || p.Phone == userName || p.Email == userName); if (userInfo == null) { throw new BusinessException($"不存在账号为{userName}的用户"); } if (userInfo.Status == Status.Invalid) { throw new BusinessException($"账号为被激活,请先激活该账号"); } if (!_passwordHelper.EncryptPassword(userInfo.UserName, password).Equals(userInfo.Password)) { throw new BusinessException($"密码不正确"); } var payload = new Dictionary <string, object>() { { "UserId", userInfo.Id }, { "UserName", userInfo.UserName }, { "OrgId", userInfo.OrgId } }; return(payload); }
public async Task <long> Create(CreateUserInput input, long?tenanId = null) { var userInfo = await CheckUserInfo(input, tenanId); userInfo.Password = _passwordHelper.EncryptPassword(userInfo.UserName, userInfo.Password); using (var locker = await _lockerProvider.CreateLockAsync("CreateUser")) { return(await locker.Lock(async() => { await UnitOfWorkAsync(async(conn, trans) => { var userId = await _userRepository.InsertAndGetIdAsync(userInfo, conn, trans); foreach (var roleId in input.RoleIds) { var role = await _roleRepository.SingleOrDefaultAsync(p => p.Id == roleId, conn, trans); if (role == null) { throw new BusinessException($"系统中不存在Id为{roleId}的角色信息"); } await _userRoleRepository.InsertAsync(new UserRole { UserId = userId, RoleId = roleId, TenantId = userInfo.TenantId }, conn, trans); } foreach (var userGroupId in input.UserGroupIds) { var userGroup = await _userGroupRepository.SingleOrDefaultAsync(p => p.Id == userGroupId); if (userGroup == null) { throw new BusinessException($"系统中不存在Id为{userGroupId}的用户组信息"); } await _userUserGroupRelationRepository.InsertAsync( new UserUserGroupRelation { UserId = userId, UserGroupId = userGroupId, TenantId = userInfo.TenantId }, conn, trans); } }, Connection); return userInfo.Id; })); } }
public async Task Create(CreateUserInput input) { var userInfo = input.MapTo <UserInfo>(); var departAppServiceProxy = GetService <IDepartmentAppService>(); if (userInfo.OrgId.HasValue) { if (!await departAppServiceProxy.Check(userInfo.OrgId.Value)) { throw new BusinessException($"不存在Id为{userInfo.OrgId}的部门信息"); } } var positionAppServiceProxy = GetService <IPositionAppService>(); if (userInfo.PositionId.HasValue) { if (!await positionAppServiceProxy.Check(userInfo.PositionId.Value)) { throw new BusinessException($"不存在Id为{userInfo.PositionId}的职位信息"); } } userInfo.Password = _passwordHelper.EncryptPassword(userInfo.UserName, userInfo.Password); await UnitOfWorkAsync(async (conn, trans) => { var userId = await _userRepository.InsertAndGetIdAsync(userInfo, conn, trans); foreach (var roleId in input.RoleIds) { var role = await _roleRepository.SingleOrDefaultAsync(p => p.Id == roleId); if (role == null) { throw new BusinessException($"系统中不存在Id为{roleId}的角色信息"); } await _userRoleRepository.InsertAsync(new UserRole() { UserId = userId, RoleId = roleId }, conn, trans); } }, Connection); }
public async Task <IDictionary <string, object> > Login(LoginInput input) { if (AppConfig.ServerOptions.Environment != RuntimeEnvironment.Development) { await ValidCaptcha(input); } await ValidTenant(input.TenantId); var sql = "SELECT * FROM `UserInfo` WHERE (UserName=@UserName OR Phone=@UserName OR Email=@UserName) AND TenantId=@TenantId AND IsDeleted=@IsDeleted"; await using (Connection) { var userInfo = await Connection.QuerySingleOrDefaultAsync <UserInfo>(sql, new { UserName = input.UserName, TenantId = input.TenantId, IsDeleted = HeroConstants.UnDeletedFlag }); if (userInfo == null) { throw new BusinessException($"不存在账号为{input.UserName}的用户"); } if (userInfo.Status == Status.Invalid) { throw new BusinessException("账号为被激活,请先激活该账号"); } if (!_passwordHelper.EncryptPassword(userInfo.UserName, input.Password).Equals(userInfo.Password)) { throw new BusinessException("密码不正确"); } var payload = new Dictionary <string, object> { { ClaimTypes.UserId, userInfo.Id }, { ClaimTypes.UserName, userInfo.UserName }, { ClaimTypes.OrgId, userInfo.OrgId }, }; if (userInfo.TenantId.HasValue) { payload.Add(ClaimTypes.TenantId, userInfo.TenantId); } return(payload); } }
public async Task CreateUserInfo(UserInfo userInfo) { var rpcParams = new Dictionary <string, object>() { { "confName", IdentityConstants.SysConfPwdModeName } }; var pwdConfig = await _serviceProxyProvider.Invoke <GetSystemConfOutput>(rpcParams, ApiConsts.BasicData.GetSysConfApi); if (pwdConfig == null) { throw new BusinessException("获取用户加密模式失败,请先完成系统初始化"); } var generatePwdMode = ConvertHelper.ParseEnum <GeneratePwdMode>(pwdConfig.ConfigValue); var plainPwd = string.Empty; if (generatePwdMode == GeneratePwdMode.Fixed) { rpcParams = new Dictionary <string, object>() { { "confName", IdentityConstants.SysConfFieldModeName } }; var fixedPwdConf = await _serviceProxyProvider.Invoke <GetSystemConfOutput>(rpcParams, ApiConsts.BasicData.GetSysConfApi); if (pwdConfig == null) { throw new BusinessException("未配置员工用户默认密码"); } plainPwd = fixedPwdConf.ConfigValue; } else { plainPwd = PasswordGenerator.GetRandomPwd(IdentityConstants.RandomLen); // :todo email send pwd } userInfo.Password = _passwordHelper.EncryptPassword(userInfo.UserName, plainPwd); await _userRepository.InsertAsync(userInfo); }