ch some
This commit is contained in:
@@ -10,9 +10,11 @@ import (
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/config"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -109,15 +111,27 @@ func enforceOperator(c *gin.Context, service Authorizer, userID int64) {
|
||||
c.Set(contextBranchIDs, ids)
|
||||
|
||||
if !operatorRoleAllows(c.Request.Method, c.Request.URL.Path) {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
abortForbidden(c, "route not in operator allow-list", logrus.Fields{"userId": userID})
|
||||
return
|
||||
}
|
||||
|
||||
if !operatorProfileOrFile(c.Request.URL.Path) && len(ids) == 0 {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
if !operatorProfileOrFile(c.Request.URL.Path) && !operatorClientsList(c.Request.URL.Path) && len(ids) == 0 {
|
||||
abortForbidden(c, "operator has no assigned branches", logrus.Fields{"userId": userID})
|
||||
return
|
||||
}
|
||||
|
||||
if perm := operatorPathPermission(c.Request.Method, c.Request.URL.Path); perm != "" {
|
||||
granted, err := service.HasPermission(c.Request.Context(), userID, perm)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if !granted {
|
||||
abortForbidden(c, "missing permission for operator path", logrus.Fields{"userId": userID, "permission": perm})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if !operatorWriteAllowed(c, service, ids) {
|
||||
return
|
||||
}
|
||||
@@ -125,6 +139,31 @@ func enforceOperator(c *gin.Context, service Authorizer, userID int64) {
|
||||
c.Next()
|
||||
}
|
||||
|
||||
// abortForbidden logs why a request was denied (method, path, and any extra
|
||||
// fields the caller passes) before writing the 403, since appresult.ErrForbidden
|
||||
// alone gives the client and the logs no way to tell which rbac check failed.
|
||||
func abortForbidden(c *gin.Context, reason string, fields logrus.Fields) {
|
||||
entry := logging.GetLogger().WithFields(logrus.Fields{
|
||||
"method": c.Request.Method,
|
||||
"path": c.Request.URL.Path,
|
||||
})
|
||||
for k, v := range fields {
|
||||
entry = entry.WithField(k, v)
|
||||
}
|
||||
entry.Warn("rbac forbidden: " + reason)
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
}
|
||||
|
||||
// EffectiveGuard resolves the guard a role with the given code/guard_name
|
||||
// would run requests under - the same resolution Guard() applies to the
|
||||
// caller on every request. Exposed so callers that grant a role (e.g. the
|
||||
// user handler) can ask "would this reach guardAdmin?" against the role's
|
||||
// real guard_name instead of matching against a hardcoded list of role
|
||||
// names, which drifts the moment a new role is added.
|
||||
func EffectiveGuard(code, guardName string) string {
|
||||
return effectiveGuard(code, guardName)
|
||||
}
|
||||
|
||||
func effectiveGuard(code, guardName string) string {
|
||||
code = strings.ToLower(strings.TrimSpace(code))
|
||||
guard := strings.ToLower(strings.TrimSpace(guardName))
|
||||
@@ -132,7 +171,7 @@ func effectiveGuard(code, guardName string) string {
|
||||
switch code {
|
||||
case "admin", "superadmin", "manager":
|
||||
return guardAdmin
|
||||
case roleOperator:
|
||||
case roleOperator, "operatot":
|
||||
return guardOperator
|
||||
}
|
||||
|
||||
@@ -220,6 +259,8 @@ func enforceOwnWrites(c *gin.Context, userID int64) bool {
|
||||
}
|
||||
payload["userId"] = userID
|
||||
payload["user_id"] = userID
|
||||
payload["createdBy"] = userID
|
||||
payload["created_by"] = userID
|
||||
rewritten, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return true
|
||||
@@ -251,6 +292,7 @@ func scopeOwnedLists(c *gin.Context, userID int64) {
|
||||
|
||||
query := c.Request.URL.Query()
|
||||
query.Set("user_id", strconv.FormatInt(userID, 10))
|
||||
query.Set("created_by", strconv.FormatInt(userID, 10))
|
||||
c.Request.URL.RawQuery = query.Encode()
|
||||
}
|
||||
|
||||
@@ -319,6 +361,9 @@ var userCatalogPrefixes = []string{
|
||||
"/loan-types",
|
||||
"/loans-documents",
|
||||
"/currencies",
|
||||
"/branches",
|
||||
"/provinces",
|
||||
"/users/clients",
|
||||
}
|
||||
|
||||
var userSelfPrefixes = []string{
|
||||
@@ -328,6 +373,7 @@ var userSelfPrefixes = []string{
|
||||
|
||||
var userOwnedPrefixes = []string{
|
||||
"/cards",
|
||||
"/card-balances",
|
||||
"/card-orders",
|
||||
"/card-pin-orders",
|
||||
"/card-rekwizit-orders",
|
||||
@@ -362,6 +408,11 @@ func operatorProfileOrFile(path string) bool {
|
||||
return rest == "/profile" || rest == "/file" || strings.HasPrefix(rest, "/file/")
|
||||
}
|
||||
|
||||
func operatorClientsList(path string) bool {
|
||||
rest := adminREST(path)
|
||||
return rest == "/users/clients" || strings.HasPrefix(rest, "/users/clients/")
|
||||
}
|
||||
|
||||
func operatorWriteAllowed(c *gin.Context, service Authorizer, branchIDs []int64) bool {
|
||||
switch c.Request.Method {
|
||||
case http.MethodPost, http.MethodPut, http.MethodPatch:
|
||||
@@ -391,9 +442,21 @@ func operatorWriteAllowed(c *gin.Context, service Authorizer, branchIDs []int64)
|
||||
}
|
||||
|
||||
if payload.BranchId != nil && *payload.BranchId > 0 && !containsInt64(branchIDs, *payload.BranchId) {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
abortForbidden(c, "payload branchId not in operator's assigned branches", logrus.Fields{
|
||||
"requestedBranchId": *payload.BranchId,
|
||||
"allowedBranchIds": branchIDs,
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
// A new order's customer has no prior record in the branch to be
|
||||
// "visible" through yet — the branchId check above already scopes the
|
||||
// create to the operator's own branch, so only enforce user visibility
|
||||
// when reassigning an existing record (PUT/PATCH).
|
||||
if c.Request.Method == http.MethodPost {
|
||||
return true
|
||||
}
|
||||
|
||||
if payload.UserId != nil && *payload.UserId > 0 {
|
||||
visible, err := service.UserVisibleInBranches(c.Request.Context(), *payload.UserId, branchIDs)
|
||||
if err != nil {
|
||||
@@ -401,7 +464,10 @@ func operatorWriteAllowed(c *gin.Context, service Authorizer, branchIDs []int64)
|
||||
return false
|
||||
}
|
||||
if !visible {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, appresult.ErrForbidden)
|
||||
abortForbidden(c, "payload userId not visible in operator's branches", logrus.Fields{
|
||||
"requestedUserId": *payload.UserId,
|
||||
"allowedBranchIds": branchIDs,
|
||||
})
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -419,19 +485,65 @@ func adminREST(path string) string {
|
||||
return rest
|
||||
}
|
||||
|
||||
// operatorPathPermission returns "cards" or "loans" for saha endpoints.
|
||||
// Empty string: shared catalog (profile, file, dashboard, branches, provinces,
|
||||
// users/clients, currencies read) — any operator may call it.
|
||||
func operatorPathPermission(method, path string) string {
|
||||
rest := adminREST(path)
|
||||
|
||||
switch {
|
||||
case matchesPrefix(rest, []string{
|
||||
"/cards",
|
||||
"/card-balances",
|
||||
"/card-orders",
|
||||
"/card-pin-orders",
|
||||
"/card-rekwizit-orders",
|
||||
"/visa-master-payment-orders",
|
||||
"/payment-cards",
|
||||
"/reasons-cards",
|
||||
}):
|
||||
return "cards"
|
||||
|
||||
case matchesPrefix(rest, []string{
|
||||
"/loan-orders",
|
||||
"/loans-repayment",
|
||||
"/loan-paid-off-letters",
|
||||
"/loan-types",
|
||||
"/loans-documents",
|
||||
}):
|
||||
return "loans"
|
||||
|
||||
case matchesPrefix(rest, []string{"/users/clients"}):
|
||||
return ""
|
||||
|
||||
case matchesPrefix(rest, []string{"/users", "/branch-users"}):
|
||||
if method == http.MethodGet {
|
||||
return "users.view"
|
||||
}
|
||||
return "users.manage"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
var operatorWritePrefixes = []string{
|
||||
"/profile",
|
||||
"/file",
|
||||
"/users",
|
||||
"/branch-users",
|
||||
"/cards",
|
||||
"/card-balances",
|
||||
"/card-orders",
|
||||
"/card-pin-orders",
|
||||
"/card-rekwizit-orders",
|
||||
"/visa-master-payment-orders",
|
||||
"/payment-cards",
|
||||
"/reasons-cards",
|
||||
"/loan-orders",
|
||||
"/loans-repayment",
|
||||
"/loan-paid-off-letters",
|
||||
"/loan-types",
|
||||
"/loans-documents",
|
||||
}
|
||||
|
||||
var operatorGetPrefixes = []string{
|
||||
@@ -440,6 +552,7 @@ var operatorGetPrefixes = []string{
|
||||
"/dashboard",
|
||||
"/users",
|
||||
"/cards",
|
||||
"/card-balances",
|
||||
"/card-orders",
|
||||
"/card-pin-orders",
|
||||
"/card-rekwizit-orders",
|
||||
|
||||
Reference in New Issue
Block a user