Search in sources :

Example 1 with Authorization

use of io.vertigo.account.authorization.metamodel.Authorization in project vertigo by KleeGroup.

the class UserAuthorizations method addAuthorization.

/**
 * Add a authorization to this User.
 * Authorization must be previously declared.
 *
 * @param authorization Authorization to add
 * @return this UserAuthorizations
 */
public UserAuthorizations addAuthorization(final Authorization authorization) {
    Assertion.checkNotNull(authorization);
    // -----
    authorizationRefs.put(authorization.getName(), new DefinitionReference<>(authorization));
    if (authorization.getEntityDefinition().isPresent()) {
        authorizationMapRefs.computeIfAbsent(new DefinitionReference<>(authorization.getEntityDefinition().get()), key -> new HashSet<>()).add(new DefinitionReference<>(authorization));
        for (final Authorization grantedAuthorization : authorization.getGrants()) {
            if (!hasAuthorization(grantedAuthorization::getName)) {
                // On test pour ne pas créer de boucle
                addAuthorization(grantedAuthorization);
            }
        }
    }
    return this;
}
Also used : AuthorizationName(io.vertigo.account.authorization.metamodel.AuthorizationName) DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) Set(java.util.Set) HashMap(java.util.HashMap) DefinitionReference(io.vertigo.core.definition.DefinitionReference) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Role(io.vertigo.account.authorization.metamodel.Role) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) Assertion(io.vertigo.lang.Assertion) Authorization(io.vertigo.account.authorization.metamodel.Authorization) Collections(java.util.Collections) Authorization(io.vertigo.account.authorization.metamodel.Authorization) DefinitionReference(io.vertigo.core.definition.DefinitionReference) HashSet(java.util.HashSet)

Example 2 with Authorization

use of io.vertigo.account.authorization.metamodel.Authorization in project vertigo by KleeGroup.

the class VSecurityManagerTest method testSecuritySqlOnEntity.

@Test
public void testSecuritySqlOnEntity() {
    final Record recordTooExpensive = createRecord();
    recordTooExpensive.setAmount(10000d);
    final Record recordOtherUser = createRecord();
    recordOtherUser.setUtiIdOwner(2000L);
    final Record recordOtherUserAndTooExpensive = createRecord();
    recordOtherUserAndTooExpensive.setUtiIdOwner(2000L);
    recordOtherUserAndTooExpensive.setAmount(10000d);
    final Authorization recordRead = getAuthorization(RecordAuthorizations.ATZ_RECORD$READ);
    final UserSession userSession = securityManager.<TestUserSession>createUserSession();
    try {
        securityManager.startCurrentUserSession(userSession);
        authorizationManager.obtainUserAuthorizations().withSecurityKeys("utiId", DEFAULT_UTI_ID).withSecurityKeys("typId", DEFAULT_TYPE_ID).withSecurityKeys("montantMax", DEFAULT_MONTANT_MAX).addAuthorization(recordRead);
        final boolean canReadRecord = authorizationManager.hasAuthorization(RecordAuthorizations.ATZ_RECORD$READ);
        Assert.assertTrue(canReadRecord);
        final SqlDialect sqlDialect = new PostgreSqlDataBase().getSqlDialect();
        final Tuple2<String, CriteriaCtx> readRecordSql = authorizationManager.getCriteriaSecurity(Record.class, RecordOperations.READ).toSql(sqlDialect);
        // read -> MONTANT<=${montantMax} or UTI_ID_OWNER=${utiId}
        Assert.assertEquals("( AMOUNT <= #AMOUNT_0# OR UTI_ID_OWNER = #UTI_ID_OWNER_1# ) ", readRecordSql.getVal1());
        Assert.assertEquals(100.0, readRecordSql.getVal2().getAttributeValue("AMOUNT_0"));
        Assert.assertEquals(1000L, readRecordSql.getVal2().getAttributeValue("UTI_ID_OWNER_1"));
    } finally {
        securityManager.stopCurrentUserSession();
    }
}
Also used : Authorization(io.vertigo.account.authorization.metamodel.Authorization) CriteriaCtx(io.vertigo.dynamo.criteria.CriteriaCtx) TestUserSession(io.vertigo.account.data.TestUserSession) UserSession(io.vertigo.persona.security.UserSession) SqlDialect(io.vertigo.database.sql.vendor.SqlDialect) Record(io.vertigo.account.authorization.model.Record) TestUserSession(io.vertigo.account.data.TestUserSession) PostgreSqlDataBase(io.vertigo.database.impl.sql.vendor.postgresql.PostgreSqlDataBase) Test(org.junit.Test)

Example 3 with Authorization

use of io.vertigo.account.authorization.metamodel.Authorization in project vertigo by KleeGroup.

the class VSecurityManagerTest method testToString.

@Test
public void testToString() {
    final Authorization admUsr = getAuthorization(GlobalAuthorizations.ATZ_ADMUSR);
    admUsr.toString();
    final Authorization admPro = getAuthorization(GlobalAuthorizations.ATZ_ADMPRO);
    admPro.toString();
/*Pour la couverture de code, et 35min de dette technique.... */
}
Also used : Authorization(io.vertigo.account.authorization.metamodel.Authorization) Test(org.junit.Test)

Example 4 with Authorization

use of io.vertigo.account.authorization.metamodel.Authorization in project vertigo by KleeGroup.

the class VSecurityManagerTest method testSecuritySearchOnEntity.

@Test
public void testSecuritySearchOnEntity() {
    final Record recordTooExpensive = createRecord();
    recordTooExpensive.setAmount(10000d);
    final Record recordOtherUser = createRecord();
    recordOtherUser.setUtiIdOwner(2000L);
    final Record recordOtherUserAndTooExpensive = createRecord();
    recordOtherUserAndTooExpensive.setUtiIdOwner(2000L);
    recordOtherUserAndTooExpensive.setAmount(10000d);
    final Authorization recordRead = getAuthorization(RecordAuthorizations.ATZ_RECORD$READ);
    final UserSession userSession = securityManager.<TestUserSession>createUserSession();
    try {
        securityManager.startCurrentUserSession(userSession);
        authorizationManager.obtainUserAuthorizations().withSecurityKeys("utiId", DEFAULT_UTI_ID).withSecurityKeys("typId", DEFAULT_TYPE_ID).withSecurityKeys("montantMax", DEFAULT_MONTANT_MAX).addAuthorization(recordRead).addAuthorization(getAuthorization(RecordAuthorizations.ATZ_RECORD$READ_HP)).addAuthorization(getAuthorization(RecordAuthorizations.ATZ_RECORD$WRITE)).addAuthorization(getAuthorization(RecordAuthorizations.ATZ_RECORD$CREATE)).addAuthorization(getAuthorization(RecordAuthorizations.ATZ_RECORD$DELETE));
        final boolean canReadRecord = authorizationManager.hasAuthorization(RecordAuthorizations.ATZ_RECORD$READ);
        Assert.assertTrue(canReadRecord);
        // read -> MONTANT<=${montantMax} or UTI_ID_OWNER=${utiId}
        Assert.assertEquals("(+AMOUNT:<=100.0) (+UTI_ID_OWNER:1000)", authorizationManager.getSearchSecurity(Record.class, RecordOperations.READ));
        Assert.assertEquals("(AMOUNT:<=100.0 UTI_ID_OWNER:1000)", authorizationManager.getSearchSecurity(Record.class, RecordOperations.READ2));
        Assert.assertEquals("(*:*)", authorizationManager.getSearchSecurity(Record.class, RecordOperations.READ_HP));
        Assert.assertEquals("(+UTI_ID_OWNER:1000 +ETA_CD:<ARC) (+TYP_ID:10 +AMOUNT:<=100.0 +ETA_CD:<ARC)", authorizationManager.getSearchSecurity(Record.class, RecordOperations.WRITE));
        Assert.assertEquals("(+TYP_ID:10 +AMOUNT:<=100.0)", authorizationManager.getSearchSecurity(Record.class, RecordOperations.CREATE));
        Assert.assertEquals("(+TYP_ID:10) (+UTI_ID_OWNER:1000 +ETA_CD:<PUB)", authorizationManager.getSearchSecurity(Record.class, RecordOperations.DELETE));
        final boolean canReadNotify = authorizationManager.hasAuthorization(RecordAuthorizations.ATZ_RECORD$NOTIFY);
        Assert.assertFalse(canReadNotify);
        Assert.assertEquals("", authorizationManager.getSearchSecurity(Record.class, RecordOperations.NOTIFY));
    } finally {
        securityManager.stopCurrentUserSession();
    }
}
Also used : Authorization(io.vertigo.account.authorization.metamodel.Authorization) TestUserSession(io.vertigo.account.data.TestUserSession) UserSession(io.vertigo.persona.security.UserSession) Record(io.vertigo.account.authorization.model.Record) TestUserSession(io.vertigo.account.data.TestUserSession) Test(org.junit.Test)

Example 5 with Authorization

use of io.vertigo.account.authorization.metamodel.Authorization in project vertigo by KleeGroup.

the class AuthorizationDeserializer method deserialize.

/**
 * {@inheritDoc}
 */
@Override
public Authorization deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) {
    final JsonObject jsonAuthorization = json.getAsJsonObject();
    final String code = jsonAuthorization.get("name").getAsString();
    final String label = jsonAuthorization.get("label").getAsString();
    final Optional<String> comment = Optional.ofNullable(jsonAuthorization.get("__comment")).map(JsonElement::getAsString);
    return new Authorization(code, label, comment);
}
Also used : Authorization(io.vertigo.account.authorization.metamodel.Authorization) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Aggregations

Authorization (io.vertigo.account.authorization.metamodel.Authorization)14 UserSession (io.vertigo.persona.security.UserSession)10 Test (org.junit.Test)10 TestUserSession (io.vertigo.account.data.TestUserSession)9 Record (io.vertigo.account.authorization.model.Record)8 DtDefinition (io.vertigo.dynamo.domain.metamodel.DtDefinition)3 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 AuthorizationName (io.vertigo.account.authorization.metamodel.AuthorizationName)2 SecuredEntity (io.vertigo.account.authorization.metamodel.SecuredEntity)2 Assertion (io.vertigo.lang.Assertion)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 AuthorizationManager (io.vertigo.account.authorization.AuthorizationManager)1 UserAuthorizations (io.vertigo.account.authorization.UserAuthorizations)1 OperationName (io.vertigo.account.authorization.metamodel.OperationName)1 Role (io.vertigo.account.authorization.metamodel.Role)1