From 259c5dccd541854e573f30986ab65fe4bcb951a0 Mon Sep 17 00:00:00 2001 From: Dragomir Todorov Date: Thu, 31 Jan 2019 16:37:23 +0200 Subject: [PATCH] NY-6840: Delete user avatar --- .../biz/nynja/account/models/Account.java | 43 ++++++++++++++++- .../account/services/AccountServiceImpl.java | 47 +++++++++++++++++++ .../decomposition/AccountProvider.java | 11 ++++- 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/main/java/biz/nynja/account/models/Account.java b/src/main/java/biz/nynja/account/models/Account.java index 34807d5..f6e26c8 100644 --- a/src/main/java/biz/nynja/account/models/Account.java +++ b/src/main/java/biz/nynja/account/models/Account.java @@ -4,7 +4,6 @@ package biz.nynja.account.models; import java.io.Serializable; -import java.nio.ByteBuffer; import java.time.LocalDate; import java.util.Set; import java.util.UUID; @@ -18,6 +17,7 @@ import biz.nynja.account.grpc.AccountDetails.Builder; import biz.nynja.account.grpc.CreatePendingAccountRequest; import biz.nynja.account.grpc.Date; import biz.nynja.account.grpc.Role; +import biz.nynja.account.grpc.UpdateAccountRequest; @Table public class Account implements Serializable { @@ -385,4 +385,45 @@ public class Account implements Serializable { } + public UpdateAccountRequest toUpdateAccountProto() { + + biz.nynja.account.grpc.UpdateAccountRequest.Builder builder = UpdateAccountRequest.newBuilder(); + + if (getAccountId() != null) { + builder.setAccountId(getAccountId().toString()); + } + if (getAccountMark() != null) { + builder.setAccountMark(getAccountMark()); + } + if (getAccountName() != null) { + builder.setAccountName(getAccountName()); + } + if (getFirstName() != null) { + builder.setFirstName(getFirstName()); + } + if (getLastName() != null) { + builder.setLastName(getLastName()); + } + if (getUsername() != null) { + builder.setUsername(getUsername()); + } + if (getAvatar() != null) { + builder.setAvatar(avatar); + } + if (getRoles() != null) { + for (String role : getRoles()) { + builder.addRoles(Role.valueOf(role)); + } + } + if (getAccessStatus() != null) { + builder.setAccessStatus(AccessStatus.valueOf(getAccessStatus())); + } + if (getBirthday() != null) { + builder.setBirthday(Date.newBuilder().setYear(getBirthday().getYear()) + .setMonth(getBirthday().getMonthValue()).setDay(getBirthday().getDayOfMonth()).build()); + } + + return builder.build(); + } + } diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index 191f704..5c6575a 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -33,6 +33,7 @@ import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; import biz.nynja.account.grpc.ContactType; import biz.nynja.account.grpc.CreatePendingAccountRequest; import biz.nynja.account.grpc.CreatePendingAccountResponse; +import biz.nynja.account.grpc.DeleteAccountAvatarRequest; import biz.nynja.account.grpc.DeleteAccountRequest; import biz.nynja.account.grpc.DeleteAuthenticationProviderRequest; import biz.nynja.account.grpc.DeleteContactInfoRequest; @@ -1275,6 +1276,52 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas responseObserver.onCompleted(); } + @Override + @PerformPermissionCheck + @Permitted(role = RoleConstants.ACCOUNT_ADMIN) + @Permitted(role = RoleConstants.USER) + public void deleteAccountAvatar(DeleteAccountAvatarRequest request, + StreamObserver responseObserver) { + logger.info("Updating account..."); + logger.debug("Updating account...: {}", request); + + if ((request.getAccountId() == null) || (request.getAccountId().isEmpty())) { + logAndBuildGrpcStatusResponse(responseObserver, StatusResponse.newBuilder(), "Missing account ID", "", + Cause.MISSING_ACCOUNT_ID, "Missing account ID"); + return; + } + if (!util.isValidUuid(request.getAccountId())) { + logAndBuildGrpcStatusResponse(responseObserver, StatusResponse.newBuilder(), "Invalid account id: {}", + request.getAccountId(), Cause.INVALID_ACCOUNT_ID, "Invalid Account ID"); + return; + } + + if (!permissionsValidator.isRpcAllowed(request.getAccountId())) { + logAndBuildGrpcStatusResponse(responseObserver, StatusResponse.newBuilder(), "Can not update account {}.", + request.getAccountId(), Cause.ERROR_PERMISSION_DENIED, "Permission denied"); + return; + } + + Optional account = accountProvider.retrieveAccountById(request.getAccountId()); + if (!account.isPresent()) { + logAndBuildGrpcStatusResponse(responseObserver, StatusResponse.newBuilder(), + "Failed to delete avatar. Account {} not found.", request.getAccountId(), Cause.ACCOUNT_NOT_FOUND, + "Failed to delete avatar. Account not found."); + } + + UpdateAccountRequest updateRequest = UpdateAccountRequest.newBuilder(account.get().toUpdateAccountProto()) + .setAvatar("").build(); + + Account updatedAccount = accountRepositoryAdditional.updateAccount(updateRequest); + if (updatedAccount == null) { + logAndBuildGrpcStatusResponse(responseObserver, StatusResponse.newBuilder(), + "Failed to delete avatar of account {}.", request.getAccountId(), Cause.INTERNAL_SERVER_ERROR, + "Failed to delete avatar"); + } + responseObserver.onNext(StatusResponse.newBuilder().setStatus("SUCCESS").build()); + responseObserver.onCompleted(); + } + private static void logAndBuildGrpcSearchResponse(StreamObserver responseObserver, SearchResponse.Builder newBuilder, String logMessage, String logValue, Cause cause, String causeMessage) { logger.error(logMessage, logValue); diff --git a/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java b/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java index 0e5bd08..a062706 100644 --- a/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java +++ b/src/main/java/biz/nynja/account/services/decomposition/AccountProvider.java @@ -8,7 +8,6 @@ import java.util.Optional; import java.util.UUID; import java.util.stream.Collectors; -import biz.nynja.account.phone.PhoneNumberNormalizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -27,6 +26,7 @@ import biz.nynja.account.models.AccountByProfileId; import biz.nynja.account.models.AccountByQrCode; import biz.nynja.account.models.AccountByUsername; import biz.nynja.account.models.AuthenticationProvider; +import biz.nynja.account.phone.PhoneNumberNormalizer; import biz.nynja.account.repositories.AccountByProfileIdRepository; import biz.nynja.account.repositories.AccountByQrCodeRepository; import biz.nynja.account.repositories.AccountByUsernameRepository; @@ -180,4 +180,13 @@ public class AccountProvider { phoneNumberNormalizer.getNormalizedPhoneNumber(request.getAuthenticationIdentifier())) .build(); } + + public Optional retrieveAccountById(String accountId) { + Account account = accountRepository.findByAccountId(UUID.fromString(accountId)); + if (account == null || account.getAccountId() == null) { + return Optional.empty(); + } + + return Optional.of(account); + } } -- GitLab