Added better Tenanting

* Removed Tenanting from base objects as some models may be tenantless
* Admins are naturally not restricted by tenants
* Users *ARE* the tenants (for now) so they don't require a tenant ID
either
* User-owned models should all include the Tenanted model as their base
* Created .Create and .Save methods attached to base model
This commit is contained in:
🐙PiperYxzzy
2022-05-01 12:48:40 +02:00
parent 8ab45e2401
commit dd8d2a677d
3 changed files with 25 additions and 11 deletions

View File

@@ -1,7 +1,6 @@
package models
import (
"errors"
"time"
"github.com/google/uuid"
@@ -9,11 +8,10 @@ import (
)
type Base struct {
Uid uuid.UUID `gorm:"type:uuid;primary_key;"`
Created time.Time
Uid uuid.UUID `gorm:"type:uuid;primary_key;<-:create"`
Created time.Time `gorm:"<-:create"`
Updated time.Time
Deleted time.Time `sql:"index"`
Tenant uuid.UUID `sql:"index"`
Deleted time.Time `gorm:"index"`
}
func (b *Base) BeforeCreate(scope *gorm.DB) error {
@@ -23,10 +21,6 @@ func (b *Base) BeforeCreate(scope *gorm.DB) error {
}
func (b *Base) BeforeSave(scope *gorm.DB) error {
if b.Tenant == uuid.Nil {
return errors.New("cannot save an untenanted object")
}
b.Updated = time.Now()
return nil
}