-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
AuthenticationActivityDao.kt
96 lines (82 loc) · 3.01 KB
/
AuthenticationActivityDao.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.gotson.komga.infrastructure.jooq
import org.gotson.komga.domain.model.AuthenticationActivity
import org.gotson.komga.domain.model.KomgaUser
import org.gotson.komga.domain.persistence.AuthenticationActivityRepository
import org.gotson.komga.jooq.Tables
import org.gotson.komga.jooq.tables.records.AuthenticationActivityRecord
import org.jooq.Condition
import org.jooq.DSLContext
import org.jooq.impl.DSL
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageImpl
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Component
import java.time.LocalDateTime
@Component
class AuthenticationActivityDao(
private val dsl: DSLContext
) : AuthenticationActivityRepository {
private val aa = Tables.AUTHENTICATION_ACTIVITY
private val sorts = mapOf(
"dateTime" to aa.DATE_TIME,
"email" to aa.EMAIL,
"success" to aa.SUCCESS,
"ip" to aa.IP,
"error" to aa.ERROR,
"userId" to aa.USER_ID,
"userAgent" to aa.USER_AGENT,
)
override fun findAll(pageable: Pageable): Page<AuthenticationActivity> {
val conditions: Condition = DSL.trueCondition()
return findAll(conditions, pageable)
}
override fun findAllByUser(user: KomgaUser, pageable: Pageable): Page<AuthenticationActivity> {
val conditions = aa.USER_ID.eq(user.id).or(aa.EMAIL.eq(user.email))
return findAll(conditions, pageable)
}
private fun findAll(conditions: Condition, pageable: Pageable): PageImpl<AuthenticationActivity> {
val count = dsl.fetchCount(aa)
val orderBy = pageable.sort.toOrderBy(sorts)
val items = dsl.selectFrom(aa)
.where(conditions)
.orderBy(orderBy)
.apply { if (pageable.isPaged) limit(pageable.pageSize).offset(pageable.offset) }
.fetchInto(aa)
.map { it.toDomain() }
val pageSort = if (orderBy.size > 1) pageable.sort else Sort.unsorted()
return PageImpl(
items,
if (pageable.isPaged) PageRequest.of(pageable.pageNumber, pageable.pageSize, pageSort)
else PageRequest.of(0, maxOf(count, 20), pageSort),
count.toLong(),
)
}
override fun insert(activity: AuthenticationActivity) {
dsl.insertInto(aa, aa.USER_ID, aa.EMAIL, aa.IP, aa.USER_AGENT, aa.SUCCESS, aa.ERROR)
.values(activity.userId, activity.email, activity.ip, activity.userAgent, activity.success, activity.error)
.execute()
}
override fun deleteByUser(user: KomgaUser) {
dsl.deleteFrom(aa)
.where(aa.USER_ID.eq(user.id))
.or(aa.EMAIL.eq(user.email))
.execute()
}
override fun deleteOlderThan(dateTime: LocalDateTime) {
dsl.deleteFrom(aa)
.where(aa.DATE_TIME.lt(dateTime))
.execute()
}
private fun AuthenticationActivityRecord.toDomain() =
AuthenticationActivity(
userId = userId,
email = email,
ip = ip,
userAgent = userAgent,
success = success,
error = error,
dateTime = dateTime.toCurrentTimeZone(),
)
}