155 lines
3.9 KiB
Go
155 lines
3.9 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
|
"cloud.o-forge.io/core/oc-lib/tools"
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/google/uuid"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
// single instance of the validator used in every model Struct to validate the fields
|
|
var validate = validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
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)
|
|
*/
|
|
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"`
|
|
CreatorID string `json:"creator_id" bson:"creator_id" default:"unknown"`
|
|
|
|
CreationDate time.Time `json:"creation_date" bson:"creation_date"`
|
|
UpdateDate time.Time `json:"update_date" bson:"update_date"`
|
|
UpdaterID string `json:"updater_id" bson:"updater_id"`
|
|
AccessMode AccessMode `json:"access_mode" bson:"access_mode" default:"0"`
|
|
}
|
|
|
|
func (r *AbstractObject) GenerateID() {
|
|
if r.UUID == "" {
|
|
r.UUID = uuid.New().String()
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// GetName implements ShallowDBObject.
|
|
func (ao AbstractObject) GetName() string {
|
|
return ao.Name
|
|
}
|
|
|
|
func (ao *AbstractObject) UpToDate(user string, create bool) {
|
|
ao.UpdateDate = time.Now()
|
|
ao.UpdaterID = user
|
|
if create {
|
|
ao.CreationDate = time.Now()
|
|
ao.CreatorID = user
|
|
}
|
|
}
|
|
|
|
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 {
|
|
return &dbs.Filters{
|
|
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
|
|
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
|
|
}}
|
|
}
|
|
|
|
func (dma *AbstractObject) Deserialize(j map[string]interface{}, obj DBObject) DBObject {
|
|
b, err := json.Marshal(j)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
json.Unmarshal(b, obj)
|
|
return obj
|
|
}
|
|
|
|
func (dma *AbstractObject) Serialize(obj DBObject) map[string]interface{} {
|
|
var m map[string]interface{}
|
|
b, err := json.Marshal(obj)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
json.Unmarshal(b, &m)
|
|
return m
|
|
}
|
|
|
|
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
|
|
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 {
|
|
if dma.Request == nil {
|
|
return ""
|
|
}
|
|
return dma.Request.Username
|
|
}
|
|
|
|
func (dma *AbstractAccessor) GetPeerID() string {
|
|
if dma.Request == nil {
|
|
return ""
|
|
}
|
|
return dma.Request.PeerID
|
|
}
|
|
func (dma *AbstractAccessor) GetGroups() []string {
|
|
if dma.Request == nil {
|
|
return []string{}
|
|
}
|
|
return dma.Request.Groups
|
|
}
|
|
func (dma *AbstractAccessor) GetLogger() *zerolog.Logger {
|
|
return &dma.Logger
|
|
}
|
|
func (dma *AbstractAccessor) GetType() tools.DataType {
|
|
return dma.Type
|
|
}
|
|
|
|
func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
|
|
if dma.Request == nil {
|
|
return nil
|
|
}
|
|
return dma.Request.Caller
|
|
}
|