Search in sources :

Example 1 with UserContext

use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.

the class CopyValuesControllerTest method testQuery.

private GetDataResponse testQuery(String path, ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline> pipeline, Map<String, String[]> params) {
    N2oInvocationFactory invocationFactory = Mockito.mock(N2oInvocationFactory.class);
    TestDataProviderEngine testDataProviderEngine = new TestDataProviderEngine();
    testDataProviderEngine.setResourceLoader(new DefaultResourceLoader());
    Mockito.when(invocationFactory.produce(Mockito.any(Class.class))).thenReturn(testDataProviderEngine);
    ContextEngine contextEngine = Mockito.mock(ContextEngine.class);
    UserContext userContext = new UserContext(contextEngine);
    ContextProcessor contextProcessor = new ContextProcessor(userContext);
    N2oQueryProcessor queryProcessor = new N2oQueryProcessor(invocationFactory, new N2oQueryExceptionHandler());
    N2oEnvironment env = new N2oEnvironment();
    env.setContextProcessor(contextProcessor);
    queryProcessor.setEnvironment(env);
    N2oSubModelsProcessor subModelsProcessor = Mockito.mock(N2oSubModelsProcessor.class);
    Mockito.doNothing().when(subModelsProcessor);
    DataProcessingStack dataProcessingStack = Mockito.mock(SpringDataProcessingStack.class);
    CopyValuesController copyValuesController = new CopyValuesController(dataProcessingStack, queryProcessor, subModelsProcessor, null, null);
    Map<String, Object> map = new HashMap<>();
    map.put("CopyValuesController", copyValuesController);
    N2oRouter router = new N2oRouter(builder.getEnvironment(), pipeline);
    N2oControllerFactory factory = new N2oControllerFactory(map);
    factory.setEnvironment(builder.getEnvironment());
    DataController controller = new DataController(factory, builder.getEnvironment(), router);
    return controller.getData(path, params, userContext);
}
Also used : N2oInvocationFactory(net.n2oapp.framework.engine.data.N2oInvocationFactory) CopyValuesController(net.n2oapp.framework.ui.controller.query.CopyValuesController) SpringDataProcessingStack(net.n2oapp.framework.engine.modules.stack.SpringDataProcessingStack) DataProcessingStack(net.n2oapp.framework.engine.modules.stack.DataProcessingStack) HashMap(java.util.HashMap) UserContext(net.n2oapp.framework.api.user.UserContext) N2oSubModelsProcessor(net.n2oapp.framework.config.util.N2oSubModelsProcessor) TestDataProviderEngine(net.n2oapp.framework.engine.data.json.TestDataProviderEngine) ContextEngine(net.n2oapp.framework.api.context.ContextEngine) N2oQueryProcessor(net.n2oapp.framework.engine.data.N2oQueryProcessor) N2oEnvironment(net.n2oapp.framework.config.compile.pipeline.N2oEnvironment) N2oRouter(net.n2oapp.framework.config.register.route.N2oRouter) ContextProcessor(net.n2oapp.framework.api.context.ContextProcessor) N2oQueryExceptionHandler(net.n2oapp.framework.engine.data.N2oQueryExceptionHandler) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 2 with UserContext

use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.

the class DefaultValuesControllerTest method testQuery.

private GetDataResponse testQuery(String path, ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline> pipeline, Map<String, String[]> params) {
    N2oInvocationFactory invocationFactory = Mockito.mock(N2oInvocationFactory.class);
    TestDataProviderEngine testDataProviderEngine = new TestDataProviderEngine();
    testDataProviderEngine.setResourceLoader(new DefaultResourceLoader());
    Mockito.when(invocationFactory.produce(Mockito.any(Class.class))).thenReturn(testDataProviderEngine);
    ContextEngine contextEngine = Mockito.mock(ContextEngine.class);
    UserContext userContext = new UserContext(contextEngine);
    ContextProcessor contextProcessor = new ContextProcessor(userContext);
    N2oQueryProcessor queryProcessor = new N2oQueryProcessor(invocationFactory, new N2oQueryExceptionHandler());
    N2oEnvironment env = new N2oEnvironment();
    env.setContextProcessor(contextProcessor);
    queryProcessor.setEnvironment(env);
    N2oSubModelsProcessor subModelsProcessor = Mockito.mock(N2oSubModelsProcessor.class);
    Mockito.doNothing().when(subModelsProcessor);
    DataProcessingStack dataProcessingStack = Mockito.mock(SpringDataProcessingStack.class);
    SimpleDefaultValuesController valuesController = new SimpleDefaultValuesController(dataProcessingStack, queryProcessor, subModelsProcessor, null, null);
    Map<String, Object> map = new HashMap<>();
    map.put("SimpleDefaultValuesController", valuesController);
    N2oRouter router = new N2oRouter(builder.getEnvironment(), pipeline);
    N2oControllerFactory factory = new N2oControllerFactory(map);
    factory.setEnvironment(builder.getEnvironment());
    DataController controller = new DataController(factory, builder.getEnvironment(), router);
    return controller.getData(path, params, userContext);
}
Also used : N2oInvocationFactory(net.n2oapp.framework.engine.data.N2oInvocationFactory) SpringDataProcessingStack(net.n2oapp.framework.engine.modules.stack.SpringDataProcessingStack) DataProcessingStack(net.n2oapp.framework.engine.modules.stack.DataProcessingStack) SimpleDefaultValuesController(net.n2oapp.framework.ui.controller.query.SimpleDefaultValuesController) HashMap(java.util.HashMap) UserContext(net.n2oapp.framework.api.user.UserContext) N2oSubModelsProcessor(net.n2oapp.framework.config.util.N2oSubModelsProcessor) TestDataProviderEngine(net.n2oapp.framework.engine.data.json.TestDataProviderEngine) ContextEngine(net.n2oapp.framework.api.context.ContextEngine) N2oQueryProcessor(net.n2oapp.framework.engine.data.N2oQueryProcessor) N2oEnvironment(net.n2oapp.framework.config.compile.pipeline.N2oEnvironment) N2oRouter(net.n2oapp.framework.config.register.route.N2oRouter) ContextProcessor(net.n2oapp.framework.api.context.ContextProcessor) N2oQueryExceptionHandler(net.n2oapp.framework.engine.data.N2oQueryExceptionHandler) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader)

Example 3 with UserContext

use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.

the class SecurityProviderTest method collectRestrictionsRemovePermitAllFilters.

@Test
public void collectRestrictionsRemovePermitAllFilters() {
    SecurityProvider securityProvider = new SecurityProvider(permissionApi, true);
    UserContext userContext = new UserContext(new TestContextEngine());
    SecurityFilters securityFilters = new SecurityFilters();
    ArrayList<N2oObjectFilter> authFilters = new ArrayList<>();
    authFilters.add(new N2oObjectFilter("authGender", "man", FilterType.eq, "authGenderFilter"));
    authFilters.add(new N2oObjectFilter("authPosition", "developer", FilterType.eq, "authPositionFilter"));
    securityFilters.setAuthenticatedFilters(authFilters);
    // проверка удаления фильтров по всем пользователям
    when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
    Set<String> permitAllRemoveFilters = new HashSet<>();
    permitAllRemoveFilters.add("authPositionFilter");
    securityFilters.setRemovePermitAllFilters(permitAllRemoveFilters);
    List<Restriction> restrictions = securityProvider.collectRestrictions(securityFilters, userContext);
    assertThat(restrictions.contains(new Restriction("authPosition", "developer", FilterType.eq)), is(false));
    assertThat(restrictions.contains(new Restriction("authGender", "man", FilterType.eq)), is(true));
}
Also used : Restriction(net.n2oapp.framework.api.criteria.Restriction) N2oObjectFilter(net.n2oapp.framework.access.metadata.accesspoint.model.N2oObjectFilter) UserContext(net.n2oapp.framework.api.user.UserContext) SecurityProvider(net.n2oapp.framework.access.data.SecurityProvider) TestContextEngine(net.n2oapp.framework.api.test.TestContextEngine) Test(org.junit.Test)

Example 4 with UserContext

use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.

the class SecurityProviderTest method checkRestrictions.

@Test
public void checkRestrictions() {
    SecurityProvider securityProvider = new SecurityProvider(permissionApi, true);
    SecurityProvider notStrictSecurityProvider = new SecurityProvider(permissionApi, false);
    UserContext userContext = new UserContext(new TestContextEngine());
    SecurityFilters securityFilters = new SecurityFilters();
    securityFilters.setAuthenticatedFilters(Arrays.asList(new N2oObjectFilter("foo", "1", FilterType.eq, "filter1"), new N2oObjectFilter("name", FilterType.isNotNull, "filter7"), new N2oObjectFilter("surname", "1", FilterType.eqOrIsNull, "filter6")));
    securityFilters.setAnonymousFilters(Arrays.asList(new N2oObjectFilter("age", FilterType.isNull, "filter8"), new N2oObjectFilter("foo", "1", FilterType.notEq, "filter2")));
    securityFilters.setRoleFilters(Collections.singletonMap("role1", Collections.singletonList(new N2oObjectFilter("bar", new String[] { "1", "2", "3" }, FilterType.in, "filter3"))));
    securityFilters.setPermissionFilters(Collections.singletonMap("permission1", Collections.singletonList(new N2oObjectFilter("list", new String[] { "1", "2", "#{three}" }, FilterType.contains, "filter4"))));
    securityFilters.setUserFilters(Collections.singletonMap("username1", Collections.singletonList(new N2oObjectFilter("name", "#{username}", FilterType.eq, "filter5"))));
    // аутентифицирован
    when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
    // foo == 1 and name != null and surname == null
    securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("name", "Ivan"), securityFilters, userContext);
    // foo != 1
    notStrictSecurityProvider.checkRestrictions(new DataSet().add("foo", 1).add("name", "Ivan"), securityFilters, userContext);
    try {
        securityProvider.checkRestrictions(new DataSet().add("name", "Ivan").add("foo", 2), securityFilters, userContext);
        Assert.fail();
    } catch (AccessDeniedException e) {
        assertThat(e.getMessage(), endsWith("foo"));
    }
    // foo == null
    notStrictSecurityProvider.checkRestrictions(new DataSet(), securityFilters, userContext);
    try {
        securityProvider.checkRestrictions(new DataSet().add("name", "Ivan"), securityFilters, userContext);
        Assert.fail();
    } catch (AccessDeniedException e) {
        assertThat(e.getMessage(), endsWith("foo"));
    }
    // анонимный доступ
    when(permissionApi.hasAuthentication(userContext)).thenReturn(false);
    // foo != 1
    securityProvider.checkRestrictions(new DataSet().add("foo", 2), securityFilters, userContext);
    // foo == 1
    try {
        securityProvider.checkRestrictions(new DataSet().add("foo", 1), securityFilters, userContext);
        Assert.fail();
    } catch (AccessDeniedException e) {
        assertThat(e.getMessage(), endsWith("foo"));
    }
    // age != null
    try {
        securityProvider.checkRestrictions(new DataSet().add("foo", 3).add("age", 10), securityFilters, userContext);
        Assert.fail();
    } catch (AccessDeniedException e) {
        assertThat(e.getMessage(), endsWith("age"));
    }
    // доступ аутентифицированным и по ролям
    when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
    when(permissionApi.hasRole(userContext, "role1")).thenReturn(true);
    // bar in (1, 2, 3)
    try {
        securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("name", "Ivan"), securityFilters, userContext);
    } catch (AccessDeniedException e) {
        Assert.fail();
    }
    // bar not in (1, 2, 3)
    try {
        securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 4).add("name", "Ivan"), securityFilters, userContext);
        Assert.fail();
    } catch (AccessDeniedException e) {
        assertThat(e.getMessage(), endsWith("bar"));
    }
    // доступ аутентифицированным, по ролям и по полномочиям
    when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
    when(permissionApi.hasRole(userContext, "role1")).thenReturn(true);
    when(permissionApi.hasPermission(userContext, "permission1")).thenReturn(true);
    userContext.set("three", 3);
    // list contains (1, 2, 3)
    try {
        securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("name", "Ivan").add("list", Arrays.asList(3, 2, 1, 4)), securityFilters, userContext);
    } catch (AccessDeniedException e) {
        Assert.fail();
    }
    // list not contains (1, 2, 3)
    try {
        securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("name", "Ivan").add("list", Arrays.asList(1, 2)), securityFilters, userContext);
        Assert.fail();
    } catch (AccessDeniedException e) {
        assertThat(e.getMessage(), endsWith("list"));
    }
    // доступ аутентифицированным, по ролям, по полномочиям, по имени пользователя
    when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
    when(permissionApi.hasRole(userContext, "role1")).thenReturn(true);
    when(permissionApi.hasPermission(userContext, "permission1")).thenReturn(true);
    when(permissionApi.hasUsername(userContext, "username1")).thenReturn(true);
    userContext.set("username", "Joe");
    // name == #{username}
    try {
        securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("list", Arrays.asList(3, 2, 1, 4)).add("name", "Joe"), securityFilters, userContext);
    } catch (AccessDeniedException e) {
        Assert.fail();
    }
    // name != #{username}
    try {
        securityProvider.checkRestrictions(new DataSet().add("foo", 1).add("bar", 2).add("list", Arrays.asList(3, 2, 1, 4)).add("name", "Doe"), securityFilters, userContext);
    } catch (AccessDeniedException e) {
        assertThat(e.getMessage(), endsWith("name"));
    }
}
Also used : AccessDeniedException(net.n2oapp.framework.access.exception.AccessDeniedException) N2oObjectFilter(net.n2oapp.framework.access.metadata.accesspoint.model.N2oObjectFilter) DataSet(net.n2oapp.criteria.dataset.DataSet) UserContext(net.n2oapp.framework.api.user.UserContext) SecurityProvider(net.n2oapp.framework.access.data.SecurityProvider) TestContextEngine(net.n2oapp.framework.api.test.TestContextEngine) Test(org.junit.Test)

Example 5 with UserContext

use of net.n2oapp.framework.api.user.UserContext in project n2o-framework by i-novus-llc.

the class SecurityProviderTest method checkAccessHasRole.

@Test
public void checkAccessHasRole() {
    SecurityProvider securityProvider = new SecurityProvider(permissionApi, true);
    UserContext userContext = new UserContext(new TestContextEngine());
    when(permissionApi.hasAuthentication(userContext)).thenReturn(true);
    when(permissionApi.hasRole(userContext, "admin")).thenReturn(true);
    when(permissionApi.hasRole(userContext, "role1")).thenReturn(false);
    when(permissionApi.hasRole(userContext, "role2")).thenReturn(false);
    Security.SecurityObject securityObject = new Security.SecurityObject();
    securityObject.setDenied(false);
    securityObject.setPermitAll(false);
    securityObject.setAnonymous(true);
    securityObject.setAuthenticated(false);
    securityObject.setRoles(new HashSet<>(Arrays.asList("role2", "role1", "admin")));
    Map<String, Security.SecurityObject> securityObjectMap = new HashMap<>();
    securityObjectMap.put("custom", securityObject);
    Security security = new Security();
    security.setSecurityMap(securityObjectMap);
    try {
        securityProvider.checkAccess(security, userContext);
        Assert.fail("Expected exception to be thrown");
    } catch (Exception e) {
        assertThat(e, instanceOf(AccessDeniedException.class));
    }
    when(permissionApi.hasRole(userContext, "admin")).thenReturn(false);
    try {
        securityProvider.checkAccess(security, userContext);
        Assert.fail("Expected exception to be thrown");
    } catch (Exception e) {
        assertThat(e, instanceOf(AccessDeniedException.class));
    }
}
Also used : UserContext(net.n2oapp.framework.api.user.UserContext) SecurityProvider(net.n2oapp.framework.access.data.SecurityProvider) TestContextEngine(net.n2oapp.framework.api.test.TestContextEngine) AccessDeniedException(net.n2oapp.framework.access.exception.AccessDeniedException) UnauthorizedException(net.n2oapp.framework.access.exception.UnauthorizedException) Test(org.junit.Test)

Aggregations

UserContext (net.n2oapp.framework.api.user.UserContext)22 SecurityProvider (net.n2oapp.framework.access.data.SecurityProvider)19 TestContextEngine (net.n2oapp.framework.api.test.TestContextEngine)19 Test (org.junit.Test)19 N2oObjectFilter (net.n2oapp.framework.access.metadata.accesspoint.model.N2oObjectFilter)11 Restriction (net.n2oapp.framework.api.criteria.Restriction)10 AccessDeniedException (net.n2oapp.framework.access.exception.AccessDeniedException)6 UnauthorizedException (net.n2oapp.framework.access.exception.UnauthorizedException)5 DataSet (net.n2oapp.criteria.dataset.DataSet)4 HashMap (java.util.HashMap)3 ContextEngine (net.n2oapp.framework.api.context.ContextEngine)3 N2oRouter (net.n2oapp.framework.config.register.route.N2oRouter)3 N2oInvocationFactory (net.n2oapp.framework.engine.data.N2oInvocationFactory)3 TestDataProviderEngine (net.n2oapp.framework.engine.data.json.TestDataProviderEngine)3 DataProcessingStack (net.n2oapp.framework.engine.modules.stack.DataProcessingStack)3 SpringDataProcessingStack (net.n2oapp.framework.engine.modules.stack.SpringDataProcessingStack)3 DefaultResourceLoader (org.springframework.core.io.DefaultResourceLoader)3 ContextProcessor (net.n2oapp.framework.api.context.ContextProcessor)2 N2oEnvironment (net.n2oapp.framework.config.compile.pipeline.N2oEnvironment)2 N2oSubModelsProcessor (net.n2oapp.framework.config.util.N2oSubModelsProcessor)2