forked from cozy/cozy-stack
/
flags.go
49 lines (41 loc) · 1.1 KB
/
flags.go
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
package settings
import (
"net/http"
"github.com/cozy/cozy-stack/model/feature"
"github.com/cozy/cozy-stack/pkg/jsonapi"
"github.com/cozy/cozy-stack/web/middlewares"
"github.com/labstack/echo/v4"
)
type apiFlags struct {
*feature.Flags
include bool
}
func (f *apiFlags) Relationships() jsonapi.RelationshipMap {
return nil
}
func (f *apiFlags) Included() []jsonapi.Object {
if !f.include {
return nil
}
included := make([]jsonapi.Object, len(f.Sources))
for i, source := range f.Sources {
included[i] = &apiFlags{source, false}
}
return included
}
func (f *apiFlags) Links() *jsonapi.LinksList {
return &jsonapi.LinksList{Self: "/settings/flags"}
}
func getFlags(c echo.Context) error {
// Any request with a token can ask for the context (no permissions are required)
if _, err := middlewares.GetPermission(c); err != nil {
return echo.NewHTTPError(http.StatusForbidden)
}
inst := middlewares.GetInstance(c)
flags, err := feature.GetFlags(inst)
if err != nil {
return err
}
include := c.QueryParam("include") != ""
return jsonapi.Data(c, http.StatusOK, &apiFlags{flags, include}, nil)
}