Skip to content

Commit

Permalink
fix: opslog FetchOwnerId fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiu Jian committed Apr 26, 2020
1 parent 74eef22 commit 1882232
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pkg/cloudcommon/db/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func FetchUserInfo(ctx context.Context, data jsonutils.JSONObject) (mcclient.IId
}
return &ownerId, nil
}
return nil, nil
return FetchProjectInfo(ctx, data)
}

func FetchProjectInfo(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error) {
Expand Down
62 changes: 43 additions & 19 deletions pkg/cloudcommon/db/opslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/reflectutils"
"yunion.io/x/pkg/util/stringutils"
"yunion.io/x/sqlchemy"
Expand Down Expand Up @@ -613,6 +614,10 @@ func (self *SOpsLogManager) FilterByName(q *sqlchemy.SQuery, name string) *sqlch
func (self *SOpsLogManager) FilterByOwner(q *sqlchemy.SQuery, ownerId mcclient.IIdentityProvider, scope rbacutils.TRbacScope) *sqlchemy.SQuery {
if ownerId != nil {
switch scope {
case rbacutils.ScopeUser:
if len(ownerId.GetUserId()) > 0 {
q = q.Filter(sqlchemy.Equals(q.Field("user_id"), ownerId.GetUserId()))
}
case rbacutils.ScopeProject:
if len(ownerId.GetProjectId()) > 0 {
q = q.Filter(sqlchemy.OR(
Expand All @@ -627,25 +632,24 @@ func (self *SOpsLogManager) FilterByOwner(q *sqlchemy.SQuery, ownerId mcclient.I
sqlchemy.Equals(q.Field("domain_id"), ownerId.GetProjectDomainId()),
))
}
default:
// systemScope, no filter
}
/* if len(ownerId.GetProjectId()) > 0 {
q = q.Filter(sqlchemy.OR(
sqlchemy.Equals(q.Field("owner_tenant_id"), ownerId.GetProjectId()),
sqlchemy.Equals(q.Field("tenant_id"), ownerId.GetProjectId()),
))
} else if len(ownerId.GetProjectDomainId()) > 0 {
q = q.Filter(sqlchemy.OR(
sqlchemy.Equals(q.Field("owner_domain_id"), ownerId.GetProjectDomainId()),
sqlchemy.Equals(q.Field("domain_id"), ownerId.GetProjectDomainId()),
))
}
*/
}
return q
}

func (self *SOpsLog) GetOwnerId() mcclient.IIdentityProvider {
owner := SOwnerId{DomainId: self.OwnerDomainId, ProjectId: self.OwnerProjectId}
owner := SOwnerId{
Domain: self.ProjectDomain,
DomainId: self.ProjectDomainId,
Project: self.Project,
ProjectId: self.ProjectId,
User: self.User,
UserId: self.UserId,
UserDomain: self.Domain,
UserDomainId: self.DomainId,
}
return &owner
}

Expand All @@ -654,7 +658,7 @@ func (self *SOpsLog) IsSharable(reqCred mcclient.IIdentityProvider) bool {
}

func (manager *SOpsLogManager) ResourceScope() rbacutils.TRbacScope {
return rbacutils.ScopeProject
return rbacutils.ScopeUser
}

func (manager *SOpsLogManager) GetPagingConfig() *SPagingConfig {
Expand All @@ -666,7 +670,15 @@ func (manager *SOpsLogManager) GetPagingConfig() *SPagingConfig {
}

func (manager *SOpsLogManager) FetchOwnerId(ctx context.Context, data jsonutils.JSONObject) (mcclient.IIdentityProvider, error) {
return FetchProjectInfo(ctx, data)
ownerId := SOwnerId{}
err := data.Unmarshal(&ownerId)
if err != nil {
return nil, errors.Wrap(err, "data.Unmarshal")
}
if ownerId.IsValid() {
return &ownerId, nil
}
return FetchUserInfo(ctx, data)
}

func (manager *SOpsLogManager) ValidateCreateData(ctx context.Context,
Expand All @@ -675,13 +687,25 @@ func (manager *SOpsLogManager) ValidateCreateData(ctx context.Context,
query jsonutils.JSONObject,
data apis.OpsLogCreateInput,
) (apis.OpsLogCreateInput, error) {
data.Project = ownerId.GetProjectName()
data.ProjectId = ownerId.GetProjectId()
data.ProjectDomainId = ownerId.GetProjectDomainId()
data.ProjectDomain = ownerId.GetProjectDomain()
return data, nil
}

func (log *SOpsLog) CustomizeCreate(ctx context.Context,
userCred mcclient.TokenCredential,
ownerId mcclient.IIdentityProvider,
query jsonutils.JSONObject,
data jsonutils.JSONObject) error {
log.User = ownerId.GetUserName()
log.UserId = ownerId.GetUserId()
log.Domain = ownerId.GetDomainName()
log.DomainId = ownerId.GetDomainId()
log.Project = ownerId.GetProjectName()
log.ProjectId = ownerId.GetProjectId()
log.ProjectDomain = ownerId.GetProjectDomain()
log.ProjectDomainId = ownerId.GetProjectDomainId()
return log.SModelBase.CustomizeCreate(ctx, userCred, ownerId, query, data)
}

func (manager *SOpsLogManager) FetchCustomizeColumns(
ctx context.Context,
userCred mcclient.TokenCredential,
Expand Down
26 changes: 18 additions & 8 deletions pkg/cloudcommon/db/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
package db

type SOwnerId struct {
Domain string
DomainId string
Project string
ProjectId string
Domain string `json:"project_domain"`
DomainId string `json:"project_domain_id"`
Project string `json:"tenant"`
ProjectId string `json:"tenant_id"`

User string
UserId string
UserDomain string
UserDomainId string
User string `json:"user"`
UserId string `json:"user_id"`
UserDomain string `json:"domain"`
UserDomainId string `json:"domain_id"`
}

func (o *SOwnerId) GetProjectId() string {
Expand Down Expand Up @@ -65,3 +65,13 @@ func (o *SOwnerId) GetDomainId() string {
func (o *SOwnerId) GetDomainName() string {
return o.UserDomain
}

func (ownerId SOwnerId) IsValid() bool {
if len(ownerId.User) > 0 && len(ownerId.UserId) > 0 &&
len(ownerId.UserDomain) > 0 && len(ownerId.UserDomainId) > 0 &&
len(ownerId.Project) > 0 && len(ownerId.ProjectId) > 0 &&
len(ownerId.Domain) > 0 && len(ownerId.DomainId) > 0 {
return true
}
return false
}

0 comments on commit 1882232

Please sign in to comment.