public async Task <Result <bool> > Handle(UpdateGateCommand command, CancellationToken cancellationToken) { try { var user = await _userRepository.GetUserByEmail(command.ModifiedBy); var adminAccess = await _userGateRepository.CheckAdminAccess(command.Id, user.Id); if (!adminAccess && !user.Role.Name.Equals("Admin")) { return(Result <bool> .AccessDenied("You have no access to modify gate!")); } var gateType = await _gateTypeRepository.GetGateTypeByName(command.GateTypeName); var account = await _accountRepository.GetAccountByName(command.AccountName); var currentGate = await _gateRepository.Get(command.Id); await UpdateUserGates(command.Id, command.ModifiedBy, command.Users); UpdateGateProperties(currentGate, command, gateType, account); var res = await _gateRepository.Update(currentGate); return(Result <bool> .Ok(res)); } catch (Exception e) { return(Result <bool> .Failure(e.Message)); } }
public async Task <Result <GateResponse> > Handle(GetGateRequest request, CancellationToken cancellationToken) { try { var user = await _userRepository.GetUserByEmail(request.RequestedBy); var access = await _userGateRepository.CheckAccess(request.Id, user.Id); var adminAccess = await _userGateRepository.CheckAdminAccess(request.Id, user.Id) || user.Role.Name == "Admin" || _gateRepository.IsAccountAdminOfTheGate(request.Id, user.Id); if (!access && user.Role.Name == "User" && !(_gateRepository.IsAccountAdminOfTheGate(request.Id, user.Id))) { return(Result <GateResponse> .AccessDenied("No access to this gate!")); } var gate = await _gateRepository.Get(request.Id); var response = CreateResponse(gate, adminAccess); return(Result <GateResponse> .Ok(response)); } catch (Exception e) { return(Result <GateResponse> .Failure(e.Message)); } }
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); } }
public async Task<Result<ListResult<AccountResponse>>> Handle(GetAllAccountsRequest request, CancellationToken cancellationToken) { try { var user = await _userRepository.GetUserByEmail(request.RequestEmail); if (user.Role.Name != "Admin" && !IsAccountAdmin(user.Id)) { return Result<ListResult<AccountResponse>>.AccessDenied("No access!"); } var result = await _accountRepository.GetList(request.PaginationEntry, request.Sorting, request.Filtering); if (user.Role.Name != "Admin") { var records = result.Records; var newRecords = records.ToList().Where(x => x.Admins.Select(x => x.UserId).Contains(user.Id)); result = new ListResult<Account>(newRecords, newRecords.Count()); } var response = CreateListResponse(result.Records.ToList()); return Result<ListResult<AccountResponse>>.Ok(response); } catch (Exception e) { return Result<ListResult<AccountResponse>>.Failure(e.Message); } }
public async Task <Result <int> > Handle(SumAdminsByAccount request, CancellationToken cancellationToken) { try { if (!IsAdminOrAccountAdmin(request.RequestedEmail, request.AccountId)) { return(Result <int> .AccessDenied("No access!")); } var result = await _userRepository.GetSumOfAdminsByAccountId(request.AccountId); return(Result <int> .Ok(result)); } catch (Exception e) { return(Result <int> .Failure(e.Message)); } }
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)); } }
public async Task <Result <int> > Handle(SumErrorsByAccount request, CancellationToken cancellationToken) { try { if (!IsAdminOrAccountAdmin(request.RequestedEmail, request.AccountId)) { return(Result <int> .AccessDenied("No access!")); } var result = await _logService.GetAll(); var errorNum = result.Where(x => x.EventType.Name == "Error" && x.AccountId == request.AccountId).Count(); return(Result <int> .Ok(errorNum)); } catch (Exception e) { return(Result <int> .Failure(e.Message)); } }
public async Task <Result <ListResult <GetEntersResponse> > > Handle(GetEnters request, CancellationToken cancellationToken) { try { if (!IsAdminOrAccountAdmin(request.RequestedEmail, request.AccountId)) { return(Result <ListResult <GetEntersResponse> > .AccessDenied("No access!")); } var result = await _logService.GetAll(); var myLogs = result.Where(x => x.AccountId == request.AccountId && x.EventTypeId == 4 && x.Action == "success" && x.CreatedAt >= request.From && x.CreatedAt <= request.To); var listResponse = new List <GetEntersResponse>(); foreach (var log in myLogs) { var res = listResponse.FirstOrDefault(x => x.Email == log.User.Email && x.GateName == log.Gate.Name && x.FirstUse.Date == log.CreatedAt.Date); if (res == null) { listResponse.Add(new GetEntersResponse { Name = $"{log.User.FirstName} {log.User.LastName}", Email = log.User.Email, FirstUse = log.CreatedAt, Date = log.CreatedAt.Date, GateName = log.Gate.Name, LastUse = log.CreatedAt, IsUserOfAccount = await _accountUserRepository.Get(request.AccountId, log.UserId.Value) == null ? false : true }); } else { if (res.FirstUse > log.CreatedAt) { res.FirstUse = log.CreatedAt; } if (res.LastUse < log.CreatedAt) { res.LastUse = log.CreatedAt; } } } if (!String.IsNullOrWhiteSpace(request.Filtering)) { listResponse = listResponse.Where(x => x.Name.Contains(request.Filtering) || x.Email.Contains(request.Filtering) || x.GateName.Contains(request.Filtering)).ToList(); } var response = new ListResult <GetEntersResponse>(listResponse, listResponse.Count()); return(Result <ListResult <GetEntersResponse> > .Ok(response)); } catch (Exception e) { return(Result <ListResult <GetEntersResponse> > .Failure(e.Message)); } }