Skip to content

Commit bb8da9b

Browse files
authored
Merge pull request apache#30 from sjzar/master
PILIVDN-763 pili-statd 支持无uid参数查询(admin权限)
2 parents a564c32 + fdf54b6 commit bb8da9b

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

go/src/pili.qiniu.com/statd.v4/http/http.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ func queryOf(req *http.Request) (query statd.Query, err error) {
126126
return
127127
}
128128

129-
query.Where["uid"] = uid
129+
if uid != 0 {
130+
query.Where["uid"] = uid
131+
}
130132

131133
if query.From.IsZero() {
132134
err = fmt.Errorf("need begin arg")
@@ -148,9 +150,10 @@ func queryOf(req *http.Request) (query statd.Query, err error) {
148150
return
149151
}
150152

151-
// uid参数支持两张形式
153+
// uid参数支持三种形式
152154
// a. http://host/path?$uid=123456, 需要admin权限
153155
// b. 通用的七牛鉴权
156+
// c. 不带uid参数, 需要admin权限
154157
func extractUid(req *http.Request) (uint32, error) {
155158

156159
auth := req.Header.Get("Authorization")
@@ -161,14 +164,17 @@ func extractUid(req *http.Request) (uint32, error) {
161164

162165
uid := user.Uid
163166

164-
if ustr := req.FormValue("$uid"); ustr != "" && user.Utype&proto.USER_TYPE_ADMIN != 0 {
165-
var n uint64
166-
n, err = strconv.ParseUint(ustr, 10, 31)
167-
if err != nil {
168-
err = fmt.Errorf("parse uid(%v) failed", ustr)
169-
return 0, err
167+
if user.Utype&proto.USER_TYPE_ADMIN != 0 {
168+
uid = uint32(0)
169+
if ustr := req.FormValue("$uid"); ustr != "" {
170+
var n uint64
171+
n, err = strconv.ParseUint(ustr, 10, 31)
172+
if err != nil {
173+
err = fmt.Errorf("parse uid(%v) failed", ustr)
174+
return 0, err
175+
}
176+
uid = uint32(n)
170177
}
171-
uid = uint32(n)
172178
}
173179

174180
return uid, err

go/src/pili.qiniu.com/statd.v4/http/http_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,28 @@ foo:
123123
ast.Equal(2, result2[0].Vals["foo"]["hit"])
124124
}
125125

126+
{
127+
cli := client.New(srv.URL, stubTransport{123, true})
128+
arg := client.Args{
129+
From: start,
130+
To: start.Add(time.Minute * 5),
131+
Granule: client.G5min,
132+
SelectFields: []string{"hit", "flow"},
133+
}
134+
135+
result, err := cli.Query(xl, table, arg)
136+
ast.NoError(err)
137+
ast.Equal(1, len(result))
138+
ast.Equal(6, result[0].Vals["hit"])
139+
ast.Equal(60, result[0].Vals["flow"])
140+
141+
result2, err := cli.Group(xl, table, arg, "bucket")
142+
ast.NoError(err)
143+
ast.Equal(1, len(result2))
144+
ast.Equal(3, result2[0].Vals["bar"]["hit"])
145+
ast.Equal(3, result2[0].Vals["foo"]["hit"])
146+
}
147+
126148
{
127149
cli := client.New(srv.URL, stubTransport{123, false})
128150
arg := client.Args{

0 commit comments

Comments
 (0)