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-4535. Remove Ability to edit all users team #2341

Merged
merged 20 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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 @@ -60,6 +60,7 @@
public class WebServiceAdmin implements DBWServiceAdmin {

private static final Log log = Log.getLog(WebServiceAdmin.class);
public static final String All_USERS_TEAM = "all_users";
DenisSinelnikov marked this conversation as resolved.
Show resolved Hide resolved

private final Map<String, WebPermissionDescriptor> permissionDescriptorByName = WebServiceRegistry.getInstance()
.getWebServices()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import io.cloudbeaver.DBWebException;
import io.cloudbeaver.auth.*;
import io.cloudbeaver.model.app.WebAppConfiguration;
import io.cloudbeaver.model.app.WebAuthApplication;
Expand Down Expand Up @@ -212,19 +213,45 @@ public void deleteUser(String userId) throws DBCException {
application.getEventController().addEvent(event);
}

@Override
public void setUserTeams(String userId, String[] teamIds, String grantorId) throws DBCException {
try (Connection dbCon = database.openConnection()) {
try (JDBCTransaction txn = new JDBCTransaction(dbCon)) {
setUserTeams(dbCon, userId, teamIds, grantorId);
txn.commit();
String defaultUserTeam = application.getAppConfiguration().getDefaultUserTeam();
if (!Arrays.asList(teamIds).contains(defaultUserTeam)) {
DenisSinelnikov marked this conversation as resolved.
Show resolved Hide resolved
String[] updatedTeamIds = Arrays.copyOf(teamIds, teamIds.length + 1);
DenisSinelnikov marked this conversation as resolved.
Show resolved Hide resolved
updatedTeamIds[teamIds.length] = defaultUserTeam;

if (containsAllUsers(updatedTeamIds)) {
setUserTeams(dbCon, userId, updatedTeamIds, grantorId);
txn.commit();
} else {
throw new DBCException(application.getAppConfiguration().getDefaultUserTeam() + " role not editable");
DenisSinelnikov marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
if (containsAllUsers(teamIds)) {
setUserTeams(dbCon, userId, teamIds, grantorId);
txn.commit();
} else {
throw new DBCException(application.getAppConfiguration().getDefaultUserTeam() + " role not editable");
DenisSinelnikov marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
} catch (SQLException e) {
throw new DBCException("Error saving user teams in database", e);
}
addSubjectPermissionsUpdateEvent(userId, SMSubjectType.user);
}

private boolean containsAllUsers(String[] array) {
for (String element : array) {
if (application.getAppConfiguration().getDefaultUserTeam().equals(element)) {
return true;
}
}
return false;
}


public void setUserTeams(@NotNull Connection dbCon, String userId, String[] teamIds, String grantorId)
throws SQLException {
JDBCUtils.executeStatement(
Expand Down Expand Up @@ -1037,6 +1064,9 @@ public void deleteTeam(String teamId, boolean force) throws DBCException {
if (CommonUtils.isNotEmpty(defaultUsersTeam) && defaultUsersTeam.equals(teamId)) {
throw new DBCException("Default users team cannot be deleted");
}
if (teamId.equals(defaultUsersTeam)) {
DenisSinelnikov marked this conversation as resolved.
Show resolved Hide resolved
throw new DBCException("You can not delete all_users team");
}
try (Connection dbCon = database.openConnection()) {
if (!force) {
try (PreparedStatement dbStat = dbCon.prepareStatement(
Expand Down Expand Up @@ -2202,7 +2232,8 @@ private String findOrCreateExternalUserByCredentials(
}
Object reverseProxyUserTeams = sessionParameters.get(SMConstants.SESSION_PARAM_TRUSTED_USER_TEAMS);
if (reverseProxyUserTeams instanceof List) {
setUserTeams(userId, ((List<?>) reverseProxyUserTeams).stream().map(Object::toString).toArray(String[]::new), userId);
List<String> reverseProxyUserTeamsList = (List<String>) reverseProxyUserTeams;
DenisSinelnikov marked this conversation as resolved.
Show resolved Hide resolved
setUserTeams(userId, reverseProxyUserTeamsList.toArray(new String[0]), userId);
}
}
return userId;
Expand Down
Loading