rm -rf permission module
This commit is contained in:
1 parent
c6b9cb7462
commit
848a07324f
10 files changed
+193
-119
No files matched your search
@@ -155,6 +155,8 @@ INSERT INTO permissions (code, name, guard_name, module) VALUES
|
|||||||
('backups.manage', 'Manage backups', 'admin', 'backups'),
|
('backups.manage', 'Manage backups', 'admin', 'backups'),
|
||||||
('currencies.view', 'View currencies', 'admin', 'currencies'),
|
('currencies.view', 'View currencies', 'admin', 'currencies'),
|
||||||
('currencies.manage', 'Manage currencies', 'admin', 'currencies'),
|
('currencies.manage', 'Manage currencies', 'admin', 'currencies'),
|
||||||
|
('payment-settings.view', 'View payment settings', 'admin', 'payment-settings'),
|
||||||
|
('payment-settings.manage', 'Manage payment settings', 'admin', 'payment-settings'),
|
||||||
-- operator: two sahalar only
|
-- operator: two sahalar only
|
||||||
('cards', 'Cards', 'operator', 'cards'),
|
('cards', 'Cards', 'operator', 'cards'),
|
||||||
('loans', 'Loans', 'operator', 'loans');
|
('loans', 'Loans', 'operator', 'loans');
|
||||||
|
|||||||
+12
-9
@@ -4977,14 +4977,14 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/permission-roles": {
|
"/permission-roles/bulk": {
|
||||||
"post": {
|
"post": {
|
||||||
"security": [
|
"security": [
|
||||||
{
|
{
|
||||||
"BearerAuth": []
|
"BearerAuth": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"description": "Creates a permission-role assignment, or updates the existing pair if already assigned",
|
"description": "Replaces roleId's whole permission set with permissionIds - pairs not listed are removed, missing ones are added, an empty list clears all permissions from the role",
|
||||||
"consumes": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
@@ -4994,15 +4994,15 @@ const docTemplate = `{
|
|||||||
"tags": [
|
"tags": [
|
||||||
"permission-roles"
|
"permission-roles"
|
||||||
],
|
],
|
||||||
"summary": "Assign permission to role",
|
"summary": "Set a role's permissions",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"description": "Permission-role payload",
|
"description": "Role + the permission IDs it should have",
|
||||||
"name": "body",
|
"name": "body",
|
||||||
"in": "body",
|
"in": "body",
|
||||||
"required": true,
|
"required": true,
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/permissionrole.CreatePermissionRoleDTO"
|
"$ref": "#/definitions/permissionrole.SetRolePermissionsDTO"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -9359,15 +9359,18 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"permissionrole.CreatePermissionRoleDTO": {
|
"permissionrole.SetRolePermissionsDTO": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
"permissionId",
|
"permissionIds",
|
||||||
"roleId"
|
"roleId"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"permissionId": {
|
"permissionIds": {
|
||||||
"type": "integer"
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"roleId": {
|
"roleId": {
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
|
|||||||
+12
-9
@@ -4971,14 +4971,14 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/permission-roles": {
|
"/permission-roles/bulk": {
|
||||||
"post": {
|
"post": {
|
||||||
"security": [
|
"security": [
|
||||||
{
|
{
|
||||||
"BearerAuth": []
|
"BearerAuth": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"description": "Creates a permission-role assignment, or updates the existing pair if already assigned",
|
"description": "Replaces roleId's whole permission set with permissionIds - pairs not listed are removed, missing ones are added, an empty list clears all permissions from the role",
|
||||||
"consumes": [
|
"consumes": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
@@ -4988,15 +4988,15 @@
|
|||||||
"tags": [
|
"tags": [
|
||||||
"permission-roles"
|
"permission-roles"
|
||||||
],
|
],
|
||||||
"summary": "Assign permission to role",
|
"summary": "Set a role's permissions",
|
||||||
"parameters": [
|
"parameters": [
|
||||||
{
|
{
|
||||||
"description": "Permission-role payload",
|
"description": "Role + the permission IDs it should have",
|
||||||
"name": "body",
|
"name": "body",
|
||||||
"in": "body",
|
"in": "body",
|
||||||
"required": true,
|
"required": true,
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/permissionrole.CreatePermissionRoleDTO"
|
"$ref": "#/definitions/permissionrole.SetRolePermissionsDTO"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -9353,15 +9353,18 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"permissionrole.CreatePermissionRoleDTO": {
|
"permissionrole.SetRolePermissionsDTO": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
"permissionId",
|
"permissionIds",
|
||||||
"roleId"
|
"roleId"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"permissionId": {
|
"permissionIds": {
|
||||||
"type": "integer"
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"roleId": {
|
"roleId": {
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
|
|||||||
+40
-37
@@ -1364,14 +1364,16 @@ definitions:
|
|||||||
- module
|
- module
|
||||||
- name
|
- name
|
||||||
type: object
|
type: object
|
||||||
permissionrole.CreatePermissionRoleDTO:
|
permissionrole.SetRolePermissionsDTO:
|
||||||
properties:
|
properties:
|
||||||
permissionId:
|
permissionIds:
|
||||||
type: integer
|
items:
|
||||||
|
type: integer
|
||||||
|
type: array
|
||||||
roleId:
|
roleId:
|
||||||
type: integer
|
type: integer
|
||||||
required:
|
required:
|
||||||
- permissionId
|
- permissionIds
|
||||||
- roleId
|
- roleId
|
||||||
type: object
|
type: object
|
||||||
province.CreateProvinceDTO:
|
province.CreateProvinceDTO:
|
||||||
@@ -4941,39 +4943,6 @@ paths:
|
|||||||
summary: Update payment settings
|
summary: Update payment settings
|
||||||
tags:
|
tags:
|
||||||
- payment-settings
|
- payment-settings
|
||||||
/permission-roles:
|
|
||||||
post:
|
|
||||||
consumes:
|
|
||||||
- application/json
|
|
||||||
description: Creates a permission-role assignment, or updates the existing pair
|
|
||||||
if already assigned
|
|
||||||
parameters:
|
|
||||||
- description: Permission-role payload
|
|
||||||
in: body
|
|
||||||
name: body
|
|
||||||
required: true
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/permissionrole.CreatePermissionRoleDTO'
|
|
||||||
produces:
|
|
||||||
- application/json
|
|
||||||
responses:
|
|
||||||
"201":
|
|
||||||
description: Created
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/appresult.AppSuccess'
|
|
||||||
"400":
|
|
||||||
description: Bad Request
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/appresult.AppError'
|
|
||||||
"500":
|
|
||||||
description: Internal Server Error
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/appresult.AppError'
|
|
||||||
security:
|
|
||||||
- BearerAuth: []
|
|
||||||
summary: Assign permission to role
|
|
||||||
tags:
|
|
||||||
- permission-roles
|
|
||||||
/permission-roles/{id}:
|
/permission-roles/{id}:
|
||||||
delete:
|
delete:
|
||||||
description: Deletes a permission-role assignment
|
description: Deletes a permission-role assignment
|
||||||
@@ -5007,6 +4976,40 @@ paths:
|
|||||||
summary: Remove permission from role
|
summary: Remove permission from role
|
||||||
tags:
|
tags:
|
||||||
- permission-roles
|
- permission-roles
|
||||||
|
/permission-roles/bulk:
|
||||||
|
post:
|
||||||
|
consumes:
|
||||||
|
- application/json
|
||||||
|
description: Replaces roleId's whole permission set with permissionIds - pairs
|
||||||
|
not listed are removed, missing ones are added, an empty list clears all permissions
|
||||||
|
from the role
|
||||||
|
parameters:
|
||||||
|
- description: Role + the permission IDs it should have
|
||||||
|
in: body
|
||||||
|
name: body
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/permissionrole.SetRolePermissionsDTO'
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"201":
|
||||||
|
description: Created
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/appresult.AppSuccess'
|
||||||
|
"400":
|
||||||
|
description: Bad Request
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/appresult.AppError'
|
||||||
|
"500":
|
||||||
|
description: Internal Server Error
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/appresult.AppError'
|
||||||
|
security:
|
||||||
|
- BearerAuth: []
|
||||||
|
summary: Set a role's permissions
|
||||||
|
tags:
|
||||||
|
- permission-roles
|
||||||
/permissions:
|
/permissions:
|
||||||
get:
|
get:
|
||||||
description: Returns paginated permissions with optional filters
|
description: Returns paginated permissions with optional filters
|
||||||
|
|||||||
@@ -145,10 +145,6 @@ func (h *handler) create(c *gin.Context) {
|
|||||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !IsKnownModule(dto.Module) {
|
|
||||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
item, err := h.repository.Create(c.Request.Context(), dto)
|
item, err := h.repository.Create(c.Request.Context(), dto)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -186,10 +182,6 @@ func (h *handler) update(c *gin.Context) {
|
|||||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !IsKnownModule(dto.Module) {
|
|
||||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
item, err := h.repository.Update(c.Request.Context(), id, dto)
|
item, err := h.repository.Update(c.Request.Context(), id, dto)
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
package permission
|
package permission
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ModuleDashboard = "dashboard"
|
ModuleDashboard = "dashboard"
|
||||||
ModuleCards = "cards"
|
ModuleCards = "cards"
|
||||||
ModuleLoans = "loans"
|
ModuleLoans = "loans"
|
||||||
ModuleUsers = "users"
|
ModuleUsers = "users"
|
||||||
ModuleRoles = "roles"
|
ModuleRoles = "roles"
|
||||||
ModuleBranches = "branches"
|
ModuleBranches = "branches"
|
||||||
ModuleProvinces = "provinces"
|
ModuleProvinces = "provinces"
|
||||||
ModuleActivityLogs = "activity-logs"
|
ModuleActivityLogs = "activity-logs"
|
||||||
ModuleBackups = "backups"
|
ModuleBackups = "backups"
|
||||||
ModuleCurrencies = "currencies"
|
ModuleCurrencies = "currencies"
|
||||||
|
ModulePaymentSettings = "payment-settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// moduleOrder is a display-order hint only, not a whitelist: the
|
||||||
|
// permissions.module field accepts any value (see permission.Repository.
|
||||||
|
// Modules, which lists what's actually in use). BuildTree/GroupProfileModules
|
||||||
|
// sort these first, then append any other module alphabetically, so a
|
||||||
|
// newly invented module still renders correctly without editing this file.
|
||||||
var moduleOrder = []string{
|
var moduleOrder = []string{
|
||||||
ModuleDashboard,
|
ModuleDashboard,
|
||||||
ModuleCards,
|
ModuleCards,
|
||||||
@@ -24,28 +30,11 @@ var moduleOrder = []string{
|
|||||||
ModuleActivityLogs,
|
ModuleActivityLogs,
|
||||||
ModuleBackups,
|
ModuleBackups,
|
||||||
ModuleCurrencies,
|
ModuleCurrencies,
|
||||||
|
ModulePaymentSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
var guardOrder = []string{"operator", "admin", "web"}
|
var guardOrder = []string{"operator", "admin", "web"}
|
||||||
|
|
||||||
var knownModules = map[string]struct{}{
|
|
||||||
ModuleDashboard: {},
|
|
||||||
ModuleCards: {},
|
|
||||||
ModuleLoans: {},
|
|
||||||
ModuleUsers: {},
|
|
||||||
ModuleRoles: {},
|
|
||||||
ModuleBranches: {},
|
|
||||||
ModuleProvinces: {},
|
|
||||||
ModuleActivityLogs: {},
|
|
||||||
ModuleBackups: {},
|
|
||||||
ModuleCurrencies: {},
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsKnownModule(module string) bool {
|
|
||||||
_, ok := knownModules[module]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// ModuleOrder is the display order of sahalar in trees and profile.
|
// ModuleOrder is the display order of sahalar in trees and profile.
|
||||||
func ModuleOrder() []string {
|
func ModuleOrder() []string {
|
||||||
out := make([]string, len(moduleOrder))
|
out := make([]string, len(moduleOrder))
|
||||||
|
|||||||
@@ -21,22 +21,64 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) permissionr
|
|||||||
return &repository{client: client, logger: logger}
|
return &repository{client: client, logger: logger}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *repository) Create(ctx context.Context, dto permissionrole.CreatePermissionRoleDTO) (permissionrole.PermissionRoleDTO, error) {
|
// SetPermissions replaces dto.RoleId's whole permission set with
|
||||||
query := `
|
// dto.PermissionIds inside one transaction: pairs not in the list are
|
||||||
WITH upserted AS (
|
// soft-deleted, pairs in the list are upserted. Both statements run on the
|
||||||
INSERT INTO permission_role (permission_id, role_id)
|
// same tx so a role never ends up with permissions only half-updated.
|
||||||
VALUES ($1, $2)
|
func (r *repository) SetPermissions(ctx context.Context, dto permissionrole.SetRolePermissionsDTO) ([]permissionrole.PermissionRoleDTO, error) {
|
||||||
ON CONFLICT (permission_id, role_id) WHERE deleted_at IS NULL DO UPDATE SET
|
tx, err := r.client.Begin(ctx)
|
||||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
if err != nil {
|
||||||
RETURNING id, permission_id, role_id, created_at, updated_at
|
return nil, err
|
||||||
)
|
}
|
||||||
SELECT upserted.id, upserted.permission_id, p.name, upserted.role_id, r.name, upserted.created_at, upserted.updated_at
|
defer tx.Rollback(ctx)
|
||||||
FROM upserted
|
|
||||||
JOIN permissions p ON p.id = upserted.permission_id
|
|
||||||
JOIN roles r ON r.id = upserted.role_id`
|
|
||||||
|
|
||||||
row := r.client.QueryRow(ctx, query, dto.PermissionId, dto.RoleId)
|
_, err = tx.Exec(ctx,
|
||||||
return scanPermissionRole(row)
|
`UPDATE permission_role SET deleted_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||||
|
WHERE role_id = $1 AND deleted_at IS NULL AND NOT (permission_id = ANY($2::bigint[]))`,
|
||||||
|
dto.RoleId, dto.PermissionIds,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var items []permissionrole.PermissionRoleDTO
|
||||||
|
if len(dto.PermissionIds) > 0 {
|
||||||
|
query := `
|
||||||
|
WITH upserted AS (
|
||||||
|
INSERT INTO permission_role (permission_id, role_id)
|
||||||
|
SELECT unnest($1::bigint[]), $2
|
||||||
|
ON CONFLICT (permission_id, role_id) WHERE deleted_at IS NULL DO UPDATE SET
|
||||||
|
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||||
|
RETURNING id, permission_id, role_id, created_at, updated_at
|
||||||
|
)
|
||||||
|
SELECT upserted.id, upserted.permission_id, p.name, upserted.role_id, r.name, upserted.created_at, upserted.updated_at
|
||||||
|
FROM upserted
|
||||||
|
JOIN permissions p ON p.id = upserted.permission_id
|
||||||
|
JOIN roles r ON r.id = upserted.role_id`
|
||||||
|
|
||||||
|
rows, err := tx.Query(ctx, query, dto.PermissionIds, dto.RoleId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for rows.Next() {
|
||||||
|
item, err := scanPermissionRole(rows)
|
||||||
|
if err != nil {
|
||||||
|
rows.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, item)
|
||||||
|
}
|
||||||
|
rowsErr := rows.Err()
|
||||||
|
rows.Close()
|
||||||
|
if rowsErr != nil {
|
||||||
|
return nil, rowsErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *repository) Delete(ctx context.Context, id int64) error {
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||||
|
|||||||
@@ -12,7 +12,13 @@ type PermissionRoleDTO struct {
|
|||||||
UpdatedAt time.Time `json:"updatedAt"`
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreatePermissionRoleDTO struct {
|
// SetRolePermissionsDTO replaces a role's whole permission set: after the
|
||||||
PermissionId int64 `json:"permissionId" binding:"required"`
|
// call, the role is assigned exactly PermissionIds - pairs not listed are
|
||||||
RoleId int64 `json:"roleId" binding:"required"`
|
// removed, pairs listed but missing are added, and pairs already present
|
||||||
|
// are left as-is. An empty PermissionIds clears all permissions from the
|
||||||
|
// role. This is the only way to attach/update a role's permissions - there
|
||||||
|
// is no single-pair create endpoint.
|
||||||
|
type SetRolePermissionsDTO struct {
|
||||||
|
RoleId int64 `json:"roleId" binding:"required"`
|
||||||
|
PermissionIds []int64 `json:"permissionIds" binding:"required"`
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,16 @@
|
|||||||
package permissionrole
|
package permissionrole
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"online-back/internal/appresult"
|
"online-back/internal/appresult"
|
||||||
"online-back/internal/handlers"
|
"online-back/internal/handlers"
|
||||||
"online-back/internal/shared"
|
"online-back/internal/shared"
|
||||||
"online-back/pkg/logging"
|
"online-back/pkg/logging"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/jackc/pgx/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
const baseURL = "/permission-roles"
|
const baseURL = "/permission-roles"
|
||||||
@@ -22,24 +26,37 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler
|
|||||||
|
|
||||||
func (h *handler) Register(router *gin.RouterGroup) {
|
func (h *handler) Register(router *gin.RouterGroup) {
|
||||||
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
||||||
group.POST("", h.create)
|
group.POST("/bulk", h.setPermissions)
|
||||||
group.DELETE("/:id", h.delete)
|
group.DELETE("/:id", h.delete)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create godoc
|
// setPermissions godoc
|
||||||
// @Summary Assign permission to role
|
// @Summary Set a role's permissions
|
||||||
// @Description Creates a permission-role assignment, or updates the existing pair if already assigned
|
// @Description Replaces roleId's whole permission set with permissionIds - pairs not listed are removed, missing ones are added, an empty list clears all permissions from the role
|
||||||
// @Tags permission-roles
|
// @Tags permission-roles
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
// @Param body body CreatePermissionRoleDTO true "Permission-role payload"
|
// @Param body body SetRolePermissionsDTO true "Role + the permission IDs it should have"
|
||||||
// @Success 201 {object} appresult.AppSuccess
|
// @Success 201 {object} appresult.AppSuccess
|
||||||
// @Failure 400 {object} appresult.AppError
|
// @Failure 400 {object} appresult.AppError
|
||||||
// @Failure 500 {object} appresult.AppError
|
// @Failure 500 {object} appresult.AppError
|
||||||
// @Router /permission-roles [post]
|
// @Router /permission-roles/bulk [post]
|
||||||
func (h *handler) create(c *gin.Context) {
|
func (h *handler) setPermissions(c *gin.Context) {
|
||||||
shared.CreateAssoc[CreatePermissionRoleDTO, PermissionRoleDTO](c, h.logger, h.repository, "permission role")
|
var dto SetRolePermissionsDTO
|
||||||
|
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := h.repository.SetPermissions(c.Request.Context(), dto)
|
||||||
|
if err != nil {
|
||||||
|
h.logger.Errorf("set role permissions failed: %v", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, appresult.SuccessWithData(items))
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete godoc
|
// delete godoc
|
||||||
@@ -55,5 +72,20 @@ func (h *handler) create(c *gin.Context) {
|
|||||||
// @Failure 500 {object} appresult.AppError
|
// @Failure 500 {object} appresult.AppError
|
||||||
// @Router /permission-roles/{id} [delete]
|
// @Router /permission-roles/{id} [delete]
|
||||||
func (h *handler) delete(c *gin.Context) {
|
func (h *handler) delete(c *gin.Context) {
|
||||||
shared.DeleteAssoc[CreatePermissionRoleDTO, PermissionRoleDTO](c, h.logger, h.repository, "permission role")
|
id, ok := shared.ParseID(c.Param("id"))
|
||||||
|
if !ok {
|
||||||
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
h.logger.Errorf("delete permission role failed: %v", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, appresult.SuccessWithData("permission role removed"))
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,8 @@ package permissionrole
|
|||||||
import "context"
|
import "context"
|
||||||
|
|
||||||
type Repository interface {
|
type Repository interface {
|
||||||
Create(ctx context.Context, dto CreatePermissionRoleDTO) (PermissionRoleDTO, error)
|
// SetPermissions replaces the role's permission set. See
|
||||||
|
// SetRolePermissionsDTO for the exact (add/remove/clear) semantics.
|
||||||
|
SetPermissions(ctx context.Context, dto SetRolePermissionsDTO) ([]PermissionRoleDTO, error)
|
||||||
Delete(ctx context.Context, id int64) error
|
Delete(ctx context.Context, id int64) error
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user