admin

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package admin 提供管理员后台功能

Index

Constants

View Source
const (
	AdminRequired = "未经授权访问"
)

Variables

View Source
var (
	// ErrCannotDeactivateSelf 当前登录用户不能停用自己。/ The current user cannot deactivate themselves.
	ErrCannotDeactivateSelf = errors.New("不能停用当前登录用户")
	// ErrCannotRevokeOwnAdmin 当前登录用户不能取消自己的管理员权限。/ The current user cannot revoke their own admin permission.
	ErrCannotRevokeOwnAdmin = errors.New("不能取消当前登录用户的管理员权限")
)

Functions

func CreateUserHandler

func CreateUserHandler(c *gin.Context)

CreateUserHandler 创建用户 @Tags admin @Accept json @Produce json @Param request body CreateUserRequest true "创建用户请求" @Success 200 {object} CreateUserResponse @Router /api/v1/admin/users [post]

func DeleteUserHandler

func DeleteUserHandler(c *gin.Context)

DeleteUserHandler 删除用户 @Tags admin @Produce json @Param id path int true "用户ID" @Success 200 {object} DeleteUserResponse @Router /api/v1/admin/users/{id} [delete]

func GetUserHandler

func GetUserHandler(c *gin.Context)

GetUserHandler 获取单个用户 @Tags admin @Produce json @Param id path int true "用户ID" @Success 200 {object} GetUserResponse @Router /api/v1/admin/users/{id} [get]

func ListCLITokensHandler added in v1.2.0

func ListCLITokensHandler(c *gin.Context)

ListCLITokensHandler 列出全部 CLI 令牌元数据,不返回原文。 ListCLITokensHandler lists CLI token metadata and never returns plaintext. @Tags admin @Param request query ListCLITokensRequest true "request query" @Produce json @Success 200 {object} ListCLITokensResponse @Router /api/v1/admin/cli-tokens [get]

func ListUsersHandler

func ListUsersHandler(c *gin.Context)

ListUsersHandler 获取用户列表 @Tags admin @Param request query ListUsersRequest true "request query" @Produce json @Success 200 {object} ListUsersResponse @Router /api/v1/admin/users [get]

func LoginAdminRequired

func LoginAdminRequired() gin.HandlerFunc

LoginAdminRequired 管理员权限验证中间件

func UpdateUserHandler

func UpdateUserHandler(c *gin.Context)

UpdateUserHandler 更新用户 @Tags admin @Accept json @Produce json @Param id path int true "用户ID" @Param request body UpdateUserRequest true "更新用户请求" @Success 200 {object} UpdateUserResponse @Router /api/v1/admin/users/{id} [put]

func ValidateSelfUpdate added in v1.3.0

func ValidateSelfUpdate(actorID, targetID uint64, req *UpdateUserRequest) error

ValidateSelfUpdate 拒绝当前登录用户停用自己或取消自己的管理员权限。 用户管理接口要求调用者是已启用的管理员,因此只要拦住改自己,系统里就始终至少保留一个可登录的管理员; 否则唯一的管理员一旦停用自己,网页会话与 CLI 令牌都会被拒绝,只能改库恢复(2026-10-23 现场误点状态开关)。 ValidateSelfUpdate rejects the current user deactivating themselves or revoking their own admin permission. User management requires the caller to be an active admin, so blocking self-changes always leaves at least one admin who can sign in; otherwise a sole admin who deactivates themselves is rejected on both web sessions and CLI tokens and can only be recovered by editing the database (a mis-clicked status switch on 2026-10-23).

Types

type CreateCLITokenRequest added in v1.2.0

type CreateCLITokenRequest struct {
	UserID    uint64 `json:"user_id" binding:"required"`
	Name      string `json:"name" binding:"required,max=100"`
	ExpiresIn string `json:"expires_in"`
}

CreateCLITokenRequest 是管理员签发令牌请求。 CreateCLITokenRequest is the administrator token issue request.

type CreateCLITokenResponse added in v1.2.0

type CreateCLITokenResponse struct {
	ErrorMsg string                `json:"error_msg"`
	Data     *CreateCLITokenResult `json:"data"`
}

CreateCLITokenResponse 是管理员签发令牌响应。 CreateCLITokenResponse is the administrator token issue response.

type CreateCLITokenResult added in v1.2.0

type CreateCLITokenResult struct {
	auth.CLITokenInfo
	Token string `json:"token,omitempty"`
}

CreateCLITokenResult 是签发令牌后仅一次包含原文的结果。 CreateCLITokenResult is the issue result and includes plaintext only once.

func NewCreateCLITokenResult added in v1.2.0

func NewCreateCLITokenResult(issue *auth.CLITokenIssue, user *auth.User, now time.Time) *CreateCLITokenResult

NewCreateCLITokenResult 根据签发结果和所属用户组装一次性响应。 NewCreateCLITokenResult builds the one-time issue response for the owning user.

type CreateUserRequest

type CreateUserRequest struct {
	Username string `json:"username" binding:"required,min=3,max=50"`
	Password string `json:"password" binding:"required,min=6,max=100"`
	Nickname string `json:"nickname" binding:"max=100"`
	Email    string `json:"email" binding:"omitempty,max=255,email"`
	IsAdmin  bool   `json:"is_admin"`
}

CreateUserRequest 创建用户请求

type CreateUserResponse

type CreateUserResponse struct {
	ErrorMsg string         `json:"error_msg"`
	Data     *auth.UserInfo `json:"data"`
}

CreateUserResponse 创建用户响应

type DeleteUserResponse

type DeleteUserResponse struct {
	ErrorMsg string `json:"error_msg"`
	Data     any    `json:"data"`
}

DeleteUserResponse 删除用户响应

type GetUserResponse

type GetUserResponse struct {
	ErrorMsg string         `json:"error_msg"`
	Data     *auth.UserInfo `json:"data"`
}

GetUserResponse 获取用户响应

type ListCLITokensData added in v1.2.0

type ListCLITokensData struct {
	Total   int64               `json:"total"`
	Active  int64               `json:"active"`
	Expired int64               `json:"expired"`
	Revoked int64               `json:"revoked"`
	Tokens  []auth.CLITokenInfo `json:"tokens"`
}

ListCLITokensData 是令牌列表响应数据。 ListCLITokensData is the token list response payload.

type ListCLITokensRequest added in v1.2.0

type ListCLITokensRequest struct {
	Current  int    `json:"current" form:"current" binding:"min=1"`
	Size     int    `json:"size" form:"size" binding:"min=1,max=100"`
	UserID   uint64 `json:"user_id" form:"user_id"`
	Username string `json:"username" form:"username"`
	Name     string `json:"name" form:"name"`
	Status   string `json:"status" form:"status"`
}

ListCLITokensRequest 是管理员令牌列表请求。 ListCLITokensRequest is the administrator token list request.

type ListCLITokensResponse added in v1.2.0

type ListCLITokensResponse struct {
	ErrorMsg string             `json:"error_msg"`
	Data     *ListCLITokensData `json:"data"`
}

ListCLITokensResponse 是管理员令牌列表响应。 ListCLITokensResponse is the administrator token list response.

type ListUsersRequest

type ListUsersRequest struct {
	Current  int    `json:"current" form:"current" binding:"min=1"`
	Size     int    `json:"size" form:"size" binding:"min=1,max=100"`
	Username string `json:"username" form:"username"`
	IsActive *bool  `json:"is_active" form:"is_active"`
	IsAdmin  *bool  `json:"is_admin" form:"is_admin"`
}

ListUsersRequest 用户列表请求

type ListUsersResponse

type ListUsersResponse struct {
	ErrorMsg string `json:"error_msg"`
	Data     *struct {
		Total int64            `json:"total"`
		Users []*auth.UserInfo `json:"users"`
	} `json:"data"`
}

ListUsersResponse 用户列表响应

type RevokeCLITokenResponse added in v1.2.0

type RevokeCLITokenResponse struct {
	ErrorMsg string             `json:"error_msg"`
	Data     *auth.CLITokenInfo `json:"data"`
}

RevokeCLITokenResponse 是管理员吊销令牌响应。 RevokeCLITokenResponse is the administrator token revoke response.

type UpdateUserRequest

type UpdateUserRequest struct {
	Nickname string  `json:"nickname" binding:"max=100"`
	Password string  `json:"password" binding:"omitempty,min=6,max=100"`
	Email    *string `json:"email" binding:"omitempty,max=255,email"`
	IsActive *bool   `json:"is_active"`
	IsAdmin  *bool   `json:"is_admin"`
}

UpdateUserRequest 更新用户请求

type UpdateUserResponse

type UpdateUserResponse struct {
	ErrorMsg string         `json:"error_msg"`
	Data     *auth.UserInfo `json:"data"`
}

UpdateUserResponse 更新用户响应

Jump to

Keyboard shortcuts

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