/// <summary> /// Registers a new access point. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> public async Task Consume(ConsumeContext <IRegisterAccessPoint> context) { if (!Thread.CurrentPrincipal.IsInRole(WellKnownRoles.Manager)) { context.Respond(new VoidResult("Not authorized")); return; } var result = await _listDepartmentsRequest.Request(ListCommand.WithoutParameters); var departmentExists = result.Departments.Any(x => x.DepartmentName == context.Message.AccessPoint.Department); if (!departmentExists) { context.Respond(result); return; } var accessPoint = new Data.Entities.AccessPoint { AccessPointId = context.Message.AccessPoint.AccessPointId, Name = context.Message.AccessPoint.Name, Description = context.Message.AccessPoint.Description, Department = context.Message.AccessPoint.Department, ManagedBy = Thread.CurrentPrincipal.UserName() }; _databaseContext.AccessPoints.Insert(accessPoint); _databaseContext.Commit(); context.Respond(new VoidResult()); }
/// <summary> /// Allows access for the specified access point. /// </summary> /// <param name="accessPointId">The access point identifier.</param> /// <param name="strategy">The strategy.</param> /// <returns></returns> public async Task <IVoidResult> AllowAccess(Guid accessPointId, IAccessManagementStrategy strategy) { var errorMessage = await strategy.ValidateRequest(); if (errorMessage != null) { return(new VoidResult(errorMessage)); } var accessPoint = _databaseContext.AccessPoints.GetById(accessPointId); if (accessPoint == null) { return(new VoidResult($"Access point {accessPointId} is not registered.")); } var accessRights = strategy.FindAccessRights() ?? strategy.CreateAccessRights(); var rule = strategy.FindAccessRule(accessRights, accessPoint); if (rule == null) { rule = strategy.CreateAccessRule(); rule.AccessPoint = accessPoint; accessRights.AddAccessRule(rule); } else { if (!strategy.UpdateAccessRule(rule)) { return(new VoidResult()); } } if (accessRights.Id == 0) { _databaseContext.AccessRights.Insert(accessRights); } else { _databaseContext.AccessRights.Update(accessRights); } _databaseContext.Commit(); await strategy.OnAccessGranted(_bus, accessPoint); return(new VoidResult()); }
/// <summary> /// Logs the access attempt. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public Task Consume(ConsumeContext <IAccessAttempted> context) { if (!Thread.CurrentPrincipal.IsInRole(WellKnownRoles.ClientService)) { Log.Warn("Invalid credential."); return(Task.FromResult(false)); } var accessPoint = _databaseContext.AccessPoints.GetById(context.Message.AccessPointId); if (accessPoint == null) { Log.Warn("Invalid access point attempted. AccessPointId: " + context.Message.AccessPointId); return(Task.FromResult(false)); } var user = _databaseContext.Users.Filter(x => x.BiometricHash == context.Message.BiometricHash).SingleOrDefault(); var entry = new Data.Entities.LogEntry { AccessPoint = accessPoint, AttemptedHash = context.Message.BiometricHash, UserName = user?.UserName, CreatedUtc = context.Message.CreatedUtc, Failed = context.Message.Failed }; _databaseContext.Logs.Insert(entry); _databaseContext.Commit(); return(Task.FromResult(true)); }
/// <summary> /// Grants management rights for the specified user. /// </summary> /// <param name="context">The context.</param> /// <returns></returns> public async Task Consume(ConsumeContext <IGrantManagementRights> context) { if (!Thread.CurrentPrincipal.IsInRole(WellKnownRoles.Manager)) { await context.RespondAsync(new VoidResult("Not authorized")); return; } var userResult = await _findUserRequest.Request(new FindUserByName(context.Message.UserName)); if (userResult.User == null) { await context.RespondAsync(new VoidResult("User cannot be found")); return; } var entity = _databaseContext.DelegatedRights .Filter(x => x.Grantor == Thread.CurrentPrincipal.UserName() && x.Grantee == context.Message.UserName) .SingleOrDefault(); if (entity != null) { await context.RespondAsync(new VoidResult()); return; } entity = new DelegatedRights { Grantor = Thread.CurrentPrincipal.UserName(), Grantee = userResult.User.UserName }; _databaseContext.DelegatedRights.Insert(entity); _databaseContext.Commit(); await _bus.Publish(new ManagementRightsGranted(entity.Grantor, entity.Grantee)); await context.RespondAsync(new VoidResult()); }
public async Task Invoke(HttpContext context, IDatabaseContext databaseContext, LogService logService) { try { await _next(context); databaseContext.Commit(); } catch (Exception ex) { var error = ex.ToString(); Debug.WriteLine(error); Console.Write(error); databaseContext.Rollback(); logService.Error(error); await HandleExceptionAsync(context, ex); } }
public TEntity Add(TEntity entity) { _dbSet.Add(entity); _databaseContext.Commit(); return(entity); }
/// <summary> /// Sync commit to the context /// </summary> public void Commit() { _ctx.Commit(); }
public void Commit() => _context.Commit();
public void Commit() { _context.Commit(); }
public void Commit() { _databaseContext.Commit(); }
public async Task LogEmailTransaction(EmailLogs emailLogDetails) { await _databaseContext.Repository <EmailLogs>().AddAsync(emailLogDetails); _databaseContext.Commit(); }