<commit_before>package com.softteco.toolset.security;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.softteco.toolset.restlet.UserSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 *
 * @author serge
 */
public class SecurityInterceptor implements MethodInterceptor {

    @Inject
    private Provider<UserSession> userSessionProvider;
    
    @Override
    public Object invoke(final MethodInvocation mi) throws Throwable {
        System.out.println("CURRENT USER: " + userSessionProvider.get().getUsername() + " " + userSessionProvider.get().isLoggedIn() + " " + Arrays.toString(userSessionProvider.get().getRoles().toArray(new String[0])));
        handleAssertAuthorizedUser(mi);
        handleAssertUser(mi);
        handleAssertRoles(mi);
        return mi.proceed();
    }

    private void handleAssertRoles(final MethodInvocation mi) throws SecurityException {
        final List<String> roles = new ArrayList<>();
        if (mi.getMethod().getDeclaringClass().getAnnotation(AssertRoles.class) != null) {
            roles.addAll(Arrays.asList(mi.getMethod().getDeclaringClass().getAnnotation(AssertRoles.class).roles()));
        }
        if (mi.getMethod().getDeclaringClass().getAnnotation(AssertRole.class) != null) {
            roles.add(mi.getMethod().getDeclaringClass().getAnnotation(AssertRole.class).role());
        }
        final AssertRoles methodAssertRoles = mi.getMethod().getAnnotation(AssertRoles.class);
        final AssertRole methodAssertRole = mi.getMethod().getAnnotation(AssertRole.class);
        if (methodAssertRoles != null) {
            if (!methodAssertRoles.append()) {
                roles.clear();
            }
            roles.addAll(Arrays.asList(methodAssertRoles.roles()));
        } else if (methodAssertRole != null) {
            if (!methodAssertRole.append()) {
                roles.clear();
            }
            roles.add(methodAssertRole.role());<commit_msg>Fixed issue.<commit_after>package com.softteco.toolset.security;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.softteco.toolset.restlet.UserSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 *
 * @author serge
 */
public class SecurityInterceptor implements MethodInterceptor {

    @Inject
    private Provider<UserSession> userSessionProvider;
    
    @Override
    public Object invoke(final MethodInvocation mi) throws Throwable {
        System.out.println("CURRENT USER: " + userSessionProvider.get().getUsername() + " " + userSessionProvider.get().isLoggedIn());
        handleAssertAuthorizedUser(mi);
        handleAssertUser(mi);
        
        System.out.println("ROLES: " + Arrays.toString(userSessionProvider.get().getRoles().toArray(new String[0])));
        handleAssertRoles(mi);
        return mi.proceed();
    }

    private void handleAssertRoles(final MethodInvocation mi) throws SecurityException {
        final List<String> roles = new ArrayList<>();
        if (mi.getMethod().getDeclaringClass().getAnnotation(AssertRoles.class) != null) {
            roles.addAll(Arrays.asList(mi.getMethod().getDeclaringClass().getAnnotation(AssertRoles.class).roles()));
        }
        if (mi.getMethod().getDeclaringClass().getAnnotation(AssertRole.class) != null) {
            roles.add(mi.getMethod().getDeclaringClass().getAnnotation(AssertRole.class).role());
        }
        final AssertRoles methodAssertRoles = mi.getMethod().getAnnotation(AssertRoles.class);
        final AssertRole methodAssertRole = mi.getMethod().getAnnotation(AssertRole.class);
        if (methodAssertRoles != null) {
            if (!methodAssertRoles.append()) {
                roles.clear();
            }
            roles.addAll(Arrays.asList(methodAssertRoles.roles()));
        } else if (methodAssertRole != null) {
            if (!methodAssertRole.append()) {
                roles.clear();
            }
            roles.add(methodAssertRole.role());
{'fraction_non_alphanumeric': 0.12426453283125441, 'fraction_numerical': 0.0004706989879971758, 'mean_word_length': 3.367934224049332, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1697112', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1185, 'n_words': 183}