-
Notifications
You must be signed in to change notification settings - Fork 330
/
context.go
127 lines (112 loc) · 3.38 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2019 The OpenSDS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// This is self defined context which is stored in context.Input.data.
// It is used to transport data in the pipe line.
package context
import (
"encoding/json"
"reflect"
"github.com/emicklei/go-restful"
log "github.com/sirupsen/logrus"
)
const (
KContext = "context"
)
const (
DefaultTenantId = "tenantId"
DefaultUserId = "userId"
NoAuthAdminTenantId = "adminTenantId"
)
func NewAdminContext() *Context {
return &Context{
TenantId: NoAuthAdminTenantId,
IsAdmin: true,
UserId: "unknown",
}
}
func NewContext() *Context {
return &Context{
TenantId: DefaultTenantId,
IsAdmin: false,
UserId: DefaultUserId,
}
}
func NewContextFromJson(s string) *Context {
ctx := &Context{}
err := json.Unmarshal([]byte(s), ctx)
if err != nil {
log.Errorf("Unmarshal json to context failed, reason: %v", err)
}
return ctx
}
func NewInternalTenantContext(tenantId, userId string, isAdmin bool) *Context {
return &Context{
TenantId: tenantId,
UserId: userId,
IsAdmin: isAdmin,
}
}
func GetContext(req *restful.Request) *Context {
ctx, _ := req.Attribute("context").(*Context)
if ctx == nil {
ctx = &Context{}
}
return ctx
}
type Context struct {
IsAdmin bool `policy:"true" json:"is_admin"`
AuthToken string `policy:"true" json:"auth_token"`
UserId string `policy:"true" json:"user_id"`
TenantId string `policy:"true" json:"tenant_id"`
DomainId string `policy:"true" json:"domain_id"`
UserDomainId string `policy:"true" json:"user_domain_id"`
ProjectDomainId string `policy:"true" json:"project_domain_id"`
Roles []string `policy:"true" json:"roles"`
UserName string `policy:"true" json:"user_name"`
ProjectName string `policy:"true" json:"project_name"`
DomainName string `policy:"true" json:"domain_name"`
UserDomainName string `policy:"true" json:"user_domain_name"`
ProjectDomainName string `policy:"true" json:"project_domain_name"`
IsAdminTenant bool `policy:"true" json:"is_admin_tenant"`
}
func (ctx *Context) ToPolicyValue() map[string]interface{} {
ctxMap := map[string]interface{}{}
t := reflect.TypeOf(ctx).Elem()
v := reflect.ValueOf(ctx).Elem()
for i := 0; i < t.NumField(); i++ {
field := v.Field(i)
name := t.Field(i).Tag.Get("json")
if t.Field(i).Tag.Get("policy") == "false" {
continue
}
if field.Kind() == reflect.String && field.String() == "" {
continue
}
if field.Kind() == reflect.Slice && field.Len() == 0 {
continue
}
if field.Kind() == reflect.Map && field.Len() == 0 {
continue
}
ctxMap[name] = field.Interface()
}
return ctxMap
}
func (ctx *Context) ToJson() string {
b, err := json.Marshal(ctx)
if err != nil {
log.Errorf("Context convert to json failed, reason: %v", err)
}
return string(b)
}