权限管理接口实现
This commit is contained in:
@@ -41,6 +41,32 @@ func newAdmin(db *gorm.DB, opts ...gen.DOOption) admin {
|
||||
_admin.LastLogin = field.NewTime(tableName, "last_login")
|
||||
_admin.LastLoginIP = field.NewField(tableName, "last_login_ip")
|
||||
_admin.LastLoginUA = field.NewString(tableName, "last_login_ua")
|
||||
_admin.Roles = adminManyToManyRoles{
|
||||
db: db.Session(&gorm.Session{}),
|
||||
|
||||
RelationField: field.NewRelation("Roles", "models.AdminRole"),
|
||||
Permissions: struct {
|
||||
field.RelationField
|
||||
Parent struct {
|
||||
field.RelationField
|
||||
}
|
||||
Children struct {
|
||||
field.RelationField
|
||||
}
|
||||
}{
|
||||
RelationField: field.NewRelation("Roles.Permissions", "models.Permission"),
|
||||
Parent: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Roles.Permissions.Parent", "models.Permission"),
|
||||
},
|
||||
Children: struct {
|
||||
field.RelationField
|
||||
}{
|
||||
RelationField: field.NewRelation("Roles.Permissions.Children", "models.Permission"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_admin.fillFieldMap()
|
||||
|
||||
@@ -65,6 +91,7 @@ type admin struct {
|
||||
LastLogin field.Time
|
||||
LastLoginIP field.Field
|
||||
LastLoginUA field.String
|
||||
Roles adminManyToManyRoles
|
||||
|
||||
fieldMap map[string]field.Expr
|
||||
}
|
||||
@@ -111,7 +138,7 @@ func (a *admin) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (a *admin) fillFieldMap() {
|
||||
a.fieldMap = make(map[string]field.Expr, 14)
|
||||
a.fieldMap = make(map[string]field.Expr, 15)
|
||||
a.fieldMap["id"] = a.ID
|
||||
a.fieldMap["created_at"] = a.CreatedAt
|
||||
a.fieldMap["updated_at"] = a.UpdatedAt
|
||||
@@ -126,18 +153,113 @@ func (a *admin) fillFieldMap() {
|
||||
a.fieldMap["last_login"] = a.LastLogin
|
||||
a.fieldMap["last_login_ip"] = a.LastLoginIP
|
||||
a.fieldMap["last_login_ua"] = a.LastLoginUA
|
||||
|
||||
}
|
||||
|
||||
func (a admin) clone(db *gorm.DB) admin {
|
||||
a.adminDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||
a.Roles.db = db.Session(&gorm.Session{Initialized: true})
|
||||
a.Roles.db.Statement.ConnPool = db.Statement.ConnPool
|
||||
return a
|
||||
}
|
||||
|
||||
func (a admin) replaceDB(db *gorm.DB) admin {
|
||||
a.adminDo.ReplaceDB(db)
|
||||
a.Roles.db = db.Session(&gorm.Session{})
|
||||
return a
|
||||
}
|
||||
|
||||
type adminManyToManyRoles struct {
|
||||
db *gorm.DB
|
||||
|
||||
field.RelationField
|
||||
|
||||
Permissions struct {
|
||||
field.RelationField
|
||||
Parent struct {
|
||||
field.RelationField
|
||||
}
|
||||
Children struct {
|
||||
field.RelationField
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a adminManyToManyRoles) Where(conds ...field.Expr) *adminManyToManyRoles {
|
||||
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 adminManyToManyRoles) WithContext(ctx context.Context) *adminManyToManyRoles {
|
||||
a.db = a.db.WithContext(ctx)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a adminManyToManyRoles) Session(session *gorm.Session) *adminManyToManyRoles {
|
||||
a.db = a.db.Session(session)
|
||||
return &a
|
||||
}
|
||||
|
||||
func (a adminManyToManyRoles) Model(m *models.Admin) *adminManyToManyRolesTx {
|
||||
return &adminManyToManyRolesTx{a.db.Model(m).Association(a.Name())}
|
||||
}
|
||||
|
||||
func (a adminManyToManyRoles) Unscoped() *adminManyToManyRoles {
|
||||
a.db = a.db.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type adminManyToManyRolesTx struct{ tx *gorm.Association }
|
||||
|
||||
func (a adminManyToManyRolesTx) Find() (result []*models.AdminRole, err error) {
|
||||
return result, a.tx.Find(&result)
|
||||
}
|
||||
|
||||
func (a adminManyToManyRolesTx) Append(values ...*models.AdminRole) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Append(targetValues...)
|
||||
}
|
||||
|
||||
func (a adminManyToManyRolesTx) Replace(values ...*models.AdminRole) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Replace(targetValues...)
|
||||
}
|
||||
|
||||
func (a adminManyToManyRolesTx) Delete(values ...*models.AdminRole) (err error) {
|
||||
targetValues := make([]interface{}, len(values))
|
||||
for i, v := range values {
|
||||
targetValues[i] = v
|
||||
}
|
||||
return a.tx.Delete(targetValues...)
|
||||
}
|
||||
|
||||
func (a adminManyToManyRolesTx) Clear() error {
|
||||
return a.tx.Clear()
|
||||
}
|
||||
|
||||
func (a adminManyToManyRolesTx) Count() int64 {
|
||||
return a.tx.Count()
|
||||
}
|
||||
|
||||
func (a adminManyToManyRolesTx) Unscoped() *adminManyToManyRolesTx {
|
||||
a.tx = a.tx.Unscoped()
|
||||
return &a
|
||||
}
|
||||
|
||||
type adminDo struct{ gen.DO }
|
||||
|
||||
func (a adminDo) Debug() *adminDo {
|
||||
|
||||
Reference in New Issue
Block a user