示例#1
0
        public bool Validate(string enteredPassword, Password correctPassword)
        {
            var computedHash = _passwordHashService.Hash(enteredPassword, correctPassword.Salt);

            return(!computedHash
                   .Where((computedHashByte, i) => computedHashByte != correctPassword.Hash[i])
                   .Any());
        }
示例#2
0
        public async Task <int> Create(UserCreateModel userModel)
        {
            var user = new User
            {
                Email     = userModel.Email,
                FirstName = userModel.FirstName,
                LastName  = userModel.LastName,
                Password  = _passwordHashService.Hash(userModel.CleartextPassword)
            };

            await _unitOfWork.Users.Add(user);

            await _unitOfWork.Complete();

            return(user.UserId);
        }
示例#3
0
        public async Task <bool> ResetPasswordAsync(ResetUserPasswordRequest resetUserPasswordRequest)
        {
            var userResponse = await _userService.GetUserByEmailAsync(resetUserPasswordRequest.Email);

            if (userResponse == null)
            {
                throw new ArgumentException(string.Format(ValidationMessages.NotFound, ValidationMessages.User));
            }
            var changePasswordResponse = await _loginRepository.UpdatePasswordAsync(userResponse.Id, _passwordHashService.Hash(resetUserPasswordRequest.Password));

            return(changePasswordResponse > 0);
        }
示例#4
0
        public async Task <bool> ChangeUserPasswordAsync(ChangeUserPasswordRequest changeUserPasswordRequest)
        {
            var userId       = _authenticateService.GetUserId();
            var userResponse = await _userRepository.GetUserByIdAsync(userId);

            var passwordResult = _passwordHashService.Verify(userResponse.Password, changeUserPasswordRequest.Password);

            if (!passwordResult)
            {
                throw new ArgumentException(string.Format(ValidationMessages.Invalid, nameof(changeUserPasswordRequest.Password)));
            }

            var changePasswordResponse = await _loginRepository.UpdatePasswordAsync(userResponse.Id, _passwordHashService.Hash(changeUserPasswordRequest.NewPassword));

            return(changePasswordResponse > 0);
        }
        public async Task <IActionResult> Get(string account, string password)
        {
            if (!IsLocalRequest())
            {
                return(NotFound());
            }
            var set = dbContext.Set <PlatformUser>();

            if (set.Any(x => x.Account == account))
            {
                return(BadRequest("account exists"));
            }
            var newUser = new PlatformUser
            {
                Account  = account,
                Password = passwordHashService.Hash(password),
                Name     = account,
                Status   = UserStatus.Active
            };
            await set.AddAsync(newUser);

            var flagSet   = dbContext.Set <PlatformFlag>();
            var groupSet  = dbContext.Set <PlatformGroup>();
            var groupFlag = await flagSet.FindAsync("SuperGroup");

            PlatformGroup group;

            if (groupFlag == null)
            {
                group = new PlatformGroup {
                    Name = "Administrator", IsEnabled = true
                };
                await groupSet.AddAsync(group);

                await dbContext.SaveChangesAsync();

                groupFlag = new PlatformFlag {
                    Flag = "SuperGroup", Value = group.Id.ToString()
                };
                await flagSet.AddAsync(groupFlag);
            }
            else
            {
                group = await groupSet.FindAsync(long.Parse(groupFlag.Value));
            }
            var roleSet = dbContext.Set <PlatformGroupRole>();
            var roles   = functionService.Functions.SelectMany(x => x.Permissions.Select(y => new { Function = x, Permission = y })).Select(x => new PlatformGroupRole
            {
                FunctionRoleCode = x.Permission.PermissionCode,
                FunctionCode     = x.Function.Code,
                Permission       = PermissionStatus.Granted,
                PlatformGroupId  = group.Id
            });

            dbContext.UpdateSet(roles,
                                x => x.PlatformGroupId == group.Id, (x, y) => x.FunctionCode == y.FunctionCode && x.FunctionRoleCode == y.FunctionRoleCode,
                                (o, n) => o.Permission = n.Permission
                                );
            await dbContext.Set <PlatformUserGroup>().AddAsync(new PlatformUserGroup {
                PlatformGroup = group, PlatformUser = newUser
            });

            await dbContext.SaveChangesAsync();

            return(Ok());
        }