Skip to content

Commit a77a590

Browse files
committed
Add api to change paasToken
Signed-off-by: loheagn <[email protected]>
1 parent 57b6a2c commit a77a590

File tree

10 files changed

+97
-1
lines changed

10 files changed

+97
-1
lines changed

cloudapi-common/src/main/kotlin/cn/edu/buaa/scs/utils/HttpClient.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ class HttpClientWrapper(val client: HttpClient, val basePath: String) {
3232
return handleResponse(response)
3333
}
3434

35+
suspend inline fun <reified T> put(
36+
path: String,
37+
body: Any? = null,
38+
contentType: ContentType = ContentType.Application.Json,
39+
): Result<T> {
40+
val response = client.put(basePath + path) {
41+
contentType(contentType)
42+
setBody(body)
43+
}
44+
return handleResponse(response)
45+
}
46+
3547
suspend inline fun <reified T> delete(
3648
path: String,
3749
body: Any? = null,

cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/bugit/GitClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ object GitClient : IProjectManager {
119119
return client.delete("/orgs/$projectName/memberships/$memberID")
120120
}
121121

122+
override suspend fun changePassword(username: String, password: String): Result<Unit> {
123+
return client.put("/admin/users/${username.lowercase()}/password", ChangePasswordReq(password))
124+
}
125+
122126
suspend fun getRepoListOfProject(projectName: String): Result<List<GitRepo>> {
123127
return client.get("orgs/$projectName/repos")
124128
}

cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/bugit/Models.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,8 @@ data class CreateUserReq(
108108
val email: String,
109109
val password: String
110110
)
111+
112+
data class ChangePasswordReq(
113+
@JsonProperty("password")
114+
val password: String
115+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* cloudapi_v2
3+
* buaa scs cloud api v2
4+
*
5+
* The version of the OpenAPI document: 2.0
6+
7+
*
8+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9+
* https://openapi-generator.tech
10+
* Do not edit the class manually.
11+
*/
12+
package cn.edu.buaa.scs.controller.models
13+
14+
15+
/**
16+
*
17+
* @param paasToken
18+
*/
19+
data class PutPaasTokenRequest(
20+
val paasToken: kotlin.String
21+
)
22+

cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/harbor/HarborClient.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ object HarborClient : IProjectManager {
8787
deleteProjectMember(projectName, memberID)
8888
}
8989

90+
override suspend fun changePassword(username: String, password: String): Result<Unit> = runCatching {
91+
val users = userClient.searchUsers(username.lowercase())
92+
if (users.isEmpty()) throw Exception("User $username not found in harbor")
93+
userClient.updateUserPassword(users[0].userId!!, PasswordReq(newPassword = password))
94+
}
95+
9096
private fun existProject(projectName: String): Boolean {
9197
return try {
9298
projectClient.getProject(projectName)

cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/kube/BusinessKubeClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ metadata:
149149
override suspend fun removeProjectMember(projectName: String, memberID: String): Result<Unit> {
150150
return Result.success(Unit)
151151
}
152+
153+
override suspend fun changePassword(username: String, password: String): Result<Unit> {
154+
return Result.success(Unit)
155+
}
152156
}
153157

154158
fun Route.podLogWsRoute() {

cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/project/Interface.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ interface IProjectManager {
2424
suspend fun addProjectMember(projectName: String, memberID: String): Result<Unit>
2525

2626
suspend fun removeProjectMember(projectName: String, memberID: String): Result<Unit>
27+
28+
suspend fun changePassword(username: String, password: String): Result<Unit>
2729
}
2830

2931
val managerList = listOf(
3032
GitClient,
3133
HarborClient,
3234
BusinessKubeClient,
33-
)
35+
)

cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/route/Auth.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package cn.edu.buaa.scs.route
22

33
import cn.edu.buaa.scs.controller.models.GetCaptcha200Response
44
import cn.edu.buaa.scs.controller.models.PostLoginRequest
5+
import cn.edu.buaa.scs.controller.models.PutPaasTokenRequest
56
import cn.edu.buaa.scs.model.Authentication
67
import cn.edu.buaa.scs.service.auth
8+
import cn.edu.buaa.scs.service.project
79
import cn.edu.buaa.scs.utils.token
810
import cn.edu.buaa.scs.utils.user
911
import io.ktor.server.application.*
@@ -60,4 +62,12 @@ fun Route.authRoute() {
6062
call.respond(call.auth.checkPermission(entityType, entityId, action))
6163
}
6264
}
65+
66+
route("/paasToken") {
67+
put {
68+
val password = call.receive<PutPaasTokenRequest>().paasToken
69+
call.project.changePassword(password)
70+
call.respond("OK")
71+
}
72+
}
6373
}

cloudapi-web/src/main/kotlin/cn/edu/buaa/scs/service/Project.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ class ProjectService(val call: ApplicationCall) : IService, FileService.FileDeco
208208
}
209209
}
210210

211+
suspend fun changePassword(password: String) {
212+
val user = call.user()
213+
managerList.forEach {
214+
it.changePassword(user.id, password).getOrThrow()
215+
}
216+
user.paasToken = password
217+
user.flushChanges()
218+
}
219+
211220
suspend fun addProjectMember(projectID: Long, memberID: String, role: ProjectRole): ProjectMember {
212221
val project = Project.id(projectID)
213222
if (!call.user().isProjectAdmin(project)) {

openapi/cloudapi_v2.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,28 @@ paths:
22602260
description: 根据ID删除某项实验工作流配置
22612261
security:
22622262
- Authorization: []
2263+
/paasToken:
2264+
put:
2265+
summary: 修改PaaS平台通用token
2266+
operationId: put-paasToken
2267+
responses:
2268+
'200':
2269+
description: OK
2270+
tags:
2271+
- 鉴权
2272+
description: 修改PaaS平台通用token
2273+
security:
2274+
- Authorization: []
2275+
requestBody:
2276+
content:
2277+
application/json:
2278+
schema:
2279+
type: object
2280+
properties:
2281+
paasToken:
2282+
type: string
2283+
required:
2284+
- paasToken
22632285
components:
22642286
schemas:
22652287
AssignmentResponse:

0 commit comments

Comments
 (0)