oc-lib/models/utils/abstracts.go

267 lines
8.6 KiB
Go
Raw Normal View History

2024-07-18 11:51:12 +02:00
package utils
import (
2024-08-05 10:10:58 +02:00
"encoding/json"
2024-07-31 10:35:03 +02:00
"errors"
2024-11-28 14:04:58 +01:00
"fmt"
2024-10-02 12:23:22 +02:00
"time"
2024-07-26 16:41:08 +02:00
2024-08-01 09:13:10 +02:00
"cloud.o-forge.io/core/oc-lib/dbs"
2024-07-18 13:35:14 +02:00
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/tools"
2024-07-18 18:14:12 +02:00
"github.com/go-playground/validator/v10"
2024-07-19 10:54:58 +02:00
"github.com/google/uuid"
2024-07-18 11:51:12 +02:00
"github.com/rs/zerolog"
2024-11-28 13:19:01 +01:00
mgb "go.mongodb.org/mongo-driver/mongo"
2024-07-18 11:51:12 +02:00
)
// single instance of the validator used in every model Struct to validate the fields
2024-07-19 10:54:58 +02:00
var validate = validator.New(validator.WithRequiredStructEnabled())
2024-12-04 12:14:55 +01:00
type AccessMode int
const (
Private AccessMode = iota
Public
)
/*
* AbstractObject is a struct that represents the basic fields of an object
* it defines the object id and name
* every data in base root model should inherit from this struct (only exception is the ResourceModel)
*/
2024-07-19 10:54:58 +02:00
type AbstractObject struct {
2024-12-04 12:14:55 +01:00
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`
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"`
AccessMode AccessMode `json:"access_mode" bson:"access_mode" default:"0"`
2024-07-19 10:54:58 +02:00
}
2024-11-29 10:25:42 +01:00
func (r *AbstractObject) GenerateID() {
if r.UUID == "" {
r.UUID = uuid.New().String()
}
2024-11-28 16:49:41 +01:00
}
// GetID implements ShallowDBObject.
func (ao AbstractObject) GetID() string {
2024-07-19 13:27:34 +02:00
return ao.UUID
}
2024-11-28 16:49:41 +01:00
// GetName implements ShallowDBObject.
func (ao AbstractObject) GetName() string {
2024-07-23 11:22:50 +02:00
return ao.Name
}
2024-12-04 12:14:55 +01:00
func (ao *AbstractObject) UpToDate(user string, create bool) {
2024-10-02 12:23:22 +02:00
ao.UpdateDate = time.Now()
2024-12-04 12:14:55 +01:00
ao.LastPeerWriter = user
if create {
ao.CreatorID = user
}
2024-10-02 12:23:22 +02:00
}
2024-12-04 11:33:08 +01:00
func (ao *AbstractObject) VerifyAuth(username string, peerID string, groups []string) bool {
2024-12-04 12:14:55 +01:00
return ao.AccessMode == Public || ao.CreatorID == username
2024-11-28 11:05:54 +01:00
}
2024-11-29 10:25:42 +01:00
func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters {
return &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
}}
}
2024-11-28 11:05:54 +01:00
func (dma *AbstractObject) Deserialize(j map[string]interface{}, obj DBObject) DBObject {
2024-08-05 10:10:58 +02:00
b, err := json.Marshal(j)
if err != nil {
return nil
}
2024-11-28 11:05:54 +01:00
json.Unmarshal(b, obj)
return obj
2024-08-05 10:10:58 +02:00
}
2024-11-28 11:05:54 +01:00
func (dma *AbstractObject) Serialize(obj DBObject) map[string]interface{} {
2024-08-05 10:10:58 +02:00
var m map[string]interface{}
2024-11-28 11:05:54 +01:00
b, err := json.Marshal(obj)
2024-08-05 10:10:58 +02:00
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}
2024-07-18 11:51:12 +02:00
type AbstractAccessor struct {
2024-12-04 11:33:08 +01:00
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
2024-11-28 11:05:54 +01:00
ResourceModelAccessor Accessor
}
2024-12-04 11:33:08 +01:00
func (dma *AbstractAccessor) GetUser() string {
return dma.User
}
2024-11-29 10:25:42 +01:00
func (dma *AbstractAccessor) GetPeerID() string {
return dma.PeerID
}
func (dma *AbstractAccessor) GetGroups() []string {
return dma.Groups
}
func (dma *AbstractAccessor) GetLogger() *zerolog.Logger {
return &dma.Logger
}
2024-11-28 11:05:54 +01:00
func (dma *AbstractAccessor) VerifyAuth() string {
return ""
2024-07-18 11:51:12 +02:00
}
2024-11-28 16:49:41 +01:00
func (dma *AbstractAccessor) GetType() tools.DataType {
2024-07-18 16:46:54 +02:00
return dma.Type
}
func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
return dma.Caller
}
// GenericLoadOne loads one object from the database (generic)
2024-11-29 10:25:42 +01:00
func GenericStoreOne(data DBObject, a Accessor) (DBObject, int, error) {
2024-07-19 11:00:15 +02:00
data.GenerateID()
2024-12-04 12:14:55 +01:00
data.UpToDate(a.GetUser(), true)
2024-08-01 09:13:10 +02:00
f := dbs.Filters{
2024-08-02 14:07:43 +02:00
Or: map[string][]dbs.Filter{
"abstractresource.abstractobject.name": {{
2024-08-01 09:13:10 +02:00
Operator: dbs.LIKE.String(),
Value: data.GetName(),
2024-08-02 14:07:43 +02:00
}},
"abstractobject.name": {{
2024-08-01 09:13:10 +02:00
Operator: dbs.LIKE.String(),
Value: data.GetName(),
2024-08-02 14:07:43 +02:00
}},
2024-08-01 09:13:10 +02:00
},
}
2024-12-04 11:33:08 +01:00
if !data.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
2024-12-02 09:11:45 +01:00
return nil, 403, errors.New("You are not allowed to access this collaborative area")
}
2024-11-29 10:25:42 +01:00
if cursor, _, _ := a.Search(&f, ""); len(cursor) > 0 {
return nil, 409, errors.New(a.GetType().String() + " with name " + data.GetName() + " already exists")
2024-07-31 10:35:03 +02:00
}
2024-07-19 10:54:58 +02:00
err := validate.Struct(data)
if err != nil {
2024-07-19 11:27:58 +02:00
return nil, 422, err
2024-07-19 10:54:58 +02:00
}
2024-11-29 10:25:42 +01:00
id, code, err := mongo.MONGOService.StoreOne(data, data.GetID(), a.GetType().String())
2024-07-19 10:54:58 +02:00
if err != nil {
2024-11-29 10:25:42 +01:00
a.GetLogger().Error().Msg("Could not store " + data.GetName() + " to db. Error: " + err.Error())
2024-07-19 11:27:58 +02:00
return nil, code, err
2024-07-19 10:54:58 +02:00
}
2024-11-29 10:25:42 +01:00
return a.LoadOne(id)
2024-07-19 10:54:58 +02:00
}
// GenericLoadOne loads one object from the database (generic)
2024-11-29 10:25:42 +01:00
func GenericDeleteOne(id string, a Accessor) (DBObject, int, error) {
res, code, err := a.LoadOne(id)
2024-07-18 15:35:30 +02:00
if err != nil {
2024-11-29 10:25:42 +01:00
a.GetLogger().Error().Msg("Could not retrieve " + id + " to db. Error: " + err.Error())
2024-07-19 11:27:58 +02:00
return nil, code, err
2024-07-18 15:35:30 +02:00
}
2024-12-04 11:33:08 +01:00
if !res.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
2024-12-02 09:11:45 +01:00
return nil, 403, errors.New("You are not allowed to access this collaborative area")
}
2024-11-29 10:25:42 +01:00
_, code, err = mongo.MONGOService.DeleteOne(id, a.GetType().String())
2024-07-18 11:51:12 +02:00
if err != nil {
2024-11-29 10:25:42 +01:00
a.GetLogger().Error().Msg("Could not delete " + id + " to db. Error: " + err.Error())
2024-07-19 11:27:58 +02:00
return nil, code, err
2024-07-18 11:51:12 +02:00
}
2024-07-19 11:27:58 +02:00
return res, 200, nil
2024-07-18 11:51:12 +02:00
}
// GenericLoadOne loads one object from the database (generic)
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
2024-11-29 10:25:42 +01:00
func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObject, int, error) {
r, c, err := a.LoadOne(id)
2024-07-23 08:40:24 +02:00
if err != nil {
return nil, c, err
}
2024-12-04 12:14:55 +01:00
r.UpToDate(a.GetUser(), false)
2024-12-04 11:33:08 +01:00
if !r.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
2024-12-02 09:11:45 +01:00
return nil, 403, errors.New("You are not allowed to access this collaborative area")
}
2024-11-28 11:05:54 +01:00
change := set.Serialize(set) // get the changes
loaded := r.Serialize(r) // get the loaded object
2024-07-24 14:49:04 +02:00
for k, v := range change { // apply the changes, with a flatten method
2024-07-24 14:49:04 +02:00
loaded[k] = v
}
2024-11-29 10:25:42 +01:00
id, code, err := mongo.MONGOService.UpdateOne(new.Deserialize(loaded, new), id, a.GetType().String())
2024-07-18 11:51:12 +02:00
if err != nil {
2024-11-29 10:25:42 +01:00
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
2024-07-19 11:27:58 +02:00
return nil, code, err
2024-07-18 11:51:12 +02:00
}
2024-11-29 10:25:42 +01:00
return a.LoadOne(id)
2024-07-18 11:51:12 +02:00
}
2024-11-12 12:45:03 +01:00
2024-11-29 10:25:42 +01:00
func GenericLoadOne[T DBObject](id string, f func(DBObject) (DBObject, int, error), a Accessor) (DBObject, int, error) {
2024-11-28 11:05:54 +01:00
var data T
2024-11-29 10:25:42 +01:00
res_mongo, code, err := mongo.MONGOService.LoadOne(id, a.GetType().String())
2024-11-28 11:05:54 +01:00
if err != nil {
2024-11-29 10:25:42 +01:00
a.GetLogger().Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
2024-11-28 11:05:54 +01:00
return nil, code, err
}
res_mongo.Decode(&data)
2024-12-04 11:33:08 +01:00
if !data.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
2024-12-02 09:11:45 +01:00
return nil, 403, errors.New("You are not allowed to access this collaborative area")
}
2024-11-28 11:05:54 +01:00
return f(data)
}
2024-11-29 10:25:42 +01:00
func genericLoadAll[T DBObject](res *mgb.Cursor, code int, err error, f func(DBObject) ShallowDBObject, a Accessor) ([]ShallowDBObject, int, error) {
2024-11-28 11:05:54 +01:00
objs := []ShallowDBObject{}
2024-12-03 09:25:27 +01:00
var results []T
2024-11-28 11:05:54 +01:00
if err != nil {
2024-11-29 10:25:42 +01:00
a.GetLogger().Error().Msg("Could not retrieve any from db. Error: " + err.Error())
2024-11-28 11:05:54 +01:00
return nil, code, err
}
2024-11-28 13:19:01 +01:00
if err = res.All(mongo.MngoCtx, &results); err != nil {
2024-11-28 11:05:54 +01:00
return nil, 404, err
}
for _, r := range results {
2024-12-04 11:33:08 +01:00
if !r.VerifyAuth(a.GetUser(), a.GetPeerID(), a.GetGroups()) {
2024-11-28 11:05:54 +01:00
continue
}
2024-11-28 13:19:01 +01:00
objs = append(objs, f(r))
2024-11-28 11:05:54 +01:00
}
return objs, 200, nil
}
2024-11-28 13:19:01 +01:00
func GenericLoadAll[T DBObject](f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
2024-11-28 16:49:41 +01:00
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType().String())
2024-12-02 16:59:08 +01:00
fmt.Println("res_mongo", res_mongo)
2024-11-28 13:19:01 +01:00
return genericLoadAll[T](res_mongo, code, err, f, wfa)
}
2024-11-28 11:05:54 +01:00
func GenericSearch[T DBObject](filters *dbs.Filters, search string, defaultFilters *dbs.Filters,
2024-11-28 13:19:01 +01:00
f func(DBObject) ShallowDBObject, wfa Accessor) ([]ShallowDBObject, int, error) {
2024-11-28 11:05:54 +01:00
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = defaultFilters
}
2024-11-28 16:49:41 +01:00
res_mongo, code, err := mongo.MONGOService.Search(filters, wfa.GetType().String())
2024-11-28 13:19:01 +01:00
return genericLoadAll[T](res_mongo, code, err, f, wfa)
2024-11-28 11:05:54 +01:00
}
2024-11-12 12:45:03 +01:00
// GenericLoadOne loads one object from the database (generic)
// json expected in entry is a flatted object no need to respect the inheritance hierarchy
2024-11-29 10:25:42 +01:00
func GenericRawUpdateOne(set DBObject, id string, a Accessor) (DBObject, int, error) {
id, code, err := mongo.MONGOService.UpdateOne(set, id, a.GetType().String())
2024-11-12 12:45:03 +01:00
if err != nil {
2024-11-29 10:25:42 +01:00
a.GetLogger().Error().Msg("Could not update " + id + " to db. Error: " + err.Error())
2024-11-12 12:45:03 +01:00
return nil, code, err
}
2024-11-29 10:25:42 +01:00
return a.LoadOne(id)
2024-11-28 11:05:54 +01:00
}