From dc44b6acc09ae6ba54ed3a67fdbf1163d39b3e6c Mon Sep 17 00:00:00 2001 From: KP9lKk Date: Tue, 14 Jan 2025 15:42:47 +0300 Subject: [PATCH] sample for any class --- app/build.gradle.kts | 2 + app/src/main/kotlin/datasource.kt | 26 ++++++++ app/src/main/kotlin/main.kt | 60 +++++++------------ app/src/main/kotlin/model/AdminUser.kt | 9 +++ app/src/main/kotlin/model/DefaultUser.kt | 10 ++++ app/src/main/kotlin/model/ManagerUser.kt | 9 +++ app/src/main/kotlin/model/User.kt | 8 +++ app/src/main/kotlin/model/UserAuthorize.kt | 10 ---- .../main/kotlin/service/AdminServiceImpl.kt | 3 + 9 files changed, 87 insertions(+), 50 deletions(-) create mode 100644 app/src/main/kotlin/datasource.kt create mode 100644 app/src/main/kotlin/model/AdminUser.kt create mode 100644 app/src/main/kotlin/model/DefaultUser.kt create mode 100644 app/src/main/kotlin/model/ManagerUser.kt create mode 100644 app/src/main/kotlin/model/User.kt delete mode 100644 app/src/main/kotlin/model/UserAuthorize.kt create mode 100644 app/src/main/kotlin/service/AdminServiceImpl.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 00a91aa..c057864 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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 { diff --git a/app/src/main/kotlin/datasource.kt b/app/src/main/kotlin/datasource.kt new file mode 100644 index 0000000..3f50cd2 --- /dev/null +++ b/app/src/main/kotlin/datasource.kt @@ -0,0 +1,26 @@ +import io.github.serpro69.kfaker.Faker +import model.* +import java.util.* +val faker = Faker() + +fun generateUsers(count: Int):List { + val userList = mutableListOf() + 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 +} diff --git a/app/src/main/kotlin/main.kt b/app/src/main/kotlin/main.kt index e0bba3a..6b1fe2a 100644 --- a/app/src/main/kotlin/main.kt +++ b/app/src/main/kotlin/main.kt @@ -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() fun main(){ - val secureRandom = SecureRandom() - val salt = byteArrayOf() - secureRandom.nextBytes(salt) -} - -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() -} -fun matches(password: String, passwordHashed: String, salt: ByteArray): Boolean{ - return hashedPassword(password, salt).contentEquals(passwordHashed) -} -fun registration(): UserAuthorize { - val userLogin = readlnOrNull() - checkNotNull(userLogin) - require(userLogin.length >= 4){ - "Никнейм должен быть больше 4 символов" + val users = generateUsers(10) + users.forEach { + println(it.password) } - userLogin.forEach { - require(it.isLetterOrDigit()) - require("@" in userLogin) - require(userLogin.contains("@")) + val randomUserIndex = Random.nextInt(0, users.size) + val findUser = users[randomUserIndex] + println(findUser::class.java) + when(findUser){ + is AdminUser -> { + println("adm") + } + is ManagerUser -> { + println("manager") + } + is DefaultUser ->{ + println("default") + } } - return UserAuthorize( - authorizeList.size + 1, - password = "123", - login = userLogin, - lastAuthorizeDate = LocalDate.now() - ) } \ No newline at end of file diff --git a/app/src/main/kotlin/model/AdminUser.kt b/app/src/main/kotlin/model/AdminUser.kt new file mode 100644 index 0000000..f221faa --- /dev/null +++ b/app/src/main/kotlin/model/AdminUser.kt @@ -0,0 +1,9 @@ +package model + +import java.util.UUID + +class AdminUser( + userUUID: UUID, + userLogin: String, + userPassword: String, +):User(userUUID, userLogin, userPassword) \ No newline at end of file diff --git a/app/src/main/kotlin/model/DefaultUser.kt b/app/src/main/kotlin/model/DefaultUser.kt new file mode 100644 index 0000000..e094c86 --- /dev/null +++ b/app/src/main/kotlin/model/DefaultUser.kt @@ -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) \ No newline at end of file diff --git a/app/src/main/kotlin/model/ManagerUser.kt b/app/src/main/kotlin/model/ManagerUser.kt new file mode 100644 index 0000000..403cd20 --- /dev/null +++ b/app/src/main/kotlin/model/ManagerUser.kt @@ -0,0 +1,9 @@ +package model + +import java.util.UUID + +class ManagerUser( + userUUID: UUID, + userLogin: String, + userPassword: String, +):User(userUUID, userLogin, userPassword) \ No newline at end of file diff --git a/app/src/main/kotlin/model/User.kt b/app/src/main/kotlin/model/User.kt new file mode 100644 index 0000000..3c549bf --- /dev/null +++ b/app/src/main/kotlin/model/User.kt @@ -0,0 +1,8 @@ +package model + +import java.util.UUID + +open class User( + val userUUID: UUID, + var login: String, + var password: String, ) \ No newline at end of file diff --git a/app/src/main/kotlin/model/UserAuthorize.kt b/app/src/main/kotlin/model/UserAuthorize.kt deleted file mode 100644 index 2bb583c..0000000 --- a/app/src/main/kotlin/model/UserAuthorize.kt +++ /dev/null @@ -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 -) diff --git a/app/src/main/kotlin/service/AdminServiceImpl.kt b/app/src/main/kotlin/service/AdminServiceImpl.kt new file mode 100644 index 0000000..864ba73 --- /dev/null +++ b/app/src/main/kotlin/service/AdminServiceImpl.kt @@ -0,0 +1,3 @@ +package service + +class AdminServiceImpl \ No newline at end of file