Skip to content

Commit 6185f2f

Browse files
committed
add id get and delete to admin auth api
1 parent a379bac commit 6185f2f

File tree

3 files changed

+129
-8
lines changed

3 files changed

+129
-8
lines changed

routers/api/v1/admin/auths.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
api "code.gitea.io/gitea/modules/structs"
1414
"code.gitea.io/gitea/modules/timeutil"
1515
"code.gitea.io/gitea/modules/web"
16+
auth_service "code.gitea.io/gitea/services/auth"
1617
"code.gitea.io/gitea/services/auth/source/ldap"
1718
"code.gitea.io/gitea/services/auth/source/oauth2"
1819
"code.gitea.io/gitea/services/auth/source/pam"
@@ -47,6 +48,37 @@ func ListAuthSources(ctx *context.APIContext) {
4748
ctx.JSON(http.StatusOK, result)
4849
}
4950

51+
// GetAuthSource get an authentication source by id
52+
func GetAuthSource(ctx *context.APIContext) {
53+
// swagger:operation GET /admin/auths/{id} admin adminGetAuthSource
54+
// ---
55+
// summary: Get an authentication source
56+
// produces:
57+
// - application/json
58+
// parameters:
59+
// - name: id
60+
// in: path
61+
// description: id of the authentication source to get
62+
// type: integer
63+
// format: int64
64+
// required: true
65+
// responses:
66+
// "200":
67+
// "$ref": "#/responses/AuthSource"
68+
69+
source, err := auth.GetSourceByID(ctx.ParamsInt64(":id"))
70+
if err != nil {
71+
ctx.ServerError("auth.GetSourceByID", err)
72+
return
73+
}
74+
result, err := convert.ToAuthSource(source)
75+
if err != nil {
76+
ctx.Error(http.StatusInternalServerError, "convert.ToAuthSource", err)
77+
return
78+
}
79+
ctx.JSON(http.StatusOK, result)
80+
}
81+
5082
// CreateAuthSource creates new authentication source
5183
func CreateAuthSource(ctx *context.APIContext) {
5284
// swagger:operation POST /admin/auths admin adminCreateAuthSource
@@ -118,3 +150,38 @@ func CreateAuthSource(ctx *context.APIContext) {
118150
}
119151
ctx.JSON(http.StatusCreated, result)
120152
}
153+
154+
// DeleteAuthSource delete an authentication source
155+
func DeleteAuthSource(ctx *context.APIContext) {
156+
// swagger:operation DELETE /admin/auths/{id} admin adminDeleteAuthSource
157+
// ---
158+
// summary: Delete an authentication source
159+
// produces:
160+
// - application/json
161+
// parameters:
162+
// - name: id
163+
// in: path
164+
// description: id of the authentication source to delete
165+
// type: integer
166+
// format: int64
167+
// required: true
168+
// responses:
169+
// "204":
170+
// "$ref": "#/responses/empty"
171+
172+
source, err := auth.GetSourceByID(ctx.ParamsInt64(":id"))
173+
if err != nil {
174+
ctx.ServerError("auth.GetSourceByID", err)
175+
return
176+
}
177+
178+
if err = auth_service.DeleteSource(source); err != nil {
179+
if auth.IsErrSourceInUse(err) {
180+
ctx.Error(http.StatusInternalServerError, "auth_service.DeleteSource", err)
181+
} else {
182+
ctx.Error(http.StatusInternalServerError, "auth_service.DeleteSource", err)
183+
}
184+
return
185+
}
186+
ctx.Status(http.StatusNoContent)
187+
}

routers/api/v1/api.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,8 +1233,10 @@ func Routes(ctx gocontext.Context) *web.Route {
12331233

12341234
m.Group("/admin", func() {
12351235
m.Group("/auths", func() {
1236-
m.Get("", admin.ListAuthSources)
1237-
m.Post("", bind(api.CreateAuthSource{}), admin.CreateAuthSource)
1236+
m.Combo("").Get(admin.ListAuthSources).
1237+
Post(bind(api.CreateAuthSource{}), admin.CreateAuthSource)
1238+
m.Combo("/{id}").Get(admin.GetAuthSource).
1239+
Delete(admin.DeleteAuthSource)
12381240
})
12391241
m.Group("/cron", func() {
12401242
m.Get("", admin.ListCronTasks)

templates/swagger/v1_json.tmpl

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,58 @@
125125
}
126126
}
127127
},
128+
"/admin/auths/{id}": {
129+
"get": {
130+
"produces": [
131+
"application/json"
132+
],
133+
"tags": [
134+
"admin"
135+
],
136+
"summary": "Get an authentication source",
137+
"operationId": "adminGetAuthSource",
138+
"parameters": [
139+
{
140+
"type": "integer",
141+
"format": "int64",
142+
"description": "id of the authentication source to get",
143+
"name": "id",
144+
"in": "path",
145+
"required": true
146+
}
147+
],
148+
"responses": {
149+
"200": {
150+
"$ref": "#/responses/AuthSource"
151+
}
152+
}
153+
},
154+
"delete": {
155+
"produces": [
156+
"application/json"
157+
],
158+
"tags": [
159+
"admin"
160+
],
161+
"summary": "Delete an authentication source",
162+
"operationId": "adminDeleteAuthSource",
163+
"parameters": [
164+
{
165+
"type": "integer",
166+
"format": "int64",
167+
"description": "id of the authentication source to delete",
168+
"name": "id",
169+
"in": "path",
170+
"required": true
171+
}
172+
],
173+
"responses": {
174+
"204": {
175+
"$ref": "#/responses/empty"
176+
}
177+
}
178+
}
179+
},
128180
"/admin/cron": {
129181
"get": {
130182
"produces": [
@@ -20242,12 +20294,6 @@
2024220294
}
2024320295
}
2024420296
},
20245-
"CreateAuthSource": {
20246-
"description": "CreateAuthSource",
20247-
"schema": {
20248-
"$ref": "#/definitions/CreateAuthSource"
20249-
}
20250-
},
2025120297
"Branch": {
2025220298
"description": "Branch",
2025320299
"schema": {
@@ -20405,6 +20451,12 @@
2040520451
"$ref": "#/definitions/ContentsResponse"
2040620452
}
2040720453
},
20454+
"CreateAuthSource": {
20455+
"description": "CreateAuthSource",
20456+
"schema": {
20457+
"$ref": "#/definitions/CreateAuthSource"
20458+
}
20459+
},
2040820460
"CronList": {
2040920461
"description": "CronList",
2041020462
"schema": {

0 commit comments

Comments
 (0)