权限管理接口实现

This commit is contained in:
2026-03-18 18:09:32 +08:00
parent 9d996acf5f
commit bb895eccdf
44 changed files with 1958 additions and 161 deletions

View File

@@ -35,6 +35,21 @@ func newUserRole(db *gorm.DB, opts ...gen.DOOption) userRole {
_userRole.Description = field.NewString(tableName, "description")
_userRole.Active = field.NewBool(tableName, "active")
_userRole.Sort = field.NewInt32(tableName, "sort")
_userRole.Permissions = userRoleManyToManyPermissions{
db: db.Session(&gorm.Session{}),
RelationField: field.NewRelation("Permissions", "models.Permission"),
Parent: struct {
field.RelationField
}{
RelationField: field.NewRelation("Permissions.Parent", "models.Permission"),
},
Children: struct {
field.RelationField
}{
RelationField: field.NewRelation("Permissions.Children", "models.Permission"),
},
}
_userRole.fillFieldMap()
@@ -53,6 +68,7 @@ type userRole struct {
Description field.String
Active field.Bool
Sort field.Int32
Permissions userRoleManyToManyPermissions
fieldMap map[string]field.Expr
}
@@ -93,7 +109,7 @@ func (u *userRole) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
}
func (u *userRole) fillFieldMap() {
u.fieldMap = make(map[string]field.Expr, 8)
u.fieldMap = make(map[string]field.Expr, 9)
u.fieldMap["id"] = u.ID
u.fieldMap["created_at"] = u.CreatedAt
u.fieldMap["updated_at"] = u.UpdatedAt
@@ -102,18 +118,110 @@ func (u *userRole) fillFieldMap() {
u.fieldMap["description"] = u.Description
u.fieldMap["active"] = u.Active
u.fieldMap["sort"] = u.Sort
}
func (u userRole) clone(db *gorm.DB) userRole {
u.userRoleDo.ReplaceConnPool(db.Statement.ConnPool)
u.Permissions.db = db.Session(&gorm.Session{Initialized: true})
u.Permissions.db.Statement.ConnPool = db.Statement.ConnPool
return u
}
func (u userRole) replaceDB(db *gorm.DB) userRole {
u.userRoleDo.ReplaceDB(db)
u.Permissions.db = db.Session(&gorm.Session{})
return u
}
type userRoleManyToManyPermissions struct {
db *gorm.DB
field.RelationField
Parent struct {
field.RelationField
}
Children struct {
field.RelationField
}
}
func (a userRoleManyToManyPermissions) Where(conds ...field.Expr) *userRoleManyToManyPermissions {
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 userRoleManyToManyPermissions) WithContext(ctx context.Context) *userRoleManyToManyPermissions {
a.db = a.db.WithContext(ctx)
return &a
}
func (a userRoleManyToManyPermissions) Session(session *gorm.Session) *userRoleManyToManyPermissions {
a.db = a.db.Session(session)
return &a
}
func (a userRoleManyToManyPermissions) Model(m *models.UserRole) *userRoleManyToManyPermissionsTx {
return &userRoleManyToManyPermissionsTx{a.db.Model(m).Association(a.Name())}
}
func (a userRoleManyToManyPermissions) Unscoped() *userRoleManyToManyPermissions {
a.db = a.db.Unscoped()
return &a
}
type userRoleManyToManyPermissionsTx struct{ tx *gorm.Association }
func (a userRoleManyToManyPermissionsTx) Find() (result []*models.Permission, err error) {
return result, a.tx.Find(&result)
}
func (a userRoleManyToManyPermissionsTx) Append(values ...*models.Permission) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Append(targetValues...)
}
func (a userRoleManyToManyPermissionsTx) Replace(values ...*models.Permission) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Replace(targetValues...)
}
func (a userRoleManyToManyPermissionsTx) Delete(values ...*models.Permission) (err error) {
targetValues := make([]interface{}, len(values))
for i, v := range values {
targetValues[i] = v
}
return a.tx.Delete(targetValues...)
}
func (a userRoleManyToManyPermissionsTx) Clear() error {
return a.tx.Clear()
}
func (a userRoleManyToManyPermissionsTx) Count() int64 {
return a.tx.Count()
}
func (a userRoleManyToManyPermissionsTx) Unscoped() *userRoleManyToManyPermissionsTx {
a.tx = a.tx.Unscoped()
return &a
}
type userRoleDo struct{ gen.DO }
func (u userRoleDo) Debug() *userRoleDo {