feat(notify): filter receivers who join the project under the domain where the requester is currently located

This commit is contained in:
rainzm
2021-04-07 16:08:28 +08:00
parent ed46b2f1a1
commit c88d015984
3 changed files with 42 additions and 0 deletions
+2
View File
@@ -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 {
+1
View File
@@ -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) {
+39
View File
@@ -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
}