From bb82d3033141f53c47487dca96d2787bc554b153 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Tue, 14 Jan 2020 17:08:29 +0800 Subject: [PATCH] validate share on delete, update pulibc scope on mark standard --- pkg/image/models/images.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/pkg/image/models/images.go b/pkg/image/models/images.go index 5106c6da7a..4b96dd1322 100644 --- a/pkg/image/models/images.go +++ b/pkg/image/models/images.go @@ -658,6 +658,9 @@ func (self *SImage) ValidateDeleteCondition(ctx context.Context) error { if self.IsGuestImage.IsTrue() { return httperrors.NewForbiddenError("image is the part of guest image") } + if self.IsPublic || len(self.GetSharedProjects()) > 0 { + return httperrors.NewForbiddenError("image is shared") + } return self.SVirtualResourceBase.ValidateDeleteCondition(ctx) } @@ -1242,13 +1245,24 @@ func (self *SImage) PerformMarkStandard( data jsonutils.JSONObject, ) (jsonutils.JSONObject, error) { isStandard := jsonutils.QueryBoolean(data, "is_standard", false) - if (!self.IsStandard.IsTrue() && isStandard) || (self.IsStandard.IsTrue() && !isStandard) { + if !self.IsStandard.IsTrue() && isStandard { + params := jsonutils.NewDict() + params.Set("scope", jsonutils.NewString("system")) + _, err := self.PerformPublic(ctx, userCred, query, params) + if err != nil { + return nil, err + } diff, err := db.Update(self, func() error { - if isStandard { - self.IsStandard = tristate.True - } else { - self.IsStandard = tristate.False - } + self.IsStandard = tristate.True + return nil + }) + if err != nil { + return nil, httperrors.NewGeneralError(err) + } + db.OpsLog.LogEvent(self, db.ACT_UPDATE, diff, userCred) + } else if self.IsStandard.IsTrue() && !isStandard { + diff, err := db.Update(self, func() error { + self.IsStandard = tristate.False return nil }) if err != nil {