sample for any class
This commit is contained in:
parent
3a05eebec3
commit
dc44b6acc0
@ -10,6 +10,8 @@ plugins {
|
||||
dependencies {
|
||||
// Project "app" depends on project "utils". (Project paths are separated with ":", so ":utils" refers to the top-level "utils" project.)
|
||||
implementation(project(":utils"))
|
||||
// https://mvnrepository.com/artifact/io.github.serpro69/kotlin-faker
|
||||
implementation("io.github.serpro69:kotlin-faker:2.0.0-rc.7")
|
||||
}
|
||||
|
||||
application {
|
||||
|
26
app/src/main/kotlin/datasource.kt
Normal file
26
app/src/main/kotlin/datasource.kt
Normal file
@ -0,0 +1,26 @@
|
||||
import io.github.serpro69.kfaker.Faker
|
||||
import model.*
|
||||
import java.util.*
|
||||
val faker = Faker()
|
||||
|
||||
fun generateUsers(count: Int):List<User> {
|
||||
val userList = mutableListOf<User>()
|
||||
repeat(count){
|
||||
userList.add(AdminUser(
|
||||
UUID.randomUUID(),
|
||||
faker.name.neutralFirstName(),
|
||||
faker.string.numerify("555")
|
||||
))
|
||||
userList.add(ManagerUser(
|
||||
UUID.randomUUID(),
|
||||
faker.phoneNumber.phoneNumber(),
|
||||
faker.string.numerify("1010")
|
||||
))
|
||||
userList.add(DefaultUser(
|
||||
UUID.randomUUID(),
|
||||
faker.name.neutralFirstName(),
|
||||
faker.string.numerify("555")
|
||||
))
|
||||
}
|
||||
return userList
|
||||
}
|
@ -1,45 +1,25 @@
|
||||
import model.UserAuthorize
|
||||
import java.security.KeyStore.SecretKeyEntry
|
||||
import java.security.SecureRandom
|
||||
import java.time.LocalDate
|
||||
import javax.crypto.SecretKeyFactory
|
||||
import javax.crypto.spec.PBEKeySpec
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
import model.AdminUser
|
||||
import model.DefaultUser
|
||||
import model.ManagerUser
|
||||
import kotlin.random.Random
|
||||
|
||||
val authorizeList = mutableListOf<UserAuthorize>()
|
||||
fun main(){
|
||||
val secureRandom = SecureRandom()
|
||||
val salt = byteArrayOf()
|
||||
secureRandom.nextBytes(salt)
|
||||
val users = generateUsers(10)
|
||||
users.forEach {
|
||||
println(it.password)
|
||||
}
|
||||
|
||||
fun hashedPassword(password: String, salt: ByteArray): String{
|
||||
val pbeKeySpec = PBEKeySpec(
|
||||
password.toCharArray(),
|
||||
salt,
|
||||
65536,
|
||||
128)
|
||||
val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1")
|
||||
return factory.generateSecret(pbeKeySpec).encoded.toString()
|
||||
val randomUserIndex = Random.nextInt(0, users.size)
|
||||
val findUser = users[randomUserIndex]
|
||||
println(findUser::class.java)
|
||||
when(findUser){
|
||||
is AdminUser -> {
|
||||
println("adm")
|
||||
}
|
||||
fun matches(password: String, passwordHashed: String, salt: ByteArray): Boolean{
|
||||
return hashedPassword(password, salt).contentEquals(passwordHashed)
|
||||
is ManagerUser -> {
|
||||
println("manager")
|
||||
}
|
||||
fun registration(): UserAuthorize {
|
||||
val userLogin = readlnOrNull()
|
||||
checkNotNull(userLogin)
|
||||
require(userLogin.length >= 4){
|
||||
"Никнейм должен быть больше 4 символов"
|
||||
is DefaultUser ->{
|
||||
println("default")
|
||||
}
|
||||
userLogin.forEach {
|
||||
require(it.isLetterOrDigit())
|
||||
require("@" in userLogin)
|
||||
require(userLogin.contains("@"))
|
||||
}
|
||||
return UserAuthorize(
|
||||
authorizeList.size + 1,
|
||||
password = "123",
|
||||
login = userLogin,
|
||||
lastAuthorizeDate = LocalDate.now()
|
||||
)
|
||||
}
|
9
app/src/main/kotlin/model/AdminUser.kt
Normal file
9
app/src/main/kotlin/model/AdminUser.kt
Normal file
@ -0,0 +1,9 @@
|
||||
package model
|
||||
|
||||
import java.util.UUID
|
||||
|
||||
class AdminUser(
|
||||
userUUID: UUID,
|
||||
userLogin: String,
|
||||
userPassword: String,
|
||||
):User(userUUID, userLogin, userPassword)
|
10
app/src/main/kotlin/model/DefaultUser.kt
Normal file
10
app/src/main/kotlin/model/DefaultUser.kt
Normal file
@ -0,0 +1,10 @@
|
||||
package model
|
||||
|
||||
import io.github.serpro69.kfaker.Faker
|
||||
import java.util.UUID
|
||||
|
||||
class DefaultUser(
|
||||
userUUID: UUID,
|
||||
userLogin: String,
|
||||
userPassword: String,
|
||||
):User(userUUID, userLogin, userPassword)
|
9
app/src/main/kotlin/model/ManagerUser.kt
Normal file
9
app/src/main/kotlin/model/ManagerUser.kt
Normal file
@ -0,0 +1,9 @@
|
||||
package model
|
||||
|
||||
import java.util.UUID
|
||||
|
||||
class ManagerUser(
|
||||
userUUID: UUID,
|
||||
userLogin: String,
|
||||
userPassword: String,
|
||||
):User(userUUID, userLogin, userPassword)
|
8
app/src/main/kotlin/model/User.kt
Normal file
8
app/src/main/kotlin/model/User.kt
Normal file
@ -0,0 +1,8 @@
|
||||
package model
|
||||
|
||||
import java.util.UUID
|
||||
|
||||
open class User(
|
||||
val userUUID: UUID,
|
||||
var login: String,
|
||||
var password: String, )
|
@ -1,10 +0,0 @@
|
||||
package model
|
||||
|
||||
import java.time.LocalDate
|
||||
|
||||
data class UserAuthorize(
|
||||
val userId: Int,
|
||||
val login: String,
|
||||
var password: String,
|
||||
var lastAuthorizeDate: LocalDate
|
||||
)
|
3
app/src/main/kotlin/service/AdminServiceImpl.kt
Normal file
3
app/src/main/kotlin/service/AdminServiceImpl.kt
Normal file
@ -0,0 +1,3 @@
|
||||
package service
|
||||
|
||||
class AdminServiceImpl
|
Loading…
Reference in New Issue
Block a user