Skip to content

Commit 61155ec

Browse files
committed
add id get and delete to admin auth api
1 parent e6843fd commit 61155ec

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
@@ -1500,8 +1500,10 @@ func Routes() *web.Route {
15001500

15011501
m.Group("/admin", func() {
15021502
m.Group("/auths", func() {
1503-
m.Get("", admin.ListAuthSources)
1504-
m.Post("", bind(api.CreateAuthSource{}), admin.CreateAuthSource)
1503+
m.Combo("").Get(admin.ListAuthSources).
1504+
Post(bind(api.CreateAuthSource{}), admin.CreateAuthSource)
1505+
m.Combo("/{id}").Get(admin.GetAuthSource).
1506+
Delete(admin.DeleteAuthSource)
15051507
})
15061508
m.Group("/cron", func() {
15071509
m.Get("", admin.ListCronTasks)

templates/swagger/v1_json.tmpl

Lines changed: 58 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)