コード例 #1
0
#pragma warning disable 1998
        public async override Task Execute(IServiceProvider provider, ActionHandler handler, IAction input, User?user)
        {
            AuthorizationAttribute?attribute = handler.GetType().GetCustomAttribute <AuthorizationAttribute>();

            if (attribute != null)
            {
                if (attribute.Scope == null || attribute.Action == null)
                {
                    throw new ArgumentNullException("Authorization attribute improperly set up. Missing scope or action.");
                }

                if (user == null)
                {
                    throw new AuthorizationException("Authentication required.");
                }

                if (attribute.Action != null && attribute.Scope != null)
                {
                    var roles = await roleRepo.FindForUser(user);

                    var perms = await permissionRepo.FindForRoles(roles);

                    if (!perms.HasPermission(attribute.Action, attribute.Scope))
                    {
                        throw new AuthorizationException("Not permitted.");
                    }
                }
            }
        }
コード例 #2
0
        public async override Task Execute(IServiceProvider provider, ActionHandler handler, IAction input, User?user)
        {
            ValidationAttribute?attribute = handler.GetType().GetCustomAttribute <ValidationAttribute>();

            if (attribute != null)
            {
                var validator = (provider.GetRequiredService(attribute.Validator) as IValidator) !;

                var result = await validator.ValidateAsync(input);

                if (!result.IsValid)
                {
                    throw new ValidationException(result);
                }
            }
        }