Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 21 additions & 3 deletions src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -1450,14 +1450,32 @@ public int deleteUserMetadata(String userId) throws StorageQueryException {
@Override
public void addRoleToUser(String userId, String role)
throws StorageQueryException, UnknownRoleException, DuplicateUserRoleMappingException {
// TODO

try {
UserRolesQueries.addRoleToUser(this, userId, role);
} catch (SQLException e) {
if (e instanceof PSQLException) {
PostgreSQLConfig config = Config.getConfig(this);
ServerErrorMessage serverErrorMessage = ((PSQLException) e).getServerErrorMessage();
if (isForeignKeyConstraintError(serverErrorMessage, config.getUserRolesTable(), "role")) {
throw new UnknownRoleException();
}
if (isPrimaryKeyError(serverErrorMessage, config.getUserRolesTable())) {
throw new DuplicateUserRoleMappingException();
}
}
throw new StorageQueryException(e);
}

}

@Override
public String[] getRolesForUser(String userId) throws StorageQueryException {
// TODO
return new String[0];
try {
return UserRolesQueries.getRolesForUser(this, userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,24 @@ public static String[] getRoles(Start start) throws SQLException, StorageQueryEx
});
}

public static int addRoleToUser(Start start, String userId, String role)
throws SQLException, StorageQueryException {
String QUERY = "INSERT INTO " + getConfig(start).getUserRolesTable() + "(user_id, role) VALUES(?, ?);";
return update(start, QUERY, pst -> {
pst.setString(1, userId);
pst.setString(2, role);
});
}

public static String[] getRolesForUser(Start start, String userId) throws SQLException, StorageQueryException {
String QUERY = "SELECT role FROM " + getConfig(start).getUserRolesTable() + " WHERE user_id = ? ;";

return execute(start, QUERY, pst -> pst.setString(1, userId), result -> {
ArrayList<String> roles = new ArrayList<>();
while (result.next()) {
roles.add(result.getString("role"));
}
return roles.toArray(String[]::new);
});
}
}