Compare commits
2 Commits
36c83618ee
...
de9ddfd0bb
Author | SHA1 | Date | |
---|---|---|---|
de9ddfd0bb | |||
6a920f8507 |
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
8
src/main/kotlin/Interface/AdminService.kt
Normal file
8
src/main/kotlin/Interface/AdminService.kt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package Interface
|
||||||
|
|
||||||
|
import model.User
|
||||||
|
|
||||||
|
interface AdminService {
|
||||||
|
fun removeUserById(userId: Int): Boolean
|
||||||
|
fun getAllUsers(): List<User>
|
||||||
|
}
|
9
src/main/kotlin/Interface/AuthorizeService.kt
Normal file
9
src/main/kotlin/Interface/AuthorizeService.kt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package Interface
|
||||||
|
|
||||||
|
import model.User
|
||||||
|
|
||||||
|
/*
|
||||||
|
Interface AuthorizeService {
|
||||||
|
fun registration(user: User):Boolean
|
||||||
|
fun
|
||||||
|
}*/
|
6
src/main/kotlin/Interface/PasswordService.kt
Normal file
6
src/main/kotlin/Interface/PasswordService.kt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package Interface
|
||||||
|
|
||||||
|
interface PasswordService{
|
||||||
|
fun hashPassword(password:String, salt:ByteArray):String
|
||||||
|
fun matches(password:String, salt:ByteArray, hashedPassword:String):Boolean
|
||||||
|
}
|
25
src/main/kotlin/RepositoryImpl/AdminServiceImpl.kt
Normal file
25
src/main/kotlin/RepositoryImpl/AdminServiceImpl.kt
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package RepositoryImpl
|
||||||
|
|
||||||
|
import Interface.AdminService
|
||||||
|
import model.User
|
||||||
|
|
||||||
|
class AdminServiceImpl : AdminService {
|
||||||
|
private val users = mutableListOf<User>()
|
||||||
|
|
||||||
|
override fun removeUserById(userId: Int): Boolean {
|
||||||
|
val user = users.find { it.userId == userId }
|
||||||
|
return if (user != null) {
|
||||||
|
users.remove(user)
|
||||||
|
true }
|
||||||
|
else
|
||||||
|
{ false }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getAllUsers(): List<User> {
|
||||||
|
return users
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
11
src/main/kotlin/RepositoryImpl/AuthorizeServiceImpl.kt
Normal file
11
src/main/kotlin/RepositoryImpl/AuthorizeServiceImpl.kt
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
/*
|
||||||
|
package RepositoryImpl
|
||||||
|
|
||||||
|
import Interface.AuthorizeService
|
||||||
|
import model.User
|
||||||
|
|
||||||
|
class AuthorizeServiceImpl: AuthorizeService{
|
||||||
|
override fun registration(user: User): Boolean {
|
||||||
|
adminService.addUser(user
|
||||||
|
}
|
||||||
|
}*/
|
20
src/main/kotlin/RepositoryImpl/PasswordServiceImpl.kt
Normal file
20
src/main/kotlin/RepositoryImpl/PasswordServiceImpl.kt
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package RepositoryImpl
|
||||||
|
|
||||||
|
import Interface.PasswordService
|
||||||
|
import javax.crypto.SecretKeyFactory
|
||||||
|
import javax.crypto.spec.PBEKeySpec
|
||||||
|
|
||||||
|
class PasswordServiceImpl : PasswordService {
|
||||||
|
override fun hashPassword(password: String, salt: ByteArray): String {
|
||||||
|
val spec = PBEKeySpec(password.toCharArray(), salt, 10000, 256)
|
||||||
|
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
||||||
|
val hash = factory.generateSecret(spec).encoded
|
||||||
|
return salt.joinToString("") { "%02x".format(it) } + ":" + hash.joinToString("") { "%02x".format(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun matches(password: String, salt: ByteArray, hashedPassword: String): Boolean {
|
||||||
|
val generatedHash = hashPassword(password, salt)
|
||||||
|
return generatedHash == hashedPassword
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,73 +1,178 @@
|
|||||||
import model.UserAuthorize
|
|
||||||
import model.Roles
|
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
import java.util.zip.Checksum
|
import java.security.SecureRandom
|
||||||
|
import javax.crypto.SecretKeyFactory
|
||||||
|
import javax.crypto.spec.PBEKeySpec
|
||||||
|
|
||||||
|
/*
|
||||||
val authorizeList = mutableListOf<UserAuthorize>()
|
val authorizeList = mutableListOf<UserAuthorize>()
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
fun main(){
|
fun main(){
|
||||||
authorizeList.add(registration())
|
authorizeList.add(registration())
|
||||||
authorizeList.forEach{ println(it)}
|
val user = authorize()
|
||||||
|
if (user != null){
|
||||||
|
when(user.role){"администратор" -> adminPanel()
|
||||||
|
else -> println("уходи ты не админ")}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun authorize():UserAuthorize{
|
fun adminPanel() {
|
||||||
println("Введите почту или телефон")
|
println("Добро пожаловать в панель администратора!")
|
||||||
val phone = readlnOrNull()
|
var action: Int
|
||||||
val email = readlnOrNull()
|
do {
|
||||||
val checkPhoneEmail = readlnOrNull()
|
println("Выберите действие:")
|
||||||
checkNotNull(email)
|
println("1. Вывести всех пользователей")
|
||||||
checkNotNull(phone)
|
println("2. Поменять пароль пользователя")
|
||||||
checkNotNull(checkPhoneEmail)
|
println("3. Удалить пользователя")
|
||||||
require(checkPhoneEmail == phone || checkPhoneEmail == email)
|
println("4. Вывести пользователей по дате последнего входа (убывание)")
|
||||||
require("@" in email)
|
println("0. Выйти из панели администратора")
|
||||||
require("+" in phone && phone.length == 11)
|
|
||||||
|
action = readln().toInt()
|
||||||
|
|
||||||
|
when (action) {
|
||||||
|
1 -> showAllUsers()
|
||||||
|
2 -> changeUserPassword()
|
||||||
|
3 -> deleteUser()
|
||||||
|
4 -> showUsersSortedByDate()
|
||||||
|
0 -> println("Выход из панели администратора.")
|
||||||
|
else -> println("Неверный выбор. Попробуйте снова.")
|
||||||
|
}
|
||||||
|
} while (action != 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun showAllUsers() {
|
||||||
|
if (authorizeList.isEmpty()) {
|
||||||
|
println("Нет пользователей.")
|
||||||
|
} else {
|
||||||
|
println("Список пользователей:")
|
||||||
|
authorizeList.forEach { println(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun changeUserPassword() {
|
||||||
|
println("Введите id лошка которому надо поменять пароль")
|
||||||
|
val userId = readln().toInt()
|
||||||
|
|
||||||
|
|
||||||
|
val user = authorizeList.find { userId == it.userId }
|
||||||
|
|
||||||
|
if (user != null) {
|
||||||
|
|
||||||
|
println("Введите новый пароль:")
|
||||||
|
var newPass = readln()
|
||||||
|
|
||||||
|
|
||||||
|
check(newPass.length >= 8) { "Пароль должен быть не менее 8 символов." }
|
||||||
|
check(newPass.any { it.isDigit() && it.isLetter() }) { "Пароль должен содержать и буквы, и цифры." }
|
||||||
|
|
||||||
|
|
||||||
|
val newHashPassword = hashPassword(newPass)
|
||||||
|
|
||||||
|
|
||||||
|
user.password = newHashPassword
|
||||||
|
|
||||||
|
println("Пароль успешно изменен.")
|
||||||
|
} else {
|
||||||
|
println("Пользователь не найден.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun deleteUser() {
|
||||||
|
println("Введите id пользователя, которого нужно удалить:")
|
||||||
|
val userId = readln().toInt()
|
||||||
|
|
||||||
|
// Находим пользователя по userId
|
||||||
|
val user = authorizeList.find { it.userId == userId }
|
||||||
|
|
||||||
|
if (user != null) {
|
||||||
|
// Удаляем пользователя из списка
|
||||||
|
authorizeList.remove(user)
|
||||||
|
println("Пользователь с id $userId успешно удален.")
|
||||||
|
} else {
|
||||||
|
println("Пользователь с id $userId не найден.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun showUsersSortedByDate(){
|
||||||
|
val sorted = authorizeList.sortedByDescending { it.lastAuthorizeDate }
|
||||||
|
println(sorted)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun authorize(): UserAuthorize? {
|
||||||
|
println("Введите телефон или почту для авторизации")
|
||||||
|
val emailOrPhone = readln()
|
||||||
|
println("Введите пароль для авторизации ")
|
||||||
|
val pass = readln()
|
||||||
|
|
||||||
|
val hashedPassword = hashPassword(pass)
|
||||||
|
|
||||||
|
authorizeList.forEach {
|
||||||
|
|
||||||
|
if ((emailOrPhone == it.phone || emailOrPhone == it.email) && hashedPassword == it.password) {
|
||||||
|
println(it.role)
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Пользователь не найден")
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fun hashPassword(password: String): String {
|
||||||
|
val salt = ByteArray(16)
|
||||||
|
SecureRandom().nextBytes(salt) // Генерация случайной соли
|
||||||
|
|
||||||
|
val spec = PBEKeySpec(password.toCharArray(), salt, 10000, 256)
|
||||||
|
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
||||||
|
val hash = factory.generateSecret(spec).encoded
|
||||||
|
|
||||||
|
return salt.joinToString("") { "%02x".format(it) } + ":" + hash.joinToString("") { "%02x".format(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun registration(): UserAuthorize {
|
fun registration(): UserAuthorize {
|
||||||
println("Введите логин")
|
println("Введите логин:")
|
||||||
val userLogin = readlnOrNull()
|
val userLogin = readln()
|
||||||
checkNotNull(userLogin)
|
check(userLogin.length >= 4) { "Имя пользователя должно быть не менее 4 символов." }
|
||||||
require(userLogin.length >= 4)
|
|
||||||
|
|
||||||
println("Введите пароль")
|
println("Введите пароль:")
|
||||||
val password = readlnOrNull()
|
var password = readln()
|
||||||
checkNotNull(password)
|
println("Введите пароль повторно:")
|
||||||
require(password.length>=8)
|
var passwordAgain = readln()
|
||||||
println("Введите пароль повторно")
|
check(password == passwordAgain) { "Пароли не совпадают." }
|
||||||
val passwordAgain = password
|
|
||||||
require(password.length>=8 && password == passwordAgain)
|
|
||||||
password.forEach { it.isLetterOrDigit() }
|
|
||||||
|
|
||||||
while (passwordAgain != password){
|
check(password.length >= 8) { "Пароль должен быть не менее 8 символов." }
|
||||||
|
check(password.any { it.isDigit() || it.isLetter() }) { "Пароль должен содержать и буквы, и цифры." }
|
||||||
|
|
||||||
println("Введите пароль повторно")
|
val hashedPassword = hashPassword(password)
|
||||||
val passwordAgain = password
|
|
||||||
require(password.length>=8 && password == passwordAgain)
|
|
||||||
password.forEach { it.isLetterOrDigit() }
|
|
||||||
|
|
||||||
}
|
println("Введите email:")
|
||||||
|
val email = readln()
|
||||||
|
check("@" in email) { "Email должен содержать символ '@'." }
|
||||||
|
|
||||||
println("Введите email")
|
println("Введите телефон:")
|
||||||
val email = readlnOrNull()
|
val phone = readln()
|
||||||
checkNotNull(email)
|
check(phone.length == 12 && phone.startsWith("+")) { "Телефон должен начинаться с '+' и содержать 11 символов." }
|
||||||
require("@" in email)
|
|
||||||
|
|
||||||
println("Введите телефон")
|
|
||||||
val phone = readlnOrNull()
|
|
||||||
checkNotNull(phone)
|
|
||||||
require("+" in phone && phone.length == 11)
|
|
||||||
|
|
||||||
|
|
||||||
|
val newUser = UserAuthorize(
|
||||||
|
userId = authorizeList.size + 1,
|
||||||
|
|
||||||
|
|
||||||
return UserAuthorize(authorizeList.size +1,
|
|
||||||
password = password,
|
|
||||||
login = userLogin,
|
login = userLogin,
|
||||||
|
password = hashedPassword,
|
||||||
lastAuthorizeDate = LocalDate.now(),
|
lastAuthorizeDate = LocalDate.now(),
|
||||||
email = email,
|
email = email,
|
||||||
phone = phone)
|
phone = phone,
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
authorizeList.add(newUser)
|
||||||
|
|
||||||
|
println("Пользователь зарегистрирован успешно!")
|
||||||
|
|
||||||
|
return newUser
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
14
src/main/kotlin/model/AdminUser.kt
Normal file
14
src/main/kotlin/model/AdminUser.kt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import java.time.LocalDate
|
||||||
|
|
||||||
|
class AdminUser(
|
||||||
|
userId: Int,
|
||||||
|
login: String,
|
||||||
|
password: String,
|
||||||
|
lastAuthorizeDate: LocalDate,
|
||||||
|
email: String,
|
||||||
|
phone: String
|
||||||
|
) : User(userId, login, password, lastAuthorizeDate, email, phone) {
|
||||||
|
override val role: String = "администратор"
|
||||||
|
}
|
14
src/main/kotlin/model/DefaultUser.kt
Normal file
14
src/main/kotlin/model/DefaultUser.kt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import java.time.LocalDate
|
||||||
|
|
||||||
|
class DefaultUser(
|
||||||
|
userId: Int,
|
||||||
|
login: String,
|
||||||
|
password: String,
|
||||||
|
lastAuthorizeDate: LocalDate,
|
||||||
|
email: String,
|
||||||
|
phone: String
|
||||||
|
) : User(userId, login, password, lastAuthorizeDate, email, phone) {
|
||||||
|
override val role: String = "пользователь"
|
||||||
|
}
|
14
src/main/kotlin/model/ManagerUser.kt
Normal file
14
src/main/kotlin/model/ManagerUser.kt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import java.time.LocalDate
|
||||||
|
|
||||||
|
class ManagerUser(
|
||||||
|
userId: Int,
|
||||||
|
login: String,
|
||||||
|
password: String,
|
||||||
|
lastAuthorizeDate: LocalDate,
|
||||||
|
email: String,
|
||||||
|
phone: String
|
||||||
|
) : User(userId, login, password, lastAuthorizeDate, email, phone) {
|
||||||
|
override val role: String = "менеджер"
|
||||||
|
}
|
@ -1,6 +0,0 @@
|
|||||||
package model
|
|
||||||
|
|
||||||
data class Roles (
|
|
||||||
val roleId: Int,
|
|
||||||
val roleName: String
|
|
||||||
)
|
|
@ -1,14 +1,16 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
|
|
||||||
data class UserAuthorize(
|
open class User(
|
||||||
val userId: Int,
|
val userId: Int,
|
||||||
val login: String,
|
val login: String,
|
||||||
var password: String,
|
var password: String,
|
||||||
var lastAuthorizeDate: LocalDate,
|
var lastAuthorizeDate: LocalDate,
|
||||||
var email: String,
|
var email: String,
|
||||||
var phone: String
|
var phone: String
|
||||||
|
) { open val role: String = "пользователь" }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
)
|
|
Loading…
Reference in New Issue
Block a user