-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
24 changed files
with
1,625 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
botalka/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/monitoring/MicrometerCount.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
package ru.vityaman.lms.botalka.app.spring.monitoring | ||
|
||
import io.micrometer.core.instrument.Counter | ||
import io.micrometer.core.instrument.Meter | ||
import ru.vityaman.lms.botalka.core.monitoring.Count | ||
import ru.vityaman.lms.botalka.core.monitoring.StatusCount | ||
|
||
class MicrometerCount( | ||
private val origin: Counter, | ||
) : Count { | ||
override fun add(amount: Int) = | ||
origin.increment(amount.toDouble()) | ||
} | ||
|
||
fun status(provider: Meter.MeterProvider<Counter>) = StatusCount( | ||
successes = MicrometerCount(provider.withTag("status", "success")), | ||
failures = MicrometerCount(provider.withTag("status", "failure")), | ||
) |
53 changes: 52 additions & 1 deletion
53
botalka/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/security/SpringAuthority.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,59 @@ | ||
package ru.vityaman.lms.botalka.app.spring.security | ||
|
||
import org.springframework.stereotype.Component | ||
import ru.vityaman.lms.botalka.app.spring.monitoring.MetricsFactory | ||
import ru.vityaman.lms.botalka.app.spring.monitoring.MicrometerCount | ||
import ru.vityaman.lms.botalka.app.spring.monitoring.status | ||
import ru.vityaman.lms.botalka.commons.ExhaustiveMap | ||
import ru.vityaman.lms.botalka.core.model.User | ||
import ru.vityaman.lms.botalka.core.security.persmission.Authority | ||
import ru.vityaman.lms.botalka.core.security.persmission.BasicAuthority | ||
import ru.vityaman.lms.botalka.core.security.persmission.MeteredAuthority | ||
import ru.vityaman.lms.botalka.core.security.persmission.Permission.Kind.GetEvents | ||
import ru.vityaman.lms.botalka.core.security.persmission.Permission.Kind.PostHomework | ||
import ru.vityaman.lms.botalka.core.security.persmission.Permission.Kind.ResolvePromotion | ||
import ru.vityaman.lms.botalka.core.security.persmission.Permission.Kind.SendFeedback | ||
import ru.vityaman.lms.botalka.core.security.persmission.Permission.Kind.SendSubmission | ||
|
||
@Component | ||
class SpringAuthority : Authority by BasicAuthority() | ||
class SpringAuthority( | ||
metrics: MetricsFactory, | ||
) : Authority by | ||
MeteredAuthority( | ||
run { | ||
MeteredAuthority.Metrics( | ||
role = { kind: String -> | ||
MicrometerCount( | ||
metrics | ||
.counter("lms_authorization_user_role") | ||
.withTag("kind", kind), | ||
) | ||
}.let { | ||
ExhaustiveMap.from { | ||
when (it) { | ||
User.Role.ADMIN -> it("admin") | ||
User.Role.TEACHER -> it("teacher") | ||
User.Role.STUDENT -> it("student") | ||
} | ||
} | ||
}, | ||
permission = { kind: String -> | ||
metrics.counter( | ||
"lms_authorization_permission", | ||
"kind" to kind, | ||
).let { cnt -> status(cnt) } | ||
}.let { | ||
ExhaustiveMap.from { | ||
when (it) { | ||
ResolvePromotion -> it("resolve-promotion") | ||
GetEvents -> it("get-events") | ||
SendFeedback -> it("send-feedback") | ||
SendSubmission -> it("send-submission") | ||
PostHomework -> it("post-homework") | ||
} | ||
} | ||
}, | ||
) | ||
}, | ||
BasicAuthority(), | ||
) |
35 changes: 35 additions & 0 deletions
35
botalka/src/main/kotlin/ru/vityaman/lms/botalka/commons/ExhaustiveMap.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ru.vityaman.lms.botalka.commons | ||
|
||
import java.util.EnumMap | ||
import kotlin.enums.enumEntries | ||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
interface ExhaustiveMap<K, V> { | ||
operator fun get(key: K): V | ||
|
||
companion object { | ||
inline fun <reified K : Enum<K>, V> from( | ||
vararg pairs: Pair<K, V>, | ||
): ExhaustiveMap<K, V> = object : ExhaustiveMap<K, V> { | ||
private val origin = pairs | ||
.associateTo(EnumMap(K::class.java)) { (l, r) -> l to r } | ||
|
||
init { | ||
val keys = pairs.map { it.first } | ||
require(keys.distinct().size == keys.size) | ||
require(enumEntries<K>().size == keys.size) | ||
} | ||
|
||
override operator fun get(key: K): V = origin[key]!! | ||
} | ||
|
||
inline fun <reified K : Enum<K>, V> from( | ||
mapping: (K) -> V, | ||
): ExhaustiveMap<K, V> { | ||
val entries = enumEntries<K>() | ||
return Array(entries.size) { | ||
entries[it] to mapping(entries[it]) | ||
}.let { from(pairs = it) } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.