commit 58b3df11f01f8652dd76ad3b14877567ab1ebd5b Author: 1billy17 Date: Sat Jan 18 11:46:53 2025 +0300 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ddbf4c --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Kotlin ### +.kotlin + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..df543e3 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..8226e53 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/KotlinJavaRuntime.xml b/.idea/libraries/KotlinJavaRuntime.xml new file mode 100644 index 0000000..0c43400 --- /dev/null +++ b/.idea/libraries/KotlinJavaRuntime.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..31e1ebc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..06d5800 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/authorization_test.iml b/authorization_test.iml new file mode 100644 index 0000000..4eba30b --- /dev/null +++ b/authorization_test.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt new file mode 100644 index 0000000..aad1277 --- /dev/null +++ b/src/main/kotlin/Main.kt @@ -0,0 +1,162 @@ +import model.AdminUser +import model.ManagerUser +import model.DefaultUser +import model.User +import java.time.LocalDate +import javax.crypto.SecretKeyFactory +import javax.crypto.spec.PBEKeySpec +import java.util.Base64 + + +val authorizationList = mutableListOf() + + +private const val HASH_LENGTH = 256 +private const val ITERATIONS = 65536 +val salt = "sosalt" + + +fun main() { + while (true) { + println("1) Регаться тут") + println("2) Авторизироваться тут") + var choice = readln() + if (choice == "1") { + authorizationList.add(registration()) + authorizationList.forEach{ + println(it.userId) + println(it.login) + println(it.password) + println(it.email) + println(it.lastAuthorizeDate) + } + } + else { + println(authorization()) + } + } +} + + +fun hashPassword(password: String, salt: String): String { + val saltBytes = Base64.getDecoder().decode(salt) + val spec = PBEKeySpec(password.toCharArray(), saltBytes, ITERATIONS, HASH_LENGTH) + val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256") + val hash = factory.generateSecret(spec).encoded + return Base64.getEncoder().encodeToString(hash) +} + + +fun registration(): User{ + println("Почту пиши") + val user_Email = readlnOrNull() + checkNotNull(user_Email) + require(user_Email.contains("@")) { + "Добавь @" + } + + println("Пароль пиши") + val user_Password = readlnOrNull() + checkNotNull(user_Password) + require(user_Password.length >= 8 && + user_Password.any { it.isDigit() } && + user_Password.any { it.isLetter() && it in 'a'..'z' || it in 'A'..'Z' } + ) { + "Длинее делай или цифру/букву добавляй" + } + + println("Заново пароль пиши") + val user_Password1 = readlnOrNull() + checkNotNull(user_Password1) + require(user_Password1 == user_Password) { + "Ну ты и тупорылый" + } + + println("Логин пиши") + val user_Login = readlnOrNull() + checkNotNull(user_Login) + require(user_Login.length >= 4) { + "Длинее делай" + } + + + println("ТЫ КТО? 1) лох 2) админо 3) манагер") + val role = readlnOrNull() + checkNotNull(role) + + return when(role) { + "1" -> return DefaultUser( + userId = authorizationList.size + 1, + login = user_Login, + password = hashPassword(user_Password, salt), + email = user_Email, + lastAuthorizeDate = LocalDate.now() + ) + + "2" -> return AdminUser( + userId = authorizationList.size + 1, + login = user_Login, + password = hashPassword(user_Password, salt), + email = user_Email, + lastAuthorizeDate = LocalDate.now() + ) + + "3" -> return ManagerUser( + userId = authorizationList.size + 1, + login = user_Login, + password = hashPassword(user_Password, salt), + email = user_Email, + lastAuthorizeDate = LocalDate.now() + ) + + else -> { throw IllegalArgumentException("error") } + } +} + + +fun authorization(): String{ + var adminFlg = true + println("Почту пиши") + val user_Email = readlnOrNull() + checkNotNull(user_Email) + val user = authorizationList.find { it.email == user_Email } + require(user != null) { + "нет такого" + } + + println("Пароль пиши") + val user_Password = readlnOrNull() + checkNotNull(user_Password) + require(user.password == hashPassword(user_Password, salt)) { + "не то" + } + + if (user is AdminUser) { + while (adminFlg == true) { + adminFlg = adminFunctional(user) + } + } + + return "такой есть" +} + + +fun adminFunctional(user: AdminUser): Boolean { + println("1) Показать всех") + println("2) Показать всех сорт") + println("3) Поменять пароль") + println("4) Удалить чела") + println("5) Пока") + + var flg = readln() + when(flg) { + "1" -> user.allUsers(authorizationList) + "2" -> user.allUsersSort(authorizationList) + "3" -> user.updateUserPassword(authorizationList) + "4" -> user.removeUser(authorizationList) + "5" -> return false + } + + return true +} + diff --git a/src/main/kotlin/model/AdminUser.kt b/src/main/kotlin/model/AdminUser.kt new file mode 100644 index 0000000..9f57a3a --- /dev/null +++ b/src/main/kotlin/model/AdminUser.kt @@ -0,0 +1,51 @@ +package model + +import hashPassword +import salt +import java.time.LocalDate + +class AdminUser( + userId: Int, + login: String, + password: String, + email: String, + lastAuthorizeDate: LocalDate, +): User(userId, login, password, email, lastAuthorizeDate), IAdminUser { + + override fun allUsers(authorizationList: MutableList) { + for (user in authorizationList) { + println(user.login) + } + } + + override fun allUsersSort(authorizationList: MutableList) { + authorizationList.sortByDescending { it.lastAuthorizeDate } + for (user in authorizationList) { + println(user.login + " зашел в " + user.lastAuthorizeDate) + } + } + + override fun updateUserPassword(authorizationList: MutableList) { + println("Почту пиши для апдейта") + val email = readln() + println("Пароль пиши для апдейта") + val password = readln() + + val user = authorizationList.find { it.email == email } + require(user != null) { + "нет такого" + } + + user.password = hashPassword(password, salt) + } + + override fun removeUser(authorizationList: MutableList) { + println("Почту пиши для апдейта") + val email = readln() + + val removed = authorizationList.removeIf { it.email == email } + if (removed) { + println("удалили") + } else { println("не удалили") } + } +} \ No newline at end of file diff --git a/src/main/kotlin/model/DefaultUser.kt b/src/main/kotlin/model/DefaultUser.kt new file mode 100644 index 0000000..c4a9ae1 --- /dev/null +++ b/src/main/kotlin/model/DefaultUser.kt @@ -0,0 +1,11 @@ +package model + +import java.time.LocalDate + +class DefaultUser( + userId: Int, + login: String, + password: String, + email: String, + lastAuthorizeDate: LocalDate, +): User(userId, login, password, email, lastAuthorizeDate) \ No newline at end of file diff --git a/src/main/kotlin/model/IAdminUser.kt b/src/main/kotlin/model/IAdminUser.kt new file mode 100644 index 0000000..57856f8 --- /dev/null +++ b/src/main/kotlin/model/IAdminUser.kt @@ -0,0 +1,8 @@ +package model + +interface IAdminUser { + fun allUsers(authorizationList: MutableList) + fun allUsersSort(authorizationList: MutableList) + fun updateUserPassword(authorizationList: MutableList) + fun removeUser(authorizationList: MutableList) +} \ 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..981449a --- /dev/null +++ b/src/main/kotlin/model/ManagerUser.kt @@ -0,0 +1,11 @@ +package model + +import java.time.LocalDate + +class ManagerUser( + userId: Int, + login: String, + password: String, + email: String, + lastAuthorizeDate: LocalDate, +): User(userId, login, password, email, lastAuthorizeDate) \ No newline at end of file diff --git a/src/main/kotlin/model/User.kt b/src/main/kotlin/model/User.kt new file mode 100644 index 0000000..072f415 --- /dev/null +++ b/src/main/kotlin/model/User.kt @@ -0,0 +1,11 @@ +package model + +import java.time.LocalDate + +open class User( + val userId: Int, + val login: String, + var password: String, + var email: String, + var lastAuthorizeDate: LocalDate, +) \ No newline at end of file