Add backups, currencies and dashboard APIs, and scope access by guard_name.
Web users may only CRUD their own records, operators stay in assigned branches, and admin/manager/superadmin keep full access. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
0d000f9af6
commit
28509eb4ca
65 files changed
+9132
-296
No files matched your search
@@ -17,15 +17,40 @@ func NewService(client postgresql.Client) *Service {
|
||||
return &Service{client: client}
|
||||
}
|
||||
|
||||
// Authorizer is the lookup Guard, RequirePermission and GuardUser need.
|
||||
type Authorizer interface {
|
||||
RoleLookup(ctx context.Context, userID int64) (code, guardName string, err error)
|
||||
BranchIDs(ctx context.Context, userID int64) ([]int64, error)
|
||||
HasPermission(ctx context.Context, userID int64, code string) (bool, error)
|
||||
UserVisibleInBranches(ctx context.Context, userID int64, branchIDs []int64) (bool, error)
|
||||
}
|
||||
|
||||
// HasPermission checks the role's baseline grant, then lets a per-user
|
||||
// permission_user override win: an explicit "deny" always wins (even over
|
||||
// another allow), an explicit "allow" grants a permission the role does
|
||||
// not, and no override falls back to the role.
|
||||
func (s *Service) HasPermission(ctx context.Context, userID int64, code string) (bool, error) {
|
||||
const query = `
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM role_user ru
|
||||
JOIN permission_role pr ON pr.role_id = ru.role_id
|
||||
JOIN permissions p ON p.id = pr.permission_id
|
||||
WHERE ru.user_id = $1 AND p.code = $2
|
||||
)`
|
||||
SELECT
|
||||
CASE
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM permission_user pu
|
||||
JOIN permissions p ON p.id = pu.permission_id
|
||||
WHERE pu.user_id = $1 AND p.code = $2 AND pu.effect = 'deny'
|
||||
) THEN false
|
||||
WHEN EXISTS (
|
||||
SELECT 1 FROM permission_user pu
|
||||
JOIN permissions p ON p.id = pu.permission_id
|
||||
WHERE pu.user_id = $1 AND p.code = $2 AND pu.effect = 'allow'
|
||||
) THEN true
|
||||
ELSE EXISTS (
|
||||
SELECT 1
|
||||
FROM role_user ru
|
||||
JOIN permission_role pr ON pr.role_id = ru.role_id
|
||||
JOIN permissions p ON p.id = pr.permission_id
|
||||
WHERE ru.user_id = $1 AND p.code = $2
|
||||
)
|
||||
END`
|
||||
|
||||
var granted bool
|
||||
if err := s.client.QueryRow(ctx, query, userID, code).Scan(&granted); err != nil {
|
||||
@@ -34,21 +59,21 @@ func (s *Service) HasPermission(ctx context.Context, userID int64, code string)
|
||||
return granted, nil
|
||||
}
|
||||
|
||||
func (s *Service) RoleCode(ctx context.Context, userID int64) (string, error) {
|
||||
func (s *Service) RoleLookup(ctx context.Context, userID int64) (string, string, error) {
|
||||
const query = `
|
||||
SELECT r.code
|
||||
SELECT r.code, r.guard_name
|
||||
FROM role_user ru
|
||||
JOIN roles r ON r.id = ru.role_id
|
||||
WHERE ru.user_id = $1
|
||||
ORDER BY ru.id
|
||||
LIMIT 1`
|
||||
|
||||
var code string
|
||||
err := s.client.QueryRow(ctx, query, userID).Scan(&code)
|
||||
var code, guardName string
|
||||
err := s.client.QueryRow(ctx, query, userID).Scan(&code, &guardName)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil
|
||||
return "", "", nil
|
||||
}
|
||||
return code, err
|
||||
return code, guardName, err
|
||||
}
|
||||
|
||||
func (s *Service) BranchIDs(ctx context.Context, userID int64) ([]int64, error) {
|
||||
|
||||
Reference in new issue
Block a user