add username to our trip

This commit is contained in:
mr 2024-12-04 12:14:55 +01:00
parent fd01f535a1
commit 1fcbc7c08a
10 changed files with 43 additions and 18 deletions

View File

@ -56,3 +56,7 @@ func (d *Booking) GetName() string {
func (d *Booking) GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { func (d *Booking) GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor {
return New(tools.BOOKING, username, peerID, groups, caller) // Create a new instance of the accessor return New(tools.BOOKING, username, peerID, groups, caller) // Create a new instance of the accessor
} }
func (d *Booking) VerifyAuth(username string, peerID string, groups []string) bool {
return true
}

View File

@ -84,7 +84,7 @@ func (ao *CollaborativeArea) VerifyAuth(username string, peerID string, groups [
} }
} }
} }
return false return ao.AbstractObject.VerifyAuth(username, peerID, groups)
} }
func (d *CollaborativeArea) GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { func (d *CollaborativeArea) GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor {

View File

@ -23,3 +23,7 @@ func (r *Rule) GenerateID() {
func (d *Rule) GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { func (d *Rule) GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor {
return New(tools.RULE, username, peerID, groups, caller) return New(tools.RULE, username, peerID, groups, caller)
} }
func (d *Rule) VerifyAuth(username string, peerID string, groups []string) bool {
return true
}

View File

@ -80,3 +80,7 @@ func (d *Peer) GetAccessor(username string, peerID string, groups []string, call
data := New(tools.PEER, username, peerID, groups, caller) // Create a new instance of the accessor data := New(tools.PEER, username, peerID, groups, caller) // Create a new instance of the accessor
return data return data
} }
func (d *Peer) VerifyAuth(username string, peerID string, groups []string) bool {
return true
}

View File

@ -57,7 +57,7 @@ func (abs *AbstractResource) VerifyAuth(username string, peerID string, groups [
} }
} }
} }
return false return abs.AbstractObject.VerifyAuth(username, peerID, groups)
} }
/* /*
@ -125,7 +125,7 @@ func (ao *ResourceModel) GetID() string {
return ao.UUID return ao.UUID
} }
func (ao *ResourceModel) UpToDate() {} func (ao *ResourceModel) UpToDate(user string, create bool) {}
func (r *ResourceModel) GenerateID() { func (r *ResourceModel) GenerateID() {
r.UUID = uuid.New().String() r.UUID = uuid.New().String()

View File

@ -18,16 +18,25 @@ import (
// single instance of the validator used in every model Struct to validate the fields // single instance of the validator used in every model Struct to validate the fields
var validate = validator.New(validator.WithRequiredStructEnabled()) var validate = validator.New(validator.WithRequiredStructEnabled())
type AccessMode int
const (
Private AccessMode = iota
Public
)
/* /*
* AbstractObject is a struct that represents the basic fields of an object * AbstractObject is a struct that represents the basic fields of an object
* it defines the object id and name * it defines the object id and name
* every data in base root model should inherit from this struct (only exception is the ResourceModel) * every data in base root model should inherit from this struct (only exception is the ResourceModel)
*/ */
type AbstractObject struct { type AbstractObject struct {
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"` UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"` Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
UpdateDate time.Time `json:"update_date" bson:"update_date"` UpdateDate time.Time `json:"update_date" bson:"update_date"`
LastPeerWriter string `json:"last_peer_writer" bson:"last_peer_writer"` LastPeerWriter string `json:"last_peer_writer" bson:"last_peer_writer"`
CreatorID string `json:"creator_id" bson:"creator_id" default:"unknown"`
AccessMode AccessMode `json:"access_mode" bson:"access_mode" default:"0"`
} }
func (r *AbstractObject) GenerateID() { func (r *AbstractObject) GenerateID() {
@ -46,13 +55,16 @@ func (ao AbstractObject) GetName() string {
return ao.Name return ao.Name
} }
func (ao *AbstractObject) UpToDate() { func (ao *AbstractObject) UpToDate(user string, create bool) {
ao.UpdateDate = time.Now() ao.UpdateDate = time.Now()
// ao.LastPeerWriter, _ = static.GetMyLocalJsonPeer() ao.LastPeerWriter = user
if create {
ao.CreatorID = user
}
} }
func (ao *AbstractObject) VerifyAuth(username string, peerID string, groups []string) bool { func (ao *AbstractObject) VerifyAuth(username string, peerID string, groups []string) bool {
return true return ao.AccessMode == Public || ao.CreatorID == username
} }
func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters { func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters {
@ -120,6 +132,7 @@ func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
// GenericLoadOne loads one object from the database (generic) // GenericLoadOne loads one object from the database (generic)
func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) { func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
data.GenerateID() data.GenerateID()
data.UpToDate(a.GetUser(), true)
f := dbs.Filters{ f := dbs.Filters{
Or: map[string][]dbs.Filter{ Or: map[string][]dbs.Filter{
"abstractresource.abstractobject.name": {{ "abstractresource.abstractobject.name": {{
@ -175,6 +188,7 @@ func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObje
if err != nil { if err != nil {
return nil, c, err return nil, c, err
} }
r.UpToDate(a.GetUser(), false)
if !r.VerifyAuth(a.GetUser(), 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") return nil, 403, errors.New("You are not allowed to access this collaborative area")
} }

View File

@ -20,7 +20,7 @@ type DBObject interface {
GenerateID() GenerateID()
GetID() string GetID() string
GetName() string GetName() string
UpToDate() UpToDate(user string, create bool)
VerifyAuth(username string, PeerID string, groups []string) bool VerifyAuth(username string, PeerID string, groups []string) bool
Deserialize(j map[string]interface{}, obj DBObject) DBObject Deserialize(j map[string]interface{}, obj DBObject) DBObject
Serialize(obj DBObject) map[string]interface{} Serialize(obj DBObject) map[string]interface{}

View File

@ -108,10 +108,8 @@ func (ao *Workflow) VerifyAuth(username string, peerID string, groups []string)
} }
isAuthorized = shared.VerifyAuth(username, peerID, groups) isAuthorized = shared.VerifyAuth(username, peerID, groups)
} }
} else {
isAuthorized = true
} }
return isAuthorized return ao.AbstractObject.VerifyAuth(username, peerID, groups) || isAuthorized
} }
/* /*

View File

@ -118,3 +118,7 @@ func (d *WorkflowExecution) GetName() string {
func (d *WorkflowExecution) GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor { func (d *WorkflowExecution) GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) utils.Accessor {
return New(tools.WORKFLOW_EXECUTION, username, peerID, groups, caller) // Create a new instance of the accessor return New(tools.WORKFLOW_EXECUTION, username, peerID, groups, caller) // Create a new instance of the accessor
} }
func (d *WorkflowExecution) VerifyAuth(username string, peerID string, groups []string) bool {
return true
}

View File

@ -1,8 +1,6 @@
package workspace package workspace
import ( import (
"fmt"
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area" "cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
"cloud.o-forge.io/core/oc-lib/models/resources" "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
@ -23,7 +21,6 @@ func (d *Workspace) GetAccessor(username string, peerID string, groups []string,
} }
func (ao *Workspace) VerifyAuth(username string, peerID string, groups []string) bool { func (ao *Workspace) VerifyAuth(username string, peerID string, groups []string) bool {
fmt.Println("Workspace.VerifyAuth", ao.Shared)
if ao.Shared != "" { if ao.Shared != "" {
shared, code, _ := shallow_collaborative_area.New(tools.COLLABORATIVE_AREA, username, peerID, groups, nil).LoadOne(ao.Shared) shared, code, _ := shallow_collaborative_area.New(tools.COLLABORATIVE_AREA, username, peerID, groups, nil).LoadOne(ao.Shared)
if code != 200 || shared == nil { if code != 200 || shared == nil {
@ -31,5 +28,5 @@ func (ao *Workspace) VerifyAuth(username string, peerID string, groups []string)
} }
return shared.VerifyAuth(username, peerID, groups) return shared.VerifyAuth(username, peerID, groups)
} }
return true return ao.AbstractObject.VerifyAuth(username, peerID, groups)
} }