Skip to content

Commit 29ae6b2

Browse files
kakj-gorecallsong
andauthored
check permission add project and app name (#3038)
* shared http transport (#3014) * shared http transport * disable connection pool * not permission return project name and app name Co-authored-by: RecallSong <[email protected]>
1 parent c581d45 commit 29ae6b2

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

apistructs/permission.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ type PermissionList struct {
172172
Exist bool `json:"exist"`
173173

174174
// 无权限(access=false)时,该字段返回联系人 ID 列表,例如无应用权限时,返回应用管理员列表
175-
ContactsWhenNoPermission []string `json:"contactsWhenNoPermission,omitempty"`
175+
ContactsWhenNoPermission []string `json:"contactsWhenNoPermission,omitempty"`
176+
ScopeInfo *ScopeInfo `json:"scopeInfo"`
177+
}
178+
179+
type ScopeInfo struct {
180+
ProjectName string `json:"projectName"`
181+
AppName string `json:"appName"`
176182
}
177183

178184
// PermissionListResponse 权限列表响应信息

modules/core-services/endpoints/permission.go

+40
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,51 @@ func (e *Endpoints) ScopeRoleAccess(ctx context.Context, r *http.Request, vars m
203203
for _, mem := range members {
204204
permission.ContactsWhenNoPermission = append(permission.ContactsWhenNoPermission, mem.UserID)
205205
}
206+
207+
permission, err = e.buildScopeInfo(accessReq, permission)
208+
if err != nil {
209+
return apierrors.ErrAccessPermission.InternalError(err).ToResp(), nil
210+
}
206211
}
207212

208213
return httpserver.OkResp(permission, permission.ContactsWhenNoPermission)
209214
}
210215

216+
func (e *Endpoints) buildScopeInfo(accessReq apistructs.ScopeRoleAccessRequest, permission apistructs.PermissionList) (apistructs.PermissionList, error) {
217+
queryScopeType := accessReq.Scope.Type
218+
queryScopeID, err := strconv.ParseInt(accessReq.Scope.ID, 10, 64)
219+
if err != nil {
220+
return permission, err
221+
}
222+
223+
// point appName
224+
if queryScopeType == "app" {
225+
app, err := e.app.Get(queryScopeID)
226+
if err != nil {
227+
return permission, err
228+
}
229+
if permission.ScopeInfo == nil {
230+
permission.ScopeInfo = &apistructs.ScopeInfo{}
231+
}
232+
permission.ScopeInfo.AppName = app.Name
233+
queryScopeType = "project"
234+
queryScopeID = app.ProjectID
235+
}
236+
237+
// point projectName
238+
if queryScopeType == "project" {
239+
project, err := e.project.GetModelProject(queryScopeID)
240+
if err != nil {
241+
return permission, err
242+
}
243+
if permission.ScopeInfo == nil {
244+
permission.ScopeInfo = &apistructs.ScopeInfo{}
245+
}
246+
permission.ScopeInfo.ProjectName = project.DisplayName
247+
}
248+
return permission, nil
249+
}
250+
211251
// 获取权限
212252
func (e *Endpoints) getPermission(userID string, scopeType apistructs.ScopeType, scopeID int64) (apistructs.ScopeRole, error) {
213253
// 若为系统管理员 & 查询系统范围权限,则返回true;若系统管理员查询企业/项目/应用等,应返回false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) 2021 Terminus, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package endpoints
16+
17+
import (
18+
"reflect"
19+
"testing"
20+
21+
"bou.ke/monkey"
22+
23+
"github.com/erda-project/erda/apistructs"
24+
"github.com/erda-project/erda/modules/core-services/model"
25+
"github.com/erda-project/erda/modules/core-services/services/application"
26+
"github.com/erda-project/erda/modules/core-services/services/project"
27+
)
28+
29+
func TestEndpoints_buildScopeInfo(t *testing.T) {
30+
type args struct {
31+
accessReq apistructs.ScopeRoleAccessRequest
32+
permission apistructs.PermissionList
33+
}
34+
tests := []struct {
35+
name string
36+
args args
37+
want apistructs.PermissionList
38+
wantErr bool
39+
}{
40+
{
41+
name: "test_app_name",
42+
args: args{
43+
accessReq: apistructs.ScopeRoleAccessRequest{
44+
Scope: apistructs.Scope{
45+
Type: "app",
46+
ID: "1",
47+
},
48+
},
49+
permission: apistructs.PermissionList{},
50+
},
51+
want: apistructs.PermissionList{
52+
ScopeInfo: &apistructs.ScopeInfo{
53+
ProjectName: "test",
54+
AppName: "test",
55+
},
56+
},
57+
wantErr: false,
58+
},
59+
}
60+
for _, tt := range tests {
61+
t.Run(tt.name, func(t *testing.T) {
62+
e := &Endpoints{}
63+
64+
var app = &application.Application{}
65+
66+
patch1 := monkey.PatchInstanceMethod(reflect.TypeOf(app), "Get", func(app *application.Application, applicationID int64) (*model.Application, error) {
67+
return &model.Application{
68+
Name: "test",
69+
ProjectID: 1,
70+
}, nil
71+
})
72+
defer patch1.Unpatch()
73+
74+
var pj = &project.Project{}
75+
patch2 := monkey.PatchInstanceMethod(reflect.TypeOf(pj), "GetModelProject", func(project *project.Project, projectID int64) (*model.Project, error) {
76+
return &model.Project{
77+
DisplayName: "test",
78+
}, nil
79+
})
80+
defer patch2.Unpatch()
81+
82+
e.app = app
83+
e.project = pj
84+
85+
got, err := e.buildScopeInfo(tt.args.accessReq, tt.args.permission)
86+
if (err != nil) != tt.wantErr {
87+
t.Errorf("buildScopeInfo() error = %v, wantErr %v", err, tt.wantErr)
88+
return
89+
}
90+
if !reflect.DeepEqual(got, tt.want) {
91+
t.Errorf("buildScopeInfo() got = %v, want %v", got, tt.want)
92+
}
93+
})
94+
}
95+
}

0 commit comments

Comments
 (0)