aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/api_user_list.go
blob: ba6dff579c935327717468c8a254aca41e77170b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package controllers

import (
	"context"
	"net/http"
	"time"

	"code.crute.us/mcrute/cloud-identity-broker/app/models"

	glecho "code.crute.us/mcrute/golib/echo"
	"code.crute.us/mcrute/golib/echo/controller"
	"github.com/labstack/echo/v4"
)

type jsonListUser struct {
	Username  string     `bson:"_id" json:"key_id"`
	IsAdmin   bool       `json:"is_admin"`
	IsService bool       `json:"is_service"`
	SelfLink  string     `json:"self"`
	Deleted   *time.Time `json:"deleted,omitempty"`
}

type APIUserListHandler struct {
	store models.UserStore
}

func NewAPIUserListHandler(s models.UserStore) echo.HandlerFunc {
	al := &APIUserListHandler{store: s}
	h := &controller.ContentTypeNegotiatingHandler{
		DefaultHandler: al.Handle,
		Handlers: map[string]echo.HandlerFunc{
			contentTypeV2: al.Handle,
		},
	}
	return h.Handle
}

func (h *APIUserListHandler) Handle(c echo.Context) error {
	users, err := h.store.List(context.Background())
	if err != nil {
		return echo.ErrInternalServerError
	}

	out := map[string]*jsonListUser{}
	for _, v := range users {
		out[v.Username] = &jsonListUser{
			Username:  v.Username,
			IsAdmin:   v.IsAdmin,
			IsService: v.IsService,
			SelfLink:  glecho.URLFor(c, "/api/user", v.Username).String(),
			Deleted:   v.Deleted,
		}
	}

	return c.JSON(http.StatusOK, out)
}