mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
f3e6f3b23b
* feat(command): remove org * refactor: imports, unused code, error handling * reduce org removed in action * add org deletion to projections * add org removal to projections * add org removal to projections * org removed projection * lint import * projections * fix: table names in tests * fix: table names in tests * logging * add org state * fix(domain): add Owner removed to object details * feat(ListQuery): add with owner removed * fix(org-delete): add bool to functions to select with owner removed * fix(org-delete): add bools to user grants with events to determine if dependencies lost owner * fix(org-delete): add unit tests for owner removed and org removed events * fix(org-delete): add handling of org remove for grants and members * fix(org-delete): correction of unit tests for owner removed * fix(org-delete): update projections, unit tests and get functions * fix(org-delete): add change date to authnkeys and owner removed to org metadata * fix(org-delete): include owner removed for login names * fix(org-delete): some column fixes in projections and build for queries with owner removed * indexes * fix(org-delete): include review changes * fix(org-delete): change user projection name after merge * fix(org-delete): include review changes for project grant where no project owner is necessary * fix(org-delete): include auth and adminapi tables with owner removed information * fix(org-delete): cleanup username and orgdomain uniqueconstraints when org is removed * fix(org-delete): add permissions for org.remove * remove unnecessary unique constraints * fix column order in primary keys * fix(org-delete): include review changes * fix(org-delete): add owner removed indexes and chang setup step to create tables * fix(org-delete): move PK order of instance_id and change added user_grant from review * fix(org-delete): no params for prepareUserQuery * change to step 6 * merge main * fix(org-delete): OldUserName rename to private * fix linting * cleanup * fix: remove org test * create prerelease * chore: delete org-delete as prerelease Co-authored-by: Stefan Benz <stefan@caos.ch> Co-authored-by: Livio Spring <livio.a@gmail.com> Co-authored-by: Fabi <38692350+hifabienne@users.noreply.github.com> Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
172 lines
4.8 KiB
Go
172 lines
4.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/zitadel/logging"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
)
|
|
|
|
func PrepareGetByKey(table string, key ColumnKey, id string) func(db *gorm.DB, res interface{}) error {
|
|
return func(db *gorm.DB, res interface{}) error {
|
|
err := db.Table(table).
|
|
Where(fmt.Sprintf("%s = ?", key.ToColumnName()), id).
|
|
Take(res).
|
|
Error
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return caos_errs.ThrowNotFound(err, "VIEW-XRI9c", "object not found")
|
|
}
|
|
logging.LogWithFields("VIEW-xVShS", "AggregateID", id).WithError(err).Warn("get from view error")
|
|
return caos_errs.ThrowInternal(err, "VIEW-J92Td", "Errors.Internal")
|
|
}
|
|
}
|
|
|
|
func PrepareGetByQuery(table string, queries ...SearchQuery) func(db *gorm.DB, res interface{}) error {
|
|
return func(db *gorm.DB, res interface{}) error {
|
|
query := db.Table(table)
|
|
for _, q := range queries {
|
|
var err error
|
|
query, err = SetQuery(query, q.GetKey(), q.GetValue(), q.GetMethod())
|
|
if err != nil {
|
|
return caos_errs.ThrowInvalidArgument(err, "VIEW-KaGue", "query is invalid")
|
|
}
|
|
}
|
|
|
|
err := query.Take(res).Error
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return caos_errs.ThrowNotFound(err, "VIEW-hodc6", "object not found")
|
|
}
|
|
logging.LogWithFields("VIEW-Mg6la", "table ", table).WithError(err).Warn("get from cache error")
|
|
return caos_errs.ThrowInternal(err, "VIEW-qJBg9", "cache error")
|
|
}
|
|
}
|
|
|
|
func PrepareBulkSave(table string) func(db *gorm.DB, objects ...interface{}) error {
|
|
return func(db *gorm.DB, objects ...interface{}) error {
|
|
db = db.Table(table)
|
|
db = db.Begin()
|
|
if err := db.Error; err != nil {
|
|
return caos_errs.ThrowInternal(err, "REPOS-Fl0Is", "unable to begin")
|
|
}
|
|
for _, object := range objects {
|
|
err := db.Save(object).Error
|
|
if err != nil {
|
|
return caos_errs.ThrowInternal(err, "VIEW-oJJSm", "unable to put object to view")
|
|
}
|
|
}
|
|
if err := db.Commit().Error; err != nil {
|
|
return caos_errs.ThrowInternal(err, "REPOS-IfhUE", "unable to commit")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func PrepareSave(table string) func(db *gorm.DB, object interface{}) error {
|
|
return func(db *gorm.DB, object interface{}) error {
|
|
err := db.Table(table).Save(object).Error
|
|
if err != nil {
|
|
return caos_errs.ThrowInternal(err, "VIEW-2m9fs", "unable to put object to view")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func PrepareSaveOnConflict(table string, conflictColumns, updateColumns []string) func(db *gorm.DB, object interface{}) error {
|
|
updates := make([]string, len(updateColumns))
|
|
for i, column := range updateColumns {
|
|
updates[i] = column + "=excluded." + column
|
|
}
|
|
onConflict := fmt.Sprintf("ON CONFLICT (%s) DO UPDATE SET %s", strings.Join(conflictColumns, ","), strings.Join(updates, ","))
|
|
return func(db *gorm.DB, object interface{}) error {
|
|
err := db.Table(table).Set("gorm:insert_option", onConflict).Save(object).Error
|
|
if err != nil {
|
|
return caos_errs.ThrowInternal(err, "VIEW-AfC7G", "unable to put object to view")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func PrepareDeleteByKey(table string, key ColumnKey, id interface{}) func(db *gorm.DB) error {
|
|
return func(db *gorm.DB) error {
|
|
err := db.Table(table).
|
|
Where(fmt.Sprintf("%s = ?", key.ToColumnName()), id).
|
|
Delete(nil).
|
|
Error
|
|
if err != nil {
|
|
return caos_errs.ThrowInternal(err, "VIEW-die73", "could not delete object")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func PrepareUpdateByKeys(table string, column ColumnKey, value interface{}, keys ...Key) func(db *gorm.DB) error {
|
|
return func(db *gorm.DB) error {
|
|
for _, key := range keys {
|
|
db = db.Table(table).
|
|
Where(fmt.Sprintf("%s = ?", key.Key.ToColumnName()), key.Value)
|
|
}
|
|
err := db.
|
|
Update(column.ToColumnName(), value).
|
|
Error
|
|
if err != nil {
|
|
return caos_errs.ThrowInternal(err, "VIEW-ps099xj", "could not update object")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
type Key struct {
|
|
Key ColumnKey
|
|
Value interface{}
|
|
}
|
|
|
|
func PrepareDeleteByKeys(table string, keys ...Key) func(db *gorm.DB) error {
|
|
return func(db *gorm.DB) error {
|
|
for _, key := range keys {
|
|
db = db.Table(table).
|
|
Where(fmt.Sprintf("%s = ?", key.Key.ToColumnName()), key.Value)
|
|
}
|
|
err := db.
|
|
Delete(nil).
|
|
Error
|
|
if err != nil {
|
|
return caos_errs.ThrowInternal(err, "VIEW-die73", "could not delete object")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func PrepareDeleteByObject(table string, object interface{}) func(db *gorm.DB) error {
|
|
return func(db *gorm.DB) error {
|
|
err := db.Table(table).
|
|
Delete(object).
|
|
Error
|
|
if err != nil {
|
|
return caos_errs.ThrowInternal(err, "VIEW-lso9w", "could not delete object")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func PrepareTruncate(table string) func(db *gorm.DB) error {
|
|
return func(db *gorm.DB) error {
|
|
err := db.
|
|
Exec("TRUNCATE " + table).
|
|
Error
|
|
if err != nil {
|
|
return caos_errs.ThrowInternal(err, "VIEW-lso9w", "could not truncate table")
|
|
}
|
|
return nil
|
|
}
|
|
}
|