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

fix: useridmapping functions #157

Merged
merged 1 commit into from
Sep 15, 2023
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
45 changes: 44 additions & 1 deletion src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import io.supertokens.pluginInterface.useridmapping.UserIdMappingStorage;
import io.supertokens.pluginInterface.useridmapping.exception.UnknownSuperTokensUserIdException;
import io.supertokens.pluginInterface.useridmapping.exception.UserIdMappingAlreadyExistsException;
import io.supertokens.pluginInterface.useridmapping.sqlStorage.UserIdMappingSQLStorage;
import io.supertokens.pluginInterface.usermetadata.UserMetadataStorage;
import io.supertokens.pluginInterface.usermetadata.sqlStorage.UserMetadataSQLStorage;
import io.supertokens.pluginInterface.userroles.UserRolesStorage;
Expand Down Expand Up @@ -108,7 +109,7 @@
public class Start
implements SessionSQLStorage, EmailPasswordSQLStorage, EmailVerificationSQLStorage, ThirdPartySQLStorage,
JWTRecipeSQLStorage, PasswordlessSQLStorage, UserMetadataSQLStorage, UserRolesSQLStorage, UserIdMappingStorage,
MultitenancyStorage, MultitenancySQLStorage, DashboardSQLStorage, TOTPSQLStorage, ActiveUsersStorage, AuthRecipeSQLStorage {
UserIdMappingSQLStorage, MultitenancyStorage, MultitenancySQLStorage, DashboardSQLStorage, TOTPSQLStorage, ActiveUsersStorage, AuthRecipeSQLStorage {

// these configs are protected from being modified / viewed by the dev using the SuperTokens
// SaaS. If the core is not running in SuperTokens SaaS, this array has no effect.
Expand Down Expand Up @@ -2911,6 +2912,17 @@ public void unlinkAccounts_Transaction(AppIdentifier appIdentifier, TransactionC
}
}

@Override
public boolean doesUserIdExist_Transaction(TransactionConnection con, AppIdentifier appIdentifier,
String externalUserId) throws StorageQueryException {
try {
Connection sqlCon = (Connection) con.getConnection();
return GeneralQueries.doesUserIdExist_Transaction(this, sqlCon, appIdentifier, externalUserId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

@Override
public boolean checkIfUsesAccountLinking(AppIdentifier appIdentifier) throws StorageQueryException {
try {
Expand Down Expand Up @@ -2942,4 +2954,35 @@ public int getUsersCountWithMoreThanOneLoginMethod(AppIdentifier appIdentifier)
public Thread getMainThread() {
return mainThread;
}

@Override
public UserIdMapping getUserIdMapping_Transaction(TransactionConnection con, AppIdentifier appIdentifier,
String userId, boolean isSuperTokensUserId)
throws StorageQueryException {
try {
Connection sqlCon = (Connection) con.getConnection();
if (isSuperTokensUserId) {
return UserIdMappingQueries.getuseraIdMappingWithSuperTokensUserId_Transaction(this, sqlCon, appIdentifier,
userId);
}

return UserIdMappingQueries.getUserIdMappingWithExternalUserId_Transaction(this, sqlCon, appIdentifier, userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

@Override
public UserIdMapping[] getUserIdMapping_Transaction(TransactionConnection con, AppIdentifier appIdentifier,
String userId) throws StorageQueryException {
try {
Connection sqlCon = (Connection) con.getConnection();
return UserIdMappingQueries.getUserIdMappingWithEitherSuperTokensUserIdOrExternalUserId_Transaction(this,
sqlCon,
appIdentifier,
userId);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,21 @@ public static boolean doesUserIdExist(Start start, AppIdentifier appIdentifier,
}, ResultSet::next);
}

public static boolean doesUserIdExist_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier, String userId)
throws SQLException, StorageQueryException {
// We query both tables cause there is a case where a primary user ID exists, but its associated
// recipe user ID has been deleted AND there are other recipe user IDs linked to this primary user ID already.
String QUERY = "SELECT 1 FROM " + getConfig(start).getAppIdToUserIdTable()
+ " WHERE app_id = ? AND user_id = ? UNION SELECT 1 FROM " + getConfig(start).getUsersTable() +
" WHERE app_id = ? AND primary_or_recipe_user_id = ?";
return execute(sqlCon, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
pst.setString(3, appIdentifier.getAppId());
pst.setString(4, userId);
}, ResultSet::next);
}

public static boolean doesUserIdExist(Start start, TenantIdentifier tenantIdentifier, String userId)
throws SQLException, StorageQueryException {
// We query both tables cause there is a case where a primary user ID exists, but its associated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.supertokens.storage.postgresql.utils.Utils;

import javax.annotation.Nullable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
Expand Down Expand Up @@ -216,6 +217,56 @@ public static boolean updateOrDeleteExternalUserIdInfoWithExternalUserId(Start s
return rowUpdated > 0;
}

public static UserIdMapping getuseraIdMappingWithSuperTokensUserId_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier, String userId)
throws SQLException, StorageQueryException {
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
+ " WHERE app_id = ? AND supertokens_user_id = ?";
return execute(sqlCon, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
}, result -> {
if (result.next()) {
return UserIdMappingRowMapper.getInstance().mapOrThrow(result);
}
return null;
});
}

public static UserIdMapping getUserIdMappingWithExternalUserId_Transaction(Start start, Connection sqlCon, AppIdentifier appIdentifier, String userId)
throws SQLException, StorageQueryException {
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
+ " WHERE app_id = ? AND external_user_id = ?";

return execute(sqlCon, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
}, result -> {
if (result.next()) {
return UserIdMappingRowMapper.getInstance().mapOrThrow(result);
}
return null;
});
}

public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExternalUserId_Transaction(Start start, Connection sqlCon,
AppIdentifier appIdentifier, String userId)
throws SQLException, StorageQueryException {
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
+ " WHERE app_id = ? AND (supertokens_user_id = ? OR external_user_id = ?)";

return execute(sqlCon, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
pst.setString(3, userId);
}, result -> {
ArrayList<UserIdMapping> userIdMappingArray = new ArrayList<>();
while (result.next()) {
userIdMappingArray.add(UserIdMappingRowMapper.getInstance().mapOrThrow(result));
}
return userIdMappingArray.toArray(UserIdMapping[]::new);
});
}

private static class UserIdMappingRowMapper implements RowMapper<UserIdMapping, ResultSet> {
private static final UserIdMappingRowMapper INSTANCE = new UserIdMappingRowMapper();

Expand Down
Loading