aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/api_user_list.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/api_user_list.go')
-rw-r--r--app/controllers/api_user_list.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/app/controllers/api_user_list.go b/app/controllers/api_user_list.go
new file mode 100644
index 0000000..ba6dff5
--- /dev/null
+++ b/app/controllers/api_user_list.go
@@ -0,0 +1,56 @@
1package controllers
2
3import (
4 "context"
5 "net/http"
6 "time"
7
8 "code.crute.us/mcrute/cloud-identity-broker/app/models"
9
10 glecho "code.crute.us/mcrute/golib/echo"
11 "code.crute.us/mcrute/golib/echo/controller"
12 "github.com/labstack/echo/v4"
13)
14
15type jsonListUser struct {
16 Username string `bson:"_id" json:"key_id"`
17 IsAdmin bool `json:"is_admin"`
18 IsService bool `json:"is_service"`
19 SelfLink string `json:"self"`
20 Deleted *time.Time `json:"deleted,omitempty"`
21}
22
23type APIUserListHandler struct {
24 store models.UserStore
25}
26
27func NewAPIUserListHandler(s models.UserStore) echo.HandlerFunc {
28 al := &APIUserListHandler{store: s}
29 h := &controller.ContentTypeNegotiatingHandler{
30 DefaultHandler: al.Handle,
31 Handlers: map[string]echo.HandlerFunc{
32 contentTypeV2: al.Handle,
33 },
34 }
35 return h.Handle
36}
37
38func (h *APIUserListHandler) Handle(c echo.Context) error {
39 users, err := h.store.List(context.Background())
40 if err != nil {
41 return echo.ErrInternalServerError
42 }
43
44 out := map[string]*jsonListUser{}
45 for _, v := range users {
46 out[v.Username] = &jsonListUser{
47 Username: v.Username,
48 IsAdmin: v.IsAdmin,
49 IsService: v.IsService,
50 SelfLink: glecho.URLFor(c, "/api/user", v.Username).String(),
51 Deleted: v.Deleted,
52 }
53 }
54
55 return c.JSON(http.StatusOK, out)
56}