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:
@@ -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
|
||||
}
|
||||
|
||||
21
models/tenanted.go
Normal file
21
models/tenanted.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Tenanted struct {
|
||||
Base
|
||||
Tenant uuid.UUID `gorm:"index;<-:create"`
|
||||
}
|
||||
|
||||
func (t *Tenanted) BeforeCreate(scope *gorm.DB) error {
|
||||
if t.Tenant == uuid.Nil {
|
||||
return errors.New("cannot save an untenanted object")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
type User struct {
|
||||
Auth
|
||||
Email string `gorm:"unique" sql:"index"`
|
||||
Email string `gorm:"unique;index"`
|
||||
}
|
||||
|
||||
const userJwtDuration = time.Hour * 24
|
||||
@@ -24,7 +24,6 @@ func (u *User) GetJwt() (string, int) {
|
||||
"iat": time.Now().Unix(),
|
||||
"exp": time.Now().Add(userJwtDuration).Unix(),
|
||||
"role": "user",
|
||||
"tid": u.Tenant.String(),
|
||||
})
|
||||
|
||||
jstr, err := j.SignedString(UserHmac)
|
||||
|
||||
Reference in New Issue
Block a user