oc-lib/models/utils/abstracts.go

152 lines
3.8 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-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 {
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"`
IsDraft bool `json:"is_draft" bson:"is_draft" default:"false"`
2024-12-04 12:14:55 +01:00
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
}
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
}
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
}
func (ao *AbstractObject) VerifyAuth(request *tools.APIRequest) bool {
return ao.AccessMode == Public || (request != nil && ao.CreatorID == request.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
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
}
func (r *AbstractAccessor) GetRequest() *tools.APIRequest {
return r.Request
}
2024-12-04 11:33:08 +01:00
func (dma *AbstractAccessor) GetUser() string {
if dma.Request == nil {
return ""
}
return dma.Request.Username
2024-12-04 11:33:08 +01:00
}
2024-11-29 10:25:42 +01:00
func (dma *AbstractAccessor) GetPeerID() string {
if dma.Request == nil {
return ""
}
return dma.Request.PeerID
2024-11-29 10:25:42 +01:00
}
func (dma *AbstractAccessor) GetGroups() []string {
if dma.Request == nil {
return []string{}
}
return dma.Request.Groups
2024-11-29 10:25:42 +01:00
}
func (dma *AbstractAccessor) GetLogger() *zerolog.Logger {
return &dma.Logger
}
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 {
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
}