From b23ea83f54c82d90746bf441910cd645d911c348 Mon Sep 17 00:00:00 2001 From: zabarudo Date: Thu, 19 Jan 2023 23:01:31 +0530 Subject: [PATCH 1/5] feat: Added Username Validation --- .../codecharacter/server/user/public_user/PublicUserService.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt index cecbf277..45205463 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt @@ -101,6 +101,9 @@ class PublicUserService(@Autowired private val publicUserRepository: PublicUserR fun updateUserProfile(userId: UUID, updateCurrentUserProfileDto: UpdateCurrentUserProfileDto) { val user = publicUserRepository.findById(userId).get() + if (updateCurrentUserProfileDto.name?.length!! <= 5) { + throw CustomException(HttpStatus.BAD_REQUEST, "Username must be minimum 5 characters long") + } val updatedUser = user.copy( name = updateCurrentUserProfileDto.name ?: user.name, From b4b9ba63d239a3e0ed8640225bceb43e79baa787 Mon Sep 17 00:00:00 2001 From: zabarudo Date: Sun, 22 Jan 2023 13:10:08 +0530 Subject: [PATCH 2/5] fix: Added validation for updateProfile --- .../user/public_user/PublicUserService.kt | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt index 45205463..0a2d85e2 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt @@ -101,8 +101,25 @@ class PublicUserService(@Autowired private val publicUserRepository: PublicUserR fun updateUserProfile(userId: UUID, updateCurrentUserProfileDto: UpdateCurrentUserProfileDto) { val user = publicUserRepository.findById(userId).get() - if (updateCurrentUserProfileDto.name?.length!! <= 5) { - throw CustomException(HttpStatus.BAD_REQUEST, "Username must be minimum 5 characters long") + if (updateCurrentUserProfileDto.name.isNullOrEmpty() && + updateCurrentUserProfileDto.college.isNullOrEmpty() && + updateCurrentUserProfileDto.avatarId == user.avatarId && + updateCurrentUserProfileDto.country == user.country + ) { + throw CustomException(HttpStatus.BAD_REQUEST, "No User Details have changed") + } else { + if (updateCurrentUserProfileDto.name?.length!! <= 5) { + throw CustomException(HttpStatus.BAD_REQUEST, "Username must be minimum 5 characters") + } else { + val updatedUser = + user.copy( + name = updateCurrentUserProfileDto.name ?: user.name, + country = updateCurrentUserProfileDto.country ?: user.country, + college = updateCurrentUserProfileDto.college ?: user.college, + avatarId = updateCurrentUserProfileDto.avatarId ?: user.avatarId + ) + publicUserRepository.save(updatedUser) + } } val updatedUser = user.copy( From 150663737bb355d4eb7513d5c832849b8e6a14bc Mon Sep 17 00:00:00 2001 From: zabarudo Date: Thu, 26 Jan 2023 23:59:37 +0530 Subject: [PATCH 3/5] fix: Added validation for change User Details and Register New User --- .../server/exception/RestExceptionHandler.kt | 16 +++++--- .../codecharacter/server/user/UserService.kt | 16 ++++++++ .../user/public_user/PublicUserService.kt | 40 ++++++++++--------- 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/server/src/main/kotlin/delta/codecharacter/server/exception/RestExceptionHandler.kt b/server/src/main/kotlin/delta/codecharacter/server/exception/RestExceptionHandler.kt index a115f8e2..4a2066ac 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/exception/RestExceptionHandler.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/exception/RestExceptionHandler.kt @@ -1,5 +1,6 @@ package delta.codecharacter.server.exception +import com.fasterxml.jackson.databind.exc.InvalidFormatException import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException import jakarta.validation.ConstraintViolationException import org.springframework.core.Ordered @@ -25,12 +26,15 @@ class RestExceptionHandler : ResponseEntityExceptionHandler() { status: HttpStatusCode, request: WebRequest ): ResponseEntity? { - val cause = ex.cause - return if (cause is MissingKotlinParameterException) { - ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body(mapOf("message" to "${cause.parameter.name} is missing")) - } else { - ResponseEntity.status(HttpStatus.BAD_REQUEST).body(mapOf("message" to "Unknown error")) + return when (val cause = ex.cause) { + is MissingKotlinParameterException -> + ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(mapOf("message" to "${cause.parameter.name} is missing")) + is InvalidFormatException -> + ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(mapOf("message" to "${cause.value} is of Invalid Format")) + else -> + ResponseEntity.status(HttpStatus.BAD_REQUEST).body(mapOf("message" to "Unknown Error")) } } diff --git a/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt b/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt index b93d8fd2..f132c4d0 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt @@ -129,6 +129,22 @@ class UserService( ) = registerUserRequestDto + if (username.length < 5) { + throw CustomException(HttpStatus.BAD_REQUEST, "Username must be minimum 5 characters long") + } + if (name.length < 10) { + throw CustomException(HttpStatus.BAD_REQUEST, "Name must be minimum 10 characters long") + } + if (avatarId !in 0..19) { + throw CustomException(HttpStatus.BAD_REQUEST, "AvatarId must be between 0 and 19") + } + if (college.isEmpty()) { + throw CustomException(HttpStatus.BAD_REQUEST, "College must not be empty") + } + // how to check if country in list of countries? + if (country.isEmpty()) { + throw CustomException(HttpStatus.BAD_REQUEST, "Country must not be empty") + } if (password != passwordConfirmation) { throw CustomException( HttpStatus.BAD_REQUEST, "Password and password confirmation don't match" diff --git a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt index 0a2d85e2..1c7bd5e8 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt @@ -101,31 +101,35 @@ class PublicUserService(@Autowired private val publicUserRepository: PublicUserR fun updateUserProfile(userId: UUID, updateCurrentUserProfileDto: UpdateCurrentUserProfileDto) { val user = publicUserRepository.findById(userId).get() - if (updateCurrentUserProfileDto.name.isNullOrEmpty() && - updateCurrentUserProfileDto.college.isNullOrEmpty() && - updateCurrentUserProfileDto.avatarId == user.avatarId && - updateCurrentUserProfileDto.country == user.country + + if (updateCurrentUserProfileDto.name != null && updateCurrentUserProfileDto.name!!.length < 5) { + throw CustomException(HttpStatus.BAD_REQUEST, "Username must be minimum 5 characters") + } + + // how to check if country in list of countries? + if (updateCurrentUserProfileDto.country != null && + updateCurrentUserProfileDto.country!!.isEmpty() ) { - throw CustomException(HttpStatus.BAD_REQUEST, "No User Details have changed") - } else { - if (updateCurrentUserProfileDto.name?.length!! <= 5) { - throw CustomException(HttpStatus.BAD_REQUEST, "Username must be minimum 5 characters") - } else { - val updatedUser = - user.copy( - name = updateCurrentUserProfileDto.name ?: user.name, - country = updateCurrentUserProfileDto.country ?: user.country, - college = updateCurrentUserProfileDto.college ?: user.college, - avatarId = updateCurrentUserProfileDto.avatarId ?: user.avatarId - ) - publicUserRepository.save(updatedUser) - } + throw CustomException(HttpStatus.BAD_REQUEST, "Country must not be an empty string") + } + + if (updateCurrentUserProfileDto.college != null && + updateCurrentUserProfileDto.college!!.isEmpty() + ) { + throw CustomException(HttpStatus.BAD_REQUEST, "College must not be an empty string") } + if (updateCurrentUserProfileDto.avatarId != null && + updateCurrentUserProfileDto.avatarId!! !in 0..19 + ) { + throw CustomException(HttpStatus.BAD_REQUEST, "AvatarId must be between 0 and 19") + } + val updatedUser = user.copy( name = updateCurrentUserProfileDto.name ?: user.name, country = updateCurrentUserProfileDto.country ?: user.country, college = updateCurrentUserProfileDto.college ?: user.college, + avatarId = updateCurrentUserProfileDto.avatarId ?: user.avatarId, tutorialLevel = updateTutorialLevel( updateCurrentUserProfileDto.updateTutorialLevel, user.tutorialLevel From ffe7bc171dd25b12bf2ef864651a5a54091da8ad Mon Sep 17 00:00:00 2001 From: Ram-20062003 Date: Mon, 6 Feb 2023 13:45:53 +0530 Subject: [PATCH 4/5] fix: empty field validation, messages, remove comments, DC logger --- .../server/seeders/DailyChallengeSeeder.kt | 1 + .../codecharacter/server/user/UserService.kt | 17 ++++++++--------- .../user/public_user/PublicUserService.kt | 17 +++++++++-------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt b/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt index 54a7affc..66c50118 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt @@ -44,6 +44,7 @@ class DailyChallengeSeeder { ) } dailyChallengeRepository.saveAll(dcEntities) + logger.info("Seeding Daily-Challenges Completed") } else { logger.error("dcConstants.json is empty or doesn't exist") } diff --git a/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt b/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt index f132c4d0..0228c3b8 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/user/UserService.kt @@ -129,21 +129,20 @@ class UserService( ) = registerUserRequestDto - if (username.length < 5) { + if (username.trim().length < 5) { throw CustomException(HttpStatus.BAD_REQUEST, "Username must be minimum 5 characters long") } - if (name.length < 10) { - throw CustomException(HttpStatus.BAD_REQUEST, "Name must be minimum 10 characters long") + if (name.trim().length < 5) { + throw CustomException(HttpStatus.BAD_REQUEST, "Name must be minimum 5 characters long") } if (avatarId !in 0..19) { - throw CustomException(HttpStatus.BAD_REQUEST, "AvatarId must be between 0 and 19") + throw CustomException(HttpStatus.BAD_REQUEST, "Selected Avatar is invalid") } - if (college.isEmpty()) { - throw CustomException(HttpStatus.BAD_REQUEST, "College must not be empty") + if (college.trim().isEmpty()) { + throw CustomException(HttpStatus.BAD_REQUEST, "College can not be empty") } - // how to check if country in list of countries? - if (country.isEmpty()) { - throw CustomException(HttpStatus.BAD_REQUEST, "Country must not be empty") + if (country.trim().isEmpty()) { + throw CustomException(HttpStatus.BAD_REQUEST, "Country can not be empty") } if (password != passwordConfirmation) { throw CustomException( diff --git a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt index 1c7bd5e8..ccca2e10 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/user/public_user/PublicUserService.kt @@ -102,26 +102,27 @@ class PublicUserService(@Autowired private val publicUserRepository: PublicUserR fun updateUserProfile(userId: UUID, updateCurrentUserProfileDto: UpdateCurrentUserProfileDto) { val user = publicUserRepository.findById(userId).get() - if (updateCurrentUserProfileDto.name != null && updateCurrentUserProfileDto.name!!.length < 5) { - throw CustomException(HttpStatus.BAD_REQUEST, "Username must be minimum 5 characters") + if (updateCurrentUserProfileDto.name != null && + updateCurrentUserProfileDto.name!!.trim().length < 5 + ) { + throw CustomException(HttpStatus.BAD_REQUEST, "Name must be minimum 5 characters") } - // how to check if country in list of countries? if (updateCurrentUserProfileDto.country != null && - updateCurrentUserProfileDto.country!!.isEmpty() + updateCurrentUserProfileDto.country!!.trim().isEmpty() ) { - throw CustomException(HttpStatus.BAD_REQUEST, "Country must not be an empty string") + throw CustomException(HttpStatus.BAD_REQUEST, "Country can not be an empty") } if (updateCurrentUserProfileDto.college != null && - updateCurrentUserProfileDto.college!!.isEmpty() + updateCurrentUserProfileDto.college!!.trim().isEmpty() ) { - throw CustomException(HttpStatus.BAD_REQUEST, "College must not be an empty string") + throw CustomException(HttpStatus.BAD_REQUEST, "College can not be an empty") } if (updateCurrentUserProfileDto.avatarId != null && updateCurrentUserProfileDto.avatarId!! !in 0..19 ) { - throw CustomException(HttpStatus.BAD_REQUEST, "AvatarId must be between 0 and 19") + throw CustomException(HttpStatus.BAD_REQUEST, "Selected Avatar is invalid") } val updatedUser = From 731ec925e923621f8d26a902b57f12f52f99f1ab Mon Sep 17 00:00:00 2001 From: Ram-20062003 Date: Mon, 6 Feb 2023 14:41:00 +0530 Subject: [PATCH 5/5] fix: function name --- .../delta/codecharacter/server/seeders/DailyChallengeSeeder.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt b/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt index 66c50118..3b4a3390 100644 --- a/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt +++ b/server/src/main/kotlin/delta/codecharacter/server/seeders/DailyChallengeSeeder.kt @@ -19,7 +19,7 @@ class DailyChallengeSeeder { private val logger: Logger = LoggerFactory.getLogger(DailyChallengeSeeder::class.java) @EventListener(ApplicationReadyEvent::class) - fun doSomethingAfterStartup() { + fun seedDailyChallenges() { if (dailyChallengeRepository.findAll().isEmpty()) { logger.info("Seeding daily_challenges")