add a permission check to the logo upload
This commit is contained in:
@@ -7,6 +7,10 @@ import (
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
permissionsapi "github.com/cs3org/go-cs3apis/cs3/permissions/v1beta1"
|
||||
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
|
||||
revactx "github.com/cs3org/reva/v2/pkg/ctx"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -22,6 +26,24 @@ var (
|
||||
|
||||
// UploadLogo implements the endpoint to upload a custom logo for the oCIS instance.
|
||||
func (p Web) UploadLogo(w http.ResponseWriter, r *http.Request) {
|
||||
user := revactx.ContextMustGetUser(r.Context())
|
||||
rsp, err := p.gatewayClient.CheckPermission(r.Context(), &permissionsapi.CheckPermissionRequest{
|
||||
Permission: "change-logo",
|
||||
SubjectRef: &permissionsapi.SubjectReference{
|
||||
Spec: &permissionsapi.SubjectReference_UserId{
|
||||
UserId: user.Id,
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if rsp.Status.Code != rpc.Code_CODE_OK {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
file, fileHeader, err := r.FormFile("logo")
|
||||
if err != nil {
|
||||
if errors.Is(err, http.ErrMissingFile) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package svc
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||
"github.com/owncloud/ocis/v2/services/web/pkg/config"
|
||||
)
|
||||
@@ -12,9 +13,10 @@ type Option func(o *Options)
|
||||
|
||||
// Options defines the available options for this package.
|
||||
type Options struct {
|
||||
Logger log.Logger
|
||||
Config *config.Config
|
||||
Middleware []func(http.Handler) http.Handler
|
||||
Logger log.Logger
|
||||
Config *config.Config
|
||||
Middleware []func(http.Handler) http.Handler
|
||||
GatewayClient gateway.GatewayAPIClient
|
||||
}
|
||||
|
||||
// newOptions initializes the available default options.
|
||||
@@ -48,3 +50,10 @@ func Middleware(val ...func(http.Handler) http.Handler) Option {
|
||||
o.Middleware = val
|
||||
}
|
||||
}
|
||||
|
||||
// GatewayClient provides a function to set the GatewayClient option.
|
||||
func GatewayClient(client gateway.GatewayAPIClient) Option {
|
||||
return func(o *Options) {
|
||||
o.GatewayClient = client
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,12 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/account"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/assetsfs"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/log"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/middleware"
|
||||
"github.com/owncloud/ocis/v2/services/web"
|
||||
"github.com/owncloud/ocis/v2/services/web/pkg/assets"
|
||||
"github.com/owncloud/ocis/v2/services/web/pkg/config"
|
||||
@@ -38,14 +41,22 @@ func NewService(opts ...Option) Service {
|
||||
m.Use(options.Middleware...)
|
||||
|
||||
svc := Web{
|
||||
logger: options.Logger, config: options.Config,
|
||||
mux: m,
|
||||
fs: assetsfs.New(web.Assets, options.Config.Asset.Path, options.Logger),
|
||||
logger: options.Logger,
|
||||
config: options.Config,
|
||||
mux: m,
|
||||
fs: assetsfs.New(web.Assets, options.Config.Asset.Path, options.Logger),
|
||||
gatewayClient: options.GatewayClient,
|
||||
}
|
||||
|
||||
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
|
||||
r.Get("/config.json", svc.Config)
|
||||
r.Post("/branding/logo", svc.UploadLogo)
|
||||
r.Route("/branding/logo", func(r chi.Router) {
|
||||
r.Use(middleware.ExtractAccountUUID(
|
||||
account.Logger(options.Logger),
|
||||
account.JWTSecret(options.Config.TokenManager.JWTSecret),
|
||||
))
|
||||
r.Post("/", svc.UploadLogo)
|
||||
})
|
||||
r.Mount("/", svc.Static(options.Config.HTTP.CacheTTL))
|
||||
})
|
||||
|
||||
@@ -59,10 +70,11 @@ func NewService(opts ...Option) Service {
|
||||
|
||||
// Web defines implements the business logic for Service.
|
||||
type Web struct {
|
||||
logger log.Logger
|
||||
config *config.Config
|
||||
mux *chi.Mux
|
||||
fs *assetsfs.FileSystem
|
||||
logger log.Logger
|
||||
config *config.Config
|
||||
mux *chi.Mux
|
||||
fs *assetsfs.FileSystem
|
||||
gatewayClient gateway.GatewayAPIClient
|
||||
}
|
||||
|
||||
// ServeHTTP implements the Service interface.
|
||||
|
||||
Reference in New Issue
Block a user