From 92c45f1165a89277919ab5f076e735b2fdd7e7e4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 16 Jan 2025 07:11:17 +0300 Subject: [PATCH] AdminInterface --- src/main/kotlin/AdminInterface.kt | 8 ++ src/main/kotlin/Main.kt | 80 +++++++++++++++---- src/main/kotlin/model/AdminUser.kt | 50 ++++++++++++ src/main/kotlin/model/DefaultUser.kt | 11 +++ src/main/kotlin/model/ManagerUser.kt | 11 +++ src/main/kotlin/model/Role.kt | 6 -- .../model/{UserAuthorize.kt => User.kt} | 6 +- 7 files changed, 145 insertions(+), 27 deletions(-) create mode 100644 src/main/kotlin/AdminInterface.kt create mode 100644 src/main/kotlin/model/AdminUser.kt create mode 100644 src/main/kotlin/model/DefaultUser.kt create mode 100644 src/main/kotlin/model/ManagerUser.kt delete mode 100644 src/main/kotlin/model/Role.kt rename src/main/kotlin/model/{UserAuthorize.kt => User.kt} (71%) diff --git a/src/main/kotlin/AdminInterface.kt b/src/main/kotlin/AdminInterface.kt new file mode 100644 index 0000000..19fd4f4 --- /dev/null +++ b/src/main/kotlin/AdminInterface.kt @@ -0,0 +1,8 @@ +import model.User + +interface AdminInterface { + fun allUsers(authorizeList: MutableList) + fun changeUserPassword(authorizeList: MutableList) + fun removeUser(authorizeList: MutableList) + fun allUsersSorted(authorizeList: MutableList) +} \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index c0b77bb..5265c3a 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,5 +1,7 @@ -import model.UserAuthorize -import model.Role +import model.AdminUser +import model.DefaultUser +import model.ManagerUser +import model.User import java.security.SecureRandom import java.security.spec.KeySpec import java.time.LocalDate @@ -8,12 +10,16 @@ import javax.crypto.SecretKeyFactory import javax.crypto.spec.PBEKeySpec import java.util.Base64 -val authorizeList = mutableListOf() +val authorizeList = mutableListOf() val salt = "sallllt" private const val HASH_LENGTH = 256 private const val ITERATIONS = 65536 fun main(){ + authorizeList.add(AdminUser(email = "@1", password = hashPassword("qwerty123", salt), login = "aboba", lastAuthorizeDate = LocalDate.of(2022, 11, 25), userId = 1)) + authorizeList.add(ManagerUser(email = "@2", password = hashPassword("qwerty123", salt), login = "abiba", lastAuthorizeDate = LocalDate.of(2023, 3, 10), userId = 2)) + authorizeList.add(DefaultUser(email = "@3", password = hashPassword("qwerty123", salt), login = "abobus", lastAuthorizeDate = LocalDate.of(2023, 5, 15), userId = 3)) + while (true){ println("1) registration") println("2) authorization") @@ -25,7 +31,6 @@ fun main(){ println(it.email) println(it.password) println(it.login) - println(it.role.roleName) } } else{ @@ -34,7 +39,7 @@ fun main(){ } } -fun registration(): UserAuthorize{ +fun registration(): User{ println("email") val userEmail = readlnOrNull() checkNotNull(userEmail) @@ -63,20 +68,39 @@ fun registration(): UserAuthorize{ "more longer login" } - return UserAuthorize( - authorizeList.size + 1, - login = userLogin, - password = hashPassword(userPassword, salt), - lastAuthorizeDate = LocalDate.now(), - email = userEmail, - role = Role( - roleId = 1, - roleName = "User" + println("1)admin, 2)manager, 3)defaultUser") + val role = readlnOrNull() + checkNotNull(role) + + return when(role){ + "1" -> return AdminUser( + authorizeList.size + 1, + login = userLogin, + password = hashPassword(userPassword, salt), + lastAuthorizeDate = LocalDate.now(), + email = userEmail, ) - ) + "2" -> return ManagerUser( + authorizeList.size + 1, + login = userLogin, + password = hashPassword(userPassword, salt), + lastAuthorizeDate = LocalDate.now(), + email = userEmail, + ) + "3" -> return DefaultUser( + authorizeList.size + 1, + login = userLogin, + password = hashPassword(userPassword, salt), + lastAuthorizeDate = LocalDate.now(), + email = userEmail, + ) + + else -> {throw IllegalArgumentException("error")} + } } fun authorization(): String{ + var adminChoose = true println("email") val userEmail = readlnOrNull() checkNotNull(userEmail) @@ -92,9 +116,31 @@ fun authorization(): String{ "incorrect password" } - user.lastAuthorizeDate = LocalDate.now() + if (user is AdminUser){ + while (adminChoose == true){ + adminChoose = adminFunc(user) + } + } - return user.role.roleName + return "agada" +} + +fun adminFunc(user: AdminUser): Boolean{ + println("1) вывести всех пользователей") + println("2) поменять пароль пользователя") + println("3) удалить пользователя") + println("4) вывести с сортировкой по убыванию") + println("5) назад") + + var choosen = readln() + when(choosen){ + "1" -> user.allUsers(authorizeList) + "2" -> user.changeUserPassword(authorizeList) + "3" -> user.removeUser(authorizeList) + "4" -> user.allUsersSorted(authorizeList) + "5" -> return false + } + return true } fun hashPassword(password: String, salt: String): String { diff --git a/src/main/kotlin/model/AdminUser.kt b/src/main/kotlin/model/AdminUser.kt new file mode 100644 index 0000000..d8b1fc4 --- /dev/null +++ b/src/main/kotlin/model/AdminUser.kt @@ -0,0 +1,50 @@ +package model + +import AdminInterface +import hashPassword +import salt +import java.time.LocalDate + +class AdminUser( + userId: Int, + email: String, + login: String, + password: String, + lastAuthorizeDate: LocalDate, +) : User(userId, email, login, password, lastAuthorizeDate), AdminInterface{ + override fun allUsers(authorizeList: MutableList){ + for (user in authorizeList){ + println(user.login) + } + } + override fun changeUserPassword(authorizeList: MutableList){ + println("email for update") + val email = readln() + println("new password") + val password = readln() + + val user = authorizeList.find { it.email == email } + require(user != null){ + "no user with that email" + } + + user.password = hashPassword(password, salt) + } + override fun removeUser(authorizeList: MutableList){ + println("email for removing") + val email = readln() + + val removed = authorizeList.removeIf{it.email == email} + if (removed){ + println("removed") + } else{ + println("didn't") + } + } + override fun allUsersSorted(authorizeList: MutableList){ + authorizeList.sortByDescending { it.lastAuthorizeDate } + for(user in authorizeList){ + println(user.login + " auth at " + user.lastAuthorizeDate) + } + } +} diff --git a/src/main/kotlin/model/DefaultUser.kt b/src/main/kotlin/model/DefaultUser.kt new file mode 100644 index 0000000..3885797 --- /dev/null +++ b/src/main/kotlin/model/DefaultUser.kt @@ -0,0 +1,11 @@ +package model + +import java.time.LocalDate + +class DefaultUser( + userId: Int, + email: String, + login: String, + password: String, + lastAuthorizeDate: LocalDate, +) : User(userId, email, login, password, lastAuthorizeDate) \ No newline at end of file diff --git a/src/main/kotlin/model/ManagerUser.kt b/src/main/kotlin/model/ManagerUser.kt new file mode 100644 index 0000000..a18c24d --- /dev/null +++ b/src/main/kotlin/model/ManagerUser.kt @@ -0,0 +1,11 @@ +package model + +import java.time.LocalDate + +class ManagerUser( + userId: Int, + email: String, + login: String, + password: String, + lastAuthorizeDate: LocalDate, +) : User(userId, email, login, password, lastAuthorizeDate) \ No newline at end of file diff --git a/src/main/kotlin/model/Role.kt b/src/main/kotlin/model/Role.kt deleted file mode 100644 index a5e5315..0000000 --- a/src/main/kotlin/model/Role.kt +++ /dev/null @@ -1,6 +0,0 @@ -package model - -data class Role( - val roleId: Int, - val roleName: String -) diff --git a/src/main/kotlin/model/UserAuthorize.kt b/src/main/kotlin/model/User.kt similarity index 71% rename from src/main/kotlin/model/UserAuthorize.kt rename to src/main/kotlin/model/User.kt index 915cff3..ce61180 100644 --- a/src/main/kotlin/model/UserAuthorize.kt +++ b/src/main/kotlin/model/User.kt @@ -1,13 +1,11 @@ package model import java.time.LocalDate -import java.util.Date -data class UserAuthorize( +open class User( val userId: Int, var email: String, val login: String, var password: String, var lastAuthorizeDate: LocalDate, - val role: Role -) +) \ No newline at end of file