Files
go-admin/common/actions/permission.go
T
zhangwenjian f4e3f04d30 refactor🎨: turn common/actions' data permission into a thin forward
DataPermission, PermissionAction, Permission, GetPermissionFromContext,
IsValidDataScope, PermissionKey and the five DataScope* constants now
forward to go-admin-core's sdk/contract/actions, which carries the
already-fixed logic from feat/006-security-prereq (PRD 006 F14/H1-H3).
create.go/delete.go/index.go/update.go/view.go - the five generic CRUD
actions - are untouched: they call Permission and
GetPermissionFromContext by the same names, which now resolve to
forwards with identical behaviour, and stay in this package rather
than moving to core (PRD 006 F3: core's exports are a permanent
promise every fork inherits, and CRUD shape is this framework's most
volatile surface).

PermissionKey is declared as `const PermissionKey =
contractactions.PermissionKey`, a direct reference rather than a
restated literal, per PRD 006's hard constraint 4: PermissionAction
sets this gin context key and GetPermissionFromContext reads it back,
and an independently declared copy could silently drift from core's if
one were ever edited without the other. permission_test.go replaces
the detailed data-permission regression suite - which now lives in
core, next to the logic itself - with a test of this package's own
wiring: that PermissionAction and both of this package's own read
paths (GetPermissionFromContext, and c.Get(actions.PermissionKey)
directly) still meet on the same key.

Counterproof performed and reverted (not part of this commit): with
PermissionKey redeclared here as the literal "dataPermission" and
core's copy changed to a different value, TestPermissionKeyMatches-
WhatPermissionActionSets went red while GetPermissionFromContext's own
round-trip stayed green - confirming the exported constant, not the
GetPermissionFromContext wrapper, is what an independent literal would
put at risk.

PRD 006 F3/F5.

Claude-Session: https://claude.ai/code/session_01HPTAw8b8tAdFNFn8rKdPYx
2026-09-05 10:15:58 +08:00

49 lines
1.8 KiB
Go

package actions
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
contractactions "github.com/go-admin-team/go-admin-core/v2/sdk/contract/actions"
)
// DataPermission is a thin alias of go-admin-core's sdk/contract/actions
// (PRD 006 F3/F5).
type DataPermission = contractactions.DataPermission
// The five values sys_role.data_scope can hold, referenced directly from
// go-admin-core's sdk/contract/actions rather than restated as literals -
// see that package's DataScope* doc comment and PRD 006's hard constraint 4.
const (
DataScopeAll = contractactions.DataScopeAll
DataScopeCustom = contractactions.DataScopeCustom
DataScopeDept = contractactions.DataScopeDept
DataScopeDeptTree = contractactions.DataScopeDeptTree
DataScopeSelf = contractactions.DataScopeSelf
)
// PermissionAction, Permission, GetPermissionFromContext and
// IsValidDataScope forward to go-admin-core's sdk/contract/actions (PRD 006
// F3/F5). create.go/delete.go/index.go/update.go/view.go in this package
// (the generic CRUD actions, which do not move to core) call Permission and
// GetPermissionFromContext by these same names and are unchanged by the
// move: the names now resolve to forwards instead of local definitions, and
// the behaviour is identical either way.
func PermissionAction() gin.HandlerFunc {
return contractactions.PermissionAction()
}
func Permission(tableName string, p *DataPermission) func(db *gorm.DB) *gorm.DB {
return contractactions.Permission(tableName, p)
}
func GetPermissionFromContext(c *gin.Context) *DataPermission {
return contractactions.GetPermissionFromContext(c)
}
// IsValidDataScope reports whether s is one of the five values Permission
// recognizes. See go-admin-core's sdk/contract/actions.IsValidDataScope.
func IsValidDataScope(s string) bool {
return contractactions.IsValidDataScope(s)
}