diff --git a/pkg/apis/notify/receiver.go b/pkg/apis/notify/receiver.go index 38c48baa59..d28035f0fd 100644 --- a/pkg/apis/notify/receiver.go +++ b/pkg/apis/notify/receiver.go @@ -113,6 +113,8 @@ type ReceiverListInput struct { EnabledContactType string `json:"enabled_contact_type"` VerifiedContactType string `json:"verified_contact_type"` + + ProjectDomainFilter bool `json:"project_domain_filter"` } type ReceiverUpdateInput struct { diff --git a/pkg/mcclient/options/notify/receiver.go b/pkg/mcclient/options/notify/receiver.go index 2954e30b93..099f6e60bf 100644 --- a/pkg/mcclient/options/notify/receiver.go +++ b/pkg/mcclient/options/notify/receiver.go @@ -12,6 +12,7 @@ type ReceiverListOptions struct { UName string `help:"user name in keystone"` EnabledContactType string `help:"enabled contact type"` VerifiedContactType string `help:"verified contact type"` + ProjectDomainFilter bool `help:"filter receivers who join the project under the domain where the requester is currently located"` } func (rl *ReceiverListOptions) Params() (jsonutils.JSONObject, error) { diff --git a/pkg/notify/models/receiver.go b/pkg/notify/models/receiver.go index 3663d8a473..6d2a43dffa 100644 --- a/pkg/notify/models/receiver.go +++ b/pkg/notify/models/receiver.go @@ -601,9 +601,48 @@ func (rm *SReceiverManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQue if len(input.VerifiedContactType) > 0 { q = rm.VerifiedContactFilter(input.VerifiedContactType, q) } + if input.ProjectDomainFilter && userCred.GetProjectDomainId() != "" { + userIds, err := rm.findUserIdsWithProjectDomain(ctx, userCred, userCred.GetProjectDomainId()) + if err != nil { + return nil, errors.Wrap(err, "unable to findUserIdsWithProjectDomain") + } + switch len(userIds) { + case 0: + q = q.Equals("id", "") + case 1: + q = q.Equals("id", userIds[0]) + default: + q = q.In("id", userIds) + } + } return q, nil } +func (rm *SReceiverManager) findUserIdsWithProjectDomain(ctx context.Context, userCred mcclient.TokenCredential, projectDomainId string) ([]string, error) { + session := auth.GetSession(ctx, userCred, "", "") + query := jsonutils.NewDict() + query.Set("effective", jsonutils.JSONTrue) + query.Set("project_domain_id", jsonutils.NewString(projectDomainId)) + listRet, err := modules.RoleAssignments.List(session, query) + if err != nil { + return nil, errors.Wrap(err, "unable to list RoleAssignments") + } + log.Debugf("return value for role-assignments: %s", jsonutils.Marshal(listRet)) + userIds := make([]string, 0, len(listRet.Data)) + for i := range listRet.Data { + ras := listRet.Data[i] + user, err := ras.Get("user") + if err == nil { + id, err := user.GetString("id") + if err != nil { + return nil, errors.Wrap(err, "unable to get user.id from result of RoleAssignments.List") + } + userIds = append(userIds, id) + } + } + return userIds, nil +} + func (r *SReceiverManager) AllowPerformGetTypes(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool { return true }