Bind permissions to roles and scope operator access by branch.
Replace permission_user with permission_role, enforce RBAC on admin routes, and limit operators to their assigned branches. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
427d5b4e07
commit
1fd0abbcb2
57 files changed
+1281
-657
No files matched your search
@@ -50,6 +50,16 @@ func (r *repository) Create(ctx context.Context, dto userbranch.CreateUserBranch
|
||||
return scanUserBranch(row)
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (userbranch.UserBranchDTO, error) {
|
||||
query := `
|
||||
SELECT bu.id, bu.branch_id, b.name, bu.user_id, u.name, bu.created_at, bu.updated_at
|
||||
FROM branch_user bu
|
||||
JOIN branches b ON b.id = bu.branch_id
|
||||
JOIN users u ON u.id = bu.user_id
|
||||
WHERE bu.id = $1`
|
||||
return scanUserBranch(r.client.QueryRow(ctx, query, id))
|
||||
}
|
||||
|
||||
func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM branch_user WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -78,6 +79,21 @@ func (h *handler) delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if rbac.HasBranchScope(c) {
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if !rbac.GuardBranch(c, item.BranchId) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
|
||||
@@ -4,5 +4,6 @@ import "context"
|
||||
|
||||
type Repository interface {
|
||||
Create(ctx context.Context, dto CreateUserBranchDTO) (UserBranchDTO, error)
|
||||
GetByID(ctx context.Context, id int64) (UserBranchDTO, error)
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
Reference in new issue
Block a user