mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-05 07:56:50 +08:00
42 lines
986 B
Go
42 lines
986 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"reflect"
|
|
|
|
"github.com/lzh-1625/go_process_manager/internal/app/constants"
|
|
"github.com/lzh-1625/go_process_manager/internal/app/repository"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func rOk(ctx *gin.Context, message string, data any) {
|
|
jsonData := map[string]any{
|
|
"code": 0,
|
|
"msg": message,
|
|
}
|
|
if data != nil {
|
|
jsonData["data"] = data
|
|
}
|
|
ctx.JSON(http.StatusOK, jsonData)
|
|
}
|
|
|
|
func getRole(ctx *gin.Context) constants.Role {
|
|
if v, ok := ctx.Get(constants.CTXFLG_ROLE); ok {
|
|
return v.(constants.Role)
|
|
}
|
|
return constants.ROLE_GUEST
|
|
}
|
|
|
|
func getUserName(ctx *gin.Context) string {
|
|
return ctx.GetString(constants.CTXFLG_USER_NAME)
|
|
}
|
|
|
|
func isAdmin(ctx *gin.Context) bool {
|
|
return getRole(ctx) <= constants.ROLE_ADMIN
|
|
}
|
|
|
|
func hasOprPermission(ctx *gin.Context, uuid int, op constants.OprPermission) bool {
|
|
return isAdmin(ctx) || reflect.ValueOf(repository.PermissionRepository.GetPermission(getUserName(ctx), uuid)).FieldByName(string(op)).Bool()
|
|
}
|