AdminInterface

This commit is contained in:
Your Name 2025-01-16 07:11:17 +03:00
parent a61fc7b665
commit 92c45f1165
7 changed files with 145 additions and 27 deletions

View File

@ -0,0 +1,8 @@
import model.User
interface AdminInterface {
fun allUsers(authorizeList: MutableList<User>)
fun changeUserPassword(authorizeList: MutableList<User>)
fun removeUser(authorizeList: MutableList<User>)
fun allUsersSorted(authorizeList: MutableList<User>)
}

View File

@ -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<UserAuthorize>()
val authorizeList = mutableListOf<User>()
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 {

View File

@ -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<User>){
for (user in authorizeList){
println(user.login)
}
}
override fun changeUserPassword(authorizeList: MutableList<User>){
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<User>){
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<User>){
authorizeList.sortByDescending { it.lastAuthorizeDate }
for(user in authorizeList){
println(user.login + " auth at " + user.lastAuthorizeDate)
}
}
}

View File

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

View File

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

View File

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

View File

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