Пример #1
0
        public async Task <Status> DeletePassword(Guid id, ClaimsPrincipal user, CancellationToken cancellationToken)
        {
            var function = await _passwordWalletContext.Functions.FirstOrDefaultAsync(x => x.Name == FunctionName.Wallet.DeletePassword, cancellationToken);

            await LogFunction(function.Id, Guid.Parse(user.FindFirst(ClaimTypes.NameIdentifier).Value), cancellationToken);

            var passwordToRemove = await _passwordWalletContext.Passwords.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);

            if (passwordToRemove == null)
            {
                return(new Status(false, string.Format("Cannot find password with id: {0}", id)));
            }

            var authorizationResult = await _authorizationService
                                      .AuthorizeAsync(user, passwordToRemove, Policy.OnlyOwner);

            if (!authorizationResult.Succeeded)
            {
                return(new Status(false, "You have to be an owner to delete password"));
            }

            var userIdString = user.FindFirst(ClaimTypes.NameIdentifier).Value;

            Guid.TryParse(userIdString, out Guid userId);

            var actionChanges = new DataChange
            {
                UserId        = userId,
                PreviousValue = JsonConvert.SerializeObject(passwordToRemove),
                CurrentValue  = null,
                ActionType    = ActionType.DELETE,
                RecordId      = passwordToRemove.Id,
                UpdatedAt     = DateTime.Now,
            };

            passwordToRemove.IsDeleted = true;
            actionChanges.CurrentValue = JsonConvert.SerializeObject(passwordToRemove);

            _passwordWalletContext.Update(passwordToRemove);
            await _passwordWalletContext.AddAsync(actionChanges, cancellationToken);

            await _passwordWalletContext.SaveChangesAsync();

            return(new Status
            {
                Success = true,
                Messege = "Successfully removed password from wallet!"
            });
        }
Пример #2
0
        private string UpdateUserPassword(string newPassword, bool isPasswordKept, User user)
        {
            var newSalt         = Guid.NewGuid().ToString();
            var newpasswordHash = PreapreHashPassword(newPassword, newSalt, isPasswordKept);

            user.Salt                 = newSalt;
            user.PasswordHash         = newpasswordHash;
            user.IsPasswordKeptAsHash = isPasswordKept;

            _passwordWalletContext.Update(user);

            return(newpasswordHash);
        }