test
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/config"
|
||||
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/rules/rule"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
@@ -42,6 +43,32 @@ type CollaborativeArea struct {
|
||||
SharedPeers []*peer.Peer `json:"shared_peers,omitempty" bson:"-"` // SharedPeers is the shared peers of the workspace
|
||||
}
|
||||
|
||||
func (ao *CollaborativeArea) Clear(peerID string) {
|
||||
ao.CreatorID = peerID
|
||||
if config.GetConfig().Whitelist {
|
||||
ao.AllowedPeersGroup[peerID] = []string{"*"}
|
||||
} else {
|
||||
ao.AllowedPeersGroup[peerID] = []string{}
|
||||
}
|
||||
// then reset the shared fields
|
||||
if ao.Workspaces == nil {
|
||||
ao.Workspaces = []string{}
|
||||
}
|
||||
if ao.Workflows == nil {
|
||||
ao.Workflows = []string{}
|
||||
}
|
||||
if ao.Rules == nil {
|
||||
ao.Rules = []string{}
|
||||
}
|
||||
if ao.CollaborativeAreaRule == nil {
|
||||
ao.CollaborativeAreaRule = &CollaborativeAreaRule{
|
||||
ShareMode: "private",
|
||||
ExploitedBy: "collaborators only",
|
||||
}
|
||||
}
|
||||
ao.CollaborativeAreaRule.CreatedAt = time.Now().UTC()
|
||||
}
|
||||
|
||||
func (ao *CollaborativeArea) VerifyAuth(peerID string, groups []string) bool {
|
||||
if ao.AllowedPeersGroup != nil && len(ao.AllowedPeersGroup) > 0 {
|
||||
if grps, ok := ao.AllowedPeersGroup[peerID]; ok {
|
||||
|
||||
@@ -4,15 +4,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/config"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
"cloud.o-forge.io/core/oc-lib/logs"
|
||||
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/rules/rule"
|
||||
"cloud.o-forge.io/core/oc-lib/models/peer"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
w "cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workspace"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
@@ -45,35 +43,120 @@ func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCal
|
||||
}
|
||||
|
||||
// DeleteOne deletes a collaborative area from the database, given its ID, it automatically share to peers if the workspace is shared
|
||||
func (wfa *collaborativeAreaMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
set, code, err := wfa.LoadOne(id)
|
||||
func (a *collaborativeAreaMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
set, code, err := a.LoadOne(id)
|
||||
if code != 200 {
|
||||
return nil, code, err
|
||||
}
|
||||
wfa.deleteToPeer(set.(*CollaborativeArea))
|
||||
wfa.sharedWorkflow(&CollaborativeArea{}, id) // create all shared workflows
|
||||
wfa.sharedWorkspace(&CollaborativeArea{}, id) // create all collaborative areas
|
||||
return wfa.GenericDeleteOne(id, wfa) // then add on yours
|
||||
a.deleteToPeer(set.(*CollaborativeArea))
|
||||
a.sharedWorkflow(&CollaborativeArea{}, id) // create all shared workflows
|
||||
a.sharedWorkspace(&CollaborativeArea{}, id) // create all collaborative areas
|
||||
return utils.GenericDeleteOne(id, a) // then add on yours
|
||||
}
|
||||
|
||||
// UpdateOne updates a collaborative area in the database, given its ID and the new data, it automatically share to peers if the workspace is shared
|
||||
func (a *collaborativeAreaMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
res, code, err := utils.GenericUpdateOne(set.(*CollaborativeArea).Trim(), id, a, &CollaborativeArea{})
|
||||
// a.deleteToPeer(res.(*CollaborativeArea)) // delete the collaborative area on the peer
|
||||
a.sharedWorkflow(res.(*CollaborativeArea), id) // replace all shared workflows
|
||||
a.sharedWorkspace(res.(*CollaborativeArea), id) // replace all collaborative areas (not shared worspace obj but workspace one)
|
||||
// a.sendToPeer(res.(*CollaborativeArea)) // send the collaborative area (collaborative area object) to the peers
|
||||
return res, code, err
|
||||
}
|
||||
|
||||
// StoreOne stores a collaborative area in the database, it automatically share to peers if the workspace is shared
|
||||
func (a *collaborativeAreaMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
_, id := (&peer.Peer{}).IsMySelf() // get the local peer
|
||||
data.(*CollaborativeArea).Clear(id) // set the creator
|
||||
// retrieve or proper peer
|
||||
dd, code, err := a.peerAccessor.Search(nil, "0")
|
||||
if code != 200 || len(dd) == 0 {
|
||||
return nil, code, errors.New("Could not retrieve the peer" + err.Error())
|
||||
}
|
||||
data.(*CollaborativeArea).CollaborativeAreaRule.Creator = dd[0].GetID()
|
||||
d, code, err := utils.GenericStoreOne(data.(*CollaborativeArea).Trim(), a)
|
||||
if code == 200 {
|
||||
a.sharedWorkflow(d.(*CollaborativeArea), d.GetID()) // create all shared workflows
|
||||
a.sharedWorkspace(d.(*CollaborativeArea), d.GetID()) // create all collaborative areas
|
||||
a.sendToPeer(d.(*CollaborativeArea)) // send the collaborative area (collaborative area object) to the peers
|
||||
}
|
||||
return data, code, err
|
||||
}
|
||||
|
||||
// CopyOne copies a CollaborativeArea in the database
|
||||
func (a *collaborativeAreaMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return a.StoreOne(data)
|
||||
}
|
||||
|
||||
func filterEnrich[T utils.ShallowDBObject](arr []string, a utils.Accessor) []T {
|
||||
var new []T
|
||||
res, code, _ := a.Search(&dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.id": {{Operator: dbs.IN.String(), Value: arr}},
|
||||
},
|
||||
}, "")
|
||||
if code == 200 {
|
||||
for _, r := range res {
|
||||
new = append(new, r.(T))
|
||||
}
|
||||
}
|
||||
return new
|
||||
}
|
||||
|
||||
// enrich is a function that enriches the CollaborativeArea with the shared objects
|
||||
func (a *collaborativeAreaMongoAccessor) enrich(sharedWorkspace *CollaborativeArea) *CollaborativeArea {
|
||||
sharedWorkspace.SharedWorkspaces = append(sharedWorkspace.SharedWorkspaces,
|
||||
filterEnrich[*workspace.Workspace](sharedWorkspace.Workspaces, a.workspaceAccessor)...)
|
||||
sharedWorkspace.SharedWorkflows = append(sharedWorkspace.SharedWorkflows,
|
||||
filterEnrich[*workflow.Workflow](sharedWorkspace.Workflows, a.workflowAccessor)...)
|
||||
peerskey := []string{}
|
||||
for k := range sharedWorkspace.AllowedPeersGroup {
|
||||
peerskey = append(peerskey, k)
|
||||
}
|
||||
sharedWorkspace.SharedPeers = append(sharedWorkspace.SharedPeers,
|
||||
filterEnrich[*peer.Peer](peerskey, a.peerAccessor)...)
|
||||
sharedWorkspace.SharedRules = append(sharedWorkspace.SharedRules,
|
||||
filterEnrich[*rule.Rule](sharedWorkspace.Rules, a.ruleAccessor)...)
|
||||
return sharedWorkspace
|
||||
}
|
||||
|
||||
func (a *collaborativeAreaMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericLoadOne[*CollaborativeArea](id, func(d utils.DBObject) (utils.DBObject, int, error) {
|
||||
return a.enrich(d.(*CollaborativeArea)), 200, nil
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *collaborativeAreaMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericLoadAll[*CollaborativeArea](func(d utils.DBObject) utils.ShallowDBObject {
|
||||
return a.enrich(d.(*CollaborativeArea))
|
||||
}, a)
|
||||
}
|
||||
|
||||
func (a *collaborativeAreaMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
|
||||
return utils.GenericSearch[*CollaborativeArea](filters, search, (&CollaborativeArea{}).GetObjectFilters(search),
|
||||
func(d utils.DBObject) utils.ShallowDBObject {
|
||||
return a.enrich(d.(*CollaborativeArea))
|
||||
}, a)
|
||||
}
|
||||
|
||||
/*
|
||||
sharedWorkspace is a function that shares the collaborative area to the peers
|
||||
*/
|
||||
func (wfa *collaborativeAreaMongoAccessor) sharedWorkspace(shared *CollaborativeArea, id string) {
|
||||
eldest, code, _ := wfa.LoadOne(id) // get the eldest
|
||||
func (a *collaborativeAreaMongoAccessor) sharedWorkspace(shared *CollaborativeArea, id string) {
|
||||
eldest, code, _ := a.LoadOne(id) // get the eldest
|
||||
if code == 200 {
|
||||
eld := eldest.(*CollaborativeArea)
|
||||
if eld.Workspaces != nil { // update all your workspaces in the eldest by replacing shared ref by an empty string
|
||||
for _, v := range eld.Workspaces {
|
||||
wfa.workspaceAccessor.UpdateOne(&workspace.Workspace{Shared: ""}, v)
|
||||
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.WORKSPACE] == nil {
|
||||
a.workspaceAccessor.UpdateOne(&workspace.Workspace{Shared: ""}, v)
|
||||
if a.Caller != nil || a.Caller.URLS == nil || a.Caller.URLS[tools.WORKSPACE] == nil {
|
||||
continue
|
||||
}
|
||||
paccess := (&peer.Peer{}) // send to all peers
|
||||
for k := range shared.AllowedPeersGroup { // delete the collaborative area on the peer
|
||||
b, err := paccess.LaunchPeerExecution(k, v, tools.WORKSPACE, tools.DELETE, nil, wfa.Caller)
|
||||
b, err := paccess.LaunchPeerExecution(k, v, tools.WORKSPACE, tools.DELETE, nil, a.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
a.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,8 +164,8 @@ func (wfa *collaborativeAreaMongoAccessor) sharedWorkspace(shared *Collaborative
|
||||
}
|
||||
if shared.Workspaces != nil {
|
||||
for _, v := range shared.Workspaces { // update all the collaborative areas
|
||||
workspace, code, _ := wfa.workspaceAccessor.UpdateOne(&workspace.Workspace{Shared: shared.UUID}, v) // add the shared ref to workspace
|
||||
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.WORKSPACE] == nil {
|
||||
workspace, code, _ := a.workspaceAccessor.UpdateOne(&workspace.Workspace{Shared: shared.UUID}, v) // add the shared ref to workspace
|
||||
if a.Caller != nil || a.Caller.URLS == nil || a.Caller.URLS[tools.WORKSPACE] == nil {
|
||||
continue
|
||||
}
|
||||
for k := range shared.AllowedPeersGroup {
|
||||
@@ -92,9 +175,9 @@ func (wfa *collaborativeAreaMongoAccessor) sharedWorkspace(shared *Collaborative
|
||||
paccess := (&peer.Peer{}) // send to all peers, add the collaborative area on the peer
|
||||
s := workspace.Serialize(workspace)
|
||||
s["name"] = fmt.Sprintf("%v", s["name"]) + "_" + k
|
||||
b, err := paccess.LaunchPeerExecution(k, v, tools.WORKSPACE, tools.POST, s, wfa.Caller)
|
||||
b, err := paccess.LaunchPeerExecution(k, v, tools.WORKSPACE, tools.POST, s, a.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
a.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,13 +187,13 @@ func (wfa *collaborativeAreaMongoAccessor) sharedWorkspace(shared *Collaborative
|
||||
}
|
||||
|
||||
// sharedWorkflow is a function that shares the shared workflow to the peers
|
||||
func (wfa *collaborativeAreaMongoAccessor) sharedWorkflow(shared *CollaborativeArea, id string) {
|
||||
eldest, code, _ := wfa.LoadOne(id) // get the eldest
|
||||
func (a *collaborativeAreaMongoAccessor) sharedWorkflow(shared *CollaborativeArea, id string) {
|
||||
eldest, code, _ := a.LoadOne(id) // get the eldest
|
||||
if code == 200 {
|
||||
eld := eldest.(*CollaborativeArea)
|
||||
if eld.Workflows != nil {
|
||||
for _, v := range eld.Workflows {
|
||||
data, code, _ := wfa.workflowAccessor.LoadOne(v)
|
||||
data, code, _ := a.workflowAccessor.LoadOne(v)
|
||||
if code == 200 {
|
||||
s := data.(*w.Workflow)
|
||||
new := []string{}
|
||||
@@ -121,15 +204,15 @@ func (wfa *collaborativeAreaMongoAccessor) sharedWorkflow(shared *CollaborativeA
|
||||
} // kick the shared reference in your old shared workflow
|
||||
n := &w.Workflow{}
|
||||
n.Shared = new
|
||||
wfa.workflowAccessor.UpdateOne(n, v)
|
||||
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.WORKFLOW] == nil {
|
||||
a.workflowAccessor.UpdateOne(n, v)
|
||||
if a.Caller != nil || a.Caller.URLS == nil || a.Caller.URLS[tools.WORKFLOW] == nil {
|
||||
continue
|
||||
}
|
||||
paccess := (&peer.Peer{}) // send to all peers
|
||||
for k := range shared.AllowedPeersGroup { // delete the shared workflow on the peer
|
||||
b, err := paccess.LaunchPeerExecution(k, v, tools.WORKFLOW, tools.DELETE, nil, wfa.Caller)
|
||||
b, err := paccess.LaunchPeerExecution(k, v, tools.WORKFLOW, tools.DELETE, nil, a.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
a.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -138,13 +221,13 @@ func (wfa *collaborativeAreaMongoAccessor) sharedWorkflow(shared *CollaborativeA
|
||||
}
|
||||
if shared.Workflows != nil { // update all the shared workflows
|
||||
for _, v := range shared.Workflows {
|
||||
data, code, _ := wfa.workflowAccessor.LoadOne(v)
|
||||
data, code, _ := a.workflowAccessor.LoadOne(v)
|
||||
if code == 200 {
|
||||
s := data.(*w.Workflow)
|
||||
if !slices.Contains(s.Shared, id) {
|
||||
s.Shared = append(s.Shared, id)
|
||||
workflow, code, _ := wfa.workflowAccessor.UpdateOne(s, v)
|
||||
if wfa.Caller != nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.WORKFLOW] == nil {
|
||||
workflow, code, _ := a.workflowAccessor.UpdateOne(s, v)
|
||||
if a.Caller != nil || a.Caller.URLS == nil || a.Caller.URLS[tools.WORKFLOW] == nil {
|
||||
continue
|
||||
}
|
||||
paccess := (&peer.Peer{})
|
||||
@@ -152,9 +235,9 @@ func (wfa *collaborativeAreaMongoAccessor) sharedWorkflow(shared *CollaborativeA
|
||||
if code == 200 {
|
||||
s := workflow.Serialize(workflow) // add the shared workflow on the peer
|
||||
s["name"] = fmt.Sprintf("%v", s["name"]) + "_" + k
|
||||
b, err := paccess.LaunchPeerExecution(k, shared.UUID, tools.WORKFLOW, tools.POST, s, wfa.Caller)
|
||||
b, err := paccess.LaunchPeerExecution(k, shared.UUID, tools.WORKFLOW, tools.POST, s, a.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
a.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,207 +250,29 @@ func (wfa *collaborativeAreaMongoAccessor) sharedWorkflow(shared *CollaborativeA
|
||||
}
|
||||
|
||||
// sharedWorkspace is a function that shares the collaborative area to the peers
|
||||
func (wfa *collaborativeAreaMongoAccessor) deleteToPeer(shared *CollaborativeArea) {
|
||||
if wfa.Caller == nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.COLLABORATIVE_AREA] == nil || wfa.Caller.Disabled {
|
||||
return
|
||||
}
|
||||
paccess := (&peer.Peer{})
|
||||
for k, _ := range shared.AllowedPeersGroup {
|
||||
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: k}}).IsMySelf(); ok {
|
||||
continue
|
||||
}
|
||||
b, err := paccess.LaunchPeerExecution(k, shared.UUID, tools.COLLABORATIVE_AREA, tools.DELETE, nil, wfa.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
func (a *collaborativeAreaMongoAccessor) deleteToPeer(shared *CollaborativeArea) {
|
||||
a.contactPeer(shared, tools.POST)
|
||||
}
|
||||
|
||||
// sharedWorkspace is a function that shares the collaborative area to the peers
|
||||
func (wfa *collaborativeAreaMongoAccessor) sendToPeer(shared *CollaborativeArea) {
|
||||
if wfa.Caller == nil || wfa.Caller.URLS == nil || wfa.Caller.URLS[tools.COLLABORATIVE_AREA] == nil || wfa.Caller.Disabled {
|
||||
func (a *collaborativeAreaMongoAccessor) sendToPeer(shared *CollaborativeArea) {
|
||||
a.contactPeer(shared, tools.POST)
|
||||
}
|
||||
|
||||
func (a *collaborativeAreaMongoAccessor) contactPeer(shared *CollaborativeArea, meth tools.METHOD) {
|
||||
if a.Caller == nil || a.Caller.URLS == nil || a.Caller.URLS[tools.COLLABORATIVE_AREA] == nil || a.Caller.Disabled {
|
||||
return
|
||||
}
|
||||
|
||||
paccess := (&peer.Peer{})
|
||||
for k := range shared.AllowedPeersGroup {
|
||||
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: k}}).IsMySelf(); ok || shared.IsSent {
|
||||
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: k}}).IsMySelf(); ok || (shared.IsSent && meth == tools.POST) || (!shared.IsSent && meth != tools.POST) {
|
||||
continue
|
||||
}
|
||||
shared.IsSent = true
|
||||
b, err := paccess.LaunchPeerExecution(k, k, tools.COLLABORATIVE_AREA, tools.POST, shared.Serialize(shared), wfa.Caller)
|
||||
shared.IsSent = meth == tools.POST
|
||||
b, err := paccess.LaunchPeerExecution(k, k, tools.COLLABORATIVE_AREA, meth, shared.Serialize(shared), a.Caller)
|
||||
if err != nil && b == nil {
|
||||
wfa.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
a.Logger.Error().Msg("Could not send to peer " + k + ". Error: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateOne updates a collaborative area in the database, given its ID and the new data, it automatically share to peers if the workspace is shared
|
||||
func (wfa *collaborativeAreaMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
res, code, err := wfa.GenericUpdateOne(set.(*CollaborativeArea).Trim(), id, wfa, &CollaborativeArea{})
|
||||
// wfa.deleteToPeer(res.(*CollaborativeArea)) // delete the collaborative area on the peer
|
||||
wfa.sharedWorkflow(res.(*CollaborativeArea), id) // replace all shared workflows
|
||||
wfa.sharedWorkspace(res.(*CollaborativeArea), id) // replace all collaborative areas (not shared worspace obj but workspace one)
|
||||
// wfa.sendToPeer(res.(*CollaborativeArea)) // send the collaborative area (collaborative area object) to the peers
|
||||
return res, code, err
|
||||
}
|
||||
|
||||
// StoreOne stores a collaborative area in the database, it automatically share to peers if the workspace is shared
|
||||
func (wfa *collaborativeAreaMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
_, id := (&peer.Peer{}).IsMySelf() // get the local peer
|
||||
data.(*CollaborativeArea).CreatorID = id // set the creator id
|
||||
// add the creator id to the peers
|
||||
if config.GetConfig().Whitelist {
|
||||
data.(*CollaborativeArea).AllowedPeersGroup[id] = []string{"*"}
|
||||
} else {
|
||||
data.(*CollaborativeArea).AllowedPeersGroup[id] = []string{}
|
||||
}
|
||||
// then reset the shared fields
|
||||
if data.(*CollaborativeArea).Workspaces == nil {
|
||||
data.(*CollaborativeArea).Workspaces = []string{}
|
||||
}
|
||||
if data.(*CollaborativeArea).Workflows == nil {
|
||||
data.(*CollaborativeArea).Workflows = []string{}
|
||||
}
|
||||
if data.(*CollaborativeArea).Rules == nil {
|
||||
data.(*CollaborativeArea).Rules = []string{}
|
||||
}
|
||||
if data.(*CollaborativeArea).CollaborativeAreaRule == nil {
|
||||
data.(*CollaborativeArea).CollaborativeAreaRule = &CollaborativeAreaRule{
|
||||
ShareMode: "private",
|
||||
ExploitedBy: "collaborators only",
|
||||
}
|
||||
}
|
||||
data.(*CollaborativeArea).CollaborativeAreaRule.CreatedAt = time.Now().UTC()
|
||||
// retrieve or proper peer
|
||||
dd, code, err := wfa.peerAccessor.Search(nil, "0")
|
||||
if code != 200 || len(dd) == 0 {
|
||||
return nil, code, errors.New("Could not retrieve the peer" + err.Error())
|
||||
}
|
||||
data.(*CollaborativeArea).CollaborativeAreaRule.Creator = dd[0].GetID()
|
||||
d, code, err := wfa.GenericStoreOne(data.(*CollaborativeArea).Trim(), wfa)
|
||||
if code == 200 {
|
||||
wfa.sharedWorkflow(d.(*CollaborativeArea), d.GetID()) // create all shared workflows
|
||||
wfa.sharedWorkspace(d.(*CollaborativeArea), d.GetID()) // create all collaborative areas
|
||||
wfa.sendToPeer(d.(*CollaborativeArea)) // send the collaborative area (collaborative area object) to the peers
|
||||
}
|
||||
return data, code, err
|
||||
}
|
||||
|
||||
// CopyOne copies a CollaborativeArea in the database
|
||||
func (wfa *collaborativeAreaMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.StoreOne(data)
|
||||
}
|
||||
|
||||
// enrich is a function that enriches the CollaborativeArea with the shared objects
|
||||
func (wfa *collaborativeAreaMongoAccessor) enrich(sharedWorkspace *CollaborativeArea) *CollaborativeArea {
|
||||
res, code, _ := wfa.workspaceAccessor.Search(&dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Workspaces}},
|
||||
},
|
||||
}, "")
|
||||
if code == 200 {
|
||||
for _, r := range res {
|
||||
sharedWorkspace.SharedWorkspaces = append(sharedWorkspace.SharedWorkspaces, r.(*workspace.Workspace))
|
||||
}
|
||||
}
|
||||
res, code, _ = wfa.workflowAccessor.Search(&dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Workflows}},
|
||||
},
|
||||
}, "")
|
||||
if code == 200 {
|
||||
for _, r := range res {
|
||||
sharedWorkspace.SharedWorkflows = append(sharedWorkspace.SharedWorkflows, r.(*w.Workflow))
|
||||
}
|
||||
}
|
||||
peerskey := []string{}
|
||||
for k := range sharedWorkspace.AllowedPeersGroup {
|
||||
peerskey = append(peerskey, k)
|
||||
}
|
||||
res, code, _ = wfa.peerAccessor.Search(&dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.id": {{Operator: dbs.IN.String(), Value: peerskey}},
|
||||
},
|
||||
}, "")
|
||||
if code == 200 {
|
||||
for _, r := range res {
|
||||
sharedWorkspace.SharedPeers = append(sharedWorkspace.SharedPeers, r.(*peer.Peer))
|
||||
}
|
||||
}
|
||||
res, code, _ = wfa.ruleAccessor.Search(&dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractobject.id": {{Operator: dbs.IN.String(), Value: sharedWorkspace.Rules}},
|
||||
},
|
||||
}, "")
|
||||
if code == 200 {
|
||||
for _, r := range res {
|
||||
sharedWorkspace.SharedRules = append(sharedWorkspace.SharedRules, r.(*rule.Rule))
|
||||
}
|
||||
}
|
||||
return sharedWorkspace
|
||||
}
|
||||
|
||||
// LoadOne loads a collaborative area from the database, given its ID and enrich it
|
||||
func (wfa *collaborativeAreaMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
var sharedWorkspace CollaborativeArea
|
||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType().String())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
res_mongo.Decode(&sharedWorkspace)
|
||||
if !sharedWorkspace.VerifyAuth(wfa.PeerID, wfa.Groups) {
|
||||
return nil, 403, errors.New("You are not allowed to access this collaborative area")
|
||||
}
|
||||
return wfa.enrich(&sharedWorkspace), 200, nil // enrich the collaborative area
|
||||
}
|
||||
|
||||
// LoadAll loads all the collaborative areas from the database and enrich them
|
||||
func (wfa collaborativeAreaMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType().String())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []CollaborativeArea
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
if !r.VerifyAuth(wfa.PeerID, wfa.Groups) {
|
||||
continue
|
||||
}
|
||||
objs = append(objs, wfa.enrich(&r)) // enrich the collaborative area
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
||||
|
||||
// Search searches for collaborative areas in the database, given some filters OR a search string
|
||||
func (wfa *collaborativeAreaMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||
filters = &dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{ // search by name only by default can be override
|
||||
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
||||
},
|
||||
}
|
||||
}
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType().String())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []CollaborativeArea
|
||||
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
if !r.VerifyAuth(wfa.PeerID, wfa.Groups) {
|
||||
continue
|
||||
}
|
||||
objs = append(objs, wfa.enrich(&r)) // enrich the collaborative area
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
||||
|
||||
@@ -26,30 +26,30 @@ func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCal
|
||||
}
|
||||
|
||||
// GetType returns the type of the rule
|
||||
func (wfa *ruleMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return wfa.GenericDeleteOne(id, wfa)
|
||||
func (a *ruleMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
// UpdateOne updates a rule in the database
|
||||
func (wfa *ruleMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return wfa.GenericUpdateOne(set.(*Rule), id, wfa, &Rule{})
|
||||
func (a *ruleMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return utils.GenericUpdateOne(set.(*Rule), id, a, &Rule{})
|
||||
}
|
||||
|
||||
// StoreOne stores a rule in the database
|
||||
func (wfa *ruleMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.GenericStoreOne(data.(*Rule), wfa)
|
||||
func (a *ruleMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data.(*Rule), a)
|
||||
}
|
||||
|
||||
func (wfa *ruleMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.GenericStoreOne(data, wfa)
|
||||
func (a *ruleMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return utils.GenericStoreOne(data, a)
|
||||
}
|
||||
|
||||
// LoadOne loads a rule from the database
|
||||
func (wfa *ruleMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
func (a *ruleMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
var rule Rule
|
||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType().String())
|
||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, a.GetType().String())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||
a.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
res_mongo.Decode(&rule)
|
||||
@@ -57,11 +57,11 @@ func (wfa *ruleMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
}
|
||||
|
||||
// LoadAll loads all rules from the database
|
||||
func (wfa ruleMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
||||
func (a ruleMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType().String())
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(a.GetType().String())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
a.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []Rule
|
||||
@@ -75,7 +75,7 @@ func (wfa ruleMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
||||
}
|
||||
|
||||
// Search searches for rules in the database, given some filters OR a search string
|
||||
func (wfa *ruleMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
|
||||
func (a *ruleMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||
filters = &dbs.Filters{
|
||||
@@ -84,9 +84,9 @@ func (wfa *ruleMongoAccessor) Search(filters *dbs.Filters, search string) ([]uti
|
||||
},
|
||||
}
|
||||
}
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType().String())
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, a.GetType().String())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
|
||||
a.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
var results []Rule
|
||||
|
||||
+3
-3
@@ -24,15 +24,15 @@ func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCal
|
||||
}
|
||||
|
||||
func (a *shallowSharedWorkspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return a.GenericDeleteOne(id, a)
|
||||
return utils.GenericDeleteOne(id, a)
|
||||
}
|
||||
|
||||
func (a *shallowSharedWorkspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return a.GenericUpdateOne(set.(*ShallowCollaborativeArea), id, a, &ShallowCollaborativeArea{})
|
||||
return utils.GenericUpdateOne(set.(*ShallowCollaborativeArea), id, a, &ShallowCollaborativeArea{})
|
||||
}
|
||||
|
||||
func (a *shallowSharedWorkspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return a.GenericStoreOne(data.(*ShallowCollaborativeArea), a)
|
||||
return utils.GenericStoreOne(data.(*ShallowCollaborativeArea), a)
|
||||
}
|
||||
|
||||
func (a *shallowSharedWorkspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
|
||||
Reference in New Issue
Block a user