diff --git a/models/peer/peer_mongo_accessor.go b/models/peer/peer_mongo_accessor.go index 3fe1c39..953368a 100644 --- a/models/peer/peer_mongo_accessor.go +++ b/models/peer/peer_mongo_accessor.go @@ -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 */ diff --git a/models/utils/abstracts.go b/models/utils/abstracts.go index 772a6a2..29d5419 100644 --- a/models/utils/abstracts.go +++ b/models/utils/abstracts.go @@ -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 } diff --git a/models/utils/common.go b/models/utils/common.go index 5639a18..4a34cd5 100644 --- a/models/utils/common.go +++ b/models/utils/common.go @@ -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 } diff --git a/models/utils/interfaces.go b/models/utils/interfaces.go index 8cc6c1b..679f234 100644 --- a/models/utils/interfaces.go +++ b/models/utils/interfaces.go @@ -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)