Skip to content

Commit 8ae3b1f

Browse files
committed
Add sudo functionality
Signed-off-by: Andrew Thornton <[email protected]>
1 parent bd1bf2a commit 8ae3b1f

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

routers/api/v1/api.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,35 @@ import (
6464
"gopkg.in/macaron.v1"
6565
)
6666

67+
func sudo() macaron.Handler {
68+
return func(ctx *context.APIContext) {
69+
sudo := ctx.Query("sudo")
70+
if len(sudo) <= 0 {
71+
sudo = ctx.Req.Header.Get("Sudo")
72+
}
73+
74+
if len(sudo) > 0 {
75+
if ctx.User.IsAdmin {
76+
user, err := models.GetUserByName(sudo)
77+
if err != nil {
78+
if models.IsErrUserNotExist(err) {
79+
ctx.Status(404)
80+
} else {
81+
ctx.Error(500, "GetUserByName", err)
82+
}
83+
return
84+
}
85+
ctx.User = user
86+
} else {
87+
ctx.JSON(403, map[string]string{
88+
"message": "Only administrators allowed to sudo.",
89+
})
90+
return
91+
}
92+
}
93+
}
94+
}
95+
6796
func repoAssignment() macaron.Handler {
6897
return func(ctx *context.APIContext) {
6998
userName := ctx.Params(":username")
@@ -589,5 +618,5 @@ func RegisterRoutes(m *macaron.Macaron) {
589618
m.Group("/topics", func() {
590619
m.Get("/search", repo.TopicSearch)
591620
})
592-
}, context.APIContexter())
621+
}, context.APIContexter(), sudo())
593622
}

0 commit comments

Comments
 (0)