Skip to content

Commit

Permalink
feat(notify): Support 'socpe:domain' when list contacts
Browse files Browse the repository at this point in the history
This is a temporary solution.
  • Loading branch information
rainzm committed May 28, 2020
1 parent 6c87cd3 commit cfcaa50
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions pkg/notify/models/mod_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
api "yunion.io/x/onecloud/pkg/apis/notify"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
"yunion.io/x/onecloud/pkg/notify/utils"
"yunion.io/x/onecloud/pkg/util/rbacutils"
"yunion.io/x/onecloud/pkg/util/stringutils2"
Expand Down Expand Up @@ -278,18 +280,41 @@ func (self *SContactManager) ListItemFilter(ctx context.Context, q *sqlchemy.SQu

scopeStr, err := query.GetString("scope")
if err != nil {
scopeStr = "system"
scopeStr = "project"
}
scope := rbacutils.TRbacScope(scopeStr)

if !scope.HigherEqual(rbacutils.ScopeSystem) {
switch {
case scope.HigherEqual(rbacutils.ScopeSystem):
case scope.HigherEqual(rbacutils.ScopeDomain):
uids, err := self.uidsInDomain(ctx, userCred)
if err != nil {
return q, err
}
q = q.In("uid", uids)
default:
q = q.Equals("uid", userCred.GetUserId())
}
q = q.GroupBy("uid").Desc("created_at")

return q, nil
}

func (self *SContactManager) uidsInDomain(ctx context.Context, userCred mcclient.TokenCredential) ([]string, error) {
session := auth.GetSession(ctx, userCred, "", "")
params := jsonutils.NewDict()
params.Set("scope", jsonutils.NewString("domain"))
ret, err := modules.UsersV3.List(session, params)
if err != nil {
return nil, errors.Wrap(err, "modules.Userv3.List")
}
uids := make([]string, 0, len(ret.Data))
for i := range ret.Data {
id, _ := ret.Data[i].GetString("id")
uids = append(uids, id)
}
return uids, nil
}

// Contacts query all contacts by uids and contactType
func (self *SContactManager) Contacts(uids []string, contactType string) ([]SContact, error) {
contacts := make([]SContact, 0, len(uids))
Expand Down

0 comments on commit cfcaa50

Please sign in to comment.