github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/settings/flags.go (about)

     1  package settings
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/cozy/cozy-stack/model/feature"
     7  	"github.com/cozy/cozy-stack/pkg/jsonapi"
     8  	"github.com/cozy/cozy-stack/web/middlewares"
     9  	"github.com/labstack/echo/v4"
    10  )
    11  
    12  type apiFlags struct {
    13  	*feature.Flags
    14  	include bool
    15  }
    16  
    17  func (f *apiFlags) Relationships() jsonapi.RelationshipMap {
    18  	return nil
    19  }
    20  
    21  func (f *apiFlags) Included() []jsonapi.Object {
    22  	if !f.include {
    23  		return nil
    24  	}
    25  	included := make([]jsonapi.Object, len(f.Sources))
    26  	for i, source := range f.Sources {
    27  		included[i] = &apiFlags{source, false}
    28  	}
    29  	return included
    30  }
    31  
    32  func (f *apiFlags) Links() *jsonapi.LinksList {
    33  	return &jsonapi.LinksList{Self: "/settings/flags"}
    34  }
    35  
    36  func (h *HTTPHandler) getFlags(c echo.Context) error {
    37  	// Any request with a token can ask for the context (no permissions are required)
    38  	if _, err := middlewares.GetPermission(c); err != nil {
    39  		return echo.NewHTTPError(http.StatusForbidden)
    40  	}
    41  
    42  	inst := middlewares.GetInstance(c)
    43  	flags, err := feature.GetFlags(inst)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	include := c.QueryParam("include") != ""
    48  	return jsonapi.Data(c, http.StatusOK, &apiFlags{flags, include}, nil)
    49  }