123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- // Copyright 2020 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package utils
- import (
- "access-control-monitor/errors"
- "strconv"
- "github.com/tidwall/gjson"
- "github.com/dgrijalva/jwt-go"
- "github.com/gin-gonic/gin"
- "github.com/jaryhe/gopkgs/logger"
- "go.uber.org/zap"
- )
- // GetJwtIdFromCtx 从 gin 上下文中获取 id
- func GetJwtIdFromCtx(ctx *gin.Context) (int64, error) {
- // 从上下文获取信息
- value, ok := ctx.Get("claims")
- if !ok {
- logger.Error("func",
- zap.String("call", "ctx.Get"),
- zap.String("error", "no claims data"))
- return 0, errors.ParamsError
- }
- // 解析数据
- claims, ok := value.(*jwt.StandardClaims)
- if !ok {
- logger.Error("func",
- zap.String("call", "Claims assert"),
- zap.String("error", "NOT Claims type"))
- return 0, errors.ParamsError
- }
- // 类型转换
- id, _ := strconv.ParseInt(claims.Id, 10, 64)
- return id, nil
- }
- func GetSupper(ctx *gin.Context) (bool, error) {
- value, ok := ctx.Get("claims")
- if !ok {
- logger.Error("func",
- zap.String("call", "ctx.Get"),
- zap.String("error", "no claims data"))
- return false, errors.ParamsError
- }
- // 解析数据
- claims, ok := value.(*jwt.StandardClaims)
- if !ok {
- logger.Error("func",
- zap.String("call", "Claims assert"),
- zap.String("error", "NOT Claims type"))
- return false, errors.ParamsError
- }
- supper := gjson.GetBytes(StrToBytes(claims.Subject), "supper").Bool()
- return supper, nil
- }
- // GetJwtIdFromCtx 从 gin 上下文中获取 id
- func GetJwtProjectIdFromCtx(ctx *gin.Context) (int64, error) {
- // 从上下文获取信息
- value, ok := ctx.Get("claims")
- if !ok {
- logger.Error("func",
- zap.String("call", "ctx.Get"),
- zap.String("error", "no claims data"))
- return 0, errors.ParamsError
- }
- // 解析数据
- claims, ok := value.(*jwt.StandardClaims)
- if !ok {
- logger.Error("func",
- zap.String("call", "Claims assert"),
- zap.String("error", "NOT Claims type"))
- return 0, errors.ParamsError
- }
- // 类型转换
- id := gjson.GetBytes(StrToBytes(claims.Subject), "project_id").Int()
- return id, nil
- }
|