add username to our trip
This commit is contained in:
@@ -18,16 +18,25 @@ import (
|
||||
// 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"`
|
||||
UpdateDate time.Time `json:"update_date" bson:"update_date"`
|
||||
LastPeerWriter string `json:"last_peer_writer" bson:"last_peer_writer"`
|
||||
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"`
|
||||
}
|
||||
|
||||
func (r *AbstractObject) GenerateID() {
|
||||
@@ -46,13 +55,16 @@ func (ao AbstractObject) GetName() string {
|
||||
return ao.Name
|
||||
}
|
||||
|
||||
func (ao *AbstractObject) UpToDate() {
|
||||
func (ao *AbstractObject) UpToDate(user string, create bool) {
|
||||
ao.UpdateDate = time.Now()
|
||||
// ao.LastPeerWriter, _ = static.GetMyLocalJsonPeer()
|
||||
ao.LastPeerWriter = user
|
||||
if create {
|
||||
ao.CreatorID = user
|
||||
}
|
||||
}
|
||||
|
||||
func (ao *AbstractObject) VerifyAuth(username string, peerID string, groups []string) bool {
|
||||
return true
|
||||
return ao.AccessMode == Public || ao.CreatorID == username
|
||||
}
|
||||
|
||||
func (ao *AbstractObject) GetObjectFilters(search string) *dbs.Filters {
|
||||
@@ -120,6 +132,7 @@ func (dma *AbstractAccessor) GetCaller() *tools.HTTPCaller {
|
||||
// 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": {{
|
||||
@@ -175,6 +188,7 @@ func GenericUpdateOne(set DBObject, id string, a Accessor, new DBObject) (DBObje
|
||||
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")
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ type DBObject interface {
|
||||
GenerateID()
|
||||
GetID() string
|
||||
GetName() string
|
||||
UpToDate()
|
||||
UpToDate(user string, create bool)
|
||||
VerifyAuth(username string, PeerID string, groups []string) bool
|
||||
Deserialize(j map[string]interface{}, obj DBObject) DBObject
|
||||
Serialize(obj DBObject) map[string]interface{}
|
||||
|
Reference in New Issue
Block a user