Esempio n. 1
0
 public Task <IVoidResult> DenyAccess(Guid accessPointId, IAccessManagementStrategy strategy)
 {
     Contract.Requires(accessPointId != Guid.Empty);
     Contract.Requires(strategy != null);
     Contract.Ensures(Contract.Result <Task <IVoidResult> >() != null);
     return(null);
 }
        /// <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>
        ///     Denies 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> DenyAccess(Guid accessPointId, IAccessManagementStrategy strategy)
        {
            var errorMessage = await strategy.ValidateRequest();

            if (errorMessage != null)
            {
                return(new VoidResult(errorMessage));
            }

            var accessRights = strategy.FindAccessRights();

            if (accessRights == null)
            {
                return(new VoidResult());
            }

            var accessPoint = _databaseContext.AccessPoints.GetById(accessPointId);

            if (accessPoint == null)
            {
                return(new VoidResult($"Access point {accessPointId} is not registered."));
            }

            var accessRule = strategy.FindAccessRule(accessRights, accessPoint);

            if (accessRule == null)
            {
                return(new VoidResult());
            }

            accessRights.RemoveAccessRule(accessRule);
            if (accessRights.AccessRules.Any())
            {
                _databaseContext.AccessRights.Update(accessRights);
            }

            else
            {
                _databaseContext.AccessRights.Delete(accessRights);
            }
            _databaseContext.Commit();

            await strategy.OnAccessDenied(_bus, accessPoint);

            return(new VoidResult());
        }