fix(notify): add func to get recevier subscription

This commit is contained in:
马鸿飞
2023-05-18 10:37:11 +08:00
parent 06fdb33e67
commit 7a35416cd3
12 changed files with 84 additions and 3 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ import (
_ "yunion.io/x/onecloud/cmd/climc/shell/logger"
_ "yunion.io/x/onecloud/cmd/climc/shell/misc"
_ "yunion.io/x/onecloud/cmd/climc/shell/monitor"
_ "yunion.io/x/onecloud/cmd/climc/shell/notifyv2"
_ "yunion.io/x/onecloud/cmd/climc/shell/notify"
_ "yunion.io/x/onecloud/cmd/climc/shell/quota"
_ "yunion.io/x/onecloud/cmd/climc/shell/scheduledtask"
_ "yunion.io/x/onecloud/cmd/climc/shell/scheduler"
@@ -34,4 +34,5 @@ func init() {
cmd.Perform("enable-contact-type", new(options.ReceiverEnableContactTypeInput))
cmd.PerformClass("intellij-get", new(options.ReceiverIntellijGetOptions))
cmd.PerformClass("get-types", new(options.ReceiverGetTypeOptions))
cmd.Perform("get-subscription", new(options.ReceiverGetSubscriptionOptions))
}
+8
View File
@@ -101,6 +101,14 @@ func (rt *ReceiverTriggerVerifyOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(rt.SreceiverTriggerVerifyOptions), nil
}
type ReceiverGetSubscriptionOptions struct {
ReceiverOptions
}
func (rt *ReceiverGetSubscriptionOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(rt), nil
}
type ReceiverVerifyOptions struct {
ReceiverOptions
SreceiverVerifyOptions
+25
View File
@@ -784,6 +784,31 @@ func (r *SReceiver) IsOwner(userCred mcclient.TokenCredential) bool {
return r.Id == userCred.GetUserId()
}
// 获取用户订阅
func (r *SReceiver) PerformGetSubscription(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ReceiverIntellijGetInput) (jsonutils.JSONObject, error) {
subscribers, err := getSubscriberByReceiverId(r.Id)
if err != nil {
return nil, errors.Wrap(err, "getSubscriberByReceiverId")
}
type retStruct struct {
SSubscriber
TopicName string
}
res := []retStruct{}
for _, subscriber := range subscribers {
topicModel, err := TopicManager.FetchById(subscriber.TopicId)
topic := topicModel.(*STopic)
if topic.Enabled == tristate.False {
continue
}
if err != nil {
return nil, errors.Wrap(err, "fetch topic by id")
}
res = append(res, retStruct{subscriber, topic.GetName()})
}
return jsonutils.Marshal(res), nil
}
func (rm *SReceiverManager) PerformIntellijGet(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ReceiverIntellijGetInput) (jsonutils.JSONObject, error) {
getParam := jsonutils.NewDict()
getParam.Set("scope", jsonutils.NewString(input.Scope))
+49 -2
View File
@@ -44,6 +44,7 @@ import (
"yunion.io/x/pkg/utils"
"yunion.io/x/sqlchemy"
identityapi "yunion.io/x/onecloud/pkg/apis/identity"
api "yunion.io/x/onecloud/pkg/apis/notify"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/httperrors"
@@ -80,7 +81,7 @@ type SSubscriber struct {
db.SStandaloneAnonResourceBase
db.SEnabledResourceBase
TopicID string `width:"128" charset:"ascii" nullable:"false" index:"true" get:"user" list:"user" create:"required"`
TopicId string `width:"128" charset:"ascii" nullable:"false" index:"true" get:"user" list:"user" create:"required"`
Type string `width:"16" charset:"ascii" nullable:"false" index:"true" get:"user" list:"user" create:"required"`
Identification string `width:"128" charset:"ascii" nullable:"false" index:"true"`
RoleScope string `width:"8" charset:"ascii" nullable:"false" get:"user" list:"user" create:"optional"`
@@ -685,7 +686,7 @@ func (sm *SSubscriberManager) InitializeData() error {
subscriber := SSubscriber{}
subscriber.Type = api.SUBSCRIBER_TYPE_ROLE
subscriber.Identification = roleId
subscriber.TopicID = topic.Id
subscriber.TopicId = topic.Id
subscriber.Scope = api.SUBSCRIBER_SCOPE_SYSTEM
subscriber.ResourceScope = api.SUBSCRIBER_SCOPE_SYSTEM
subscriber.Enabled = tristate.True
@@ -693,3 +694,49 @@ func (sm *SSubscriberManager) InitializeData() error {
}
return nil
}
// 根据接受人ID获取订阅
func getSubscriberByReceiverId(receiverId string) ([]SSubscriber, error) {
// 获取当前接受人所有角色
s := auth.GetAdminSession(context.Background(), options.Options.Region)
query := jsonutils.NewDict()
query.Add(jsonutils.NewString(receiverId), "user", "id")
resp, err := identity.RoleAssignments.List(s, query)
if err != nil {
return nil, errors.Wrap(err, "UserCacheManager.FetchUserByIdOrName")
}
roleAssignments := []identityapi.SRoleAssignment{}
err = jsonutils.Update(&roleAssignments, resp.Data)
if err != nil {
return nil, errors.Wrap(err, "update roleAssignments")
}
roleArr := []string{}
for _, roleAssignment := range roleAssignments {
roleArr = append(roleArr, roleAssignment.Role.Id)
}
results := []SSubscriber{}
// q1 根据角色查找
q1 := SubscriberManager.Query()
q1 = q1.Equals("type", api.SUBSCRIBER_TYPE_ROLE)
q1 = q1.In("identification", roleArr)
tempRes := []SSubscriber{}
err = db.FetchModelObjects(SubscriberManager, q1, &tempRes)
if err != nil {
return nil, errors.Wrap(err, "fetch role")
}
results = append(results, tempRes...)
// q2 根据接受人ID查找
q2 := SubscriberManager.Query()
srq := SubscriberReceiverManager.Query().Equals("receiver_id", receiverId)
srsq := srq.SubQuery()
q2 = q2.Equals("type", api.SUBSCRIBER_TYPE_RECEIVER)
q2.Join(srsq, sqlchemy.Equals(q2.Field("id"), srsq.Field("subscriber_id")))
err = db.FetchModelObjects(SubscriberManager, q2, &tempRes)
if err != nil {
return nil, errors.Wrap(err, "fetch receiver")
}
results = append(results, tempRes...)
return results, nil
}