added update space public access and code cleaning

atmsn/public_repo
atefeh 2024-05-07 14:27:48 -07:00
parent 507e4c8869
commit b775459168
7 changed files with 145 additions and 12 deletions

View File

@ -27,21 +27,21 @@ import (
"github.com/rs/zerolog/log"
)
type PublicAccessUpdateInput struct {
type UpdatePublicAccessInput struct {
EnablePublic bool `json:"enable_public"`
}
func (c *Controller) PublicAccessUpdate(ctx context.Context,
func (c *Controller) UpdatePublicAccess(ctx context.Context,
session *auth.Session,
repoRef string,
in *PublicAccessUpdateInput,
in *UpdatePublicAccessInput,
) (*Repository, error) {
repo, err := c.getRepoCheckAccess(ctx, session, repoRef, enum.PermissionRepoEdit)
if err != nil {
return nil, err
}
if err = c.sanitizeVisibilityInput(in); err != nil {
if err = c.sanitizeUpdatePublicAccessInput(in); err != nil {
return nil, fmt.Errorf("failed to sanitize input: %w", err)
}
@ -84,7 +84,7 @@ func (c *Controller) PublicAccessUpdate(ctx context.Context,
}
func (c *Controller) sanitizeVisibilityInput(in *PublicAccessUpdateInput) error {
func (c *Controller) sanitizeUpdatePublicAccessInput(in *UpdatePublicAccessInput) error {
if in.EnablePublic && !c.publicResourceCreationEnabled {
return errPublicRepoCreationDisabled
}

View File

@ -0,0 +1,61 @@
// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package space
import (
"context"
"fmt"
apiauth "github.com/harness/gitness/app/api/auth"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/types/enum"
)
type UpdatePublicAccessInput struct {
EnablePublic bool `json:"enable_public"`
}
func (c *Controller) UpdatePublicAccess(ctx context.Context,
session *auth.Session,
spaceRef string,
in *UpdatePublicAccessInput,
) (*Space, error) {
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
if err != nil {
return nil, err
}
if err = apiauth.CheckSpace(ctx, c.authorizer, session, space, enum.PermissionSpaceEdit); err != nil {
return nil, err
}
if err = c.sanitizeUpdatePublicAccessInput(in); err != nil {
return nil, fmt.Errorf("failed to sanitize input: %w", err)
}
if err = c.publicAccess.Set(ctx, enum.PublicResourceTypeSpace, space.Path, in.EnablePublic); err != nil {
return nil, fmt.Errorf("failed to update space public access: %w", err)
}
return GetSpaceOutput(ctx, c.publicAccess, space)
}
func (c *Controller) sanitizeUpdatePublicAccessInput(in *UpdatePublicAccessInput) error {
if in.EnablePublic && !c.publicResourceCreationEnabled {
return errPublicSpaceCreationDisabled
}
return nil
}

View File

@ -23,7 +23,7 @@ import (
"github.com/harness/gitness/app/api/request"
)
func HandlePublicAccessUpdate(repoCtrl *repo.Controller) http.HandlerFunc {
func HandleUpdatePublicAccess(repoCtrl *repo.Controller) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
session, _ := request.AuthSessionFrom(ctx)
@ -34,14 +34,14 @@ func HandlePublicAccessUpdate(repoCtrl *repo.Controller) http.HandlerFunc {
return
}
in := new(repo.PublicAccessUpdateInput)
in := new(repo.UpdatePublicAccessInput)
err = json.NewDecoder(r.Body).Decode(in)
if err != nil {
render.BadRequestf(ctx, w, "Invalid Request Body: %s.", err)
return
}
res, err := repoCtrl.PublicAccessUpdate(ctx, session, repoRef, in)
res, err := repoCtrl.UpdatePublicAccess(ctx, session, repoRef, in)
if err != nil {
render.TranslatedUserError(ctx, w, err)
return

View File

@ -0,0 +1,52 @@
// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package space
import (
"encoding/json"
"net/http"
"github.com/harness/gitness/app/api/controller/space"
"github.com/harness/gitness/app/api/render"
"github.com/harness/gitness/app/api/request"
)
// HandleUpdatePublicAccess updates public access mode of an existing space.
func HandleUpdatePublicAccess(spaceCtrl *space.Controller) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
session, _ := request.AuthSessionFrom(ctx)
spaceRef, err := request.GetSpaceRefFromPath(r)
if err != nil {
render.TranslatedUserError(ctx, w, err)
return
}
in := new(space.UpdatePublicAccessInput)
err = json.NewDecoder(r.Body).Decode(in)
if err != nil {
render.BadRequestf(ctx, w, "Invalid request body: %s.", err)
return
}
space, err := spaceCtrl.UpdatePublicAccess(ctx, session, spaceRef, in)
if err != nil {
render.TranslatedUserError(ctx, w, err)
return
}
render.JSON(w, http.StatusOK, space)
}
}

View File

@ -200,9 +200,9 @@ type restoreRequest struct {
repo.RestoreInput
}
type UpdatePublicAccessRequest struct {
type updateRepoPublicAccessRequest struct {
repoRequest
repo.PublicAccessUpdateInput
repo.UpdatePublicAccessInput
}
type securitySettingsRequest struct {
@ -704,7 +704,7 @@ func repoOperations(reflector *openapi3.Reflector) {
opUpdatePublicAccess.WithMapOfAnything(
map[string]interface{}{"operationId": "updatePublicAccess"})
_ = reflector.SetRequest(
&opUpdatePublicAccess, new(UpdatePublicAccessRequest), http.MethodPost)
&opUpdatePublicAccess, new(updateRepoPublicAccessRequest), http.MethodPost)
_ = reflector.SetJSONResponse(&opUpdatePublicAccess, new(repo.Repository), http.StatusOK)
_ = reflector.SetJSONResponse(&opUpdatePublicAccess, new(usererror.Error), http.StatusBadRequest)
_ = reflector.SetJSONResponse(&opUpdatePublicAccess, new(usererror.Error), http.StatusInternalServerError)

View File

@ -40,6 +40,10 @@ type updateSpaceRequest struct {
space.UpdateInput
}
type updateSpacePublicAccessRequest struct {
spaceRequest
space.UpdatePublicAccessInput
}
type moveSpaceRequest struct {
spaceRequest
space.MoveInput
@ -247,6 +251,21 @@ func spaceOperations(reflector *openapi3.Reflector) {
_ = reflector.SetJSONResponse(&opUpdate, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(http.MethodPatch, "/spaces/{space_ref}", opUpdate)
opUpdatePublicAccess := openapi3.Operation{}
opUpdatePublicAccess.WithTags("space")
opUpdatePublicAccess.WithMapOfAnything(
map[string]interface{}{"operationId": "updatePublicAccess"})
_ = reflector.SetRequest(
&opUpdatePublicAccess, new(updateSpacePublicAccessRequest), http.MethodPost)
_ = reflector.SetJSONResponse(&opUpdatePublicAccess, new(space.Space), http.StatusOK)
_ = reflector.SetJSONResponse(&opUpdatePublicAccess, new(usererror.Error), http.StatusBadRequest)
_ = reflector.SetJSONResponse(&opUpdatePublicAccess, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opUpdatePublicAccess, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opUpdatePublicAccess, new(usererror.Error), http.StatusForbidden)
_ = reflector.SetJSONResponse(&opUpdatePublicAccess, new(usererror.Error), http.StatusNotFound)
_ = reflector.Spec.AddOperation(
http.MethodPatch, "/spaces/{space_ref}/public-access", opUpdatePublicAccess)
opDelete := openapi3.Operation{}
opDelete.WithTags("space")
opDelete.WithMapOfAnything(map[string]interface{}{"operationId": "deleteSpace"})

View File

@ -241,6 +241,7 @@ func setupSpaces(r chi.Router, appCtx context.Context, spaceCtrl *space.Controll
r.Get("/templates", handlerspace.HandleListTemplates(spaceCtrl))
r.Post("/export", handlerspace.HandleExport(spaceCtrl))
r.Get("/export-progress", handlerspace.HandleExportProgress(spaceCtrl))
r.Post("/public-access", handlerspace.HandleUpdatePublicAccess(spaceCtrl))
r.Route("/members", func(r chi.Router) {
r.Get("/", handlerspace.HandleMembershipList(spaceCtrl))
@ -277,7 +278,7 @@ func setupRepos(r chi.Router,
r.Delete("/", handlerrepo.HandleSoftDelete(repoCtrl))
r.Post("/purge", handlerrepo.HandlePurge(repoCtrl))
r.Post("/restore", handlerrepo.HandleRestore(repoCtrl))
r.Post("/public-access", handlerrepo.HandlePublicAccessUpdate(repoCtrl))
r.Post("/public-access", handlerrepo.HandleUpdatePublicAccess(repoCtrl))
r.Route("/settings", func(r chi.Router) {
r.Get("/security", handlerreposettings.HandleSecurityFind(repoSettingsCtrl))