diff options
Diffstat (limited to 'opendc-web')
6 files changed, 245 insertions, 1 deletions
diff --git a/opendc-web/opendc-web-proto/src/main/kotlin/org/opendc/web/proto/user/User.kt b/opendc-web/opendc-web-proto/src/main/kotlin/org/opendc/web/proto/user/User.kt new file mode 100644 index 00000000..f18cda61 --- /dev/null +++ b/opendc-web/opendc-web-proto/src/main/kotlin/org/opendc/web/proto/user/User.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.proto.user + +/** + * A user of OpenDC. + */ +public data class User( + val userId: String, + val accounting: UserAccounting +) diff --git a/opendc-web/opendc-web-proto/src/main/kotlin/org/opendc/web/proto/user/UserAccounting.kt b/opendc-web/opendc-web-proto/src/main/kotlin/org/opendc/web/proto/user/UserAccounting.kt new file mode 100644 index 00000000..2441983a --- /dev/null +++ b/opendc-web/opendc-web-proto/src/main/kotlin/org/opendc/web/proto/user/UserAccounting.kt @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.proto.user + +import java.time.LocalDate + +/** + * Accounting data for a user. + */ +public data class UserAccounting( + val periodEnd: LocalDate, + val simulationTime: Int, + val simulationTimeBudget: Int +) diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/user/UserResource.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/user/UserResource.kt new file mode 100644 index 00000000..d640cc08 --- /dev/null +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/rest/user/UserResource.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user + +import io.quarkus.security.identity.SecurityIdentity +import org.opendc.web.proto.user.User +import org.opendc.web.server.service.UserService +import javax.annotation.security.RolesAllowed +import javax.inject.Inject +import javax.ws.rs.GET +import javax.ws.rs.Path + +/** + * A resource representing the active user. + */ +@Path("/users") +@RolesAllowed("openid") +class UserResource @Inject constructor(private val userService: UserService, private val identity: SecurityIdentity) { + /** + * Get the current active user data. + */ + @GET + @Path("me") + fun get(): User = userService.getUser(identity) +} diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt index 13440a81..11066bfb 100644 --- a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserAccountingService.kt @@ -31,6 +31,7 @@ import java.time.temporal.TemporalAdjusters import javax.enterprise.context.ApplicationScoped import javax.inject.Inject import javax.persistence.EntityExistsException +import org.opendc.web.proto.user.UserAccounting as UserAccountingDto /** * Service for tracking the simulation budget of users. @@ -45,6 +46,19 @@ class UserAccountingService @Inject constructor( private val simulationBudget: Duration ) { /** + * Return the [UserAccountingDto] object for the user with the specified [userId]. If the object does not exist in the + * database, a default value is constructed. + */ + fun getAccounting(userId: String): UserAccountingDto { + val accounting = repository.findForUser(userId) + return if (accounting != null) { + UserAccountingDto(accounting.periodEnd, accounting.simulationTime, accounting.simulationTimeBudget) + } else { + UserAccountingDto(getNextAccountingPeriod(), 0, simulationBudget.toSeconds().toInt()) + } + } + + /** * Determine whether the user with [userId] has any remaining simulation budget. * * @param userId The unique identifier of the user. @@ -67,7 +81,7 @@ class UserAccountingService @Inject constructor( */ fun consumeSimulationBudget(userId: String, seconds: Int): Boolean { val today = LocalDate.now() - val nextAccountingPeriod = today.with(TemporalAdjusters.firstDayOfNextMonth()) + val nextAccountingPeriod = getNextAccountingPeriod(today) val repository = repository // We need to be careful to prevent conflicts in case of concurrency @@ -104,4 +118,11 @@ class UserAccountingService @Inject constructor( throw IllegalStateException("Failed to allocate consume budget due to conflict") } + + /** + * Helper method to find next accounting period. + */ + private fun getNextAccountingPeriod(today: LocalDate = LocalDate.now()): LocalDate { + return today.with(TemporalAdjusters.firstDayOfNextMonth()) + } } diff --git a/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserService.kt b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserService.kt new file mode 100644 index 00000000..39352267 --- /dev/null +++ b/opendc-web/opendc-web-server/src/main/kotlin/org/opendc/web/server/service/UserService.kt @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.service + +import io.quarkus.security.identity.SecurityIdentity +import org.opendc.web.proto.user.User +import javax.enterprise.context.ApplicationScoped +import javax.inject.Inject + +/** + * Service for managing [User]s. + */ +@ApplicationScoped +class UserService @Inject constructor(private val accounting: UserAccountingService) { + /** + * Obtain the [User] object for the specified [identity]. + */ + fun getUser(identity: SecurityIdentity): User { + val userId = identity.principal.name + val accounting = accounting.getAccounting(userId) + + return User(userId, accounting) + } +} diff --git a/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt new file mode 100644 index 00000000..36af20f4 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user + +import io.quarkus.test.common.http.TestHTTPEndpoint +import io.quarkus.test.junit.QuarkusTest +import io.quarkus.test.security.TestSecurity +import io.restassured.http.ContentType +import io.restassured.module.kotlin.extensions.Then +import io.restassured.module.kotlin.extensions.When +import org.hamcrest.Matchers +import org.junit.jupiter.api.Test + +/** + * Test suite for [UserResource]. + */ +@QuarkusTest +@TestHTTPEndpoint(UserResource::class) +class UserResourceTest { + /** + * Test that tries to obtain the profile of the active user. + */ + @Test + @TestSecurity(user = "testUser", roles = ["openid"]) + fun testMe() { + When { + get("me") + } Then { + statusCode(200) + contentType(ContentType.JSON) + + body("userId", Matchers.equalTo("testUser")) + body("accounting.simulationTime", Matchers.equalTo(0)) + body("accounting.simulationTimeBudget", Matchers.greaterThan(0)) + } + } + + /** + * Test that tries to obtain the profile of the active user without authorization. + */ + @Test + fun testMeUnauthorized() { + When { + get("me") + } Then { + statusCode(401) + } + } +} |
