role

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRoleModule

func NewRoleModule(logger logging.Logger, deps *core.Dependencies) core.Module

func SetupRoleRoutes

func SetupRoleRoutes(r chi.Router, h *RoleHandler, jwtMiddleware func(http.Handler) http.Handler)

SetupRoleRoutes registers role management routes.

Types

type CopyRoleRequest

type CopyRoleRequest struct {
	Name string `json:"name" binding:"required"`
	Code string `json:"code" binding:"required"`
}

CopyRoleRequest represents the request body for cloning a role.

type CreateRoleInput

type CreateRoleInput struct {
	Name             string
	Code             string
	Description      string
	Sort             int
	ParentID         *uuid.UUID
	Permissions      []string
	DefaultDataScope string
}

type CreateRoleRequest

type CreateRoleRequest struct {
	Name             string     `json:"name" binding:"required"`
	Code             string     `json:"code" binding:"required"`
	DefaultDataScope string     `json:"defaultDataScope,omitempty"`
	Description      string     `json:"description,omitempty"`
	Sort             int        `json:"sort"`
	ParentID         *uuid.UUID `json:"parentId,omitempty"` // 父角色 ID(可选)
	Permissions      []string   `json:"permissions,omitempty"`
}

CreateRoleRequest represents the request body for creating a role.

type DeleteRoleDataScopeRuleRequest

type DeleteRoleDataScopeRuleRequest struct {
	Domain      string `json:"domain"`
	ResourceKey string `json:"resourceKey"`
}

type RoleDataScopeRule

type RoleDataScopeRule struct {
	Domain      string `json:"domain"`
	ResourceKey string `json:"resourceKey"`
	ScopeType   string `json:"scopeType"`
	ScopeValue  string `json:"scopeValue,omitempty"`
}

type RoleDataScopeRuleInput

type RoleDataScopeRuleInput struct {
	Domain      string `json:"domain"`
	ResourceKey string `json:"resourceKey"`
	ScopeType   string `json:"scopeType"`
	ScopeValue  string `json:"scopeValue,omitempty"`
}

type RoleHandler

type RoleHandler struct {
	// contains filtered or unexported fields
}

func NewRoleHandler

func NewRoleHandler(roleService *RoleService, logger logging.Logger) *RoleHandler

func (*RoleHandler) CopyRole

func (h *RoleHandler) CopyRole(w http.ResponseWriter, r *http.Request)

CopyRole handles cloning a role. @Summary Clone a role @Tags Roles @Accept json @Produce json @Param id path string true "Source Role ID" @Param request body CopyRoleRequest true "Cloning data" @Success 200 {object} ent.Role @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id}/copy [post]

func (*RoleHandler) CreateRole

func (h *RoleHandler) CreateRole(w http.ResponseWriter, r *http.Request)

CreateRole handles role creation. @Summary Create a role @Tags Roles @Accept json @Produce json @Param request body CreateRoleRequest true "Role data" @Success 201 {object} ent.Role @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles [post]

func (*RoleHandler) DeleteRole

func (h *RoleHandler) DeleteRole(w http.ResponseWriter, r *http.Request)

DeleteRole handles role deletion. @Summary Delete a role @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Success 200 {object} map[string]string @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id} [delete]

func (*RoleHandler) DeleteRoleDataScopeRule

func (h *RoleHandler) DeleteRoleDataScopeRule(w http.ResponseWriter, r *http.Request)

DeleteRoleDataScopeRule handles deleting role p2 data scope rule by domain + resourceKey. @Summary Delete role data scope rule @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Param request body DeleteRoleDataScopeRuleRequest true "Delete rule input" @Success 200 {object} map[string]string @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id}/data-scope-rules [delete]

func (*RoleHandler) GetRole

func (h *RoleHandler) GetRole(w http.ResponseWriter, r *http.Request)

GetRole handles getting role details. @Summary Get role details @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Success 200 {object} ent.Role @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id} [get]

func (*RoleHandler) GetRoleChildren

func (h *RoleHandler) GetRoleChildren(w http.ResponseWriter, r *http.Request)

GetRoleChildren handles getting child roles. @Summary Get child roles @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Param recursive query bool false "Include all descendants" default(false) @Success 200 {object} []ent.Role @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id}/children [get]

func (*RoleHandler) GetRoleParents

func (h *RoleHandler) GetRoleParents(w http.ResponseWriter, r *http.Request)

GetRoleParents handles getting parent role chain. @Summary Get parent role chain @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Success 200 {object} []ent.Role @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id}/parents [get]

func (*RoleHandler) GetRoleTree

func (h *RoleHandler) GetRoleTree(w http.ResponseWriter, r *http.Request)

GetRoleTree handles getting role tree structure. @Summary Get role tree structure @Tags Roles @Accept json @Produce json @Success 200 {object} []RoleTreeNode @Failure 500 {object} responder.Error @Router /roles/tree [get]

func (*RoleHandler) ListRoleDataScopeRules

func (h *RoleHandler) ListRoleDataScopeRules(w http.ResponseWriter, r *http.Request)

ListRoleDataScopeRules handles listing role p2 data scope rules. @Summary List role data scope rules @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Success 200 {array} RoleDataScopeRule @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id}/data-scope-rules [get]

func (*RoleHandler) ListRoleUsers

func (h *RoleHandler) ListRoleUsers(w http.ResponseWriter, r *http.Request)

ListRoleUsers handles listing users in a role. @Summary List users in a role @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Param page query int false "Page number" default(1) @Param pageSize query int false "Page size" default(20) @Success 200 {object} responder.Response @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id}/users [get]

func (*RoleHandler) ListRoles

func (h *RoleHandler) ListRoles(w http.ResponseWriter, r *http.Request)

ListRoles handles listing roles. @Summary List roles @Tags Roles @Accept json @Produce json @Param page query int false "Page number" default(1) @Param pageSize query int false "Page size" default(20) @Success 200 {object} responder.Response @Failure 500 {object} responder.Error @Router /roles [get]

func (*RoleHandler) SetRoleMenus

func (h *RoleHandler) SetRoleMenus(w http.ResponseWriter, r *http.Request)

SetRoleMenus handles updating role menu permissions. @Summary Set role menu permissions @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Param request body SetRoleMenusRequest true "Menu permissions" @Success 200 {object} map[string]string @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id}/menus [post]

func (*RoleHandler) SetRolePermissions

func (h *RoleHandler) SetRolePermissions(w http.ResponseWriter, r *http.Request)

SetRolePermissions handles updating role permission codes. @Summary Set role permission codes @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Param request body SetRolePermissionsRequest true "Permission codes" @Success 200 {object} map[string]string @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id}/permissions [post]

func (*RoleHandler) UpdateRole

func (h *RoleHandler) UpdateRole(w http.ResponseWriter, r *http.Request)

UpdateRole handles role update. @Summary Update a role @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Param request body UpdateRoleRequest true "Role data" @Success 200 {object} ent.Role @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id} [put]

func (*RoleHandler) UpsertRoleDataScopeRule

func (h *RoleHandler) UpsertRoleDataScopeRule(w http.ResponseWriter, r *http.Request)

UpsertRoleDataScopeRule handles creating or updating a role p2 data scope rule. @Summary Upsert role data scope rule @Tags Roles @Accept json @Produce json @Param id path string true "Role ID" @Param request body RoleDataScopeRuleInput true "Data scope rule" @Success 200 {object} map[string]string @Failure 400 {object} responder.Error @Failure 500 {object} responder.Error @Router /roles/{id}/data-scope-rules [put]

type RoleModule

type RoleModule struct {
	// contains filtered or unexported fields
}

func (*RoleModule) Name

func (m *RoleModule) Name() string

func (*RoleModule) RegisterPrivateRoutes

func (m *RoleModule) RegisterPrivateRoutes(r chi.Router)

RegisterPrivateRoutes registers protected role endpoints

func (*RoleModule) RegisterPublicRoutes

func (m *RoleModule) RegisterPublicRoutes(r chi.Router)

RegisterPublicRoutes - role module has no public routes

type RoleService

type RoleService struct {
	// contains filtered or unexported fields
}

func NewRoleService

func NewRoleService(client *ent.Client, logger logging.Logger, rbacManager *rbac.RBACManager) *RoleService

func (*RoleService) CopyRole

func (s *RoleService) CopyRole(ctx context.Context, sourceID uuid.UUID, newName, newCode string) (*ent.Role, error)

CopyRole clones a role and its permissions to a new role.

func (*RoleService) CreateRole

func (s *RoleService) CreateRole(
	ctx context.Context,
	name, code, description string,
	sort int,
	parentID *uuid.UUID,
	permissionCodes []string,
) (*ent.Role, error)

CreateRole creates a new role.

func (*RoleService) CreateRoleV2

func (s *RoleService) CreateRoleV2(ctx context.Context, in CreateRoleInput) (*ent.Role, error)

func (*RoleService) DeleteRole

func (s *RoleService) DeleteRole(ctx context.Context, id uuid.UUID) error

DeleteRole deletes a role if it's not a system role and has no associated users.

func (*RoleService) DeleteRoleDataScopeRule

func (s *RoleService) DeleteRoleDataScopeRule(ctx context.Context, roleID uuid.UUID, domain, resourceKey string) error

func (*RoleService) GetRole

func (s *RoleService) GetRole(ctx context.Context, id uuid.UUID) (*ent.Role, error)

GetRole retrieves a role by ID with menus loaded.

func (*RoleService) GetRoleChildren

func (s *RoleService) GetRoleChildren(ctx context.Context, roleID uuid.UUID, recursive bool) ([]*ent.Role, error)

GetRoleChildren retrieves all child roles of a given role.

func (*RoleService) GetRoleParents

func (s *RoleService) GetRoleParents(ctx context.Context, roleID uuid.UUID) ([]*ent.Role, error)

GetRoleParents retrieves the parent role chain of a given role.

func (*RoleService) GetRoleTree

func (s *RoleService) GetRoleTree(ctx context.Context) ([]*RoleTreeNode, error)

GetRoleTree retrieves all roles as a tree structure.

func (*RoleService) ListRoleDataScopeRules

func (s *RoleService) ListRoleDataScopeRules(ctx context.Context, roleID uuid.UUID) ([]RoleDataScopeRule, error)

func (*RoleService) ListRoleUsers

func (s *RoleService) ListRoleUsers(ctx context.Context, roleID uuid.UUID, page, pageSize int) ([]*ent.User, int, error)

ListRoleUsers retrieves users associated with a role.

func (*RoleService) ListRoles

func (s *RoleService) ListRoles(ctx context.Context, page, pageSize int) ([]*ent.Role, int, error)

ListRoles retrieves all roles with pagination.

func (*RoleService) SetRoleMenus

func (s *RoleService) SetRoleMenus(ctx context.Context, roleID uuid.UUID, menuIDs []uuid.UUID) error

SetRoleMenus updates the menus associated with a role.

func (*RoleService) SetRolePermissions

func (s *RoleService) SetRolePermissions(ctx context.Context, roleID uuid.UUID, codes []string) error

SetRolePermissions updates the permission codes associated with a role.

func (*RoleService) UpdateRole

func (s *RoleService) UpdateRole(ctx context.Context, id uuid.UUID, name, description string, sort int, parentID *uuid.UUID) (*ent.Role, error)

UpdateRole updates an existing role.

func (*RoleService) UpdateRoleV2

func (s *RoleService) UpdateRoleV2(ctx context.Context, in UpdateRoleInput) (*ent.Role, error)

func (*RoleService) UpsertRoleDataScopeRule

func (s *RoleService) UpsertRoleDataScopeRule(ctx context.Context, roleID uuid.UUID, in RoleDataScopeRuleInput) error

type RoleTreeNode

type RoleTreeNode struct {
	ID          uuid.UUID       `json:"id"`
	Name        string          `json:"name"`
	Code        string          `json:"code"`
	Description string          `json:"description,omitempty"`
	Sort        int             `json:"sort"`
	IsSystem    bool            `json:"isSystem"`
	ParentID    *uuid.UUID      `json:"parentId,omitempty"`
	Children    []*RoleTreeNode `json:"children,omitempty"`
}

RoleTreeNode represents a role node in the tree structure.

type SetRoleMenusRequest

type SetRoleMenusRequest struct {
	MenuIDs []uuid.UUID `json:"menuIds" binding:"required"`
}

SetRoleMenusRequest represents the request body for setting role menu permissions.

type SetRolePermissionsRequest

type SetRolePermissionsRequest struct {
	PermissionCodes []string `json:"permissionCodes" binding:"required"`
}

SetRolePermissionsRequest represents the request body for setting role permission codes.

type UpdateRoleInput

type UpdateRoleInput struct {
	ID               uuid.UUID
	Name             *string
	Description      *string
	Sort             *int
	ParentID         *uuid.UUID
	HasParentID      bool
	DefaultDataScope *string
}

type UpdateRoleRequest

type UpdateRoleRequest struct {
	Name             *string    `json:"name,omitempty"`
	Description      *string    `json:"description,omitempty"`
	Sort             *int       `json:"sort,omitempty"`
	ParentID         *uuid.UUID `json:"parentId,omitempty"` // 父角色 ID(可选,nil 表示移除父角色)
	DefaultDataScope *string    `json:"defaultDataScope,omitempty"`
}

UpdateRoleRequest represents the request body for updating a role.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL