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;
}
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();
}
}
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.... */
}
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();
}
}
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);
}
Aggregations