Add backups, currencies and dashboard APIs, and scope access by guard_name.
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

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:
guwandykandCursor committed 2026-08-31 18:14:47 +05:00
1 parent 0d000f9af6
commit 28509eb4ca
65 files changed
+9132 -296

No files matched your search

+116 -30
View File
@@ -16,13 +16,17 @@ import (
)
const (
roleUser = "user"
roleOperator = "operator"
adminPrefix = "/api/admin"
contextRole = "role"
roleUser = "user"
roleOperator = "operator"
guardWeb = "web"
guardOperator = "operator"
guardAdmin = "admin"
adminPrefix = "/api/admin"
contextRole = "role"
contextGuard = "guard"
)
func RequirePermission(service *Service, code string) gin.HandlerFunc {
func RequirePermission(service Authorizer, code string) gin.HandlerFunc {
return func(c *gin.Context) {
rawID, exists := c.Get("id")
if !exists {
@@ -46,7 +50,7 @@ func RequirePermission(service *Service, code string) gin.HandlerFunc {
}
}
func Guard(service *Service) gin.HandlerFunc {
func Guard(service Authorizer) gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.Method == http.MethodOptions || isPublicAdminPath(c.Request.URL.Path) {
c.Next()
@@ -59,26 +63,34 @@ func Guard(service *Service) gin.HandlerFunc {
return
}
code, err := service.RoleCode(c.Request.Context(), userID)
code, guardName, err := service.RoleLookup(c.Request.Context(), userID)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
}
guard := effectiveGuard(code, guardName)
c.Set("id", strconv.FormatInt(userID, 10))
c.Set(contextRole, code)
c.Set(contextGuard, guard)
c.Set(contextService, service)
switch code {
case roleOperator:
switch guard {
case guardOperator:
enforceOperator(c, service, userID)
return
case roleUser, "":
case guardWeb:
if code == "" {
c.Set(contextRole, roleUser)
}
if !userRoleAllows(c.Request.Method, c.Request.URL.Path) {
c.AbortWithStatusJSON(http.StatusForbidden, appresult.ErrForbidden)
return
}
scopeOwnedLists(c, userID)
if !enforceOwnWrites(c, userID) {
return
}
c.Next()
return
default:
@@ -88,7 +100,7 @@ func Guard(service *Service) gin.HandlerFunc {
}
}
func enforceOperator(c *gin.Context, service *Service, userID int64) {
func enforceOperator(c *gin.Context, service Authorizer, userID int64) {
ids, err := service.BranchIDs(c.Request.Context(), userID)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, appresult.ErrInternalServer)
@@ -113,6 +125,28 @@ func enforceOperator(c *gin.Context, service *Service, userID int64) {
c.Next()
}
func effectiveGuard(code, guardName string) string {
code = strings.ToLower(strings.TrimSpace(code))
guard := strings.ToLower(strings.TrimSpace(guardName))
switch code {
case "admin", "superadmin", "manager":
return guardAdmin
case roleOperator:
return guardOperator
}
switch guard {
case guardWeb, guardOperator, guardAdmin:
return guard
}
if code == roleUser || code == "" {
return guardWeb
}
return guardAdmin
}
func isPublicAdminPath(path string) bool {
switch strings.TrimSuffix(path, "/") {
case adminPrefix + "/login",
@@ -128,19 +162,31 @@ func isPublicAdminPath(path string) bool {
}
func userRoleAllows(method, path string) bool {
if method != http.MethodGet {
return false
}
rest := strings.TrimSuffix(strings.TrimPrefix(path, adminPrefix), "/")
rest := adminREST(path)
if rest == "" {
return false
}
if !strings.HasPrefix(rest, "/") {
rest = "/" + rest
}
for _, prefix := range userRolePrefixes {
if matchesPrefix(rest, userCatalogPrefixes) {
return method == http.MethodGet
}
if matchesPrefix(rest, userSelfPrefixes) {
if rest == "/profile" || strings.HasPrefix(rest, "/profile/") {
return method == http.MethodGet || method == http.MethodPut || method == http.MethodPatch
}
return method == http.MethodGet || method == http.MethodPost
}
if matchesPrefix(rest, userOwnedPrefixes) {
switch method {
case http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete:
return true
}
}
return false
}
func matchesPrefix(rest string, prefixes []string) bool {
for _, prefix := range prefixes {
if rest == prefix || strings.HasPrefix(rest, prefix+"/") {
return true
}
@@ -148,6 +194,41 @@ func userRoleAllows(method, path string) bool {
return false
}
func enforceOwnWrites(c *gin.Context, userID int64) bool {
if !matchesPrefix(adminREST(c.Request.URL.Path), userOwnedPrefixes) {
return true
}
switch c.Request.Method {
case http.MethodPost, http.MethodPut, http.MethodPatch:
default:
return true
}
body, err := io.ReadAll(c.Request.Body)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, appresult.ErrMissingParam)
return false
}
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
if len(bytes.TrimSpace(body)) == 0 {
return true
}
var payload map[string]interface{}
if err := json.Unmarshal(body, &payload); err != nil {
return true
}
payload["userId"] = userID
payload["user_id"] = userID
rewritten, err := json.Marshal(payload)
if err != nil {
return true
}
c.Request.Body = io.NopCloser(bytes.NewBuffer(rewritten))
c.Request.ContentLength = int64(len(rewritten))
return true
}
func scopeOwnedLists(c *gin.Context, userID int64) {
rest := strings.TrimSuffix(strings.TrimPrefix(c.Request.URL.Path, adminPrefix), "/")
if rest == "" {
@@ -232,27 +313,28 @@ func bearerToken(header string) string {
return strings.TrimSpace(header)
}
var userRolePrefixes = []string{
"/profile",
"/cards",
"/card-orders",
"/card-pin-orders",
var userCatalogPrefixes = []string{
"/payment-cards",
"/reasons-cards",
"/visa-master-payment-orders",
"/loan-orders",
"/loan-types",
"/loans-documents",
"/loans-repayment",
"/loan-paid-off-letters",
"/currencies",
}
var userSelfPrefixes = []string{
"/profile",
"/file",
}
var userOwnedPrefixes = []string{
"/cards",
"/card-orders",
"/card-pin-orders",
"/card-rekwizit-orders",
"/visa-master-payment-orders",
"/loan-orders",
"/loans-repayment",
"/loan-paid-off-letters",
}
func operatorRoleAllows(method, path string) bool {
@@ -280,7 +362,7 @@ func operatorProfileOrFile(path string) bool {
return rest == "/profile" || rest == "/file" || strings.HasPrefix(rest, "/file/")
}
func operatorWriteAllowed(c *gin.Context, service *Service, branchIDs []int64) bool {
func operatorWriteAllowed(c *gin.Context, service Authorizer, branchIDs []int64) bool {
switch c.Request.Method {
case http.MethodPost, http.MethodPut, http.MethodPatch:
default:
@@ -345,6 +427,7 @@ var operatorWritePrefixes = []string{
"/cards",
"/card-orders",
"/card-pin-orders",
"/card-rekwizit-orders",
"/visa-master-payment-orders",
"/loan-orders",
"/loans-repayment",
@@ -354,10 +437,12 @@ var operatorWritePrefixes = []string{
var operatorGetPrefixes = []string{
"/profile",
"/file",
"/dashboard",
"/users",
"/cards",
"/card-orders",
"/card-pin-orders",
"/card-rekwizit-orders",
"/payment-cards",
"/reasons-cards",
"/visa-master-payment-orders",
@@ -368,4 +453,5 @@ var operatorGetPrefixes = []string{
"/loan-paid-off-letters",
"/provinces",
"/branches",
"/currencies",
}