Files
oc-lib/models/utils/abstracts.go

271 lines
7.3 KiB
Go
Raw Normal View History

2024-07-18 11:51:12 +02:00
package utils
import (
2026-02-18 12:24:19 +01:00
"crypto/sha256"
2024-08-05 10:10:58 +02:00
"encoding/json"
2026-02-18 12:24:19 +01:00
"errors"
"slices"
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"
"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"
)
// 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 {
2026-02-05 15:30:23 +01:00
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"`
CreatorID string `json:"creator_id,omitempty" bson:"creator_id,omitempty"`
UserCreatorID string `json:"user_creator_id,omitempty" bson:"user_creator_id,omitempty"`
CreationDate time.Time `json:"creation_date,omitempty" bson:"creation_date,omitempty"`
UpdateDate time.Time `json:"update_date,omitempty" bson:"update_date,omitempty"`
UpdaterID string `json:"updater_id,omitempty" bson:"updater_id,omitempty"`
UserUpdaterID string `json:"user_updater_id,omitempty" bson:"user_updater_id,omitempty"`
AccessMode AccessMode `json:"access_mode" bson:"access_mode" default:"0"`
2026-02-18 12:24:19 +01:00
Signature []byte `bson:"signature,omitempty" json:"signature,omitempty"`
2024-07-19 10:54:58 +02:00
}
func (ri *AbstractObject) GetAccessor(request *tools.APIRequest) Accessor {
return nil
}
2026-02-18 12:24:19 +01:00
func (r *AbstractObject) Unsign() {
r.Signature = nil
}
2026-02-09 12:37:03 +01:00
2026-02-18 12:24:19 +01:00
func (r *AbstractObject) Sign() {
priv, err := tools.LoadKeyFromFilePrivate() // your node private key
if err != nil {
return
}
b, _ := json.Marshal(r.DeepCopy())
hash := sha256.Sum256(b)
r.Signature, err = priv.Sign(hash[:])
}
2026-02-09 12:37:03 +01:00
2025-06-24 11:29:04 +02:00
func (r *AbstractObject) SetID(id string) {
r.UUID = id
}
2026-02-18 12:24:19 +01:00
func (r *AbstractObject) DeepCopy() *AbstractObject {
var obj AbstractObject
b, err := json.Marshal(r)
if err != nil {
return nil
}
if err := json.Unmarshal(b, &obj); err != nil {
return nil
}
return &obj
}
2026-01-12 11:59:05 +01:00
func (r *AbstractObject) SetName(name string) {
r.Name = name
}
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
}
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
}
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
}
2026-02-18 12:24:19 +01:00
func (ao AbstractObject) GetSignature() []byte {
return ao.Signature
}
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
}
2025-01-13 11:24:07 +01:00
func (ao *AbstractObject) GetCreatorID() string {
return ao.CreatorID
}
func (ao *AbstractObject) UpToDate(user string, peer string, create bool) {
2024-10-02 12:23:22 +02:00
ao.UpdateDate = time.Now()
ao.UpdaterID = peer
ao.UserUpdaterID = user
2026-02-05 14:54:41 +01:00
if create && ao.CreatorID != "" {
ao.CreationDate = time.Now()
ao.CreatorID = peer
ao.UserCreatorID = user
2024-12-04 12:14:55 +01:00
}
2024-10-02 12:23:22 +02:00
}
2026-01-13 16:04:31 +01:00
func (ao *AbstractObject) VerifyAuth(callName string, request *tools.APIRequest) bool {
2026-02-18 12:24:19 +01:00
return (ao.AccessMode == Public && callName == "get") || (request != nil && (request.Admin || (ao.CreatorID == request.PeerID && request.PeerID != "")))
2024-11-28 11:05:54 +01:00
}
2026-01-22 15:55:27 +01:00
// TODO : check write per auth
2024-11-29 10:25:42 +01:00
func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters {
2025-01-17 14:54:17 +01:00
if search == "*" {
search = ""
}
2024-11-29 10:25:42 +01:00
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
}
2026-02-18 12:24:19 +01:00
type AbstractAccessor[T DBObject] 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
Request *tools.APIRequest // Caller is the http caller of the accessor (optionnal) only need in a peer connection
2024-11-28 11:05:54 +01:00
ResourceModelAccessor Accessor
2026-02-18 12:24:19 +01:00
New func() T
NotImplemented []string
2024-11-28 11:05:54 +01:00
}
2026-02-18 12:24:19 +01:00
func (r *AbstractAccessor[T]) ShouldVerifyAuth() bool {
2025-01-21 11:55:44 +01:00
return true
}
2026-02-18 12:24:19 +01:00
func (r *AbstractAccessor[T]) GetRequest() *tools.APIRequest {
return r.Request
}
2026-02-18 12:24:19 +01:00
func (dma *AbstractAccessor[T]) GetUser() string {
if dma.Request == nil {
return ""
}
return dma.Request.Username
2024-12-04 11:33:08 +01:00
}
2026-02-18 12:24:19 +01:00
func (dma *AbstractAccessor[T]) GetPeerID() string {
if dma.Request == nil {
return ""
}
return dma.Request.PeerID
2024-11-29 10:25:42 +01:00
}
2026-02-18 12:24:19 +01:00
func (dma *AbstractAccessor[T]) GetGroups() []string {
if dma.Request == nil {
return []string{}
}
return dma.Request.Groups
2024-11-29 10:25:42 +01:00
}
2026-02-18 12:24:19 +01:00
func (dma *AbstractAccessor[T]) GetLogger() *zerolog.Logger {
2024-11-29 10:25:42 +01:00
return &dma.Logger
}
2026-02-18 12:24:19 +01:00
func (dma *AbstractAccessor[T]) GetType() tools.DataType {
2024-07-18 16:46:54 +02:00
return dma.Type
}
2026-02-18 12:24:19 +01:00
func (dma *AbstractAccessor[T]) GetCaller() *tools.HTTPCaller {
if dma.Request == nil {
return nil
2024-11-12 12:45:03 +01:00
}
return dma.Request.Caller
2024-11-28 11:05:54 +01:00
}
2026-02-18 12:24:19 +01:00
/*
* Nothing special here, just the basic CRUD operations
*/
func (a *AbstractAccessor[T]) DeleteOne(id string) (DBObject, int, error) {
if len(a.NotImplemented) > 0 && slices.Contains(a.NotImplemented, "DeleteOne") {
return nil, 404, errors.New("not implemented")
}
return GenericDeleteOne(id, a)
}
func (a *AbstractAccessor[T]) UpdateOne(set DBObject, id string) (DBObject, int, error) {
if len(a.NotImplemented) > 0 && slices.Contains(a.NotImplemented, "UpdateOne") {
return nil, 404, errors.New("not implemented")
}
// should verify if a source is existing...
return GenericUpdateOne(set, id, a, a.New())
}
func (a *AbstractAccessor[T]) StoreOne(data DBObject) (DBObject, int, error) {
if len(a.NotImplemented) > 0 && slices.Contains(a.NotImplemented, "StoreOne") {
return nil, 404, errors.New("not implemented")
}
return GenericStoreOne(data.(T), a)
}
func (a *AbstractAccessor[T]) CopyOne(data DBObject) (DBObject, int, error) {
if len(a.NotImplemented) > 0 && slices.Contains(a.NotImplemented, "CopyOne") {
return nil, 404, errors.New("not implemented")
}
return GenericStoreOne(data.(T), a)
}
func (a *AbstractAccessor[T]) LoadOne(id string) (DBObject, int, error) {
return GenericLoadOne[T](id, func(d DBObject) (DBObject, int, error) {
return d, 200, nil
}, a)
}
func (a *AbstractAccessor[T]) LoadAll(isDraft bool) ([]ShallowDBObject, int, error) {
return GenericLoadAll[T](a.GetExec(isDraft), isDraft, a)
}
func (a *AbstractAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool) ([]ShallowDBObject, int, error) {
return GenericSearch[T](filters, search, a.New().GetObjectFilters(search), a.GetExec(isDraft), isDraft, a)
}
func (a *AbstractAccessor[T]) GetExec(isDraft bool) func(DBObject) ShallowDBObject {
return func(d DBObject) ShallowDBObject {
return d
}
}