public async Task<Result<AccountResponse>> Handle(GetAccountRequest request, CancellationToken cancellationToken) { try { var user = await _userRepository.GetUserByEmail(request.RequestEmail); var isAdminOfAccount = await _accountAdminRepository.IsAdminOfAccount(user.Id, request.Id); if (user.Role.Name != "Admin" && !isAdminOfAccount) { return Result<AccountResponse>.AccessDenied("No access!"); } var account = await _accountRepository.Get(request.Id); var admins = await _accountAdminRepository.GetAllUsersByAccountId(request.Id); var users = await _accountUserRepository.GetAllUsersByAccountId(request.Id); var response = CreateResponse(account, admins, users); return Result<AccountResponse>.Ok(response); } catch (Exception e) { return Result<AccountResponse>.Failure(e.Message); } }
private bool IsAdminOrAccountAdmin(string email, int accountId) { var user = _userRepository.GetUserByEmail(email).Result; var isAdminOfAccount = _accountAdminRepository.IsAdminOfAccount(user.Id, accountId).Result; if (user.Role.Name != "Admin" && !isAdminOfAccount) { return(false); } return(true); }
public async Task <Result <bool> > Handle(UpdateAccountCommand request, CancellationToken cancellationToken) { try { var user = await _userRepository.GetUserByEmail(request.ModifiedBy); var isAdminOfAccount = await _accountAdminRepository.IsAdminOfAccount(user.Id, request.Id); if (user.Role.Name != "Admin" && !isAdminOfAccount) { return(Result <bool> .AccessDenied("No access!")); } var account = await _accountRepository.Get(request.Id); if (account == null) { return(Result <bool> .BadRequest($"Account with Id: {request.Id} not found!")); } if (request.AdminEmails != null) { await UpdateAccountAdmins(account.Id, request.ModifiedBy, request.AdminEmails); } if (request.UserEmails != null) { await UpdateAccountUsers(account.Id, request.ModifiedBy, request.UserEmails); } var accountType = await _accountTypeRepository.GetAccountTypeByName(request.AccountType); UpdateAccountProperties(account, request, accountType); var res = await _accountRepository.Update(account); return(Result <bool> .Ok(res)); } catch (Exception e) { return(Result <bool> .Failure(e.Message)); } }