diff --git a/api/src/main/java/com/bivashy/auth/api/account/Account.java b/api/src/main/java/com/bivashy/auth/api/account/Account.java index 58b31167..525cde9d 100644 --- a/api/src/main/java/com/bivashy/auth/api/account/Account.java +++ b/api/src/main/java/com/bivashy/auth/api/account/Account.java @@ -20,6 +20,9 @@ import com.bivashy.auth.api.type.KickResultType; public interface Account extends PlayerIdSupplier { + + long getDatabaseId(); + @Deprecated default String getId() { return getPlayerId(); @@ -149,4 +152,5 @@ default Optional getPlayer() { default boolean isRegistered() { return getPasswordHash().getHash() != null; } + } \ No newline at end of file diff --git a/core/src/main/java/me/mastercapexd/auth/account/AuthAccountAdapter.java b/core/src/main/java/me/mastercapexd/auth/account/AuthAccountAdapter.java index f9e959ff..3c976bfb 100644 --- a/core/src/main/java/me/mastercapexd/auth/account/AuthAccountAdapter.java +++ b/core/src/main/java/me/mastercapexd/auth/account/AuthAccountAdapter.java @@ -15,10 +15,10 @@ import me.mastercapexd.auth.database.model.AccountLink; import me.mastercapexd.auth.database.model.AuthAccount; -import me.mastercapexd.auth.database.model.AuthAccountProvider; import me.mastercapexd.auth.link.user.AccountLinkAdapter; -public class AuthAccountAdapter extends AccountTemplate implements AuthAccountProvider { +public class AuthAccountAdapter extends AccountTemplate { + private final List linkUsers; private final AuthAccount authAccount; @@ -47,8 +47,8 @@ public AuthAccountAdapter(AuthAccount authAccount) { } @Override - public AuthAccount getAuthAccount() { - return authAccount; + public long getDatabaseId() { + return authAccount.getId(); } @Override @@ -144,4 +144,5 @@ public void setLastSessionStartTimestamp(long currentTimeMillis) { public int compareTo(AccountTemplate accountTemplate) { return accountTemplate.getName().compareTo(getName()); } + } diff --git a/core/src/main/java/me/mastercapexd/auth/database/adapter/AccountAdapter.java b/core/src/main/java/me/mastercapexd/auth/database/adapter/AccountAdapter.java new file mode 100644 index 00000000..2aa9463b --- /dev/null +++ b/core/src/main/java/me/mastercapexd/auth/database/adapter/AccountAdapter.java @@ -0,0 +1,15 @@ +package me.mastercapexd.auth.database.adapter; + +import com.bivashy.auth.api.account.Account; + +import me.mastercapexd.auth.database.model.AuthAccount; + +public class AccountAdapter extends AuthAccount { + + public AccountAdapter(Account account) { + super(account.getDatabaseId(), account.getPlayerId(), account.getIdentifierType(), account.getCryptoProvider(), account.getLastIpAddress(), + account.getUniqueId(), account.getName(), account.getPasswordHash() + .getHash(), account.getLastQuitTimestamp(), account.getLastSessionStartTimestamp()); + } + +} diff --git a/core/src/main/java/me/mastercapexd/auth/database/adapter/AccountLinkAdapter.java b/core/src/main/java/me/mastercapexd/auth/database/adapter/AccountLinkAdapter.java new file mode 100644 index 00000000..22a3a253 --- /dev/null +++ b/core/src/main/java/me/mastercapexd/auth/database/adapter/AccountLinkAdapter.java @@ -0,0 +1,14 @@ +package me.mastercapexd.auth.database.adapter; + +import com.bivashy.auth.api.link.user.LinkUser; + +import me.mastercapexd.auth.database.model.AccountLink; + +public class AccountLinkAdapter extends AccountLink { + + public AccountLinkAdapter(LinkUser linkUser) { + super(linkUser.getLinkType().getName(), linkUser.getLinkUserInfo().getIdentificator().asString(), linkUser.getLinkUserInfo().isConfirmationEnabled(), + null); + } + +} diff --git a/core/src/main/java/me/mastercapexd/auth/database/dao/AccountLinkDao.java b/core/src/main/java/me/mastercapexd/auth/database/dao/AccountLinkDao.java index 7df52e1e..c8353991 100644 --- a/core/src/main/java/me/mastercapexd/auth/database/dao/AccountLinkDao.java +++ b/core/src/main/java/me/mastercapexd/auth/database/dao/AccountLinkDao.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import com.bivashy.auth.api.account.Account; import com.bivashy.auth.api.config.database.schema.TableSettings; @@ -19,11 +20,13 @@ import com.j256.ormlite.table.TableUtils; import me.mastercapexd.auth.database.DatabaseHelper; +import me.mastercapexd.auth.database.adapter.AccountAdapter; +import me.mastercapexd.auth.database.adapter.AccountLinkAdapter; import me.mastercapexd.auth.database.model.AccountLink; import me.mastercapexd.auth.database.model.AuthAccount; -import me.mastercapexd.auth.database.model.AuthAccountProvider; public class AccountLinkDao extends BaseDaoImpl { + private static final String LINK_TYPE_CONFIGURATION_KEY = "linkType"; private static final String LINK_USER_ID_CONFIGURATION_KEY = "linkUserId"; private static final String LINK_ENABLED_CONFIGURATION_KEY = "linkEnabled"; @@ -72,33 +75,33 @@ private static DatabaseFieldConfig createFieldConfig(TableSettings settings, Str public void updateAccountLinks(Account account) { DEFAULT_EXCEPTION_CATCHER.execute(() -> { - if (!(account instanceof AuthAccountProvider)) - throw new IllegalArgumentException("Cannot create or update not AuthAccountProvider: " + account.getClass().getName()); - AuthAccountProvider authAccountProvider = (AuthAccountProvider) account; - AuthAccount authAccount = databaseHelper.getAuthAccountDao().createIfNotExists(authAccountProvider.getAuthAccount()); - List existingAccountLinks = new ArrayList<>(authAccount.getLinks()); - for (LinkUser linkUser : account.getLinkUsers()) { - String linkTypeName = linkUser.getLinkType().getName(); - String linkUserId = Optional.ofNullable(linkUser.getLinkUserInfo()) - .map(LinkUserInfo::getIdentificator) - .map(LinkUserIdentificator::asString) - .orElse(linkUser.getLinkType().getDefaultIdentificator().asString()); - boolean linkEnabled = linkUser.getLinkUserInfo().isConfirmationEnabled(); - Optional accountLinkOptional = existingAccountLinks.stream() - .filter(accountLink -> accountLink.getLinkType().equals(linkTypeName)) - .findFirst(); - - if (accountLinkOptional.isPresent()) { - AccountLink accountLink = accountLinkOptional.get(); - accountLink.setLinkEnabled(linkEnabled); - accountLink.setLinkUserId(linkUserId); - update(accountLink); - continue; + List existingAccountLinks = account.getLinkUsers().stream().map(AccountLinkAdapter::new).collect(Collectors.toList()); + callBatchTasks(() -> { + AuthAccount adapter = new AccountAdapter(account); + for (LinkUser linkUser : account.getLinkUsers()) { + String linkTypeName = linkUser.getLinkType().getName(); + String linkUserId = Optional.ofNullable(linkUser.getLinkUserInfo()) + .map(LinkUserInfo::getIdentificator) + .map(LinkUserIdentificator::asString) + .orElse(linkUser.getLinkType().getDefaultIdentificator().asString()); + boolean linkEnabled = linkUser.getLinkUserInfo().isConfirmationEnabled(); + Optional accountLinkOptional = existingAccountLinks.stream() + .filter(accountLink -> accountLink.getLinkType().equals(linkTypeName)) + .findFirst(); + + if (accountLinkOptional.isPresent()) { + AccountLink accountLink = accountLinkOptional.get(); + accountLink.setLinkEnabled(linkEnabled); + accountLink.setLinkUserId(linkUserId); + update(accountLink); + continue; + } + + AccountLink accountLink = new AccountLink(linkTypeName, linkUserId, linkEnabled, adapter); + create(accountLink); } - - AccountLink accountLink = new AccountLink(linkTypeName, linkUserId, linkEnabled, authAccount); - create(accountLink); - } + return null; + }); return null; }); } @@ -118,4 +121,5 @@ public QueryBuilder queryBuilder(LinkUserIdentificator linkUs .eq(AccountLink.LINK_USER_ID_FIELD_KEY, linkUserIdentificator.asString()) .queryBuilder()); } + } diff --git a/core/src/main/java/me/mastercapexd/auth/database/dao/AuthAccountDao.java b/core/src/main/java/me/mastercapexd/auth/database/dao/AuthAccountDao.java index 67585ecd..26be9311 100644 --- a/core/src/main/java/me/mastercapexd/auth/database/dao/AuthAccountDao.java +++ b/core/src/main/java/me/mastercapexd/auth/database/dao/AuthAccountDao.java @@ -21,9 +21,9 @@ import com.j256.ormlite.table.TableUtils; import me.mastercapexd.auth.database.DatabaseHelper; +import me.mastercapexd.auth.database.adapter.AccountAdapter; import me.mastercapexd.auth.database.model.AccountLink; import me.mastercapexd.auth.database.model.AuthAccount; -import me.mastercapexd.auth.database.model.AuthAccountProvider; import me.mastercapexd.auth.database.persister.CryptoProviderPersister; public class AuthAccountDao extends BaseDaoImpl { @@ -153,10 +153,7 @@ public Collection queryAllLinkedAccounts(LinkType linkType) { } public AuthAccount createOrUpdateAccount(Account account) { - if (!(account instanceof AuthAccountProvider)) - throw new IllegalArgumentException("Cannot create or update not AuthAccountProvider: " + account.getClass().getName()); - AuthAccountProvider authAccountProvider = (AuthAccountProvider) account; - AuthAccount authAccount = authAccountProvider.getAuthAccount(); + AuthAccount authAccount = new AccountAdapter(account); return DEFAULT_EXCEPTION_CATCHER.execute(() -> { Optional foundAccount = queryFirstAccountPlayerId(authAccount.getPlayerId()); diff --git a/core/src/main/java/me/mastercapexd/auth/database/model/AuthAccount.java b/core/src/main/java/me/mastercapexd/auth/database/model/AuthAccount.java index 3dd1d3dc..493bffea 100644 --- a/core/src/main/java/me/mastercapexd/auth/database/model/AuthAccount.java +++ b/core/src/main/java/me/mastercapexd/auth/database/model/AuthAccount.java @@ -70,6 +70,20 @@ public AuthAccount(String playerId, IdentifierType playerIdType, CryptoProvider this.lastSessionStartTimestamp = lastSessionStartTimestamp; } + public AuthAccount(long id, String playerId, IdentifierType playerIdType, CryptoProvider cryptoProvider, String lastIp, UUID uniqueId, String playerName, + String passwordHash, long lastQuitTimestamp, long lastSessionStartTimestamp) { + this.id = id; + this.playerId = playerId; + this.playerIdType = playerIdType; + this.cryptoProvider = cryptoProvider; + this.lastIp = lastIp; + this.uniqueId = uniqueId; + this.playerName = playerName; + this.passwordHash = passwordHash; + this.lastQuitTimestamp = lastQuitTimestamp; + this.lastSessionStartTimestamp = lastSessionStartTimestamp; + } + public long getId() { return id; } diff --git a/core/src/main/java/me/mastercapexd/auth/database/model/AuthAccountProvider.java b/core/src/main/java/me/mastercapexd/auth/database/model/AuthAccountProvider.java deleted file mode 100644 index c7c98956..00000000 --- a/core/src/main/java/me/mastercapexd/auth/database/model/AuthAccountProvider.java +++ /dev/null @@ -1,5 +0,0 @@ -package me.mastercapexd.auth.database.model; - -public interface AuthAccountProvider { - AuthAccount getAuthAccount(); -}