Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-4603. Refactor searching by group, include default group for all s… #2440

Merged
merged 26 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
552968d
CB-4603. Refactor searching by group, include default group for all s…
Mar 1, 2024
2fd73ea
Merge branch 'devel' into CB-4603-all-users-refactor
Mar 6, 2024
ab44b87
CB-4603. Refactor after review
DenisSinelnikov Mar 6, 2024
35297dc
CB-4603. Fixed test
DenisSinelnikov Mar 6, 2024
7b6ea55
CB-4603. Refactor after review
DenisSinelnikov Mar 7, 2024
0babc36
CB-4603. Refactor after review
DenisSinelnikov Mar 7, 2024
325231a
CB-4603. Refactor after review
DenisSinelnikov Mar 7, 2024
7d7450a
Merge branch 'devel' into CB-4603-all-users-refactor
mr-anton-t Mar 8, 2024
9e2d9b5
Merge branch 'devel' into CB-4603-all-users-refactor
kseniaguzeeva Mar 14, 2024
3620542
Merge branch 'devel' into CB-4603-all-users-refactor
kseniaguzeeva Mar 18, 2024
0a20691
CB-4603. Refactor searching by group, include default group for all s…
Mar 1, 2024
e7bcadd
CB-4603. Refactor after review
DenisSinelnikov Mar 6, 2024
97de2d4
CB-4603. Fixed test
DenisSinelnikov Mar 6, 2024
c036c0a
CB-4603. Refactor after review
DenisSinelnikov Mar 7, 2024
30eacda
CB-4603. Refactor after review
DenisSinelnikov Mar 7, 2024
35a0712
CB-4603. Refactor after review
DenisSinelnikov Mar 7, 2024
8ae2aba
Merge branch 'devel' into CB-4603-all-users-refactor
DenisSinelnikov Mar 25, 2024
66e52d1
Merge remote-tracking branch 'origin/CB-4603-all-users-refactor' into…
DenisSinelnikov Mar 25, 2024
75173c6
CB-4603. Bugfixes after test
DenisSinelnikov Mar 26, 2024
c1f7d82
CB-4603. Refactor after review
DenisSinelnikov Mar 26, 2024
be24bab
Merge branch 'devel' into CB-4603-all-users-refactor
DenisSinelnikov Mar 27, 2024
59f8c6c
CB-4603. Refactor after review
DenisSinelnikov Mar 27, 2024
3f7f0f5
CB-4603. Refactor after review
DenisSinelnikov Mar 27, 2024
4ed2644
CB-4603. Refactor after review
DenisSinelnikov Mar 27, 2024
c1bd7ce
Merge branch 'devel' into CB-4603-all-users-refactor
kseniaguzeeva Mar 28, 2024
d493942
Merge branch 'devel' into CB-4603-all-users-refactor
kseniaguzeeva Apr 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
app: {
anonymousAccessEnabled: true,
anonymousUserRole: "user",
defaultUserTeam: "user",
grantConnectionsAccessToAnonymousTeam: false,
supportsCustomConnections: false,
showReadOnlyConnectionInfo: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public abstract class BaseWebAppConfiguration implements WebAppConfiguration {

public BaseWebAppConfiguration() {
this.plugins = new LinkedHashMap<>();
this.defaultUserTeam = DEFAULT_APP_ANONYMOUS_TEAM_NAME;
this.resourceManagerEnabled = true;
this.enabledFeatures = null;
this.showReadOnlyConnectionInfo = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ protected void startServer() {
if (!loadServerConfiguration()) {
return;
}
if (CommonUtils.isEmpty(this.getAppConfiguration().getDefaultUserTeam())) {
throw new DBException("Default user team must be specified");
}
} catch (DBException e) {
log.error(e);
return;
Expand Down Expand Up @@ -317,11 +320,6 @@ public boolean implies(ProtectionDomain domain, Permission permission) {
});
System.setSecurityManager(new SecurityManager());
}
try {
addAllUsersToDefaultTeam();
} catch (DBException e) {
log.error("Failed insert default teams");
}

eventController.scheduleCheckJob();

Expand All @@ -332,12 +330,6 @@ public boolean implies(ProtectionDomain domain, Permission permission) {
return;
}

private void addAllUsersToDefaultTeam() throws DBException {
if (securityController instanceof CBEmbeddedSecurityController<?> controller) {
controller.addAllUsersToDefaultTeam();
}
}

protected void initializeAdditionalConfiguration() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,63 +249,20 @@ public void setUserTeams(@NotNull Connection dbCon, String userId, String[] team
}
}

public void addAllUsersToDefaultTeam() throws DBCException {
if (application.isConfigurationMode()) {
return;
}
if (CommonUtils.isEmpty(application.getAppConfiguration().getDefaultUserTeam())) {
return;
}

try (Connection dbCon = database.openConnection()) {
try (PreparedStatement dbStat = dbCon.prepareStatement(
database.normalizeTableNames("SELECT USER_ID \n" +
"FROM {table_prefix}CB_USER\n" +
"WHERE USER_ID NOT IN (\n" +
" SELECT USER_ID FROM {table_prefix}CB_USER_TEAM CUT WHERE CUT.TEAM_ID = ? \n" +
")")
)) {
dbStat.setString(1, application.getAppConfiguration().getDefaultUserTeam());
ResultSet dbResult = dbStat.executeQuery();
List<String> usersIds = new ArrayList<>();
while (dbResult.next()) {
String userId = dbResult.getString(1);
usersIds.add(userId);
}

if (usersIds.isEmpty()) {
return;
}

for (String usersId : usersIds) {
try (PreparedStatement insertStat = dbCon.prepareStatement(
database.normalizeTableNames("INSERT INTO {table_prefix}CB_USER_TEAM(USER_ID, TEAM_ID, GRANT_TIME, GRANTED_BY)" +
" VALUES(?,?,?,?)"))) {
insertStat.setString(1, usersId);
insertStat.setString(2, application.getAppConfiguration().getDefaultUserTeam());
insertStat.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
insertStat.setString(4, "CloudBeaver Application");
insertStat.executeUpdate();
}
}
}
} catch (SQLException e) {
throw new DBCException("Error while setting default user teams", e);
}
}


@NotNull
@Override
public SMTeam[] getUserTeams(String userId) throws DBException {
Map<String, SMTeam> teams = new LinkedHashMap<>();
try (Connection dbCon = database.openConnection()) {
String defaultUserTeam = application.getAppConfiguration().getDefaultUserTeam();
try (PreparedStatement dbStat = dbCon.prepareStatement(database.normalizeTableNames(
"SELECT R.*,S.IS_SECRET_STORAGE FROM {table_prefix}CB_USER_TEAM UR, {table_prefix}CB_TEAM R, " +
"{table_prefix}CB_AUTH_SUBJECT S " +
"WHERE UR.USER_ID=? AND UR.TEAM_ID=R.TEAM_ID AND S.SUBJECT_ID=R.TEAM_ID"))
"WHERE UR.USER_ID=? AND UR.TEAM_ID = R.TEAM_ID " +
"AND S.SUBJECT_ID IN (R.TEAM_ID,?)"))
) {
dbStat.setString(1, userId);
dbStat.setString(2, defaultUserTeam);
try (ResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
var team = fetchTeam(dbResult);
Expand Down Expand Up @@ -364,14 +321,16 @@ public SMUser getUserById(String userId) throws DBException {
readSubjectMetas(dbCon, user);
// Teams
try (PreparedStatement dbStat = dbCon.prepareStatement(
database.normalizeTableNames("SELECT TEAM_ID FROM {table_prefix}CB_USER_TEAM WHERE USER_ID=?"))
database.normalizeTableNames("SELECT TEAM_ID FROM {table_prefix}CB_USER_TEAM WHERE USER_ID=?"))
) {
String defaultUserTeam = application.getAppConfiguration().getDefaultUserTeam();
dbStat.setString(1, userId);
try (ResultSet dbResult = dbStat.executeQuery()) {
List<String> teamIDs = new ArrayList<>();
Set<String> teamIDs = new LinkedHashSet<>();
while (dbResult.next()) {
teamIDs.add(dbResult.getString(1));
}
teamIDs.add(defaultUserTeam);
user.setUserTeams(teamIDs.toArray(new String[0]));
}
}
Expand Down Expand Up @@ -975,21 +934,27 @@ public SMPropertyDescriptor[] getMetaParametersBySubjectType(SMSubjectType subje
@Override
public SMTeam[] readAllTeams() throws DBCException {
try (Connection dbCon = database.openConnection()) {
String defaultUserTeam = application.getAppConfiguration().getDefaultUserTeam();
Map<String, SMTeam> teams = new LinkedHashMap<>();
try (Statement dbStat = dbCon.createStatement()) {
try (ResultSet dbResult = dbStat.executeQuery(
database.normalizeTableNames("SELECT T.*,S.IS_SECRET_STORAGE FROM {table_prefix}CB_TEAM T," +
"{table_prefix}CB_AUTH_SUBJECT S " +
"WHERE T.TEAM_ID=S.SUBJECT_ID ORDER BY TEAM_ID"))) {
String query = database.normalizeTableNames(
"SELECT T.*, S.IS_SECRET_STORAGE FROM {table_prefix}CB_TEAM T, " +
"{table_prefix}CB_AUTH_SUBJECT S " +
"WHERE T.TEAM_ID IN (S.SUBJECT_ID, ?) ORDER BY TEAM_ID");
try (PreparedStatement dbPreparedStatement = dbCon.prepareStatement(query)) {
dbPreparedStatement.setString(1, defaultUserTeam);
try (ResultSet dbResult = dbPreparedStatement.executeQuery()) {
while (dbResult.next()) {
SMTeam team = fetchTeam(dbResult);
teams.put(team.getTeamId(), team);
}
}
try (ResultSet dbResult = dbStat.executeQuery(
database.normalizeTableNames("SELECT SUBJECT_ID,PERMISSION_ID\n" +
"FROM {table_prefix}CB_AUTH_PERMISSIONS AP, {table_prefix}CB_TEAM R\n" +
"WHERE AP.SUBJECT_ID=R.TEAM_ID\n"))) {
}
query = database.normalizeTableNames("SELECT SUBJECT_ID,PERMISSION_ID\n" +
"FROM {table_prefix}CB_AUTH_PERMISSIONS AP, {table_prefix}CB_TEAM R\n" +
"WHERE AP.SUBJECT_ID IN (R.TEAM_ID,?)\n");
try (PreparedStatement dbPreparedStatement = dbCon.prepareStatement(query)) {
dbPreparedStatement.setString(1, defaultUserTeam);
try (ResultSet dbResult = dbPreparedStatement.executeQuery()) {
while (dbResult.next()) {
SMTeam team = teams.get(dbResult.getString(1));
if (team != null) {
Expand All @@ -1016,16 +981,29 @@ public SMTeam findTeam(String teamId) throws DBCException {
@Override
public String[] getTeamMembers(String teamId) throws DBCException {
try (Connection dbCon = database.openConnection()) {
try (PreparedStatement dbStat = dbCon.prepareStatement(
database.normalizeTableNames("SELECT USER_ID FROM {table_prefix}CB_USER_TEAM WHERE TEAM_ID=?"))) {
dbStat.setString(1, teamId);
List<String> subjects = new ArrayList<>();
try (ResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
subjects.add(dbResult.getString(1));
if (application.getAppConfiguration().getDefaultUserTeam().equals(teamId)) {
try (PreparedStatement dbStat = dbCon.prepareStatement(
database.normalizeTableNames("SELECT USER_ID FROM {table_prefix}CB_USER"))) {
List<String> subjects = new ArrayList<>();
try (ResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
subjects.add(dbResult.getString(1));
}
}
return subjects.toArray(new String[0]);
}
} else {
try (PreparedStatement dbStat = dbCon.prepareStatement(
database.normalizeTableNames("SELECT USER_ID FROM {table_prefix}CB_USER_TEAM WHERE TEAM_ID=?"))) {
dbStat.setString(1, teamId);
List<String> subjects = new ArrayList<>();
try (ResultSet dbResult = dbStat.executeQuery()) {
while (dbResult.next()) {
subjects.add(dbResult.getString(1));
}
}
return subjects.toArray(new String[0]);
}
return subjects.toArray(new String[0]);
}
} catch (SQLException e) {
throw new DBCException("Error while reading team members", e);
Expand Down Expand Up @@ -1240,7 +1218,7 @@ public Set<String> getUserPermissions(String userId) throws DBException {
try (PreparedStatement dbStat = dbCon.prepareStatement(
database.normalizeTableNames(
"SELECT DISTINCT AP.PERMISSION_ID FROM {table_prefix}CB_AUTH_PERMISSIONS AP, {table_prefix}CB_USER_TEAM UR\n" +
"WHERE UR.TEAM_ID=AP.SUBJECT_ID AND UR.USER_ID=?"
"WHERE UR.TEAM_ID = AP.SUBJECT_ID AND UR.USER_ID=?"
)
)) {
dbStat.setString(1, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
app: {
anonymousAccessEnabled: true,
anonymousUserRole: "user",
defaultUserTeam: "user",
supportsCustomConnections: true,
enableReverseProxyAuth: true,
enabledAuthProviders: [
Expand Down
Loading