/// <summary> /// </summary> /// <param name="personId"></param> /// <param name="newAuthorization"></param> /// <param name="NewWellknownAuthorizationLevel"></param> /// <returns></returns> private async Task ModifySafeAut(Guid personId, Guid newAuthorization, WellknownAuthorizationLevel NewWellknownAuthorizationLevel) { try { if (await _context.Persons.AnyAsync(p => p.Id == personId)) { if (await AuthNotModified(personId)) { return; } var user = await _context.Persons.FirstAsync(p => p.Id == personId); if (user.SafeAuthModel == null) { return; } user.SafeAuthModel.AutId = newAuthorization; var encryptedRelation = await _securLib.EncryptEntityRelation(user, user.AutorizationLevel); user.SafeAuthModel.Control = await _securLib.EncriptLine(encryptedRelation); await _context.SaveChangesAsync().ConfigureAwait(false); } } catch (Exception ex) { StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during ModifySafeAut", MethodBase.GetCurrentMethod(), ex); } }
/// <summary> /// determine if the authorization of the car match the needed Authorization /// </summary> /// <param name="CarModelId">car anagraphic</param> /// <param name="AuthNeeded">needed Authorization</param> /// <returns></returns> public async Task <bool> IsCarAutorized(string CarModelId, WellknownAuthorizationLevel AuthNeeded) { try { CarAnagraphicModel car = await _context.Cars.FirstOrDefaultAsync(ca => ca.LicencePlate == CarModelId).ConfigureAwait(false); return(await IsAutorized(car.Owner.Id, AuthNeeded).ConfigureAwait(false)); } catch (Exception ex) { StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during IsAutorized of CAR", MethodBase.GetCurrentMethod(), ex); return(false); } }
public async Task <bool> Access(AccessModel newAccess) { newAccess.GrantedAccess = false; if (newAccess.personEntered.Equals(Guid.Empty) || !string.IsNullOrEmpty(newAccess.Plate) || !string.IsNullOrEmpty(newAccess.CardNumber)) { try { PersonModel owner = new PersonModel(); WellknownAuthorizationLevel accessLevelNeed = WellknownAuthorizationLevel.Root; if (newAccess.personEntered.Equals(Guid.Empty) && !string.IsNullOrEmpty(newAccess.Plate)) { if (await _context.Cars.AnyAsync(c => c.LicencePlate == newAccess.Plate)) { var entered = await _context.Cars.FirstAsync(c => c.LicencePlate == newAccess.Plate).ConfigureAwait(false); owner = entered.Owner; } } else if (await _context.Persons.AnyAsync(c => c.CardNumber.CardNumber == newAccess.CardNumber).ConfigureAwait(false)) { owner = await _context.Persons.FirstAsync(a => a.CardNumber.CardNumber == newAccess.CardNumber).ConfigureAwait(false); if (!await _context.Nodes.AnyAsync(n => n.Name == newAccess.NodeName && n.MacAddress == newAccess.MacAddress)) { return(false); } var node = await _context.Nodes.FirstAsync(n => n.Name == newAccess.NodeName && n.MacAddress == newAccess.MacAddress); accessLevelNeed = node.AuthValue; } if (await _autorizationManagerService.IsAutorized(owner.Id, accessLevelNeed).ConfigureAwait(false)) { newAccess.personEntered = owner.Id; newAccess.GrantedAccess = true; } await _context.Access.AddAsync(newAccess).ConfigureAwait(false); await _context.SaveChangesAsync().ConfigureAwait(false); //StaticEventHandler.SendMail(new MailEventArgs(ResourceString.AccessCarMailSubject, ResourceString.AccessCarMailBody, DateTime.UtcNow)); } catch (Exception ex) { StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during Card verification", MethodBase.GetCurrentMethod(), ex); return(false); } } return(newAccess.GrantedAccess); }
/// <summary> /// private member that verify the user an the autorization /// </summary> /// <param name="personModelId"></param> /// <param name="AuthNeeded"></param> /// <returns></returns> public async Task <bool> IsAutorized(Guid personModelId, WellknownAuthorizationLevel AuthNeeded) { try { if (await _context.Persons.AnyAsync(p => p.Id == personModelId).ConfigureAwait(false)) { PersonModel usr = await _context.Persons.FirstOrDefaultAsync(p => p.Id == personModelId).ConfigureAwait(false); if (usr.AutorizationLevel.AuthValue >= AuthNeeded && await AuthNotModified(usr.Id).ConfigureAwait(false) && (usr.AutorizationLevel.ExpirationDate.Date >= DateTime.Today.Date || usr.AutorizationLevel.AuthValue == WellknownAuthorizationLevel.Root)) { return(true); } return(false); } return(false); } catch (Exception ex) { StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during IsAutorized of person", MethodBase.GetCurrentMethod(), ex); return(false); } }
/// <summary> /// </summary> /// <param name="personModelIdRequest"></param> /// <param name="personModelId"></param> /// <param name="newAuthorization"></param> /// <returns></returns> public async Task AutorizationModify(Guid personModelIdRequest, Guid personModelId, WellknownAuthorizationLevel newAuthorization) { try { var Usr = await _context.Persons.FirstOrDefaultAsync(p => p.Id == personModelId).ConfigureAwait(false); //in case of lowering the authorization i can do only if i'm not the only one with it, and only if thiere is at least one root if (newAuthorization < Usr.AutorizationLevel.AuthValue && await _context.Persons.AnyAsync(p => p.AutorizationLevel.AuthValue == Usr.AutorizationLevel.AuthValue && p.Id != Usr.Id).ConfigureAwait(false) && await _context.Persons.AnyAsync(p => p.AutorizationLevel.AuthValue == WellknownAuthorizationLevel.Root && p.Id != Usr.Id).ConfigureAwait(false)) { Usr.AutorizationLevel.AuthValue = newAuthorization; await ModifySafeAut(Usr.Id, Usr.AutorizationLevel.Id, Usr.AutorizationLevel.AuthValue).ConfigureAwait(false); } else if (newAuthorization > Usr.AutorizationLevel.AuthValue) { var UsrRequest = await _context.Persons.FirstOrDefaultAsync(p => p.Id == personModelIdRequest).ConfigureAwait(false); if (Usr.AutorizationLevel.AuthValue == WellknownAuthorizationLevel.Root) { Usr.AutorizationLevel.AuthValue = newAuthorization; await ModifySafeAut(Usr.Id, Usr.AutorizationLevel.Id, Usr.AutorizationLevel.AuthValue).ConfigureAwait(false); } } await _context.SaveChangesAsync().ConfigureAwait(false); } catch (Exception ex) { StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during AutorizationModify", MethodBase.GetCurrentMethod(), ex); } }