Skip to content

Commit

Permalink
fix failed to get notify detail due to msp_env scope type
Browse files Browse the repository at this point in the history
  • Loading branch information
chengjoey committed Mar 12, 2024
1 parent dbda9d2 commit 5a96b55
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
13 changes: 13 additions & 0 deletions apistructs/notify_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"time"
Expand Down Expand Up @@ -132,6 +133,18 @@ type NotifyGroupDetail struct {
DingdingList []Target `json:"dingdingList"`
DingdingWorkNoticeList []Target `json:"dingdingWorknoticeList"`
WebHookList []string `json:"webhookList"`
Label string `json:"-"`
}

func (n *NotifyGroupDetail) GetScopeDetail() (scopeID, scopeType string) {
label := make(map[string]string)
err := json.Unmarshal([]byte(n.Label), &label)
if err != nil {
return
}
scopeID = label[MSPMemberScopeId]
scopeType = label[MSPMemberScope]
return
}

// CreateNotifyGroupRequest 创建通知组请求
Expand Down
59 changes: 59 additions & 0 deletions apistructs/notify_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2021 Terminus, Inc.
//
// 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.

package apistructs

import "testing"

func TestGetScopeDetail(t *testing.T) {
testCases := []struct {
name string
label string
wantScopeID string
wantScopeType string
}{
{
name: "empty label",
label: "",
wantScopeID: "",
wantScopeType: "",
},
{
name: "project scope",
label: "{\"member_scopeID\":\"1\",\"member_scopeType\":\"project\"}",
wantScopeID: "1",
wantScopeType: "project",
},
{
name: "wrong label",
label: "member_scopeID: 1",
wantScopeType: "",
wantScopeID: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
n := &NotifyGroupDetail{
Label: tc.label,
}
scopeID, scopeType := n.GetScopeDetail()
if scopeID != tc.wantScopeID {
t.Errorf("want scopeID: %s, but got: %s", tc.wantScopeID, scopeID)
}
if scopeType != tc.wantScopeType {
t.Errorf("want scopeType: %s, but got: %s", tc.wantScopeType, scopeType)
}
})
}
}
1 change: 1 addition & 0 deletions internal/core/legacy/services/notify/notify_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func (o *NotifyGroup) GetDetail(id int64, orgID int64) (*apistructs.NotifyGroupD
result.Targets = group.Targets
result.DingdingList = uniqueTargetList(result.DingdingList)
result.DingdingWorkNoticeList = uniqueTargetList(result.DingdingWorkNoticeList)
result.Label = group.Label
return result, nil
}

Expand Down
6 changes: 5 additions & 1 deletion internal/core/messenger/notifygroup/group.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,11 @@ func (n *notifyGroupService) GetNotifyGroupDetail(ctx context.Context, request *
return nil, errors.NewInternalServerError(err)
}
userIdStr := apis.GetUserID(ctx)
err = n.checkNotifyPermission(ctx, userIdStr, notifyGroup.ScopeType, notifyGroup.ScopeID, apistructs.GetAction)
scopeType, scopeID := notifyGroup.ScopeType, notifyGroup.ScopeID
if scopeType == apistructs.MSPScope {
scopeID, scopeType = notifyGroup.GetScopeDetail()
}
err = n.checkNotifyPermission(ctx, userIdStr, scopeType, scopeID, apistructs.GetAction)
if err != nil {
return nil, errors.NewPermissionError(apistructs.NotifyResource, apistructs.GetAction, err.Error())
}
Expand Down

0 comments on commit 5a96b55

Please sign in to comment.