Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
feat(group): Define a default role for API and APPLICATION
Browse files Browse the repository at this point in the history
  • Loading branch information
brasseld authored and NicolasGeraud committed Sep 10, 2018
1 parent a14c5d9 commit 2fa1064
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
Expand Up @@ -119,7 +119,7 @@ public PreparedStatement createPreparedStatement(Connection cnctn) throws SQLExc
int idx = ORM.setStatementValues(stmt, dictionary, 1);
stmt.setString(idx++, dictionary.getProvider() == null ? null : dictionary.getProvider().getType());
stmt.setString(idx++, dictionary.getProvider() == null ? null : dictionary.getProvider().getConfiguration());
stmt.setInt(idx++, dictionary.getTrigger() == null ? null : (int) dictionary.getTrigger().getRate());
stmt.setInt(idx++, dictionary.getTrigger() == null ? 0 : (int) dictionary.getTrigger().getRate());
stmt.setString(idx++, dictionary.getTrigger() == null ? null : dictionary.getTrigger().getUnit().name());

for (Object id : ids) {
Expand Down Expand Up @@ -154,6 +154,8 @@ private static String buildInsertStatement() {
}
builder.append(", ?");
builder.append(", ?");
builder.append(", ?");
builder.append(", ?");
builder.append(" )");
return builder.toString();
}
Expand All @@ -172,8 +174,8 @@ private static String buildUpdateStatement() {
builder.append(escapeReservedWord(getDBName(column.name)));
builder.append(" = ?");
}
builder.append(", source_type = ?");
builder.append(", source_configuration = ?");
builder.append(", provider_type = ?");
builder.append(", provider_configuration = ?");
builder.append(", trigger_rate = ?");
builder.append(", trigger_unit = ?");

Expand Down
Expand Up @@ -16,7 +16,6 @@
package io.gravitee.repository.jdbc.management;

import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.jdbc.management.JdbcHelper.CollatingRowMapper;
import io.gravitee.repository.jdbc.orm.JdbcObjectMapper;
import io.gravitee.repository.management.api.GroupRepository;
import io.gravitee.repository.management.model.Group;
Expand All @@ -25,11 +24,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

Expand Down Expand Up @@ -79,6 +81,7 @@ public Optional<Group> findById(String id) throws TechnicalException {
.findFirst();
if (group.isPresent()) {
addGroupEvents(group.get());
addRoles(group.get());
}
return group;
} catch (final Exception ex) {
Expand All @@ -92,6 +95,7 @@ public Group create(final Group group) throws TechnicalException {
try {
jdbcTemplate.update(ORM.buildInsertPreparedStatementCreator(group));
storeGroupEvents(group, false);
storeGroupRoles(group, false);
return findById(group.getId()).orElse(null);
} catch (final Exception ex) {
LOGGER.error("Failed to create group", ex);
Expand All @@ -107,6 +111,7 @@ public Group update(final Group group) throws TechnicalException {
try {
jdbcTemplate.update(ORM.buildUpdatePreparedStatementCreator(group, group.getId()));
storeGroupEvents(group, true);
storeGroupRoles(group, true);
return findById(group.getId()).orElseThrow(() -> new IllegalStateException(format("No group found with id [%s]", group.getId())));
} catch (final IllegalStateException ex) {
throw ex;
Expand All @@ -126,6 +131,46 @@ private void addGroupEvents(Group parent) {
parent.setEventRules(groupEvents);
}

private void addRoles(Group parent) {
Map<Integer, String> roles = getRoles(parent.getId());
parent.setRoles(roles);
}

private Map<Integer, String> getRoles(String groupId) {
Map<Integer, String> roles = new HashMap<>();
jdbcTemplate.query("select role_scope, role_name from group_roles where group_id = ?"
, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
roles.put(rs.getInt(1), rs.getString(2));
}
}, groupId);
return roles;
}

private void storeGroupRoles(Group parent, boolean deleteFirst) {
if (deleteFirst) {
jdbcTemplate.update("delete from group_roles where group_id = ?", parent.getId());
}
if (parent.getRoles() != null) {
List<Integer> scopes = new ArrayList<>(parent.getRoles().keySet());
jdbcTemplate.batchUpdate("insert into group_roles ( group_id, role_scope, role_name ) values ( ?, ?, ? )"
, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setString(1, parent.getId());
ps.setInt(2, scopes.get(i));
ps.setString(3, parent.getRoles().get(scopes.get(i)));
}

@Override
public int getBatchSize() {
return scopes.size();
}
});
}
}

private List<GroupEventRule> getEvents(String groupId) {
List<GroupEvent> groupEvents = jdbcTemplate.query("select group_event from group_event_rules where group_id = ?", (ResultSet rs, int rowNum) -> {
String value = rs.getString(1);
Expand Down
16 changes: 14 additions & 2 deletions src/main/resources/liquibase/changelogs/v1_19_0/schema.yml
Expand Up @@ -3,6 +3,18 @@ databaseChangeLog:
id: 1.19.0
author: GraviteeSource Team
changes:
- createTable:
tableName: group_roles
columns:
- column: {name: group_id, type: nvarchar(64), constraints: { nullable: false } }
- column: {name: role_scope, type: int, constraints: { nullable: false } }
- column: {name: role_name, type: nvarchar(64), constraints: { nullable: false } }

- addPrimaryKey:
constraintName: pk_group_roles
columnNames: group_id, role_scope
tableName: group_roles

- createTable:
tableName: dictionaries
columns:
Expand Down Expand Up @@ -33,5 +45,5 @@ databaseChangeLog:

- addPrimaryKey:
constraintName: pk_dictionary_property
columnNames: dictionary_id, k, v
tableName: dictionary_property
columnNames: dictionary_id, k
tableName: dictionary_property
Expand Up @@ -56,12 +56,15 @@ public class JdbcTestRepositoryInitializer implements TestRepositoryInitializer
"application_groups",
"audits",
"audit_properties",
"dictionaries",
"dictionary_property",
"events",
"event_properties",
"generic_notification_configs",
"generic_notification_config_hooks",
"groups",
"group_event_rules",
"group_roles",
"memberships",
"membership_roles",
"metadata",
Expand Down

0 comments on commit 2fa1064

Please sign in to comment.