diff --git a/src/main/java/biz/nynja/account/models/Account.java b/src/main/java/biz/nynja/account/models/Account.java index 06b7c184c996e10cd92f6e880c6886c3dd252857..93c3f70a7f072c028944027830813003a6e8af69 100644 --- a/src/main/java/biz/nynja/account/models/Account.java +++ b/src/main/java/biz/nynja/account/models/Account.java @@ -13,6 +13,7 @@ import org.springframework.data.cassandra.core.mapping.Table; import biz.nynja.account.grpc.AccountDetails; import biz.nynja.account.grpc.AccountDetails.Builder; import biz.nynja.account.grpc.CreatePendingAccountRequest; +import biz.nynja.account.grpc.Role; @Table public class Account { @@ -33,6 +34,7 @@ public class Account { private Long creationTimestamp; private Long lastUpdateTimestamp; private Set contactsInfo; + private Set roles; public UUID getAccountId() { return accountId; @@ -154,6 +156,14 @@ public class Account { this.lastUpdateTimestamp = lastUpdateTimestamp; } + public Set getRoles() { + return roles; + } + + public void setRoles(Set roles) { + this.roles = roles; + } + @Override public int hashCode() { final int prime = 31; @@ -172,6 +182,7 @@ public class Account { result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); result = prime * result + ((qrCode == null) ? 0 : qrCode.hashCode()); + result = prime * result + ((roles == null) ? 0 : roles.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @@ -255,6 +266,11 @@ public class Account { return false; } else if (!qrCode.equals(other.qrCode)) return false; + if (roles == null) { + if (other.roles != null) + return false; + } else if (!roles.equals(other.roles)) + return false; if (username == null) { if (other.username != null) return false; @@ -265,7 +281,8 @@ public class Account { @Override public String toString() { - return new StringBuilder("Account [accountId=").append(accountId).append(", profileId=").append(profileId) + StringBuilder builder = new StringBuilder(); + builder.append("Account [accountId=").append(accountId).append(", profileId=").append(profileId) .append(", accountMark=").append(accountMark).append(", authenticationProvider=") .append(authenticationProvider).append(", authenticationProviderType=") .append(authenticationProviderType).append(", firstName=").append(firstName).append(", lastName=") @@ -273,7 +290,8 @@ public class Account { .append(", username=").append(username).append(", qrCode=").append(qrCode).append(", accountStatus=") .append(accountStatus).append(", creationTimestamp=").append(creationTimestamp) .append(", lastUpdateTimestamp=").append(lastUpdateTimestamp).append(", contactsInfo=") - .append(contactsInfo).append("]").toString(); + .append(contactsInfo).append(", roles=").append(roles).append("]"); + return builder.toString(); } public static Account createPendingAccountFromProto(CreatePendingAccountRequest request) { @@ -323,9 +341,14 @@ public class Account { if (getAvatar() != null) { builder.setAvatar(com.google.protobuf.ByteString.copyFrom(avatar)); } + if (getRoles() != null) { + for (String role : getRoles()) { + builder.addRoles(Role.valueOf(role)); + } + } if (getContactsInfo() != null) { for (ContactInfo c : contactsInfo) { - builder.addContactsInfo(c.toProto()); + builder.addContactsInfo(c.toProto()); } } diff --git a/src/main/java/biz/nynja/account/models/AccountByAuthenticationProvider.java b/src/main/java/biz/nynja/account/models/AccountByAuthenticationProvider.java index 7a1b23dd3e39a078fb3f1725e0215780092c9894..9c72576dbcc3e8feb00449a7bcc32a16ecf937b8 100644 --- a/src/main/java/biz/nynja/account/models/AccountByAuthenticationProvider.java +++ b/src/main/java/biz/nynja/account/models/AccountByAuthenticationProvider.java @@ -8,6 +8,7 @@ import java.util.Set; import java.util.UUID; import biz.nynja.account.grpc.AccountDetails.Builder; +import biz.nynja.account.grpc.Role; public class AccountByAuthenticationProvider { @@ -26,6 +27,7 @@ public class AccountByAuthenticationProvider { private Long lastUpdateTimestamp; private Set contactsInfo; private String qrCode; + private Set roles; public UUID getAccountId() { return accountId; @@ -147,6 +149,14 @@ public class AccountByAuthenticationProvider { this.qrCode = qrCode; } + public Set getRoles() { + return roles; + } + + public void setRoles(Set roles) { + this.roles = roles; + } + @Override public int hashCode() { final int prime = 31; @@ -165,6 +175,7 @@ public class AccountByAuthenticationProvider { result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); result = prime * result + ((qrCode == null) ? 0 : qrCode.hashCode()); + result = prime * result + ((roles == null) ? 0 : roles.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @@ -248,6 +259,11 @@ public class AccountByAuthenticationProvider { return false; } else if (!qrCode.equals(other.qrCode)) return false; + if (roles == null) { + if (other.roles != null) + return false; + } else if (!roles.equals(other.roles)) + return false; if (username == null) { if (other.username != null) return false; @@ -258,14 +274,17 @@ public class AccountByAuthenticationProvider { @Override public String toString() { - return new StringBuilder("Account [accountId=").append(accountId).append(", profileId=").append(profileId) - .append(", accountMark=").append(accountMark).append(", authenticationProvider=") + StringBuilder builder = new StringBuilder(); + builder.append("AccountByAuthenticationProvider [profileId=").append(profileId).append(", accountId=") + .append(accountId).append(", accountMark=").append(accountMark).append(", authenticationProvider=") .append(authenticationProvider).append(", authenticationProviderType=") .append(authenticationProviderType).append(", firstName=").append(firstName).append(", lastName=") .append(lastName).append(", avatar=").append(avatar).append(", accountName=").append(accountName) .append(", username=").append(username).append(", accountStatus=").append(accountStatus) .append(", creationTimestamp=").append(creationTimestamp).append(", lastUpdateTimestamp=") - .append(lastUpdateTimestamp).append(", contactsInfo=").append(contactsInfo).append("]").toString(); + .append(lastUpdateTimestamp).append(", contactsInfo=").append(contactsInfo).append(", qrCode=") + .append(qrCode).append(", roles=").append(roles).append("]"); + return builder.toString(); } public biz.nynja.account.grpc.AccountDetails toProto() { @@ -312,6 +331,11 @@ public class AccountByAuthenticationProvider { builder.addContactsInfo(c.toProto()); } } + if (getRoles() != null) { + for (String role : getRoles()) { + builder.addRoles(Role.valueOf(role)); + } + } return builder.build(); @@ -334,6 +358,7 @@ public class AccountByAuthenticationProvider { account.setLastUpdateTimestamp(this.lastUpdateTimestamp); account.setContactsInfo(this.contactsInfo); account.setQrCode(this.qrCode); + account.setRoles(this.roles); return account; } } diff --git a/src/main/java/biz/nynja/account/models/AccountByProfileId.java b/src/main/java/biz/nynja/account/models/AccountByProfileId.java index 0f7eac92ca62bf7e02c37567678445c2a3f00a68..b53474d0045b87c8830fe09b284652eca0f17c2c 100644 --- a/src/main/java/biz/nynja/account/models/AccountByProfileId.java +++ b/src/main/java/biz/nynja/account/models/AccountByProfileId.java @@ -7,9 +7,6 @@ import java.nio.ByteBuffer; import java.util.Set; import java.util.UUID; -import org.springframework.data.cassandra.core.cql.PrimaryKeyType; -import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; - import biz.nynja.account.grpc.AccountDetails; import biz.nynja.account.grpc.AccountDetails.Builder; @@ -30,6 +27,7 @@ public class AccountByProfileId { private Long lastUpdateTimestamp; private Set contactsInfo; private String qrCode; + private Set roles; public UUID getAccountId() { return accountId; @@ -151,6 +149,14 @@ public class AccountByProfileId { this.qrCode = qrCode; } + public Set getRoles() { + return roles; + } + + public void setRoles(Set roles) { + this.roles = roles; + } + @Override public int hashCode() { final int prime = 31; @@ -169,6 +175,7 @@ public class AccountByProfileId { result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); result = prime * result + ((qrCode == null) ? 0 : qrCode.hashCode()); + result = prime * result + ((roles == null) ? 0 : roles.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @@ -252,6 +259,11 @@ public class AccountByProfileId { return false; } else if (!qrCode.equals(other.qrCode)) return false; + if (roles == null) { + if (other.roles != null) + return false; + } else if (!roles.equals(other.roles)) + return false; if (username == null) { if (other.username != null) return false; @@ -262,14 +274,17 @@ public class AccountByProfileId { @Override public String toString() { - return new StringBuilder("Account [accountId=").append(accountId).append(", profileId=").append(profileId) + StringBuilder builder = new StringBuilder(); + builder.append("AccountByProfileId [profileId=").append(profileId).append(", accountId=").append(accountId) .append(", accountMark=").append(accountMark).append(", authenticationProvider=") .append(authenticationProvider).append(", authenticationProviderType=") .append(authenticationProviderType).append(", firstName=").append(firstName).append(", lastName=") .append(lastName).append(", avatar=").append(avatar).append(", accountName=").append(accountName) .append(", username=").append(username).append(", accountStatus=").append(accountStatus) .append(", creationTimestamp=").append(creationTimestamp).append(", lastUpdateTimestamp=") - .append(lastUpdateTimestamp).append(", contactsInfo=").append(contactsInfo).append("]").toString(); + .append(lastUpdateTimestamp).append(", contactsInfo=").append(contactsInfo).append(", qrCode=") + .append(qrCode).append(", roles=").append(roles).append("]"); + return builder.toString(); } public biz.nynja.account.grpc.AccountDetails toProto() { @@ -317,6 +332,11 @@ public class AccountByProfileId { builder.addContactsInfo(c.toProto()); } } + if (getRoles() != null) { + for (String role : getRoles()) { + builder.addRoles(Role.valueOf(role)); + } + } return builder.build(); diff --git a/src/main/java/biz/nynja/account/models/AccountByUsername.java b/src/main/java/biz/nynja/account/models/AccountByUsername.java index d60c2ee1e7cc952103be547471edf73420d9dab9..9c12b6b271f659fa14f38e6a66467c5bdf912e46 100644 --- a/src/main/java/biz/nynja/account/models/AccountByUsername.java +++ b/src/main/java/biz/nynja/account/models/AccountByUsername.java @@ -7,9 +7,6 @@ import java.nio.ByteBuffer; import java.util.Set; import java.util.UUID; -import biz.nynja.account.grpc.AccountDetails; -import biz.nynja.account.grpc.AccountDetails.Builder; - public class AccountByUsername { private UUID profileId; @@ -27,6 +24,7 @@ public class AccountByUsername { private Long lastUpdateTimestamp; private Set contactsInfo; private String qrCode; + private Set roles; public UUID getProfileId() { return profileId; @@ -148,6 +146,14 @@ public class AccountByUsername { this.qrCode = qrCode; } + public Set getRoles() { + return roles; + } + + public void setRoles(Set roles) { + this.roles = roles; + } + @Override public int hashCode() { final int prime = 31; @@ -166,6 +172,7 @@ public class AccountByUsername { result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); result = prime * result + ((qrCode == null) ? 0 : qrCode.hashCode()); + result = prime * result + ((roles == null) ? 0 : roles.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @@ -249,6 +256,11 @@ public class AccountByUsername { return false; } else if (!qrCode.equals(other.qrCode)) return false; + if (roles == null) { + if (other.roles != null) + return false; + } else if (!roles.equals(other.roles)) + return false; if (username == null) { if (other.username != null) return false; @@ -259,15 +271,17 @@ public class AccountByUsername { @Override public String toString() { - return new StringBuilder("Account [accountId=").append(accountId).append(", profileId=").append(profileId) + StringBuilder builder = new StringBuilder(); + builder.append("AccountByUsername [profileId=").append(profileId).append(", accountId=").append(accountId) .append(", accountMark=").append(accountMark).append(", authenticationProvider=") .append(authenticationProvider).append(", authenticationProviderType=") .append(authenticationProviderType).append(", firstName=").append(firstName).append(", lastName=") .append(lastName).append(", avatar=").append(avatar).append(", accountName=").append(accountName) .append(", username=").append(username).append(", accountStatus=").append(accountStatus) .append(", creationTimestamp=").append(creationTimestamp).append(", lastUpdateTimestamp=") - .append(lastUpdateTimestamp).append(", contactsInfo=").append(contactsInfo) - .append("]").toString(); + .append(lastUpdateTimestamp).append(", contactsInfo=").append(contactsInfo).append(", qrCode=") + .append(qrCode).append(", roles=").append(roles).append("]"); + return builder.toString(); } } diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index a108339194edef73bce5455b85683dee83e6b63c..7a867ece08b45ed26ff82e4acb2c1a3cef719533 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -4,11 +4,12 @@ package biz.nynja.account.repositories; import java.time.Instant; -import java.util.Date; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,9 +28,8 @@ import com.datastax.driver.core.Session; import biz.nynja.account.components.AccountServiceHelper; import biz.nynja.account.components.StatementsPool; - -import biz.nynja.account.grpc.AuthProviderDetails; import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; +import biz.nynja.account.grpc.Role; import biz.nynja.account.grpc.UpdateAccountRequest; import biz.nynja.account.grpc.UpdateProfileRequest; import biz.nynja.account.models.Account; @@ -141,6 +141,9 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio newAccount.setUsername(request.getUsername()); newAccount.setCreationTimestamp(creationTimestamp); newAccount.setQrCode(request.getQrCode()); + newAccount.setRoles((request.getRolesList() == null || request.getRolesList().isEmpty()) + ? new HashSet(Arrays.asList(Role.USER.toString())) + : request.getRolesList().stream().map(n -> n.toString()).collect(Collectors.toSet())); batchOps.insert(newAccount); } @@ -160,7 +163,8 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio PendingAccount pendingAccount) { ProfileByAuthenticationProvider newProfileByAuthenticationProvider = new ProfileByAuthenticationProvider(); newProfileByAuthenticationProvider.setAuthenticationProvider(pendingAccount.getAuthenticationProvider()); - newProfileByAuthenticationProvider.setAuthenticationProviderType(pendingAccount.getAuthenticationProviderType()); + newProfileByAuthenticationProvider + .setAuthenticationProviderType(pendingAccount.getAuthenticationProviderType()); newProfileByAuthenticationProvider.setProfileId(pendingAccount.getProfileId()); batchOps.insert(newProfileByAuthenticationProvider); } @@ -340,7 +344,8 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio batchOps.update(updatedProfile); } - private void updateAuthProvidersInProfileWhenDeletingAccount(CassandraBatchOperations batchOps, Profile existingProfile, Set authProvidersToUpdate, Long lastUpdateTimestamp) { + private void updateAuthProvidersInProfileWhenDeletingAccount(CassandraBatchOperations batchOps, + Profile existingProfile, Set authProvidersToUpdate, Long lastUpdateTimestamp) { Profile updatedProfile = existingProfile; if (authProvidersToUpdate != null) { updatedProfile.setAuthenticationProviders(authProvidersToUpdate); @@ -401,15 +406,16 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } } - public PendingAccountByAuthenticationProvider findSameAuthenticationProviderInPendingAccount(AuthenticationProvider authProvider) { + public PendingAccountByAuthenticationProvider findSameAuthenticationProviderInPendingAccount( + AuthenticationProvider authProvider) { List pendingAccounts = pendingAccountByAuthenticationProviderRepository .findAllByAuthenticationProvider(authProvider.getValue()); if (pendingAccounts.isEmpty()) { return null; } - //Both authentication provider identifier and type uniquely identify the authentication provider in DB. - //For this reason we need to filter results by authentication provider type. + // Both authentication provider identifier and type uniquely identify the authentication provider in DB. + // For this reason we need to filter results by authentication provider type. for (PendingAccountByAuthenticationProvider pendingAccount : pendingAccounts) { if (pendingAccount.getAuthenticationProviderType().equals(authProvider.getType())) { return pendingAccount; @@ -426,8 +432,8 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return false; } - //Both authentication provider identifier and type uniquely identify the authentication provider in DB. - //For this reason we need to filter results by authentication provider type. + // Both authentication provider identifier and type uniquely identify the authentication provider in DB. + // For this reason we need to filter results by authentication provider type. for (AccountByAuthenticationProvider account : accounts) { if (account.getAuthenticationProviderType().equals(authProvider.getType())) { return true; @@ -479,7 +485,8 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio public boolean authenticationProviderAlreadyUsedInProfile(AuthenticationProvider authProvider) { ProfileByAuthenticationProvider profile = profileByAuthenticationProviderRepository - .findByAuthenticationProviderAndAuthenticationProviderType(authProvider.getValue(), authProvider.getType()); + .findByAuthenticationProviderAndAuthenticationProviderType(authProvider.getValue(), + authProvider.getType()); if (profile != null) { return true; } @@ -572,8 +579,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } @Override - public boolean deleteAuthenticationProvider(Profile profile, - AuthenticationProvider authProvider) { + public boolean deleteAuthenticationProvider(Profile profile, AuthenticationProvider authProvider) { BatchStatement batch = new BatchStatement(); ResultSet rs = null;