Exemple #1
0
        public RequestHandlerGuard(Type type, EntitySecurityConfigurationRegister entitySecurityConfigurationRegister)
        {
            // If child implements ISecureHandler<,,>.
            this.ContextCommand = type.GetTypeInfo().GetInterfaces()
                                  .SingleOrDefault(t =>
            {
                if (!t.GetTypeInfo().IsGenericType)
                {
                    return(false);
                }

                var genericTypeDefinition = t.GetGenericTypeDefinition();
                return
                (genericTypeDefinition == typeof(ISecureHandler <, ,>) ||
                 genericTypeDefinition == typeof(IAsyncSecureHandler <, ,>));
            });

            this.GetPermission = type.GetTypeInfo().GetMethod(nameof(ISecureHandler.GetPermission));

            if (this.ContextCommand != null)
            {
                // Get T from ISecureHandler<TContext,TRequest>
                var contextType = this.ContextCommand.GenericTypeArguments[0];

                if (!entitySecurityConfigurationRegister.Guards.TryGetValue(contextType, out var guard))
                {
                    throw new BusinessException($"Context of type `{contextType.Name}` was not registered with the `SecurityGuard`.");
                }

                this.EntitySecurityConfiguration = guard;
                this.EnforceContextPermission    =
                    this.EntitySecurityConfiguration.PermissionManager.GetTypeInfo().GetMethod(nameof(SystemPermissionManager.EnforceCanDo));
            }

            // If child implements ISecureHandler.
            this.SystemCommand = type.GetTypeInfo().GetInterfaces().SingleOrDefault(t => t == typeof(ISecureHandler));
        }
 public RequestHandlerGuardRegister(EntitySecurityConfigurationRegister entitySecurityConfigurationRegister)
 {
     this.entitySecurityConfigurationRegister = entitySecurityConfigurationRegister;
 }