cvc/src/loreal.com/dit/cmd/coupon-service/base/requester.go

34 lines
911 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package base
import (
"github.com/chenhg5/collection"
"github.com/dgrijalva/jwt-go"
)
const ROLE_COUPON_ISSUER string = "coupon_issuer"
const ROLE_COUPON_REDEEMER string = "coupon_redeemer"
const ROLE_COUPON_LISTENER string = "coupon_listener"
// Requester 解析数据
type Requester struct { // token里面添加用户信息验证token后可能会用到用户信息
jwt.StandardClaims
UserID string `json:"preferred_username"` //TODO: 能否改成username
Roles map[string]([]string) `json:"realm_access"`
Brand string `json:"brand"`
}
// HasRole 检查请求者是否有某个角色
func (r *Requester) HasRole(role string) bool {
// if !collection.Collect(requester.Roles).Has("roles") {
// return false
// }
roles := r.Roles["roles"]
if nil == roles {
return false
}
if !collection.Collect(roles).Contains(role) {
return false
}
return true
}