예제 #1
0
        /// <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);
            }
        }
예제 #2
0
        public async Task <bool> CreateCard(CardCreateModel model)
        {
            if (model == null)
            {
                return(false);
            }
            if (string.IsNullOrWhiteSpace(model.CardNumber))
            {
                return(false);
            }
            try
            {
                if (await _context.CardModels.AnyAsync(c => c.CardNumber == model.CardNumber))
                {
                    return(false);
                }
                await _context.CardModels.AddAsync(_cardCreateModelToCardConverter.Map(model));

                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                StaticEventHandler.Log(System.Diagnostics.TraceLevel.Error, "error during IsAutorized od person", MethodBase.GetCurrentMethod(), ex);
                return(false);
            }
        }
예제 #3
0
        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);
        }