/
model_rbac.go
84 lines (71 loc) · 2.24 KB
/
model_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package db
import (
"time"
"github.com/bwmarrin/snowflake"
"github.com/ekristen/dockit/pkg/common"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
// Group --
type Group struct {
ID int64 `gorm:"primaryKey;autoIncrement:false" json:"id"`
Name string `json:"name"`
Active bool `json:"active"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
Users []*User `gorm:"many2many:user_groups" json:"users,omitempty"`
Permissions []*Permission `gorm:"foreignKey:EntityID" json:"permissions,omitempty"`
}
// 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 `json:"name"`
Username string `gorm:"uniqueIndex,size:255"`
Password string `json:"-"`
Admin bool `json:"admin"`
Active bool `json:"active"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
Groups []*Group `gorm:"many2many:user_groups" json:"groups,omitempty"`
Permissions []*Permission `gorm:"foreignKey:EntityID" json:"permissions,omitempty"`
Tokens []*Token `gorm:"foreignKey:UserID" json:"users,omitempty"`
}
// 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 u.bcryptPassword(tx)
}
func (u *User) BeforeUpdate(tx *gorm.DB) error {
if tx.Statement.Changed("Password") {
return u.bcryptPassword(tx)
}
return nil
}
func (u *User) bcryptPassword(tx *gorm.DB) error {
var newPass string
switch u := tx.Statement.Dest.(type) {
case map[string]interface{}:
newPass = u["password"].(string)
case *User:
newPass = u.Password
case []*User:
newPass = u[tx.Statement.CurDestIndex].Password
}
b, err := bcrypt.GenerateFromPassword([]byte(newPass), 10)
if err != nil {
return err
}
tx.Statement.SetColumn("password", b)
return nil
}