<commit_before>package apps

import (
	"encoding/json"
	"net/http"

	"github.com/cozy/cozy-stack/pkg/apps"
	"github.com/cozy/cozy-stack/pkg/apps/registry"
	"github.com/cozy/cozy-stack/web/jsonapi"
	"github.com/cozy/echo"
)

type registryApp struct {
	*registry.App
}

// MarshalJSON is part of the jsonapi.Object interface
func (a *registryApp) MarshalJSON() ([]byte, error) {
	return json.Marshal(a.App)
}

// Links is part of the jsonapi.Object interface
func (a *registryApp) Links() *jsonapi.LinksList {
	return nil
}

// Relationships is part of the jsonapi.Object interface
func (a *registryApp) Relationships() jsonapi.RelationshipMap {
	return jsonapi.RelationshipMap{}
}

// Included is part of the jsonapi.Object interface
func (a *registryApp) Included() []jsonapi.Object {
	return []jsonapi.Object{}
}

// registryApp is a jsonapi.Object
var _ jsonapi.Object = (*registryApp)(nil)

func registryHandler(appType apps.AppType) echo.HandlerFunc {
	return func(c echo.Context) error {
		// TODO authentication, permissions, and tests
		apps := registry.All(appType)
		objs := make([]jsonapi.Object, len(apps))
		for i, a := range apps {
			objs[i] = &registryApp{a}
		}
		return jsonapi.DataList(c, http.StatusOK, objs, nil)
	}
}<commit_msg>Check permissions for getting the apps from registries<commit_after>package apps

import (
	"encoding/json"
	"net/http"

	"github.com/cozy/cozy-stack/pkg/apps"
	"github.com/cozy/cozy-stack/pkg/apps/registry"
	"github.com/cozy/cozy-stack/web/jsonapi"
	"github.com/cozy/cozy-stack/web/permissions"
	"github.com/cozy/echo"
)

type registryApp struct {
	*registry.App
}

// MarshalJSON is part of the jsonapi.Object interface
func (a *registryApp) MarshalJSON() ([]byte, error) {
	return json.Marshal(a.App)
}

// Links is part of the jsonapi.Object interface
func (a *registryApp) Links() *jsonapi.LinksList {
	return nil
}

// Relationships is part of the jsonapi.Object interface
func (a *registryApp) Relationships() jsonapi.RelationshipMap {
	return jsonapi.RelationshipMap{}
}

// Included is part of the jsonapi.Object interface
func (a *registryApp) Included() []jsonapi.Object {
	return []jsonapi.Object{}
}

// registryApp is a jsonapi.Object
var _ jsonapi.Object = (*registryApp)(nil)

func registryHandler(appType apps.AppType) echo.HandlerFunc {
	return func(c echo.Context) error {
		t := "webapps"
		if appType == apps.Konnector {
			t = "konnectors"
		}
		doctype := "io.cozy.registries." + t
		if err := permissions.AllowWholeType(c, permissions.GET, doctype); err != nil {
			return err
		}
		apps := registry.All(appType)
		objs := make([]jsonapi.Object, len(apps))
		for i, a := range apps {
			objs[i] = &registryApp{a}
		}
		return jsonapi.DataList(c, http.StatusOK, objs, nil)
	}
}
{'fraction_non_alphanumeric': 0.13423062931975263, 'fraction_numerical': 0.0, 'mean_word_length': 4.288461538461538, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3908800', 'n_tokens_mistral': 996, 'n_tokens_neox': 955, 'n_words': 236}