light modification

This commit is contained in:
mr 2025-01-21 11:55:44 +01:00
parent bc12fb53be
commit bf5a16f41b
4 changed files with 19 additions and 9 deletions

View File

@ -11,12 +11,14 @@ import (
type peerMongoAccessor struct {
utils.AbstractAccessor // AbstractAccessor contains the basic fields of an accessor (model, caller)
overrideAuth bool
}
// New creates a new instance of the peerMongoAccessor
func NewShallowAccessor() *peerMongoAccessor {
return &peerMongoAccessor{
utils.AbstractAccessor{
overrideAuth: true,
AbstractAccessor: utils.AbstractAccessor{
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
Type: tools.PEER,
},
@ -25,7 +27,8 @@ func NewShallowAccessor() *peerMongoAccessor {
func NewAccessor(request *tools.APIRequest) *peerMongoAccessor {
return &peerMongoAccessor{
utils.AbstractAccessor{
overrideAuth: false,
AbstractAccessor: utils.AbstractAccessor{
Logger: logs.CreateLogger(tools.PEER.String()), // Create a logger with the data type
Request: request,
Type: tools.PEER,
@ -33,6 +36,10 @@ func NewAccessor(request *tools.APIRequest) *peerMongoAccessor {
}
}
func (wfa *peerMongoAccessor) ShouldVerifyAuth() bool {
return !wfa.overrideAuth
}
/*
* Nothing special here, just the basic CRUD operations
*/

View File

@ -122,6 +122,10 @@ type AbstractAccessor struct {
ResourceModelAccessor Accessor
}
func (r *AbstractAccessor) ShouldVerifyAuth() bool {
return true
}
func (r *AbstractAccessor) GetRequest() *tools.APIRequest {
return r.Request
}

View File

@ -2,7 +2,6 @@ package utils
import (
"errors"
"fmt"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
@ -31,7 +30,7 @@ func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
}},
},
}
if !data.VerifyAuth(a.GetRequest()) {
if a.ShouldVerifyAuth() && !data.VerifyAuth(a.GetRequest()) {
return nil, 403, errors.New("you are not allowed to access this collaborative area")
}
if cursor, _, _ := a.Search(&f, "", data.IsDrafted()); len(cursor) > 0 {
@ -59,7 +58,7 @@ func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
a.GetLogger().Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
return nil, code, err
}
if !res.VerifyAuth(a.GetRequest()) {
if a.ShouldVerifyAuth() && !res.VerifyAuth(a.GetRequest()) {
return nil, 403, errors.New("you are not allowed to access this collaborative area")
}
_, code, err = mongo.MONGOService.DeleteOne(id, a.GetType().String())
@ -83,7 +82,7 @@ func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObje
}
set = newSet
r.UpToDate(a.GetUser(), false)
if !r.VerifyAuth(a.GetRequest()) {
if a.ShouldVerifyAuth() && !r.VerifyAuth(a.GetRequest()) {
return nil, 403, errors.New("you are not allowed to access this collaborative area")
}
change := set.Serialize(set) // get the changes
@ -108,7 +107,7 @@ func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, erro
return nil, code, err
}
res_mongo.Decode(&data)
if !data.VerifyAuth(a.GetRequest()) {
if a.ShouldVerifyAuth() && !data.VerifyAuth(a.GetRequest()) {
return nil, 403, errors.New("you are not allowed to access this collaborative area")
}
return f(data)
@ -125,12 +124,11 @@ func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, onlyDraft
return nil, 404, err
}
for _, r := range results {
if !r.VerifyAuth(a.GetRequest()) || f(r) == nil || (onlyDraft && !r.IsDrafted()) || (!onlyDraft && r.IsDrafted()) {
if (a.ShouldVerifyAuth() && !r.VerifyAuth(a.GetRequest())) || f(r) == nil || (onlyDraft && !r.IsDrafted()) || (!onlyDraft && r.IsDrafted()) {
continue
}
objs = append(objs, f(r))
}
fmt.Println("results 2", results, objs)
return objs, 200, nil
}

View File

@ -41,6 +41,7 @@ type Accessor interface {
GetGroups() []string
GetLogger() *zerolog.Logger
GetCaller() *tools.HTTPCaller
ShouldVerifyAuth() bool
Search(filters *dbs.Filters, search string, isDraft bool) ([]ShallowDBObject, int, error)
LoadAll(isDraft bool) ([]ShallowDBObject, int, error)
LoadOne(id string) (DBObject, int, error)