Skip to content

Commit

Permalink
826: Modify index on email to be lowercased
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahsporck committed Mar 2, 2023
1 parent 26565aa commit 43eb3c0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.javatime.datetime
import org.jetbrains.exposed.sql.or
import org.jetbrains.exposed.sql.transactions.TransactionManager

object Administrators : IntIdTable() {
val email = varchar("email", 100).uniqueIndex()
val email = varchar("email", 100)
val projectId = reference("projectId", Projects)
val regionId = optReference("regionId", Regions)
val role = varchar("role", 32)
Expand All @@ -36,6 +37,11 @@ object Administrators : IntIdTable() {
}
}

fun createEmailIndexIfNotExists() {
val sql = "CREATE INDEX IF NOT EXISTS email_lower_idx ON ${Administrators.nameInDatabaseCase()} (lower(${Administrators.email.nameInDatabaseCase()}))"
TransactionManager.current().exec(sql)
}

class AdministratorEntity(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<AdministratorEntity>(Administrators)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ fun setupDatabase() {
SchemaUtils.create(
Administrators
)
createEmailIndexIfNotExists()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import app.ehrenamtskarte.backend.common.database.sortByKeys
import app.ehrenamtskarte.backend.projects.database.ProjectEntity
import app.ehrenamtskarte.backend.projects.database.Projects
import app.ehrenamtskarte.backend.regions.database.RegionEntity
import org.jetbrains.exposed.sql.LowerCase
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
Expand All @@ -24,11 +25,14 @@ object AdministratorsRepository {
fun findByIds(ids: List<Int>) =
AdministratorEntity.find { Administrators.id inList ids }.sortByKeys({ it.id.value }, ids)

fun emailAlreadyExists(email: String) =
!AdministratorEntity.find { LowerCase(Administrators.email) eq email.lowercase() }.empty()

fun findByAuthData(project: String, email: String, password: String): AdministratorEntity? {
val resultRow = (Administrators innerJoin Projects)
.slice(Administrators.columns)
.select(
(Projects.project eq project) and (Administrators.email eq email)
(Projects.project eq project) and (LowerCase(Administrators.email) eq email.lowercase())
)
.firstOrNull()
return resultRow?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app.ehrenamtskarte.backend.auth.webservice.schema

import app.ehrenamtskarte.backend.auth.database.AdministratorEntity
import app.ehrenamtskarte.backend.auth.database.Administrators
import app.ehrenamtskarte.backend.auth.database.repos.AdministratorsRepository
import app.ehrenamtskarte.backend.auth.service.Authorizer
import app.ehrenamtskarte.backend.auth.webservice.schema.types.Role
Expand Down Expand Up @@ -52,7 +51,7 @@ class ManageUsersMutationService {
throw UnauthorizedException()
}

if (!AdministratorEntity.find { Administrators.email eq email }.empty()) {
if (!AdministratorsRepository.emailAlreadyExists(email)) {
throw EmailAlreadyExistsException()
}

Expand Down Expand Up @@ -102,7 +101,7 @@ class ManageUsersMutationService {

if (
newEmail != existingAdmin.email &&
!AdministratorEntity.find { Administrators.email eq newEmail }.empty()
AdministratorsRepository.emailAlreadyExists(newEmail)
) {
throw EmailAlreadyExistsException()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import app.ehrenamtskarte.backend.mail.Mailer
import app.ehrenamtskarte.backend.projects.database.Projects
import com.expediagroup.graphql.generator.annotations.GraphQLDescription
import graphql.schema.DataFetchingEnvironment
import org.jetbrains.exposed.sql.LowerCase
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
Expand All @@ -24,7 +25,7 @@ class ResetPasswordMutationService {
val projectConfig = backendConfig.projects.first { it.id == project }
transaction {
val user = Administrators.innerJoin(Projects).slice(Administrators.columns)
.select((Projects.project eq project) and (Administrators.email eq email))
.select((Projects.project eq project) and (LowerCase(Administrators.email) eq email.lowercase()))
.single().let { AdministratorEntity.wrapRow(it) }

val key = AdministratorsRepository.setNewPasswordResetKey(user)
Expand Down Expand Up @@ -64,7 +65,7 @@ class ResetPasswordMutationService {
fun resetPassword(project: String, email: String, passwordResetKey: String, newPassword: String): Boolean {
transaction {
val user = Administrators.innerJoin(Projects).slice(Administrators.columns)
.select((Projects.project eq project) and (Administrators.email eq email))
.select((Projects.project eq project) and (LowerCase(Administrators.email) eq email.lowercase()))
.single().let { AdministratorEntity.wrapRow(it) }

if (user.passwordResetKeyExpiry!!.isBefore(LocalDateTime.now())) {
Expand Down

0 comments on commit 43eb3c0

Please sign in to comment.