Skip to content

Commit

Permalink
Replace AuthAccountProvider by AuthAccountAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
bivashy committed Oct 31, 2023
1 parent febd2e0 commit a142f5d
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 41 deletions.
4 changes: 4 additions & 0 deletions api/src/main/java/com/bivashy/auth/api/account/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.bivashy.auth.api.type.KickResultType;

public interface Account extends PlayerIdSupplier {

long getDatabaseId();

@Deprecated
default String getId() {
return getPlayerId();
Expand Down Expand Up @@ -149,4 +152,5 @@ default Optional<ServerPlayer> getPlayer() {
default boolean isRegistered() {
return getPasswordHash().getHash() != null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<LinkUser> linkUsers;
private final AuthAccount authAccount;

Expand Down Expand Up @@ -47,8 +47,8 @@ public AuthAccountAdapter(AuthAccount authAccount) {
}

@Override
public AuthAccount getAuthAccount() {
return authAccount;
public long getDatabaseId() {
return authAccount.getId();
}

@Override
Expand Down Expand Up @@ -144,4 +144,5 @@ public void setLastSessionStartTimestamp(long currentTimeMillis) {
public int compareTo(AccountTemplate accountTemplate) {
return accountTemplate.getName().compareTo(getName());
}

}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AccountLink, Long> {

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";
Expand Down Expand Up @@ -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<AccountLink> 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<AccountLink> 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<AccountLink> 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<AccountLink> 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;
});
}
Expand All @@ -118,4 +121,5 @@ public QueryBuilder<AccountLink, Long> queryBuilder(LinkUserIdentificator linkUs
.eq(AccountLink.LINK_USER_ID_FIELD_KEY, linkUserIdentificator.asString())
.queryBuilder());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuthAccount, Long> {
Expand Down Expand Up @@ -153,10 +153,7 @@ public Collection<AuthAccount> 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<AuthAccount> foundAccount = queryFirstAccountPlayerId(authAccount.getPlayerId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

This file was deleted.

0 comments on commit a142f5d

Please sign in to comment.