Пример #1
0
        public static AuthorizationRules.AuthorizeOperation ToAuthorizationOperation(this PortalOperation operation)
        {
            switch (operation)
            {
            case PortalOperation.Create:
            case PortalOperation.CreateChild:
                return(AuthorizationRules.AuthorizeOperation.Create);

            case PortalOperation.Fetch:
            case PortalOperation.FetchChild:
                return(AuthorizationRules.AuthorizeOperation.Fetch);

            case PortalOperation.Insert:
            case PortalOperation.InsertChild:
            case PortalOperation.Update:
            case PortalOperation.UpdateChild:
                return(AuthorizationRules.AuthorizeOperation.Update);

            case PortalOperation.Delete:
            case PortalOperation.DeleteChild:
                return(AuthorizationRules.AuthorizeOperation.Delete);

            default:
                break;
            }

            throw new Exception($"{operation.ToString()} cannot be converted to AuthorizationOperation");
        }
Пример #2
0
        public MethodInfo MethodForOperation(PortalOperation operation, IEnumerable <Type> criteriaTypes)
        {
            var        methods        = MethodsForOperation(operation);
            MethodInfo matchingMethod = null;

            if (methods != null)
            {
                foreach (var m in methods)
                {
                    var parameters = m.GetParameters();

                    // No criteria
                    if (!criteriaTypes.Any() && !parameters.Any())
                    {
                        return(m);
                    }
                    else if (criteriaTypes.Any() && parameters.Any() && parameters.Count() >= criteriaTypes.Count())
                    {
                        var match     = true;
                        var critEnum  = criteriaTypes.GetEnumerator();
                        var paramEnum = parameters.Cast <ParameterInfo>().Select(p => p.ParameterType).GetEnumerator();

                        // With Array's Current doesn't become null
                        var paramHasValue = paramEnum.MoveNext();
                        var critHasValue  = critEnum.MoveNext();

                        // All of the criteria parameter types match up
                        // And any left over are registered
                        while (match && paramHasValue)
                        {
                            if (critHasValue && !paramEnum.Current.IsAssignableFrom(critEnum.Current))
                            {
                                match = false;
                            }
                            else if (!critHasValue && !Scope.IsRegistered(paramEnum.Current)) // For recognizing multiple positives for the same criteria
                            {
                                match = false;
                            }

                            paramHasValue = paramEnum.MoveNext();
                            critHasValue  = critEnum.MoveNext();
                        }

                        // At the end of the Crit list
                        // The parameter list can
                        if (match)
                        {
                            if (matchingMethod != null)
                            {
                                throw new Exception($"More then one method for {operation.ToString()} with criteria [{string.Join(",", criteriaTypes)}] on {typeof(T).FullName}");
                            }

                            matchingMethod = m;
                        }
                    }
                }
            }
            return(matchingMethod);
        }
Пример #3
0
        protected async Task CallOperationMethod(T target, PortalOperation operation, bool throwException = true)
        {
            var success = await OperationManager.TryCallOperation(target, operation);

            if (!success && throwException)
            {
                throw new OperationMethodCallFailedException($"{operation.ToString()} method with no criteria not found on {target.GetType().FullName}.");
            }
        }
Пример #4
0
        protected async Task CallOperationMethod(T target, PortalOperation operation, object[] criteria, Type[] criteriaTypes)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }

            var success = await OperationManager.TryCallOperation(target, operation, criteria, criteriaTypes);

            if (!success)
            {
                throw new OperationMethodCallFailedException($"{operation.ToString()} method on {target.GetType().FullName} with criteria [{string.Join(", ", criteriaTypes.Select(x => x.FullName))}] not found.");
            }
        }