/
rbac.go
49 lines (41 loc) · 1.08 KB
/
rbac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package models
import (
"time"
"github.com/bwmarrin/snowflake"
"github.com/ekristen/pipeliner/pkg/common"
"gorm.io/gorm"
)
// Group --
type Group struct {
ID int64 `gorm:"primaryKey;autoIncrement:false" json:"id"`
Name string
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
Users []*User `gorm:"many2many:user_groups"`
}
// BeforeCreate --
func (g *Group) BeforeCreate(tx *gorm.DB) error {
if g.ID == 0 {
node := tx.Statement.Context.Value(common.ContextKeyNode).(*snowflake.Node)
g.ID = node.Generate().Int64()
}
return nil
}
// User --
type User struct {
ID int64 `gorm:"primaryKey;autoIncrement:false" json:"id"`
Name string
Username string
Password string
Groups []*Group `gorm:"many2many:user_groups"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
}
// BeforeCreate --
func (u *User) BeforeCreate(tx *gorm.DB) error {
if u.ID == 0 {
node := tx.Statement.Context.Value(common.ContextKeyNode).(*snowflake.Node)
u.ID = node.Generate().Int64()
}
return nil
}