mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
fix: check host sharing status when considering whether a host is assignable
This commit is contained in:
@@ -559,10 +559,9 @@ func checkAssignHost(userCred mcclient.TokenCredential, preferHost string) error
|
||||
return httperrors.NewBadRequestError("Host %s not found", preferHost)
|
||||
}
|
||||
host := iHost.(*models.SHost)
|
||||
if db.IsAdminAllowPerform(userCred, host, "assign-host") {
|
||||
} else if db.IsDomainAllowPerform(userCred, host, "assign-host") && userCred.GetProjectDomainId() == host.DomainId {
|
||||
} else {
|
||||
return httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
|
||||
err := host.IsAssignable(userCred)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "IsAssignable")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4417,10 +4417,9 @@ func (manager *SGuestManager) PerformBatchMigrate(ctx context.Context, userCred
|
||||
host := iHost.(*SHost)
|
||||
preferHostId = host.Id
|
||||
|
||||
if db.IsAdminAllowPerform(userCred, host, "assign-host") {
|
||||
} else if db.IsDomainAllowPerform(userCred, host, "assign-host") && userCred.GetProjectDomainId() == host.DomainId {
|
||||
} else {
|
||||
return nil, httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
|
||||
err := host.IsAssignable(userCred)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "IsAssignable")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/utils"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
@@ -73,10 +74,9 @@ func ValidateScheduleCreateData(ctx context.Context, userCred mcclient.TokenCred
|
||||
}
|
||||
baremetal := bmObj.(*SHost)
|
||||
|
||||
if db.IsAdminAllowPerform(userCred, baremetal, "assign-host") {
|
||||
} else if db.IsDomainAllowPerform(userCred, baremetal, "assign-host") && userCred.GetProjectDomainId() == baremetal.DomainId {
|
||||
} else {
|
||||
return nil, httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
|
||||
err = baremetal.IsAssignable(userCred)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "IsAssignable")
|
||||
}
|
||||
|
||||
if !baremetal.GetEnabled() {
|
||||
|
||||
@@ -4928,10 +4928,9 @@ func (host *SHost) PerformHostMaintenance(ctx context.Context, userCred mcclient
|
||||
}
|
||||
host := iHost.(*SHost)
|
||||
preferHostId = host.Id
|
||||
if db.IsAdminAllowPerform(userCred, host, "assign-host") {
|
||||
} else if db.IsDomainAllowPerform(userCred, host, "assign-host") && userCred.GetProjectDomainId() == host.DomainId {
|
||||
} else {
|
||||
return nil, httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
|
||||
err := host.IsAssignable(userCred)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "IsAssignable")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5479,3 +5478,16 @@ func (manager *SHostManager) FetchHostByExtId(extid string) *SHost {
|
||||
return &host
|
||||
}
|
||||
}
|
||||
|
||||
func (host *SHost) IsAssignable(userCred mcclient.TokenCredential) error {
|
||||
if db.IsAdminAllowPerform(userCred, host, "assign-host") {
|
||||
return nil
|
||||
} else if db.IsDomainAllowPerform(userCred, host, "assign-host") &&
|
||||
(userCred.GetProjectDomainId() == host.DomainId ||
|
||||
host.PublicScope == string(rbacutils.ScopeSystem) ||
|
||||
(host.PublicScope == string(rbacutils.ScopeDomain) && utils.IsInStringArray(userCred.GetProjectDomainId(), host.GetSharedDomains()))) {
|
||||
return nil
|
||||
} else {
|
||||
return httperrors.NewNotSufficientPrivilegeError("Only system admin can assign host")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user