From 0b10c8909702b1b16e4681cfbb79bd0269ade048 Mon Sep 17 00:00:00 2001
From: Vladislav Frolov <50615459+Cheshiriks@users.noreply.github.com>
Date: Thu, 20 Jul 2023 14:42:59 +0300
Subject: [PATCH] Refactoring user active (#2356)
* Refactoring user isActive field
---
...oringServerAuthenticationSuccessHandler.kt | 3 +-
.../AuthenticationUserRepository.kt | 3 +-
.../save/authservice/utils/SecurityUtils.kt | 3 +-
db/test-data/sqlRequests/user.csv | 28 +++++++++----------
db/v-2/tables/user.xml | 17 +++++++++++
.../controllers/UsersDetailsController.kt | 2 +-
.../backend/controller/UsersControllerTest.kt | 3 +-
.../controller/UsersDetailsControllerTest.kt | 5 ++--
.../com/saveourtool/save/info/UserInfo.kt | 4 +--
.../com/saveourtool/save/info/UserStatus.kt | 28 +++++++++++++++++++
.../com/saveourtool/save/entities/User.kt | 12 ++++----
.../com/saveourtool/save/frontend/App.kt | 3 +-
.../components/views/RegistrationView.kt | 5 ++--
.../views/usersettings/UserSettingsView.kt | 2 +-
14 files changed, 85 insertions(+), 33 deletions(-)
create mode 100644 save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserStatus.kt
diff --git a/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/utils/StoringServerAuthenticationSuccessHandler.kt b/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/utils/StoringServerAuthenticationSuccessHandler.kt
index 21005080d0..e1d6bc2a46 100644
--- a/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/utils/StoringServerAuthenticationSuccessHandler.kt
+++ b/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/utils/StoringServerAuthenticationSuccessHandler.kt
@@ -3,6 +3,7 @@ package com.saveourtool.save.gateway.utils
import com.saveourtool.save.domain.Role
import com.saveourtool.save.entities.User
import com.saveourtool.save.gateway.config.ConfigurationProperties
+import com.saveourtool.save.info.UserStatus
import org.slf4j.LoggerFactory
import org.springframework.http.MediaType
@@ -58,6 +59,6 @@ fun Authentication.toUser(): User = User(
authorities.joinToString(",") { it.authority },
toIdentitySource(),
null,
- isActive = false,
+ status = UserStatus.CREATED,
originalLogins = emptyList(),
)
diff --git a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt
index c460574f29..3ee445cfa4 100644
--- a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt
+++ b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt
@@ -1,6 +1,7 @@
package com.saveourtool.save.authservice.repository
import com.saveourtool.save.entities.User
+import com.saveourtool.save.info.UserStatus
import com.saveourtool.save.utils.orNotFound
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
import org.springframework.stereotype.Component
@@ -46,7 +47,7 @@ class AuthenticationUserRepository(
linkedin = record["linkedin"] as String?,
gitHub = record["git_hub"] as String?,
twitter = record["twitter"] as String?,
- isActive = record["is_active"] as Boolean,
+ status = UserStatus.valueOf(record["status"] as String),
rating = record["rating"] as Long,
).apply {
this.id = record["id"] as Long
diff --git a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/SecurityUtils.kt b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/SecurityUtils.kt
index 1a8effbf9b..c3eaae716f 100644
--- a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/SecurityUtils.kt
+++ b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/SecurityUtils.kt
@@ -6,6 +6,7 @@ package com.saveourtool.save.authservice.utils
import com.saveourtool.save.domain.Role
import com.saveourtool.save.entities.User
+import com.saveourtool.save.info.UserStatus
import org.springframework.security.access.hierarchicalroles.RoleHierarchy
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl
import org.springframework.security.access.hierarchicalroles.RoleHierarchyUtils
@@ -70,7 +71,7 @@ fun Authentication.toUser(): User {
email = null,
role = (this as UsernamePasswordAuthenticationToken).authorities.joinToString(separator = ","),
source = identitySource,
- isActive = false,
+ status = UserStatus.CREATED,
)
}
diff --git a/db/test-data/sqlRequests/user.csv b/db/test-data/sqlRequests/user.csv
index 73f76bd5a7..0ad52f1470 100644
--- a/db/test-data/sqlRequests/user.csv
+++ b/db/test-data/sqlRequests/user.csv
@@ -1,14 +1,14 @@
-id;name;password;role;source;avatar;email;company;location;linkedin;git_hub;twitter;is_active;rating
-1;"admin";"{bcrypt}$2a$12$7jB6.fJ/uHZiti94w0.QOu0M/lPzKU1lvX9TLL3uZHFjLGIfz1gHq";"ROLE_SUPER_ADMIN";"basic";;;;;;;;true;999
-2;"JohnDoe";"{bcrypt}$2a$12$zjCjULtsSvapH5kt6KUq4eyTx3WluDwB3GepcbLA.mf/NcGZCLuvi";"ROLE_VIEWER";"basic";;;;;;;;false;-37
-3;"user";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;0
-4;"user1";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;-3
-5;"user2";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;0
-6;"user3";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;15
-7;"user4";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;34
-8;"user5";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;0
-9;"user6";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;54
-10;"user7";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;86
-11;"user8";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;0
-12;"user9";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;12
-13;"user10";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;false;24
+id;name;password;role;source;avatar;email;company;location;linkedin;git_hub;twitter;rating;status
+1;"admin";"{bcrypt}$2a$12$7jB6.fJ/uHZiti94w0.QOu0M/lPzKU1lvX9TLL3uZHFjLGIfz1gHq";"ROLE_SUPER_ADMIN";"basic";;;;;;;;999;"ACTIVE"
+2;"JohnDoe";"{bcrypt}$2a$12$zjCjULtsSvapH5kt6KUq4eyTx3WluDwB3GepcbLA.mf/NcGZCLuvi";"ROLE_VIEWER";"basic";;;;;;;;-37;"CREATED"
+3;"user";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;0;"CREATED"
+4;"user1";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;-3;"CREATED"
+5;"user2";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;0;"CREATED"
+6;"user3";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;15;"CREATED"
+7;"user4";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;34;"CREATED"
+8;"user5";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;0;"CREATED"
+9;"user6";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;54;"CREATED"
+10;"user7";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;86;"CREATED"
+11;"user8";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;0;"CREATED"
+12;"user9";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;12;"CREATED"
+13;"user10";"noop{123}";"ROLE_VIEWER";"basic";;;;;;;;24;"CREATED"
diff --git a/db/v-2/tables/user.xml b/db/v-2/tables/user.xml
index e4bd47f55e..91604026c5 100644
--- a/db/v-2/tables/user.xml
+++ b/db/v-2/tables/user.xml
@@ -71,4 +71,21 @@
+
+
+
+
+
+
+
+
+
+ UPDATE user SET status = 'ACTIVE' WHERE is_active = 1;
+
+
+
+
+
+
+
diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/UsersDetailsController.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/UsersDetailsController.kt
index 1134269cfa..94b12ba4d0 100644
--- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/UsersDetailsController.kt
+++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/UsersDetailsController.kt
@@ -114,7 +114,7 @@ class UsersDetailsController(
gitHub = newUserInfo.gitHub
linkedin = newUserInfo.linkedin
twitter = newUserInfo.twitter
- isActive = newUserInfo.isActive
+ status = newUserInfo.status
}, newUserInfo.oldName)
} else {
UserSaveStatus.CONFLICT
diff --git a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersControllerTest.kt b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersControllerTest.kt
index 629e766bcc..180beafb58 100644
--- a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersControllerTest.kt
+++ b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersControllerTest.kt
@@ -6,6 +6,7 @@ import com.saveourtool.save.domain.Role
import com.saveourtool.save.entities.OriginalLogin
import com.saveourtool.save.entities.User
import com.saveourtool.save.info.UserInfo
+import com.saveourtool.save.info.UserStatus
import com.saveourtool.save.v1
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
@@ -36,7 +37,7 @@ class UsersControllerTest {
Role.VIEWER.asSpringSecurityRole(),
"basic2",
null,
- isActive = false,
+ status = UserStatus.CREATED,
).apply { id = 99 }
val originalLogin = OriginalLogin(
diff --git a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersDetailsControllerTest.kt b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersDetailsControllerTest.kt
index b9076e1f62..79141a0220 100644
--- a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersDetailsControllerTest.kt
+++ b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersDetailsControllerTest.kt
@@ -5,6 +5,7 @@ import com.saveourtool.save.backend.SaveApplication
import com.saveourtool.save.backend.utils.InfraExtension
import com.saveourtool.save.backend.utils.mutateMockedUser
import com.saveourtool.save.info.UserInfo
+import com.saveourtool.save.info.UserStatus
import com.saveourtool.save.v1
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
@@ -34,7 +35,7 @@ class UsersDetailsControllerTest {
source = "basic",
email = "example@save.com",
company = "Example",
- isActive = true,
+ status = UserStatus.ACTIVE,
)
webClient.post()
@@ -58,7 +59,7 @@ class UsersDetailsControllerTest {
oldName = "admin",
email = "example@save.com",
company = "Example",
- isActive = true,
+ status = UserStatus.ACTIVE,
)
webClient.post()
diff --git a/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserInfo.kt b/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserInfo.kt
index 85482701e5..35bfc3c30a 100644
--- a/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserInfo.kt
+++ b/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserInfo.kt
@@ -23,7 +23,7 @@ import kotlinx.serialization.Serializable
* @property organizations
* @property globalRole
* @property id
- * @property isActive
+ * @property status
* @property oldName is always null except for the process of renaming the user.
* @property originalLogins
* @property rating
@@ -46,7 +46,7 @@ data class UserInfo(
val gitHub: String? = null,
val twitter: String? = null,
val globalRole: Role? = null,
- val isActive: Boolean = false,
+ val status: UserStatus = UserStatus.CREATED,
val rating: Long = 0,
) : Validatable {
/**
diff --git a/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserStatus.kt b/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserStatus.kt
new file mode 100644
index 0000000000..5426ebb613
--- /dev/null
+++ b/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserStatus.kt
@@ -0,0 +1,28 @@
+package com.saveourtool.save.info
+
+import kotlinx.serialization.Serializable
+
+/**
+ * Enum of user status
+ *
+ * The order of the elements is used for sorting
+ */
+@Serializable
+@Suppress("WRONG_DECLARATIONS_ORDER")
+enum class UserStatus {
+ /**
+ * User created
+ */
+ CREATED,
+
+ /**
+ * User active
+ */
+ ACTIVE,
+
+ /**
+ * User deleted
+ */
+ DELETED,
+ ;
+}
diff --git a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/User.kt b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/User.kt
index 2ad3888523..ea6e3fa919 100644
--- a/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/User.kt
+++ b/save-cloud-common/src/jvmMain/kotlin/com/saveourtool/save/entities/User.kt
@@ -2,13 +2,12 @@ package com.saveourtool.save.entities
import com.saveourtool.save.domain.Role
import com.saveourtool.save.info.UserInfo
+import com.saveourtool.save.info.UserStatus
import com.saveourtool.save.spring.entity.BaseEntity
import com.fasterxml.jackson.annotation.JsonIgnore
-import javax.persistence.Entity
-import javax.persistence.FetchType
-import javax.persistence.OneToMany
+import javax.persistence.*
/**
* @property name
@@ -22,7 +21,7 @@ import javax.persistence.OneToMany
* @property linkedin
* @property gitHub
* @property twitter
- * @property isActive
+ * @property status
* @property originalLogins
* @property rating rating of user
*/
@@ -40,7 +39,8 @@ class User(
var linkedin: String? = null,
var gitHub: String? = null,
var twitter: String? = null,
- var isActive: Boolean = false,
+ @Enumerated(EnumType.STRING)
+ var status: UserStatus = UserStatus.CREATED,
@OneToMany(
fetch = FetchType.EAGER,
mappedBy = "user",
@@ -69,7 +69,7 @@ class User(
gitHub = gitHub,
twitter = twitter,
location = location,
- isActive = isActive,
+ status = status,
rating = rating,
)
}
diff --git a/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/App.kt b/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/App.kt
index 450b19ad1b..1309b4713b 100644
--- a/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/App.kt
+++ b/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/App.kt
@@ -14,6 +14,7 @@ import com.saveourtool.save.frontend.http.getUser
import com.saveourtool.save.frontend.routing.basicRouting
import com.saveourtool.save.frontend.utils.*
import com.saveourtool.save.info.UserInfo
+import com.saveourtool.save.info.UserStatus
import com.saveourtool.save.validation.FrontendRoutes
import react.*
@@ -107,7 +108,7 @@ class App : ComponentWithScope() {
requestModalHandler {
userInfo = state.userInfo
- if (state.userInfo?.isActive == false) {
+ if (state.userInfo?.status == UserStatus.CREATED) {
Navigate {
to = "/${FrontendRoutes.REGISTRATION}"
replace = false
diff --git a/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/RegistrationView.kt b/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/RegistrationView.kt
index 94cc7cb5bc..db30fa73d7 100644
--- a/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/RegistrationView.kt
+++ b/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/RegistrationView.kt
@@ -13,6 +13,7 @@ import com.saveourtool.save.frontend.components.modal.MAX_Z_INDEX
import com.saveourtool.save.frontend.http.postImageUpload
import com.saveourtool.save.frontend.utils.*
import com.saveourtool.save.info.UserInfo
+import com.saveourtool.save.info.UserStatus
import com.saveourtool.save.utils.AvatarType
import com.saveourtool.save.v1
import com.saveourtool.save.validation.isValidName
@@ -63,7 +64,7 @@ val registrationView: FC = FC { props ->
val saveUser = useDeferredRequest {
val newUserInfo = userInfo.copy(
oldName = props.userInfo?.name!!,
- isActive = true,
+ status = UserStatus.ACTIVE,
)
val response = post(
"$apiUrl/users/save",
@@ -91,7 +92,7 @@ val registrationView: FC = FC { props ->
}
}
- if (props.userInfo?.isActive != false) {
+ if (props.userInfo?.status == UserStatus.ACTIVE) {
navigate("/", jso { replace = true })
}
diff --git a/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/usersettings/UserSettingsView.kt b/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/usersettings/UserSettingsView.kt
index ff34ae2b33..1439cc1692 100644
--- a/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/usersettings/UserSettingsView.kt
+++ b/save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/usersettings/UserSettingsView.kt
@@ -320,7 +320,7 @@ abstract class UserSettingsView : AbstractView