This commit is contained in:
adm 2025-01-14 15:43:19 +03:00
parent 5adc4b47be
commit 2f1714eeaa
10 changed files with 248 additions and 116 deletions

View File

@ -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
}
}
}
}

View File

@ -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
}

View File

@ -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
)
}

View File

@ -1,114 +1,103 @@
import model.UserAuthorize import java.io.Console
import model.Roles
import java.security.SecureRandom import java.security.SecureRandom
import java.time.LocalDate import java.time.LocalDate
import java.time.LocalDateTime
import javax.crypto.SecretKey import javax.crypto.SecretKey
import javax.crypto.SecretKeyFactory import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec import javax.crypto.spec.PBEKeySpec
import javax.swing.text.PasswordView import javax.swing.text.PasswordView
import RepositoryImpl.hashing
import RepositoryImpl.registration
import RepositoryImpl.authorization
import model.User
val authorizeList = mutableListOf<UserAuthorize>() val authorizeList = mutableListOf<User>()
fun main() { fun main() {
authorizeList.add(registration()) 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() 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(){ fun authorization(){
println() println()
println() println()
println() println()
println("Введите почту или номер телефона для авторизации") var check =true
val readUserEmailOrNumber = readln() while(check) {
println("Введите пароль") println("Введите почту или номер телефона для авторизации")
val readUserPassword = readln() val readUserEmailOrNumber = readln()
authorizeList.forEach{ println("Введите пароль")
if ((readUserEmailOrNumber==it.email || readUserEmailOrNumber==it.phone) && hashing(readUserPassword)==it.password){ val readUserPassword = readln()
it.lastAuthorizeDate= LocalDateTime.now() authorizeList.forEach {
return println(it.role.roleName +"\r\n"+it.lastAuthorizeDate) 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)
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++
} }
} }
} }
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
)
}

View File

@ -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 = "администратор"
}

View File

@ -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 = "пользователь"
}

View File

@ -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 = "менеджер"
}

View File

@ -1,6 +0,0 @@
package model
data class Roles(
val roleId : Int,
val roleName : String
)

View File

@ -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 = "пользователь"}

View File

@ -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
)