mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 02:37:24 +08:00
79 lines
3.0 KiB
Go
79 lines
3.0 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"yunion.io/x/jsonutils"
|
|
"yunion.io/x/sqlchemy"
|
|
|
|
"yunion.io/x/onecloud/pkg/mcclient"
|
|
"yunion.io/x/onecloud/pkg/util/logclient"
|
|
)
|
|
|
|
type SSharableVirtualResourceBase struct {
|
|
SVirtualResourceBase
|
|
|
|
IsPublic bool `default:"false" nullable:"false" index:"true" create:"admin_optional" list:"user" update:"admin"`
|
|
}
|
|
|
|
type SSharableVirtualResourceBaseManager struct {
|
|
SVirtualResourceBaseManager
|
|
}
|
|
|
|
func NewSharableVirtualResourceBaseManager(dt interface{}, tableName string, keyword string, keywordPlural string) SSharableVirtualResourceBaseManager {
|
|
return SSharableVirtualResourceBaseManager{SVirtualResourceBaseManager: NewVirtualResourceBaseManager(dt, tableName, keyword, keywordPlural)}
|
|
}
|
|
|
|
func (manager *SSharableVirtualResourceBaseManager) FilterByOwner(q *sqlchemy.SQuery, owner string) *sqlchemy.SQuery {
|
|
q = q.Filter(sqlchemy.OR(sqlchemy.Equals(q.Field("tenant_id"), owner), sqlchemy.IsTrue(q.Field("is_public"))))
|
|
q = q.Filter(sqlchemy.OR(sqlchemy.IsNull(q.Field("pending_deleted")), sqlchemy.IsFalse(q.Field("pending_deleted"))))
|
|
q = q.Filter(sqlchemy.OR(sqlchemy.IsNull(q.Field("is_system")), sqlchemy.IsFalse(q.Field("is_system"))))
|
|
return q
|
|
}
|
|
|
|
func (model *SSharableVirtualResourceBase) AllowGetDetails(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool {
|
|
return model.IsOwner(userCred) || model.IsPublic || IsAdminAllowGet(userCred, model)
|
|
}
|
|
|
|
func (model *SSharableVirtualResourceBase) IsSharable() bool {
|
|
return model.IsPublic
|
|
}
|
|
|
|
func (model *SSharableVirtualResourceBase) AllowPerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
|
return IsAdminAllowPerform(userCred, model, "public")
|
|
}
|
|
|
|
func (model *SSharableVirtualResourceBase) AllowPerformPrivate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) bool {
|
|
return IsAdminAllowPerform(userCred, model, "private")
|
|
}
|
|
|
|
func (model *SSharableVirtualResourceBase) PerformPublic(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
|
if !model.IsPublic {
|
|
diff, err := Update(model, func() error {
|
|
model.IsPublic = true
|
|
return nil
|
|
})
|
|
if err == nil {
|
|
OpsLog.LogEvent(model, ACT_UPDATE, diff, userCred)
|
|
logclient.AddActionLogWithContext(ctx, model, logclient.ACT_UPDATE, diff, userCred, true)
|
|
}
|
|
return nil, err
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (model *SSharableVirtualResourceBase) PerformPrivate(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
|
if model.IsPublic {
|
|
diff, err := Update(model, func() error {
|
|
model.IsPublic = false
|
|
return nil
|
|
})
|
|
if err == nil {
|
|
OpsLog.LogEvent(model, ACT_UPDATE, diff, userCred)
|
|
logclient.AddActionLogWithContext(ctx, model, logclient.ACT_UPDATE, diff, userCred, true)
|
|
}
|
|
return nil, err
|
|
}
|
|
return nil, nil
|
|
}
|