add username to our trip

This commit is contained in:
mr
2024-12-04 11:33:08 +01:00
parent 6681c455d8
commit fd01f535a1
30 changed files with 129 additions and 113 deletions

View File

@@ -51,7 +51,7 @@ func (ao *AbstractObject) UpToDate() {
// ao.LastPeerWriter, _ = static.GetMyLocalJsonPeer()
}
func (ao *AbstractObject) VerifyAuth(peerID string, groups []string) bool {
func (ao *AbstractObject) VerifyAuth(username string, peerID string, groups []string) bool {
return true
}
@@ -82,15 +82,19 @@ func (dma *AbstractObject) Serialize(obj DBObject) map[string]interface{} {
}
type AbstractAccessor struct {
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
Type tools.DataType // Type is the data type of the accessor
Caller *tools.HTTPCaller // Caller is the http caller of the accessor (optionnal) only need in a peer connection
PeerID string // PeerID is the id of the peer
Groups []string // Groups is the list of groups that can access the accessor
Logger zerolog.Logger // Logger is the logger of the accessor, it's a specilized logger for the accessor
Type tools.DataType // Type is the data type of the accessor
Caller *tools.HTTPCaller // Caller is the http caller of the accessor (optionnal) only need in a peer connection
PeerID string // PeerID is the id of the peer
Groups []string // Groups is the list of groups that can access the accessor
User string // User is the user that is using the accessor
ResourceModelAccessor Accessor
}
func (dma *AbstractAccessor) GetUser() string {
return dma.User
}
func (dma *AbstractAccessor) GetPeerID() string {
return dma.PeerID
}
@@ -128,7 +132,7 @@ func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
}},
},
}
if !data.VerifyAuth(a.GetPeerID(), a.GetGroups()) {
if !data.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
return nil, 403, errors.New("You are not allowed to access this collaborative area")
}
if cursor, _, _ := a.Search(&f, ""); len(cursor) > 0 {
@@ -153,7 +157,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.GetPeerID(), a.GetGroups()) {
if !res.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
return nil, 403, errors.New("You are not allowed to access this collaborative area")
}
_, code, err = mongo.MONGOService.DeleteOne(id, a.GetType().String())
@@ -171,7 +175,7 @@ func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObje
if err != nil {
return nil, c, err
}
if !r.VerifyAuth(a.GetPeerID(), a.GetGroups()) {
if !r.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
return nil, 403, errors.New("You are not allowed to access this collaborative area")
}
change := set.Serialize(set) // get the changes
@@ -196,7 +200,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.GetPeerID(), a.GetGroups()) {
if !data.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
return nil, 403, errors.New("You are not allowed to access this collaborative area")
}
return f(data)
@@ -213,7 +217,7 @@ func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, f func(DBO
return nil, 404, err
}
for _, r := range results {
if !r.VerifyAuth(a.GetPeerID(), a.GetGroups()) {
if !r.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
continue
}
objs = append(objs, f(r))

View File

@@ -21,15 +21,16 @@ type DBObject interface {
GetID() string
GetName() string
UpToDate()
VerifyAuth(PeerID string, groups []string) bool
VerifyAuth(username string, PeerID string, groups []string) bool
Deserialize(j map[string]interface{}, obj DBObject) DBObject
Serialize(obj DBObject) map[string]interface{}
GetAccessor(peerID string, groups []string, caller *tools.HTTPCaller) Accessor
GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) Accessor
}
// Accessor is an interface that defines the basic methods for an Accessor
type Accessor interface {
GetType() tools.DataType
GetUser() string
GetPeerID() string
GetGroups() []string
GetLogger() *zerolog.Logger