diff --git a/src/main/kotlin/RepositoryImpl/AuthorizeRepositoryImpl.kt b/src/main/kotlin/RepositoryImpl/AuthorizeRepositoryImpl.kt new file mode 100644 index 0000000..f5ffac7 --- /dev/null +++ b/src/main/kotlin/RepositoryImpl/AuthorizeRepositoryImpl.kt @@ -0,0 +1,23 @@ +package RepositoryImpl + +import java.time.LocalDate +import authorizeList + +fun authorization(){ + println() + println() + println() + var check =true + while(check) { + println("Введите почту или номер телефона для авторизации") + val readUserEmailOrNumber = readln() + println("Введите пароль") + val readUserPassword = readln() + authorizeList.forEach { + if ((readUserEmailOrNumber == it.email || readUserEmailOrNumber == it.phone) && hashing(readUserPassword) == it.password) { + it.lastAuthorizeDate = LocalDate.now() + check=false + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/RepositoryImpl/PasswordRepositoryImpl.kt b/src/main/kotlin/RepositoryImpl/PasswordRepositoryImpl.kt new file mode 100644 index 0000000..84c31bf --- /dev/null +++ b/src/main/kotlin/RepositoryImpl/PasswordRepositoryImpl.kt @@ -0,0 +1,15 @@ +package RepositoryImpl + +import javax.crypto.SecretKey +import javax.crypto.SecretKeyFactory +import javax.crypto.spec.PBEKeySpec + +fun hashing(password : String): SecretKey? { + val salt = byteArrayOf(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f) + val iterations = 10000 + val keySize = 256 + val keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1") + val keySpec = PBEKeySpec(password.toCharArray(), salt, iterations, keySize) + val secretKey = keyFactory.generateSecret(keySpec) + return secretKey +} diff --git a/src/main/kotlin/RepositoryImpl/RegistrationRepositoryImpl.kt b/src/main/kotlin/RepositoryImpl/RegistrationRepositoryImpl.kt new file mode 100644 index 0000000..7375217 --- /dev/null +++ b/src/main/kotlin/RepositoryImpl/RegistrationRepositoryImpl.kt @@ -0,0 +1,68 @@ +package RepositoryImpl + +import java.time.LocalDate +import model.User +import RepositoryImpl.hashing + +fun registration() : User { + var userLogin = "" + while (userLogin?.length!! <4){ + print("Введите никнейм: ") + userLogin = readln() + if (userLogin.length<4){ + println("Никнейм должен быть больше 4 символов. Попробуйте снова") + } + } + + var userPassword = "" + var dig = 0 + var let = 0 + while (userPassword.length<8 || dig<1 || let<1){ + println("Пароль должен быть не менее 8 символов и должен содеражть буквы и цифры") + print("Введите пароль: ") + userPassword = readln() + dig=0 + let=0 + userPassword.forEach { + if(it.isDigit()){ + dig+=1 + } + else if (it.isLetter()){ + let+=1 + } + } + } + + print("Повторите пароль: ") + var secondUserPassword = "" + while (secondUserPassword != userPassword) { + secondUserPassword = readln() + checkNotNull(secondUserPassword) + if (secondUserPassword != userPassword) { + println("Пароли не совпадают. Повторите попытку") + } + } + var hashUserPassword= hashing(userPassword) + var userEmail="" + while (!("@" in userEmail)){ + print("Введите почту: ") + userEmail= readln() + } + + + var userPhone = "1234" + while(!((userPhone.startsWith("89") && userPhone.length == 11) || (userPhone.startsWith("+7") && userPhone.length == 12))) { + print("Введите номер телефона: ") + userPhone = readln() + } + + return User( + authorizeList.size + 1, + password = hashUserPassword, + login = userLogin, + lastAuthorizeDate = LocalDate.now(), + email = userEmail, + phone = userPhone, + role = role + ) +} \ No newline at end of file diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt index f37d14a..4736fd6 100644 --- a/src/main/kotlin/main.kt +++ b/src/main/kotlin/main.kt @@ -1,114 +1,103 @@ -import model.UserAuthorize -import model.Roles +import java.io.Console import java.security.SecureRandom import java.time.LocalDate -import java.time.LocalDateTime import javax.crypto.SecretKey import javax.crypto.SecretKeyFactory import javax.crypto.spec.PBEKeySpec import javax.swing.text.PasswordView +import RepositoryImpl.hashing +import RepositoryImpl.registration +import RepositoryImpl.authorization +import model.User -val authorizeList = mutableListOf() +val authorizeList = mutableListOf() fun main() { authorizeList.add(registration()) + authorizeList.add(User( + 2, "Alex", hashing("Alex1234"), "alex@gmail.com", "89310010543", + lastAuthorizeDate = LocalDate.now(), + )) + authorizeList.add(User( + 3, "MegaBalbes", hashing("fjghk123j"), "sdfgtyghk@gmail.com", "89314230543", + lastAuthorizeDate = LocalDate.now(), + )) + authorizeList.add(User( + 4, "IVBan", hashing("d,lfmk123454sdjhy"), "dsfgxvds@gmail.com", "+79310864309", + lastAuthorizeDate = LocalDate.now(), + )) + authorizeList.add(User( + 5, "Vlad", hashing("dfghfgd7878"), "rthtre6@gmail.com", "89311054819", + lastAuthorizeDate = LocalDate.now(), + )) + authorizeList.add(User( + 6, "XDel", hashing("sadfhjh1235"), "kjhndfjhv@gmail.com", "89319654812", + lastAuthorizeDate = LocalDate.now(), + )) + authorizeList.add(User( + 7, "Amega", hashing("123dfgyt"), "lljseioev@gmail.com", "89313218457", + lastAuthorizeDate = LocalDate.now(), + )) + authorizeList.add(User( + 8, "Orbita", hashing("98547kxdbfg"), "mlkswgjb@gmail.com", "89319784561", + lastAuthorizeDate = LocalDate.now(), + )) + + authorization() + var check = true + while(check){ + println("1. Войти в админ-панель") + println("2. Выход") + var answer= readln() + when(answer){ + "1" -> admin() + "2" -> check = false + } + + } } +fun admin(){ + while(true){ + println("1. Вывести всех пользователей") + println("2. Поменять пароль пользователя") + println("3. Удалить пользователя") + println("4. Вывести всех пользователей с сортировкой по дате") + var answer = readln() + when(answer){ + "1" -> authorizeList.forEach(){ + println("ID: " + it.userId) + println("Ник: " + it.login) + println("Пароль: " + it.password?.encoded) + println("Email: " + it.email) + println("Телефон: " + it.phone) + println("Role: " + it.role) + println("Последняя авторизация: " + it.lastAuthorizeDate) + } + "2" -> println() + "3" -> println() + "4" -> println() + } + } +} + + fun authorization(){ println() println() println() - println("Введите почту или номер телефона для авторизации") - val readUserEmailOrNumber = readln() - println("Введите пароль") - val readUserPassword = readln() - authorizeList.forEach{ - if ((readUserEmailOrNumber==it.email || readUserEmailOrNumber==it.phone) && hashing(readUserPassword)==it.password){ - it.lastAuthorizeDate= LocalDateTime.now() - return println(it.role.roleName +"\r\n"+it.lastAuthorizeDate) - } - } -} - -fun hashing(password : String): SecretKey? { - val salt = byteArrayOf(0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f) - val iterations = 10000 - val keySize = 256 - val keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1") - val keySpec = PBEKeySpec(password.toCharArray(), salt, iterations, keySize) - val secretKey = keyFactory.generateSecret(keySpec) - return secretKey -} - - - -fun registration() : UserAuthorize { - var userLogin = "" - while (userLogin?.length!! <4){ - print("Введите никнейм: ") - userLogin = readln() - if (userLogin.length<4){ - println("Никнейм должен быть больше 4 символов. Попробуйте снова") - } - } - - var userPassword = "" - var dig = 0 - var let = 0 - while (userPassword.length<8 && dig<1 && let<1){ - println("Пароль должен быть не менее 8 символов и должен содеражть буквы и цифры") - print("Введите пароль: ") - dig=0 - let=0 - userPassword = readln() - userPassword.forEach { - if(it.isDigit()){ - dig++ - } - else if (it.isLetter()){ - let++ + var check =true + while(check) { + println("Введите почту или номер телефона для авторизации") + val readUserEmailOrNumber = readln() + println("Введите пароль") + val readUserPassword = readln() + authorizeList.forEach { + if ((readUserEmailOrNumber == it.email || readUserEmailOrNumber == it.phone) && hashing(readUserPassword) == it.password) { + it.lastAuthorizeDate = LocalDate.now() + check=false + return println(it.role + "\r\n" + it.lastAuthorizeDate) } } } - userPassword.forEach { - require(it.isLetterOrDigit()) - require(userPassword.length >= 8) - } - - print("Повторите пароль: ") - var secondUserPassword = "" - while (secondUserPassword != userPassword) { - secondUserPassword = readln() - checkNotNull(secondUserPassword) - if (secondUserPassword != userPassword) { - println("Пароли не совпадают") - } - } - var hashUserPassword=hashing(userPassword) - - print("Введите почту: ") - val userEmail = readlnOrNull() - checkNotNull(userEmail) - require("@" in userEmail) - - print("Введите номер телефона: ") - var userPhone = readlnOrNull() - checkNotNull(userPhone) - require((userPhone.startsWith("89") && userPhone.length == 11) || (userPhone.startsWith("+7") && userPhone.length == 12)) - print("Вы админ? (Y, N): ") - var role = Roles(1, "администратор") - var adm = readln() - if (adm!="Y"){ - role = Roles(2, "пользователь") - } - - return UserAuthorize( - authorizeList.size + 1, - password = hashUserPassword, - login = userLogin, - lastAuthorizeDate = LocalDateTime.now(), - email = userEmail, - phone = userPhone, - role = role - ) -} +} \ No newline at end of file diff --git a/src/main/kotlin/model/AdminUser.kt b/src/main/kotlin/model/AdminUser.kt new file mode 100644 index 0000000..1e4cd40 --- /dev/null +++ b/src/main/kotlin/model/AdminUser.kt @@ -0,0 +1,15 @@ +package model + +import java.time.LocalDate +import javax.crypto.SecretKey + +class AdminUser( + userId: Int, + login: String, + password: SecretKey, + email: String, + phone: String, + lastAuthorizeDate: LocalDate + ) : User(userId, login, password, email, phone, lastAuthorizeDate){ + override val role: String = "администратор" +} diff --git a/src/main/kotlin/model/DefaultUser.kt b/src/main/kotlin/model/DefaultUser.kt new file mode 100644 index 0000000..ab142df --- /dev/null +++ b/src/main/kotlin/model/DefaultUser.kt @@ -0,0 +1,15 @@ +package model + +import java.time.LocalDate +import javax.crypto.SecretKey + +class DefaultUser( + userId: Int, + login: String, + password: SecretKey, + email: String, + phone: String, + lastAuthorizeDate: LocalDate +) : User(userId, login, password,email, phone, lastAuthorizeDate) { + override val role: String = "пользователь" +} diff --git a/src/main/kotlin/model/ManagerUser.kt b/src/main/kotlin/model/ManagerUser.kt new file mode 100644 index 0000000..575d75a --- /dev/null +++ b/src/main/kotlin/model/ManagerUser.kt @@ -0,0 +1,15 @@ +package model + +import java.time.LocalDate +import javax.crypto.SecretKey + +class ManagerUser( + userId: Int, + login: String, + password: SecretKey, + email: String, + phone: String, + lastAuthorizeDate: LocalDate +) : User(userId, login, password, email, phone, lastAuthorizeDate) { + override val role: String = "менеджер" +} diff --git a/src/main/kotlin/model/Roles.kt b/src/main/kotlin/model/Roles.kt deleted file mode 100644 index bc06efd..0000000 --- a/src/main/kotlin/model/Roles.kt +++ /dev/null @@ -1,6 +0,0 @@ -package model - -data class Roles( - val roleId : Int, - val roleName : String -) diff --git a/src/main/kotlin/model/User.kt b/src/main/kotlin/model/User.kt new file mode 100644 index 0000000..65cb7f5 --- /dev/null +++ b/src/main/kotlin/model/User.kt @@ -0,0 +1,13 @@ +package model + +import java.time.LocalDate +import javax.crypto.SecretKey + +open class User( + val userId: Int, + val login: String, + var password: SecretKey?, + var email: String, + var phone: String, + var lastAuthorizeDate: LocalDate +) { open val role: String = "пользователь"} \ No newline at end of file diff --git a/src/main/kotlin/model/UserAuthorize.kt b/src/main/kotlin/model/UserAuthorize.kt deleted file mode 100644 index 8173d3a..0000000 --- a/src/main/kotlin/model/UserAuthorize.kt +++ /dev/null @@ -1,15 +0,0 @@ -package model - -import java.time.LocalDate -import java.time.LocalDateTime -import javax.crypto.SecretKey - -data class UserAuthorize( - val userId : Int, - val login : String, - var password : SecretKey?, - var email : String, - var phone : String, - var role : Roles, - var lastAuthorizeDate : LocalDateTime -)