massive draft for payment process (UNCOMPLETE)
This commit is contained in:
@@ -2,17 +2,13 @@ package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog"
|
||||
mgb "go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
// single instance of the validator used in every model Struct to validate the fields
|
||||
@@ -33,6 +29,7 @@ const (
|
||||
type AbstractObject struct {
|
||||
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
|
||||
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
|
||||
IsDraft bool `json:"is_draft" bson:"is_draft" default:"false"`
|
||||
UpdateDate time.Time `json:"update_date" bson:"update_date"`
|
||||
LastPeerWriter string `json:"last_peer_writer" bson:"last_peer_writer"`
|
||||
CreatorID string `json:"creator_id" bson:"creator_id" default:"unknown"`
|
||||
@@ -45,6 +42,22 @@ func (r *AbstractObject) GenerateID() {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *AbstractObject) StoreDraftDefault() {
|
||||
r.IsDraft = false
|
||||
}
|
||||
|
||||
func (r *AbstractObject) CanUpdate(set DBObject) (bool, DBObject) {
|
||||
return true, set
|
||||
}
|
||||
|
||||
func (r *AbstractObject) CanDelete() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (r *AbstractObject) IsDrafted() bool {
|
||||
return r.IsDraft
|
||||
}
|
||||
|
||||
// GetID implements ShallowDBObject.
|
||||
func (ao AbstractObject) GetID() string {
|
||||
return ao.UUID
|
||||
@@ -63,8 +76,8 @@ func (ao *AbstractObject) UpToDate(user string, create bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func (ao *AbstractObject) VerifyAuth(username string, peerID string, groups []string) bool {
|
||||
return ao.AccessMode == Public || ao.CreatorID == username
|
||||
func (ao *AbstractObject) VerifyAuth(request *tools.APIRequest) bool {
|
||||
return ao.AccessMode == Public || (request != nil && ao.CreatorID == request.Username)
|
||||
}
|
||||
|
||||
func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters {
|
||||
@@ -96,171 +109,43 @@ 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
|
||||
User string // User is the user that is using the accessor
|
||||
Request *tools.APIRequest // Caller is the http caller of the accessor (optionnal) only need in a peer connection
|
||||
ResourceModelAccessor Accessor
|
||||
}
|
||||
|
||||
func (r *AbstractAccessor) GetRequest() *tools.APIRequest {
|
||||
return r.Request
|
||||
}
|
||||
|
||||
func (dma *AbstractAccessor) GetUser() string {
|
||||
return dma.User
|
||||
if dma.Request == nil {
|
||||
return ""
|
||||
}
|
||||
return dma.Request.Username
|
||||
}
|
||||
|
||||
func (dma *AbstractAccessor) GetPeerID() string {
|
||||
return dma.PeerID
|
||||
if dma.Request == nil {
|
||||
return ""
|
||||
}
|
||||
return dma.Request.PeerID
|
||||
}
|
||||
func (dma *AbstractAccessor) GetGroups() []string {
|
||||
return dma.Groups
|
||||
if dma.Request == nil {
|
||||
return []string{}
|
||||
}
|
||||
return dma.Request.Groups
|
||||
}
|
||||
func (dma *AbstractAccessor) GetLogger() *zerolog.Logger {
|
||||
return &dma.Logger
|
||||
}
|
||||
|
||||
func (dma *AbstractAccessor) VerifyAuth() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (dma *AbstractAccessor) GetType() tools.DataType {
|
||||
return dma.Type
|
||||
}
|
||||
|
||||
func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
|
||||
return dma.Caller
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
|
||||
data.GenerateID()
|
||||
data.UpToDate(a.GetUser(), true)
|
||||
f := dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractresource.abstractobject.name": {{
|
||||
Operator: dbs.LIKE.String(),
|
||||
Value: data.GetName(),
|
||||
}},
|
||||
"abstractobject.name": {{
|
||||
Operator: dbs.LIKE.String(),
|
||||
Value: data.GetName(),
|
||||
}},
|
||||
},
|
||||
}
|
||||
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 {
|
||||
return nil, 409, errors.New(a.GetType().String() + " with name " + data.GetName() + " already exists")
|
||||
}
|
||||
err := validate.Struct(data)
|
||||
if err != nil {
|
||||
return nil, 422, err
|
||||
}
|
||||
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), a.GetType().String())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
return a.LoadOne(id)
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
|
||||
res, code, err := a.LoadOne(id)
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
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())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
return res, 200, nil
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
|
||||
func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObject, int, error) {
|
||||
r, c, err := a.LoadOne(id)
|
||||
if err != nil {
|
||||
return nil, c, err
|
||||
}
|
||||
r.UpToDate(a.GetUser(), false)
|
||||
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
|
||||
loaded := r.Serialize(r) // get the loaded object
|
||||
|
||||
for k, v := range change { // apply the changes, with a flatten method
|
||||
loaded[k] = v
|
||||
}
|
||||
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded, new), id, a.GetType().String())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
return a.LoadOne(id)
|
||||
}
|
||||
|
||||
func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, error), a Accessor) (DBObject, int, error) {
|
||||
var data T
|
||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, a.GetType().String())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
res_mongo.Decode(&data)
|
||||
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)
|
||||
}
|
||||
|
||||
func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, f func(DBObject) ShallowDBObject, a Accessor) ([]ShallowDBObject, int, error) {
|
||||
objs := []ShallowDBObject{}
|
||||
var results []T
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
if err = res.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
if !r.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
|
||||
continue
|
||||
}
|
||||
objs = append(objs, f(r))
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
||||
|
||||
func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType().String())
|
||||
fmt.Println("res_mongo", res_mongo)
|
||||
return genericLoadAll[T](res_mongo, code, err, f, wfa)
|
||||
}
|
||||
|
||||
func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilters *dbs.Filters,
|
||||
f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||
filters = defaultFilters
|
||||
}
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType().String())
|
||||
return genericLoadAll[T](res_mongo, code, err, f, wfa)
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
|
||||
func GenericRawUpdateOne(set DBObject, id string, a Accessor) (DBObject, int, error) {
|
||||
id, code, err := mongo.MONGOService.UpdateOne(set, id, a.GetType().String())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
return a.LoadOne(id)
|
||||
if dma.Request == nil {
|
||||
return nil
|
||||
}
|
||||
return dma.Request.Caller
|
||||
}
|
||||
|
@@ -1,8 +1,160 @@
|
||||
package utils
|
||||
|
||||
/*
|
||||
type Price struct {
|
||||
Price float64 `json:"price,omitempty" bson:"price,omitempty"`
|
||||
Currency string `json:"currency,omitempty" bson:"currency,omitempty"`
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
mgb "go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type Owner struct {
|
||||
Name string `json:"name,omitempty" bson:"name,omitempty"`
|
||||
Logo string `json:"logo,omitempty" bson:"logo,omitempty"`
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
|
||||
data.GenerateID()
|
||||
data.StoreDraftDefault()
|
||||
data.UpToDate(a.GetUser(), true)
|
||||
f := dbs.Filters{
|
||||
Or: map[string][]dbs.Filter{
|
||||
"abstractresource.abstractobject.name": {{
|
||||
Operator: dbs.LIKE.String(),
|
||||
Value: data.GetName(),
|
||||
}},
|
||||
"abstractobject.name": {{
|
||||
Operator: dbs.LIKE.String(),
|
||||
Value: data.GetName(),
|
||||
}},
|
||||
},
|
||||
}
|
||||
if !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 {
|
||||
return nil, 409, errors.New(a.GetType().String() + " with name " + data.GetName() + " already exists")
|
||||
}
|
||||
err := validate.Struct(data)
|
||||
if err != nil {
|
||||
return nil, 422, err
|
||||
}
|
||||
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), a.GetType().String())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
return a.LoadOne(id)
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
|
||||
res, code, err := a.LoadOne(id)
|
||||
if !res.CanDelete() {
|
||||
return nil, 403, errors.New("you are not allowed to delete this collaborative area")
|
||||
}
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
if !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())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
return res, 200, nil
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
|
||||
func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObject, int, error) {
|
||||
r, c, err := a.LoadOne(id)
|
||||
if err != nil {
|
||||
return nil, c, err
|
||||
}
|
||||
ok, newSet := r.CanUpdate(set)
|
||||
if !ok {
|
||||
return nil, 403, errors.New("you are not allowed to delete this collaborative area")
|
||||
}
|
||||
set = newSet
|
||||
r.UpToDate(a.GetUser(), false)
|
||||
if !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
|
||||
loaded := r.Serialize(r) // get the loaded object
|
||||
|
||||
for k, v := range change { // apply the changes, with a flatten method
|
||||
loaded[k] = v
|
||||
}
|
||||
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded, new), id, a.GetType().String())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
return a.LoadOne(id)
|
||||
}
|
||||
|
||||
func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, error), a Accessor) (DBObject, int, error) {
|
||||
var data T
|
||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, a.GetType().String())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
res_mongo.Decode(&data)
|
||||
if !data.VerifyAuth(a.GetRequest()) {
|
||||
return nil, 403, errors.New("you are not allowed to access this collaborative area")
|
||||
}
|
||||
return f(data)
|
||||
}
|
||||
|
||||
func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, onlyDraft bool, f func(DBObject) ShallowDBObject, a Accessor) ([]ShallowDBObject, int, error) {
|
||||
objs := []ShallowDBObject{}
|
||||
var results []T
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
if err = res.All(mongo.MngoCtx, &results); err != nil {
|
||||
return nil, 404, err
|
||||
}
|
||||
for _, r := range results {
|
||||
if !r.VerifyAuth(a.GetRequest()) || f(r) == nil || (onlyDraft && !r.IsDrafted()) || (!onlyDraft && r.IsDrafted()) {
|
||||
continue
|
||||
}
|
||||
objs = append(objs, f(r))
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
||||
|
||||
func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, onlyDraft bool, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType().String())
|
||||
fmt.Println("res_mongo", res_mongo)
|
||||
return genericLoadAll[T](res_mongo, code, err, onlyDraft, f, wfa)
|
||||
}
|
||||
|
||||
func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilters *dbs.Filters,
|
||||
f func(DBObject) ShallowDBObject, onlyDraft bool, wfa Accessor) ([]ShallowDBObject, int, error) {
|
||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||
filters = defaultFilters
|
||||
}
|
||||
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType().String())
|
||||
return genericLoadAll[T](res_mongo, code, err, onlyDraft, f, wfa)
|
||||
}
|
||||
|
||||
// GenericLoadOne loads one object from the database (generic)
|
||||
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
|
||||
func GenericRawUpdateOne(set DBObject, id string, a Accessor) (DBObject, int, error) {
|
||||
id, code, err := mongo.MONGOService.UpdateOne(set, id, a.GetType().String())
|
||||
if err != nil {
|
||||
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
return a.LoadOne(id)
|
||||
}
|
||||
*/
|
||||
|
@@ -20,23 +20,28 @@ type DBObject interface {
|
||||
GenerateID()
|
||||
GetID() string
|
||||
GetName() string
|
||||
IsDrafted() bool
|
||||
StoreDraftDefault()
|
||||
CanUpdate(set DBObject) (bool, DBObject)
|
||||
CanDelete() bool
|
||||
UpToDate(user string, create bool)
|
||||
VerifyAuth(username string, PeerID string, groups []string) bool
|
||||
VerifyAuth(request *tools.APIRequest) bool
|
||||
Deserialize(j map[string]interface{}, obj DBObject) DBObject
|
||||
Serialize(obj DBObject) map[string]interface{}
|
||||
GetAccessor(username string, peerID string, groups []string, caller *tools.HTTPCaller) Accessor
|
||||
GetAccessor(request *tools.APIRequest) Accessor
|
||||
}
|
||||
|
||||
// Accessor is an interface that defines the basic methods for an Accessor
|
||||
type Accessor interface {
|
||||
GetRequest() *tools.APIRequest
|
||||
GetType() tools.DataType
|
||||
GetUser() string
|
||||
GetPeerID() string
|
||||
GetGroups() []string
|
||||
GetLogger() *zerolog.Logger
|
||||
GetCaller() *tools.HTTPCaller
|
||||
Search(filters *dbs.Filters, search string) ([]ShallowDBObject, int, error)
|
||||
LoadAll() ([]ShallowDBObject, int, error)
|
||||
Search(filters *dbs.Filters, search string, isDraft bool) ([]ShallowDBObject, int, error)
|
||||
LoadAll(isDraft bool) ([]ShallowDBObject, int, error)
|
||||
LoadOne(id string) (DBObject, int, error)
|
||||
DeleteOne(id string) (DBObject, int, error)
|
||||
CopyOne(data DBObject) (DBObject, int, error)
|
||||
|
Reference in New Issue
Block a user