权限管理接口实现
This commit is contained in:
@@ -52,6 +52,52 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
|
||||
db: db.Session(&gorm.Session{}),
|
||||
|
||||
RelationField: field.NewRelation("Admin", "models.Admin"),
|
||||
Roles: struct {
|
||||
field.RelationField
|
||||
Permissions struct {
|
||||
field.RelationField
|
||||
Parent struct {
|
||||
field.RelationField
|
||||
}
|
||||
Children struct {
|
||||
field.RelationField
|
||||
}
|
||||
}
|
||||
}{
|
||||
RelationField: field.NewRelation("Admin.Roles", "models.AdminRole"),
|
||||
Permissions: struct {
|
||||
field.RelationField
|
||||
Parent struct {
|
||||
field.RelationField
|
||||
}
|
||||
Children struct {
|
||||
field.RelationField
|
||||
}
|
||||
}{
|
||||
RelationField: field.NewRelation("Admin.Roles.Permissions", "models.Permission"),
|
||||
Parent: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Admin.Roles.Permissions.Parent", "models.Permission"),
|
||||
},
|
||||
Children: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Admin.Roles.Permissions.Children", "models.Permission"),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_user.Roles = userManyToManyRoles{
|
||||
db: db.Session(&gorm.Session{}),
|
||||
|
||||
RelationField: field.NewRelation("Roles", "models.UserRole"),
|
||||
Permissions: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Roles.Permissions", "models.Permission"),
|
||||
},
|
||||
}
|
||||
|
||||
_user.fillFieldMap()
|
||||
@@ -86,6 +132,8 @@ type user struct {
|
||||
LastLoginUA field.String
|
||||
Admin userBelongsToAdmin
|
||||
|
||||
Roles userManyToManyRoles
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
|
||||
@@ -138,7 +186,7 @@ func (u *user) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (u *user) fillFieldMap() {
|
||||
u.fieldMap = make(map[string]field.Expr, 22)
|
||||
u.fieldMap = make(map[string]field.Expr, 23)
|
||||
u.fieldMap["id"] = u.ID
|
||||
u.fieldMap["created_at"] = u.CreatedAt
|
||||
u.fieldMap["updated_at"] = u.UpdatedAt
|
||||
@@ -167,12 +215,15 @@ func (u user) clone(db *gorm.DB) user {
|
||||
u.userDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
u.Admin.db = db.Session(&gorm.Session{Initialized: true})
|
||||
u.Admin.db.Statement.ConnPool = db.Statement.ConnPool
|
||||
u.Roles.db = db.Session(&gorm.Session{Initialized: true})
|
||||
u.Roles.db.Statement.ConnPool = db.Statement.ConnPool
|
||||
return u
|
||||
}
|
||||
|
||||
func (u user) replaceDB(db *gorm.DB) user {
|
||||
u.userDo.ReplaceDB(db)
|
||||
u.Admin.db = db.Session(&gorm.Session{})
|
||||
u.Roles.db = db.Session(&gorm.Session{})
|
||||
return u
|
||||
}
|
||||
|
||||
@@ -180,6 +231,19 @@ type userBelongsToAdmin struct {
|
||||
db *gorm.DB
|
||||
|
||||
field.RelationField
|
||||
|
||||
Roles struct {
|
||||
field.RelationField
|
||||
Permissions struct {
|
||||
field.RelationField
|
||||
Parent struct {
|
||||
field.RelationField
|
||||
}
|
||||
Children struct {
|
||||
field.RelationField
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a userBelongsToAdmin) Where(conds ...field.Expr) *userBelongsToAdmin {
|
||||
@@ -257,6 +321,91 @@ func (a userBelongsToAdminTx) Unscoped() *userBelongsToAdminTx {
|
||||
return &a
|
||||
}
|
||||
|
||||
type userManyToManyRoles struct {
|
||||
db *gorm.DB
|
||||
|
||||
field.RelationField
|
||||
|
||||
Permissions struct {
|
||||
field.RelationField
|
||||
}
|
||||
}
|
||||
|
||||
func (a userManyToManyRoles) Where(conds ...field.Expr) *userManyToManyRoles {
|
||||
if len(conds) == 0 {
|
||||
return &a
|
||||
}
|
||||
|
||||
exprs := make([]clause.Expression, 0, len(conds))
|
||||
for _, cond := range conds {
|
||||
exprs = append(exprs, cond.BeCond().(clause.Expression))
|
||||
}
|
||||
a.db = a.db.Clauses(clause.Where{Exprs: exprs})
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a userManyToManyRoles) WithContext(ctx context.Context) *userManyToManyRoles {
|
||||
a.db = a.db.WithContext(ctx)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a userManyToManyRoles) Session(session *gorm.Session) *userManyToManyRoles {
|
||||
a.db = a.db.Session(session)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a userManyToManyRoles) Model(m *models.User) *userManyToManyRolesTx {
|
||||
return &userManyToManyRolesTx{a.db.Model(m).Association(a.Name())}
|
||||
}
|
||||
|
||||
func (a userManyToManyRoles) Unscoped() *userManyToManyRoles {
|
||||
a.db = a.db.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type userManyToManyRolesTx struct{ tx *gorm.Association }
|
||||
|
||||
func (a userManyToManyRolesTx) Find() (result []*models.UserRole, err error) {
|
||||
return result, a.tx.Find(&result)
|
||||
}
|
||||
|
||||
func (a userManyToManyRolesTx) Append(values ...*models.UserRole) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Append(targetValues...)
|
||||
}
|
||||
|
||||
func (a userManyToManyRolesTx) Replace(values ...*models.UserRole) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Replace(targetValues...)
|
||||
}
|
||||
|
||||
func (a userManyToManyRolesTx) Delete(values ...*models.UserRole) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Delete(targetValues...)
|
||||
}
|
||||
|
||||
func (a userManyToManyRolesTx) Clear() error {
|
||||
return a.tx.Clear()
|
||||
}
|
||||
|
||||
func (a userManyToManyRolesTx) Count() int64 {
|
||||
return a.tx.Count()
|
||||
}
|
||||
|
||||
func (a userManyToManyRolesTx) Unscoped() *userManyToManyRolesTx {
|
||||
a.tx = a.tx.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type userDo struct{ gen.DO }
|
||||
|
||||
func (u userDo) Debug() *userDo {
|
||||
|
||||
Reference in New Issue
Block a user