37 lines
1017 B
Go
37 lines
1017 B
Go
package rbac
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"online-back/internal/identity/rbac/perm"
|
|
"online-back/pkg/repository/genericrepo"
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
// RoleInfoSelect selects the columns ScanRoleInfo reads from roles r; append
|
|
// the WHERE clause (it must keep r.deleted_at IS NULL).
|
|
const RoleInfoSelect = `SELECT r.id, r.code, r.scope, r.is_super, r.is_system,
|
|
ARRAY(SELECT rp.permission_code::text FROM role_permissions rp WHERE rp.role_id = r.id ORDER BY 1)
|
|
FROM roles r `
|
|
|
|
// ScanRoleInfo scans one RoleInfoSelect row; a missing row is (nil, nil).
|
|
func ScanRoleInfo(row genericrepo.Scanner) (*RoleInfo, error) {
|
|
var info RoleInfo
|
|
var scope string
|
|
var codes []string
|
|
err := row.Scan(&info.ID, &info.Code, &scope, &info.IsSuper, &info.IsSystem, &codes)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
info.Scope = Scope(scope)
|
|
info.Perms = make([]perm.Code, len(codes))
|
|
for i, code := range codes {
|
|
info.Perms[i] = perm.Code(code)
|
|
}
|
|
return &info, nil
|
|
}
|