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

Replace AuthAccountProvider by AuthAccountAdapter #110

Merged
merged 3 commits into from
Nov 2, 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
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,15 @@
package me.mastercapexd.auth.database.adapter;

import com.bivashy.auth.api.link.user.LinkUser;

import me.mastercapexd.auth.database.model.AccountLink;
import me.mastercapexd.auth.database.model.AuthAccount;

public class LinkUserAdapter extends AccountLink {

public LinkUserAdapter(LinkUser linkUser, AuthAccount account) {
super(linkUser.getLinkType().getName(), linkUser.getLinkUserInfo().getIdentificator().asString(), linkUser.getLinkUserInfo().isConfirmationEnabled(),
account);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,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.LinkUserAdapter;
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 +74,36 @@ 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;
AuthAccount accountAdapter = new AccountAdapter(account);
callBatchTasks(() -> {
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();

AccountLink accountLink = new LinkUserAdapter(linkUser, accountAdapter);

AccountLink updateId = queryBuilder()
.where().eq(AccountLink.ACCOUNT_ID_FIELD_KEY, accountAdapter.getId())
.and().eq(AccountLink.LINK_TYPE_FIELD_KEY, accountLink.getLinkType())
.queryForFirst();

if (updateId != null) {
accountLink.setId(updateId.getId());
if (accountLink.equals(updateId))
continue;
update(accountLink);
continue;
}

AccountLink newAccountLink = new AccountLink(linkTypeName, linkUserId, linkEnabled, accountAdapter);
create(newAccountLink);
}

AccountLink accountLink = new AccountLink(linkTypeName, linkUserId, linkEnabled, authAccount);
create(accountLink);
}
return null;
});
return null;
});
}
Expand All @@ -118,4 +123,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
@@ -1,11 +1,14 @@
package me.mastercapexd.auth.database.model;

import java.util.Objects;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "auth_links")
public class AccountLink {

public static final String LINK_TYPE_FIELD_KEY = "link_type";
public static final String LINK_USER_ID_FIELD_KEY = "link_user_id";
public static final String LINK_ENABLED_FIELD_KEY = "link_enabled";
Expand Down Expand Up @@ -43,6 +46,10 @@ public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getLinkType() {
return linkType;
}
Expand All @@ -59,11 +66,20 @@ public AuthAccount getAccount() {
return account;
}

public void setLinkUserId(String linkUserId) {
this.linkUserId = linkUserId;
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof AccountLink))
return false;
AccountLink that = (AccountLink) o;
return getId() == that.getId() && isLinkEnabled() == that.isLinkEnabled() && Objects.equals(getLinkType(), that.getLinkType()) &&
Objects.equals(getLinkUserId(), that.getLinkUserId()) && getAccount().getId() == that.getAccount().getId();
}

public void setLinkEnabled(boolean linkEnabled) {
this.linkEnabled = linkEnabled;
@Override
public int hashCode() {
return Objects.hash(getId(), getLinkType(), getLinkUserId(), isLinkEnabled(), getAccount().getId());
}

}
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.

Loading