public async ValueTask <Result> CheckInAgencyPermission(AgentContext agent, InAgencyPermissions permission) { var key = _flow.BuildKey(nameof(PermissionChecker), nameof(CheckInAgencyPermission), agent.AgencyId.ToString(), agent.AgentId.ToString()); var storedPermissions = await _flow.GetOrSetAsync( key : key, getValueFunction : async() => await GetPermissions(agent.AgentId, agent.AgencyId), AgentPermissionsCacheLifeTime); if (Equals(storedPermissions, default)) { return(Result.Failure("The agent isn't affiliated with the agency")); } return(!storedPermissions.HasFlag(permission) ? Result.Failure($"Agent does not have the '{permission}' permission") : Result.Success()); Task <InAgencyPermissions> GetPermissions(int agentId, int agencyId) { return(_context.AgentAgencyRelations .Where(r => r.AgentId == agentId) .Where(r => r.AgencyId == agencyId) .Select(r => r.InAgencyPermissions) .SingleOrDefaultAsync()); } }
public async ValueTask <Result> CheckInAgencyPermission(AgentContext agent, InAgencyPermissions permission) { var key = _flow.BuildKey(nameof(PermissionChecker), nameof(CheckInAgencyPermission), agent.AgencyId.ToString(), agent.AgentId.ToString()); var storedPermissions = await _flow.GetOrSetAsync(key : key, getValueFunction : async() => await GetPermissions(agent.AgentId, agent.AgencyId), AgentPermissionsCacheLifeTime); if (Equals(storedPermissions, default)) { return(Result.Failure("The agent isn't affiliated with the agency")); } var hasPermission = storedPermissions.Any(p => p.HasFlag(permission)); return(hasPermission ? Result.Success() : Result.Failure($"Agent does not have the '{permission}' permission")); // TODO: Get permissions in a single request async Task <List <InAgencyPermissions> > GetPermissions(int agentId, int agencyId) { var roleIds = await _context.AgentAgencyRelations .Where(r => r.AgentId == agentId) .Where(r => r.AgencyId == agencyId) .Select(r => r.AgentRoleIds) .SingleOrDefaultAsync(); if (roleIds == default) { return(default);
public AgentContext(int agentId, string firstName, string lastName, string email, string title, string position, int counterpartyId, string counterpartyName, int agencyId, bool isMaster, InAgencyPermissions inAgencyPermissions) { AgentId = agentId; FirstName = firstName; LastName = lastName; Email = email; Title = title; Position = position; CounterpartyId = counterpartyId; CounterpartyName = counterpartyName; AgencyId = agencyId; IsMaster = isMaster; InAgencyPermissions = inAgencyPermissions; }
public AgentContext(int agentId, string firstName, string lastName, string email, string title, string position, int agencyId, string agencyName, bool isMaster, InAgencyPermissions inAgencyPermissions, string countryHtId, string localityHtId, List <int> agencyAncestors) { AgentId = agentId; FirstName = firstName; LastName = lastName; Email = email; Title = title; Position = position; AgencyId = agencyId; AgencyName = agencyName; IsMaster = isMaster; InAgencyPermissions = inAgencyPermissions; CountryHtId = countryHtId; LocalityHtId = localityHtId; AgencyAncestors = agencyAncestors; }
private static List <InAgencyPermissions> GetActualPermissions(AgencyVerificationStates agencyVerificationState, InAgencyPermissions inAgencyPermissions) { const InAgencyPermissions readOnlyPermissions = InAgencyPermissions.AccommodationAvailabilitySearch | InAgencyPermissions.AgentInvitation | InAgencyPermissions.PermissionManagement | InAgencyPermissions.ObserveAgents; switch (agencyVerificationState) { case AgencyVerificationStates.DeclinedVerification: case AgencyVerificationStates.PendingVerification: return(new List <InAgencyPermissions>(0)); case AgencyVerificationStates.ReadOnly: return((inAgencyPermissions & readOnlyPermissions).ToList()); case AgencyVerificationStates.FullAccess: return(inAgencyPermissions.ToList()); default: throw new ArgumentException($"Invalid agency verification state {agencyVerificationState}"); } }
public static List <InAgencyPermissions> ToList(this InAgencyPermissions permissions) { return(InAgencyPermissionValues .Where(v => permissions.HasFlag(v)) .ToList()); }
private async Task <Result <List <InAgencyPermissions> > > SetInAgencyPermissions(int agentId, InAgencyPermissions permissions, AgentContext agent) { return(await Result.Success() .Bind(GetRelation) .Ensure(IsPermissionManagementRightNotLost, "Cannot revoke last permission management rights") .Map(UpdatePermissions)); async Task <Result <AgentAgencyRelation> > GetRelation() { var relation = await _context.AgentAgencyRelations .SingleOrDefaultAsync(r => r.AgentId == agentId && r.AgencyId == agent.AgencyId); return(relation is null ? Result.Failure <AgentAgencyRelation>( $"Could not find relation between the agent {agentId} and the agency {agent.AgencyId}") : Result.Success(relation)); } async Task <bool> IsPermissionManagementRightNotLost(AgentAgencyRelation relation) { if (permissions.HasFlag(InAgencyPermissions.PermissionManagement)) { return(true); } return((await _context.AgentAgencyRelations .Where(r => r.AgencyId == relation.AgencyId && r.AgentId != relation.AgentId) .ToListAsync()) .Any(c => c.InAgencyPermissions.HasFlag(InAgencyPermissions.PermissionManagement))); } async Task <List <InAgencyPermissions> > UpdatePermissions(AgentAgencyRelation relation) { relation.InAgencyPermissions = permissions; _context.AgentAgencyRelations.Update(relation); await _context.SaveChangesAsync(); return(relation.InAgencyPermissions.ToList()); } }
private Task AddCounterpartyRelation(Agent agent, AgentAgencyRelationTypes relationType, InAgencyPermissions permissions, int agencyId) { _context.AgentAgencyRelations.Add(new AgentAgencyRelation { AgentId = agent.Id, Type = relationType, InAgencyPermissions = permissions, AgencyId = agencyId, IsActive = true }); return(_context.SaveChangesAsync()); }