class NamespaceSecurityAdvice extends AbstractServiceAdvice {

    @Before("serviceMethods()")
    public void checkPermission(JoinPoint joinPoint)
    {

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();

        List<NamespacePermission> namespacePermissions = new ArrayList<>();
        if (method.isAnnotationPresent(NamespacePermissions.class))
        {
            namespacePermissions.addAll(Arrays.asList(method.getAnnotation(NamespacePermissions.class).value()));
        }
        else if (method.isAnnotationPresent(NamespacePermission.class))
        {
            namespacePermissions.add(method.getAnnotation(NamespacePermission.class));
        }

        if (!namespacePermissions.isEmpty())
        {
            String[] parameterNames = methodSignature.getParameterNames();
            Object[] args = joinPoint.getArgs();

            Map<String, Object> variables = new HashMap<>();
            for (int i = 0; i < parameterNames.length; i++)
            {
                variables.put(parameterNames[i], args[i]);
            }

            List<AccessDeniedException> accessDeniedExceptions = new ArrayList<>();
            for (NamespacePermission namespacePermission : namespacePermissions)
            {
                for (String field : namespacePermission.fields())
                {
                    try
                    {
                        namespaceSecurityHelper.checkPermission(spelExpressionHelper.evaluate(field, Object.class, variables), namespacePermission
                            .permissions());
                    }
                    catch (AccessDeniedException accessDeniedException)
                    {
                        accessDeniedExceptions.add(accessDeniedException);
                    }
                }
            }
            if (!accessDeniedExceptions.isEmpty())
            {
                throw namespaceSecurityHelper.getAccessDeniedException(accessDeniedExceptions);
            }
        }
    }

}