Skip to content

Commit

Permalink
feat: Partial Migration of CN account system
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Jun 25, 2023
1 parent 544e677 commit 2e0b746
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
*/
package com.xpdustry.foundation.common.database

import com.xpdustry.foundation.common.database.model.AccountManager
import com.xpdustry.foundation.common.database.model.PunishmentManager
import com.xpdustry.foundation.common.database.model.UserManager

interface Database {
val users: UserManager
val punishments: PunishmentManager
val accounts: AccountManager
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
*/
package com.xpdustry.foundation.common.database

import org.bson.types.ObjectId
import java.time.Instant

interface Entity<I> {
val id: I
}

val Entity<ObjectId>.timestamp: Instant
get() = id.date.toInstant()
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Foundation, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.common.database.model

import com.xpdustry.foundation.common.database.Entity
import com.xpdustry.foundation.common.hash.HashWithParams
import org.bson.types.ObjectId

// This structure stores the users bound to an account.
// The HashWithParams is the hashed usid (NOTE: It is nullable because the previous database did not it)
typealias BoundUsers = MutableMap<MindustryUUID, HashWithParams?>

data class Account(
override val id: ObjectId,
var username: String,
var password: HashWithParams,
var rank: Rank = Rank.NEWBIE,
var steam: String? = null,
var discord: String? = null,
val users: BoundUsers = mutableMapOf(),
) : Entity<ObjectId> {
val verified: Boolean
get() = steam != null || discord != null

enum class Rank {
NEWBIE,
ACTIVE,
HYPER_ACTIVE,
CONTRIBUTOR,
OVERSEER,
MODERATOR,
ADMINISTRATOR,
OWNER,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Foundation, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.common.database.model

import com.xpdustry.foundation.common.database.EntityManager
import org.bson.types.ObjectId

interface AccountManager : EntityManager<ObjectId, Account>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.xpdustry.foundation.common.database.model

import com.xpdustry.foundation.common.database.Entity
import com.xpdustry.foundation.common.database.timestamp
import org.bson.types.ObjectId
import java.net.InetAddress
import java.time.Duration
Expand All @@ -31,10 +32,6 @@ data class Punishment(
var duration: Duration? = Duration.ofDays(1L),
var pardonned: Boolean = false,
) : Entity<ObjectId> {

val timestamp: Instant
get() = id.date.toInstant()

val expired: Boolean
get() = duration != null && (pardonned || timestamp.plus(duration).isBefore(Instant.now()))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ typealias MindustryUUID = String

data class User(
override val id: MindustryUUID,
var names: Set<String> = emptySet(),
var addresses: Set<InetAddress> = emptySet(),
val names: MutableSet<String> = mutableSetOf(),
val addresses: MutableSet<InetAddress> = mutableSetOf(),
var lastName: String? = null,
var lastAddress: InetAddress? = null,
var timesJoined: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.common.database.mongo.codec
package com.xpdustry.foundation.common.database.mongo

import org.bson.BsonReader
import org.bson.BsonWriter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.common.database.mongo.codec
package com.xpdustry.foundation.common.database.mongo

import com.google.common.net.InetAddresses
import org.bson.BsonReader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.common.database.mongo.convention
package com.xpdustry.foundation.common.database.mongo

import org.bson.codecs.pojo.ClassModelBuilder
import org.bson.codecs.pojo.Convention
Expand All @@ -27,7 +27,7 @@ import java.lang.reflect.Method

// NOTE: Extremely unsafe, but it works.
// TODO: Maybe use the faster alternative sun.reflect.ReflectionFactory
object UnsafeInstanciationConvention : Convention {
object InstanciationConvention : Convention {
override fun apply(classModelBuilder: ClassModelBuilder<*>) = apply0(classModelBuilder)

private fun <T : Any> apply0(classModelBuilder: ClassModelBuilder<T>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Foundation, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.common.database.mongo

import com.mongodb.reactivestreams.client.MongoCollection
import com.xpdustry.foundation.common.database.model.Account
import com.xpdustry.foundation.common.database.model.AccountManager
import org.bson.types.ObjectId

class MongoAccountManager(
collection: MongoCollection<Account>,
) : MongoEntityManager<Account, ObjectId>(collection), AccountManager
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ import com.xpdustry.foundation.common.application.FoundationListener
import com.xpdustry.foundation.common.application.FoundationMetadata
import com.xpdustry.foundation.common.config.FoundationConfig
import com.xpdustry.foundation.common.database.Database
import com.xpdustry.foundation.common.database.model.Account
import com.xpdustry.foundation.common.database.model.AccountManager
import com.xpdustry.foundation.common.database.model.Punishment
import com.xpdustry.foundation.common.database.model.PunishmentManager
import com.xpdustry.foundation.common.database.model.User
import com.xpdustry.foundation.common.database.model.UserManager
import com.xpdustry.foundation.common.database.mongo.codec.DurationCodec
import com.xpdustry.foundation.common.database.mongo.codec.InetAddressCodec
import com.xpdustry.foundation.common.database.mongo.convention.SnakeCaseConvention
import com.xpdustry.foundation.common.database.mongo.convention.UnsafeInstanciationConvention
import com.xpdustry.foundation.common.misc.toValueFlux
import org.bson.codecs.configuration.CodecRegistries
import org.bson.codecs.pojo.Conventions
Expand All @@ -49,6 +47,7 @@ class MongoDatabase @Inject constructor(private val config: FoundationConfig, pr
private lateinit var client: MongoClient
override lateinit var users: UserManager
override lateinit var punishments: PunishmentManager
override lateinit var accounts: AccountManager

override fun onFoundationInit() {
client = MongoClients.create(
Expand Down Expand Up @@ -89,7 +88,7 @@ class MongoDatabase @Inject constructor(private val config: FoundationConfig, pr
Conventions.DEFAULT_CONVENTIONS + listOf(
SnakeCaseConvention,
Conventions.SET_PRIVATE_FIELDS_CONVENTION,
UnsafeInstanciationConvention,
InstanciationConvention,
),
)
.build(),
Expand All @@ -111,6 +110,7 @@ class MongoDatabase @Inject constructor(private val config: FoundationConfig, pr
val database = client.getDatabase(config.mongo.database)
users = MongoUserManager(database.getCollection("users", User::class.java))
punishments = MongoPunishmentManager(database.getCollection("punishments", Punishment::class.java))
accounts = MongoAccountManager(database.getCollection("accounts", Account::class.java))
}

override fun onFoundationExit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import com.xpdustry.foundation.common.database.model.MindustryUUID
import com.xpdustry.foundation.common.database.model.Punishment
import com.xpdustry.foundation.common.database.model.PunishmentManager
import com.xpdustry.foundation.common.misc.toValueFlux
import jakarta.inject.Inject
import org.bson.types.ObjectId
import reactor.core.publisher.Flux
import java.net.InetAddress

class MongoPunishmentManager @Inject constructor(collection: MongoCollection<Punishment>) :
MongoEntityManager<Punishment, ObjectId>(collection), PunishmentManager {
class MongoPunishmentManager(
collection: MongoCollection<Punishment>,
) : MongoEntityManager<Punishment, ObjectId>(collection), PunishmentManager {

override fun findAllByTargetIp(target: InetAddress): Flux<Punishment> =
collection.find(Filters.eq("target_ip", target.hostAddress)).toValueFlux()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ package com.xpdustry.foundation.common.database.mongo
import com.mongodb.reactivestreams.client.MongoCollection
import com.xpdustry.foundation.common.database.model.User
import com.xpdustry.foundation.common.database.model.UserManager
import jakarta.inject.Inject

class MongoUserManager @Inject constructor(collection: MongoCollection<User>) :
MongoEntityManager<User, String>(collection), UserManager
class MongoUserManager(collection: MongoCollection<User>) : MongoEntityManager<User, String>(collection), UserManager
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.common.database.mongo.convention
package com.xpdustry.foundation.common.database.mongo

import org.bson.codecs.pojo.ClassModelBuilder
import org.bson.codecs.pojo.Convention
Expand All @@ -27,11 +27,11 @@ object SnakeCaseConvention : Convention {
it.writeName(it.writeName.camelToSnakeCase())
}
}
}

// https://www.baeldung.com/kotlin/convert-camel-case-snake-case
private fun String.camelToSnakeCase(): String {
return this.fold(StringBuilder()) { acc, c ->
acc.append(if (acc.isNotEmpty() && c.isUpperCase()) "_${c.lowercase()}" else c.lowercase())
}.toString()
}
// https://www.baeldung.com/kotlin/convert-camel-case-snake-case
private fun String.camelToSnakeCase(): String {
return fold(StringBuilder()) { builder, char ->
builder.append(if (builder.isNotEmpty() && char.isUpperCase()) "_${char.lowercase()}" else char.lowercase())
}.toString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Foundation, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.common.hash

// TODO: Write mongo codec for that
data class HashWithParams(val hash: Hash, val params: HashParams)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Foundation, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.mindustry.core.account

import com.google.inject.Inject
import com.xpdustry.foundation.common.application.FoundationListener
import com.xpdustry.foundation.common.database.Database
import com.xpdustry.foundation.mindustry.core.command.FoundationPluginCommandManager
import jakarta.inject.Named

class AccountCommand @Inject constructor(
private val database: Database,
@param:Named("client") private val clientCommandManager: FoundationPluginCommandManager,
) : FoundationListener {
override fun onFoundationInit() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Foundation, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.mindustry.core.account

import com.xpdustry.foundation.common.application.FoundationListener

class AccountService : FoundationListener {
override fun onFoundationInit() {
super.onFoundationInit()
}
}

0 comments on commit 2e0b746

Please sign in to comment.