public OcLayerException(string message, [CanBeNull] Exception exception, string resourceName, OcLayerType layerType, string layerTypeName, IOcRequest request = null, [CanBeNull] Type entityType = null, [CanBeNull] object entity = null, [CanBeNull] object entityKey = null, [CallerMemberName] string callerName = null, [CallerFilePath] string callerFile = null, [CallerLineNumber] int callerLine = 0)
            : base(string.IsNullOrWhiteSpace(message) ? "Unspecified Layer Routing Error" : message, exception, entityType, entity, entityKey, callerName, callerFile, callerLine)
        {
            ResourceName = resourceName;
            if (ResourceName != null)
            {
                PropertySet(nameof(ResourceName), ResourceName);
            }

            LayerType = layerType;
            if (LayerType != OcLayerType.Unknown)
            {
                PropertySet(nameof(LayerType), LayerType);
            }

            LayerTypeName = layerTypeName;
            if (LayerTypeName != null)
            {
                PropertySet(nameof(LayerTypeName), LayerTypeName);
            }

            Request = request;
            if (Request != null)
            {
                PropertySet(nameof(Request), Request);
            }
        }
Ejemplo n.º 2
0
 /// <inheritdoc />
 public IOcLayer <TRequest> GetResourceLayer([NotNull] string resourceName, OcLayerType layerType, [NotNull] TRequest request)
 {
     if (!_serviceLocator.IsAllowed(LayerType, layerType))
     {
         throw NewRoutingException(resourceName, layerType, null, $"Failed to get Resource [{resourceName}] because Layer [{LayerTypeName}] is not allowed to access Layer [{layerType}]");
     }
     return(_serviceLocator.GetResourceLayer(resourceName, layerType, request));
 }
Ejemplo n.º 3
0
        internal OcLayerRestrictedCrudFactory(OcLayerType layerType, OcLayerConfiguration <TRequest> serviceLocator)
        {
            if (layerType == OcLayerType.Unknown || layerType == OcLayerType.Other)
            {
                throw new ArgumentOutOfRangeException(nameof(layerType));
            }
            if (serviceLocator == null)
            {
                throw new ArgumentNullException(nameof(serviceLocator));
            }

            LayerType       = layerType;
            LayerTypeName   = LayerType.ToString();
            _serviceLocator = serviceLocator;
        }
Ejemplo n.º 4
0
        public OcLayerRoutingException(string message, [CanBeNull] Exception exception, string resourceName, OcLayerType layerType, string layerTypeName, OcLayerType nextLayerType, string nextLayerTypeName, [CallerMemberName] string callerName = null, [CallerFilePath] string callerFile = null, [CallerLineNumber] int callerLine = 0)
            : base(message, exception, resourceName, layerType, layerTypeName, null, null, null, null, callerName, callerFile, callerLine)
        {
            NextLayerType = nextLayerType;
            if (NextLayerType != OcLayerType.Unknown)
            {
                PropertySet(nameof(NextLayerType), NextLayerType);
            }

            NextLayerTypeName = nextLayerTypeName;
            if (NextLayerTypeName != null)
            {
                PropertySet(nameof(NextLayerTypeName), NextLayerTypeName);
            }
        }
        /// <summary>
        /// Returns true if <paramref name="layerType"/> is allowed in the system.
        /// </summary>
        public bool IsAllowed(OcLayerType layerType)
        {
            if (layerType == OcLayerType.Unknown)
            {
                return(false);
            }
            if (layerType == OcLayerType.Other)
            {
                return(_layerTypeNames.Count > 0);
            }

            using (var readLock = _layerTypes.ReadLock())
            {
                readLock.Lock();

                // If no layer types have been configured as allowed, then allow all layer types.
                return(_layerTypes.Count == 0 || _layerTypes.Contains(layerType));
            }
        }
        private OcLayerBase([NotNull] IOcLayerConfiguration <TRequest> configuration, OcLayerType layerType, string layerTypeName, string resourceName, [NotNull] TRequest request)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (layerType == OcLayerType.Unknown)
            {
                throw new ArgumentOutOfRangeException(nameof(layerType), layerType, $"{nameof(layerType)} can not be {nameof(OcLayerType.Unknown)}");
            }
            if (layerType == OcLayerType.Other && string.IsNullOrWhiteSpace(layerTypeName))
            {
                throw new ArgumentOutOfRangeException(nameof(layerType), layerType, $"{nameof(layerTypeName)} must be specified for {nameof(OcLayerType.Other)}");
            }
            if (layerType != OcLayerType.Other && layerTypeName != null)
            {
                throw new ArgumentOutOfRangeException(nameof(layerType), layerType, $"{nameof(layerTypeName)} must be null unless {nameof(layerType)} is {nameof(OcLayerType.Other)}");
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (!configuration.IsAllowed(layerType, layerTypeName))
            {
                throw new ArgumentOutOfRangeException(nameof(layerType), $"Layers of LayerType [{layerType}] and LayerTypeName [{layerTypeName}] are not allowed in this systems");
            }

            InstanceId = Interlocked.Increment(ref _globalInstanceCounter);

            LayerType     = layerType;
            LayerTypeName = LayerType == OcLayerType.Other ? LayerType.ToString() : layerTypeName?.Trim();
            Debug.Assert(!string.IsNullOrWhiteSpace(LayerTypeName));

            ResourceName = DetermineResourceName(resourceName, LayerTypeName, GetType());

            Request = request;

            Factory = configuration.RestrictedFactoryCrud(LayerType, LayerTypeName);
            Debug.Assert(Factory != null);

            Log?.WriteLine($"LAYER CREATE - InstanceID [{InstanceId}] Type [{GetType().FriendlyName()}] Resource [{ResourceName}] LayerTypeName [{LayerTypeName}]");
        }
        /// <summary>
        /// Returns true if the From Layer Type is allowed to access the To Layer Type.
        /// Note that if no layer interaction restrictions have been configured then any valid layer is allowed to interact with any other valid layer.
        /// </summary>
        public bool IsAllowed(OcLayerType fromLayerType, string fromLayerTypeName, OcLayerType toLayerType, string toLayerTypeName)
        {
            if (fromLayerType == OcLayerType.Unknown || toLayerType == OcLayerType.Unknown)
            {
                return(false);
            }

            if (fromLayerType == OcLayerType.Other && string.IsNullOrWhiteSpace(fromLayerTypeName))
            {
                throw new ArgumentNullException(nameof(fromLayerTypeName));
            }
            if (toLayerType == OcLayerType.Other && string.IsNullOrWhiteSpace(toLayerTypeName))
            {
                throw new ArgumentNullException(nameof(toLayerTypeName));
            }

            if (fromLayerType != OcLayerType.Other)
            {
                fromLayerTypeName = fromLayerType.ToString();
            }
            if (toLayerType != OcLayerType.Other)
            {
                toLayerTypeName = toLayerType.ToString();
            }


            using (var readLock = _layerNameInteractions.ReadLock())
            {
                readLock.Lock();

                // If restrictions have been configured use them to answer the question
                if (_layerNameInteractions.TryGetValue(fromLayerTypeName, out var allowed))
                {
                    return(allowed?.Contains(toLayerTypeName) ?? false);
                }

                // If restrictions are not configured, assume any type of interaction is allowed (assuming the from layer type is allowed by the system).
                return(IsAllowed(fromLayerType, fromLayerTypeName) && IsAllowed(toLayerType, toLayerTypeName));
            }
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Preferred constructor when <paramref name="layerType"/> is not <see cref="OcLayerType.Other"/>
 /// It is better to have <paramref name="resourceName"/> specified manually than the alternative constructor that attempts to determine it programatically.
 /// </summary>
 /// <param name="layerType"></param>
 /// <param name="configuration"></param>
 /// <param name="resourceName"></param>
 /// <param name="request"></param>
 /// <param name="nextLayer"></param>
 protected OcLayerAdapterReadBase([NotNull] IOcLayerConfiguration <TRequest> configuration, OcLayerType layerType, string resourceName, [NotNull] TRequest request, TNextLayer nextLayer)
     : base(configuration, layerType, resourceName, request, nextLayer)
 {
 }
Ejemplo n.º 9
0
 protected OcLayerLogicReadSearchBase([NotNull] IOcLayerConfiguration <TRequest> configuration, OcLayerType layerType, string resourceName, [NotNull] TRequest request)
     : base(configuration, layerType, resourceName, request)
 {
 }
        protected OcLayerException NewConstructorException(string message, [CanBeNull] Exception exception, string resourceName, OcLayerType layerType, string layerTypeName, IOcRequest request = null, [CallerMemberName] string callerName = null, [CallerFilePath] string callerFile = null, [CallerLineNumber] int callerLine = 0)
        {
            // ReSharper disable ExplicitCallerInfoArgument
            var ex = new OcLayerException($"Error constructing {GetType().FriendlyName()}" + (string.IsNullOrWhiteSpace(message) ? "" : message), exception, resourceName, layerType, layerTypeName, request, null, null, null, callerName, callerFile, callerLine);

            // ReSharper restore ExplicitCallerInfoArgument
            Request.ResponseMessages.Add(OcApiMessageType.Error, $"Layer Constructor Exception: {ex.Summary}");
            return(ex);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Preferred constructor for use with <see cref="OcLayerType.Other"/> and specifying a <paramref name="layerTypeName"/>.
 /// It is better to have <paramref name="resourceName"/> specified manually than the alternative constructor that attempts to determine it programatically.
 /// </summary>
 /// <param name="layerTypeName"></param>
 /// <param name="configuration"></param>
 /// <param name="resourceName"></param>
 /// <param name="request"></param>
 /// <param name="nextLayerType"></param>
 /// <param name="nextLayerTypeName"></param>
 protected OcLayerAdapterCrudBase([NotNull] IOcLayerConfiguration <TRequest> configuration, [NotNull] string layerTypeName, string resourceName, [NotNull] TRequest request, OcLayerType nextLayerType, string nextLayerTypeName)
     : base(configuration, layerTypeName, resourceName, request, nextLayerType, nextLayerTypeName)
 {
 }
Ejemplo n.º 12
0
 /// <inheritdoc />
 public IOcLayerCrudSearch <TRequest, TEntity, TKey, TSearch> GetLayerCrudSearch <TEntity, TKey, TSearch>([NotNull] string resourceName, OcLayerType layerType, [NotNull] TRequest request) where TEntity : class where TSearch : class
 => GetResourceLayer(resourceName, layerType, request) as IOcLayerCrudSearch <TRequest, TEntity, TKey, TSearch>;
Ejemplo n.º 13
0
 /// <inheritdoc />
 public IOcLayerRead <TRequest, TEntity, TKey> GetLayerRead <TEntity, TKey>([NotNull] string resourceName, OcLayerType layerType, [NotNull] TRequest request) where TEntity : class
 => GetResourceLayer(resourceName, layerType, request) as IOcLayerRead <TRequest, TEntity, TKey>;
Ejemplo n.º 14
0
 protected OcLayerRoutingException NewRoutingException(string resourceName, OcLayerType nextLayerType, string nextLayerTypeName, string message, [CanBeNull] Exception exception = null, [CallerMemberName] string callerName = null, [CallerFilePath] string callerFile = null, [CallerLineNumber] int callerLine = 0)
 {
     // ReSharper disable ExplicitCallerInfoArgument
     return(new OcLayerRoutingException(message, exception, resourceName, LayerType, LayerTypeName, nextLayerType, nextLayerTypeName, callerName, callerFile, callerLine));
     // ReSharper restore ExplicitCallerInfoArgument
 }
Ejemplo n.º 15
0
 protected OcLayerApiCrudSearchBase([NotNull] IOcLayerConfiguration <TRequest> configuration, OcLayerType layerType, string resourceName, [NotNull] TRequest request, TNext nextLayer)
     : base(configuration, layerType, resourceName, request, nextLayer)
 {
 }
 protected OcLayerLogicCrudBase([NotNull] IOcLayerConfiguration <TRequest> configuration, OcLayerType layerType, string resourceName, [NotNull] TRequest request, OcLayerType nextLayerType)
     : base(configuration, layerType, resourceName, request, nextLayerType)
 {
 }
 /// <summary>
 /// Preferred constructor when <paramref name="layerType"/> is not <see cref="OcLayerType.Other"/>
 /// It is better to have <paramref name="resourceName"/> specified manually than the alternative constructor that attempts to determine it programatically.
 /// </summary>
 /// <param name="layerType"></param>
 /// <param name="configuration"></param>
 /// <param name="resourceName"></param>
 /// <param name="request"></param>
 /// <param name="nextLayerType"></param>
 protected OcLayerAdapterBase([NotNull] IOcLayerConfiguration <TRequest> configuration, OcLayerType layerType, string resourceName, [NotNull] TRequest request, OcLayerType nextLayerType)
     : base(configuration, layerType, resourceName, request, nextLayerType)
 {
     DataMapper = configuration.DataMapper;
     if (DataMapper == null)
     {
         throw NewConstructorException($"{nameof(DataMapper)} not available from {nameof(configuration)}", null, resourceName, layerType, null, request);
     }
 }
 /// <summary>
 /// If available returns the specified interface to the layer that provides <paramref name="layerType"/> services for <paramref name="resourceName"/>.
 /// Returns null if a suitable resource layer could not be found.
 /// </summary>
 /// <typeparam name="TLayer">The interface (which inherits from <see cref="IOcLayer{TRequest}"/> being sought.</typeparam>
 /// <param name="resourceName">Name of the resource that they layer provides access to</param>
 /// <param name="layerType">Type of layer to search for</param>
 protected virtual TLayer GetLayer <TLayer>(string resourceName, OcLayerType layerType) where TLayer : class, IOcLayer <TRequest>
 {
     return(Factory.GetResourceLayer(resourceName, layerType, Request) as TLayer);
 }
 /// <summary>
 /// Returns true if the specified layer type is allowed in the system.
 /// Note that if <see cref="OcLayerType.Other"/> is specified then <paramref name="layerTypeName"/> must have a value.
 /// </summary>
 public bool IsAllowed(OcLayerType layerType, string layerTypeName)
 {
     return(layerType == OcLayerType.Other ? IsAllowed(layerTypeName) : IsAllowed(layerType));
 }
 /// <summary>
 /// Alternative constructor for when <paramref name="layerType"/> is not <see cref="OcLayerType.Other"/>
 /// When possible it is better to use the other, preferred constructor and specify the <see cref="ResourceName"/> rather than attempting to determine it programatically.
 /// </summary>
 /// <param name="layerType"></param>
 /// <param name="configuration"></param>
 /// <param name="request"></param>
 protected OcLayerBase([NotNull] IOcLayerConfiguration <TRequest> configuration, OcLayerType layerType, [NotNull] TRequest request)
     : this(configuration, layerType, null, null, request)
 {
 }
 /// <summary>
 /// Returns true if <paramref name="fromLayerType"/> is allowed to access <paramref name="toLayerType"/>.
 /// Note that if no layer interaction restrictions have been configured then any valid layer is allowed to interact with any other valid layer.
 /// </summary>
 public bool IsAllowed(OcLayerType fromLayerType, OcLayerType toLayerType)
 {
     return(IsAllowed(fromLayerType, null, toLayerType, null));
 }
 protected OcLayerRepositoryCrudBase([NotNull] IOcLayerConfiguration <TRequest> configuration, OcLayerType layerType, string resourceName, [NotNull] TRequest request)
     : base(configuration, layerType, resourceName, request)
 {
 }
 protected OcLayerLogicBase([NotNull] IOcLayerConfiguration <TRequest> configuration, OcLayerType layerType, string resourceName, [NotNull] TRequest request)
     : base(configuration, layerType, resourceName, request)
 {
     AuthorizationService = configuration.AuthorizationService;
 }