mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:27:42 +00:00
feat: iam query (#3085)
* fix: only show factors with state ready * fix: get iam by id and clean up code * fix: get iam by id and clean up code * fix: remove unused code
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
package eventsourcing
|
||||
|
||||
import (
|
||||
"github.com/caos/logging"
|
||||
"github.com/caos/zitadel/internal/cache"
|
||||
"github.com/caos/zitadel/internal/cache/config"
|
||||
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
||||
)
|
||||
|
||||
type IAMCache struct {
|
||||
iamCache cache.Cache
|
||||
}
|
||||
|
||||
func StartCache(conf *config.CacheConfig) (*IAMCache, error) {
|
||||
iamCache, err := conf.Config.NewCache()
|
||||
logging.Log("EVENT-9siew").OnError(err).Panic("unable to create iam cache")
|
||||
|
||||
return &IAMCache{iamCache: iamCache}, nil
|
||||
}
|
||||
|
||||
func (c *IAMCache) getIAM(ID string) *model.IAM {
|
||||
user := &model.IAM{ObjectRoot: models.ObjectRoot{AggregateID: ID}}
|
||||
if err := c.iamCache.Get(ID, user); err != nil {
|
||||
logging.Log("EVENT-slo9x").WithError(err).Debug("error in getting cache")
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
func (c *IAMCache) cacheIAM(iam *model.IAM) {
|
||||
err := c.iamCache.Set(iam.AggregateID, iam)
|
||||
if err != nil {
|
||||
logging.Log("EVENT-os03w").WithError(err).Debug("error in setting iam cache")
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
package eventsourcing
|
||||
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/cache/config"
|
||||
"github.com/caos/zitadel/internal/eventstore/v1"
|
||||
)
|
||||
|
||||
type IAMConfig struct {
|
||||
v1.Eventstore
|
||||
Cache *config.CacheConfig
|
||||
}
|
@@ -1,30 +0,0 @@
|
||||
package eventsourcing
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
||||
)
|
||||
|
||||
func IAMByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) {
|
||||
if id == "" {
|
||||
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-0soe4", "Errors.IAM.IDMissing")
|
||||
}
|
||||
return IAMQuery(latestSequence).
|
||||
AggregateIDFilter(id), nil
|
||||
}
|
||||
|
||||
func IAMQuery(latestSequence uint64) *es_models.SearchQuery {
|
||||
return es_models.NewSearchQuery().
|
||||
AggregateTypeFilter(model.IAMAggregate).
|
||||
LatestSequenceFilter(latestSequence)
|
||||
}
|
||||
|
||||
func IAMAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, iam *model.IAM) (*es_models.Aggregate, error) {
|
||||
if iam == nil {
|
||||
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-lo04e", "Errors.Internal")
|
||||
}
|
||||
return aggCreator.NewAggregate(ctx, iam.AggregateID, model.IAMAggregate, model.IAMVersion, iam.Sequence)
|
||||
}
|
@@ -1,96 +0,0 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
iam_model "github.com/caos/zitadel/internal/iam/model"
|
||||
"github.com/caos/zitadel/internal/iam/repository/view/model"
|
||||
"github.com/caos/zitadel/internal/view/repository"
|
||||
)
|
||||
|
||||
func GetCustomTexts(db *gorm.DB, table string, aggregateID, template, lang string) ([]*model.CustomTextView, error) {
|
||||
texts := make([]*model.CustomTextView, 0)
|
||||
queries := []*iam_model.CustomTextSearchQuery{
|
||||
{
|
||||
Key: iam_model.CustomTextSearchKeyAggregateID,
|
||||
Value: aggregateID,
|
||||
Method: domain.SearchMethodEquals,
|
||||
},
|
||||
{
|
||||
Key: iam_model.CustomTextSearchKeyTemplate,
|
||||
Value: template,
|
||||
Method: domain.SearchMethodEquals,
|
||||
},
|
||||
{
|
||||
Key: iam_model.CustomTextSearchKeyLanguage,
|
||||
Value: lang,
|
||||
Method: domain.SearchMethodEquals,
|
||||
},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.CustomTextSearchRequest{Queries: queries})
|
||||
_, err := query(db, &texts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return texts, nil
|
||||
}
|
||||
|
||||
func GetCustomTextsByAggregateIDAndTemplate(db *gorm.DB, table string, aggregateID, template string) ([]*model.CustomTextView, error) {
|
||||
texts := make([]*model.CustomTextView, 0)
|
||||
queries := []*iam_model.CustomTextSearchQuery{
|
||||
{
|
||||
Key: iam_model.CustomTextSearchKeyAggregateID,
|
||||
Value: aggregateID,
|
||||
Method: domain.SearchMethodEquals,
|
||||
},
|
||||
{
|
||||
Key: iam_model.CustomTextSearchKeyTemplate,
|
||||
Value: template,
|
||||
Method: domain.SearchMethodEquals,
|
||||
},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.CustomTextSearchRequest{Queries: queries})
|
||||
_, err := query(db, &texts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return texts, nil
|
||||
}
|
||||
|
||||
func CustomTextByIDs(db *gorm.DB, table, aggregateID, template, lang, key string) (*model.CustomTextView, error) {
|
||||
customText := new(model.CustomTextView)
|
||||
aggregateIDQuery := &model.CustomTextSearchQuery{Key: iam_model.CustomTextSearchKeyAggregateID, Value: aggregateID, Method: domain.SearchMethodEquals}
|
||||
textTypeQuery := &model.CustomTextSearchQuery{Key: iam_model.CustomTextSearchKeyTemplate, Value: template, Method: domain.SearchMethodEquals}
|
||||
languageQuery := &model.CustomTextSearchQuery{Key: iam_model.CustomTextSearchKeyLanguage, Value: lang, Method: domain.SearchMethodEquals}
|
||||
keyQuery := &model.CustomTextSearchQuery{Key: iam_model.CustomTextSearchKeyKey, Value: key, Method: domain.SearchMethodEquals}
|
||||
query := repository.PrepareGetByQuery(table, aggregateIDQuery, textTypeQuery, languageQuery, keyQuery)
|
||||
err := query(db, customText)
|
||||
if caos_errs.IsNotFound(err) {
|
||||
return nil, caos_errs.ThrowNotFound(nil, "VIEW-8nUU3", "Errors.CustomCustomText.NotExisting")
|
||||
}
|
||||
return customText, err
|
||||
}
|
||||
|
||||
func PutCustomText(db *gorm.DB, table string, customText *model.CustomTextView) error {
|
||||
save := repository.PrepareSave(table)
|
||||
return save(db, customText)
|
||||
}
|
||||
|
||||
func DeleteCustomText(db *gorm.DB, table, aggregateID, template, lang, key string) error {
|
||||
aggregateIDSearch := repository.Key{Key: model.CustomTextSearchKey(iam_model.CustomTextSearchKeyAggregateID), Value: aggregateID}
|
||||
templateSearch := repository.Key{Key: model.CustomTextSearchKey(iam_model.CustomTextSearchKeyTemplate), Value: template}
|
||||
languageSearch := repository.Key{Key: model.CustomTextSearchKey(iam_model.CustomTextSearchKeyLanguage), Value: lang}
|
||||
keySearch := repository.Key{Key: model.CustomTextSearchKey(iam_model.CustomTextSearchKeyKey), Value: key}
|
||||
delete := repository.PrepareDeleteByKeys(table, aggregateIDSearch, templateSearch, keySearch, languageSearch)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteCustomTextTemplate(db *gorm.DB, table, aggregateID, template, lang string) error {
|
||||
aggregateIDSearch := repository.Key{Key: model.CustomTextSearchKey(iam_model.CustomTextSearchKeyAggregateID), Value: aggregateID}
|
||||
templateSearch := repository.Key{Key: model.CustomTextSearchKey(iam_model.CustomTextSearchKeyTemplate), Value: template}
|
||||
languageSearch := repository.Key{Key: model.CustomTextSearchKey(iam_model.CustomTextSearchKeyLanguage), Value: lang}
|
||||
delete := repository.PrepareDeleteByKeys(table, aggregateIDSearch, templateSearch, languageSearch)
|
||||
return delete(db)
|
||||
}
|
@@ -1,77 +0,0 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
iam_model "github.com/caos/zitadel/internal/iam/model"
|
||||
"github.com/caos/zitadel/internal/iam/repository/view/model"
|
||||
"github.com/caos/zitadel/internal/view/repository"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
func IAMMemberByIDs(db *gorm.DB, table, orgID, userID string) (*model.IAMMemberView, error) {
|
||||
member := new(model.IAMMemberView)
|
||||
|
||||
iamIDQuery := &model.IAMMemberSearchQuery{Key: iam_model.IAMMemberSearchKeyIamID, Value: orgID, Method: domain.SearchMethodEquals}
|
||||
userIDQuery := &model.IAMMemberSearchQuery{Key: iam_model.IAMMemberSearchKeyUserID, Value: userID, Method: domain.SearchMethodEquals}
|
||||
query := repository.PrepareGetByQuery(table, iamIDQuery, userIDQuery)
|
||||
err := query(db, member)
|
||||
if caos_errs.IsNotFound(err) {
|
||||
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Ahq2s", "Errors.IAM.MemberNotExisting")
|
||||
}
|
||||
return member, err
|
||||
}
|
||||
|
||||
func SearchIAMMembers(db *gorm.DB, table string, req *iam_model.IAMMemberSearchRequest) ([]*model.IAMMemberView, uint64, error) {
|
||||
members := make([]*model.IAMMemberView, 0)
|
||||
query := repository.PrepareSearchQuery(table, model.IAMMemberSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
||||
count, err := query(db, &members)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return members, count, nil
|
||||
}
|
||||
func IAMMembersByUserID(db *gorm.DB, table string, userID string) ([]*model.IAMMemberView, error) {
|
||||
members := make([]*model.IAMMemberView, 0)
|
||||
queries := []*iam_model.IAMMemberSearchQuery{
|
||||
{
|
||||
Key: iam_model.IAMMemberSearchKeyUserID,
|
||||
Value: userID,
|
||||
Method: domain.SearchMethodEquals,
|
||||
},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.IAMMemberSearchRequest{Queries: queries})
|
||||
_, err := query(db, &members)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return members, nil
|
||||
}
|
||||
|
||||
func PutIAMMember(db *gorm.DB, table string, role *model.IAMMemberView) error {
|
||||
save := repository.PrepareSave(table)
|
||||
return save(db, role)
|
||||
}
|
||||
|
||||
func PutIAMMembers(db *gorm.DB, table string, members ...*model.IAMMemberView) error {
|
||||
save := repository.PrepareBulkSave(table)
|
||||
m := make([]interface{}, len(members))
|
||||
for i, member := range members {
|
||||
m[i] = member
|
||||
}
|
||||
return save(db, m...)
|
||||
}
|
||||
|
||||
func DeleteIAMMember(db *gorm.DB, table, orgID, userID string) error {
|
||||
member, err := IAMMemberByIDs(db, table, orgID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
delete := repository.PrepareDeleteByObject(table, member)
|
||||
return delete(db)
|
||||
}
|
||||
|
||||
func DeleteIAMMembersByUserID(db *gorm.DB, table, userID string) error {
|
||||
delete := repository.PrepareDeleteByKey(table, model.IAMMemberSearchKey(iam_model.IAMMemberSearchKeyUserID), userID)
|
||||
return delete(db)
|
||||
}
|
@@ -1,54 +0,0 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
iam_model "github.com/caos/zitadel/internal/iam/model"
|
||||
"github.com/caos/zitadel/internal/iam/repository/view/model"
|
||||
"github.com/caos/zitadel/internal/view/repository"
|
||||
)
|
||||
|
||||
func GetMessageTexts(db *gorm.DB, table string, aggregateID string) ([]*model.MessageTextView, error) {
|
||||
texts := make([]*model.MessageTextView, 0)
|
||||
queries := []*iam_model.MessageTextSearchQuery{
|
||||
{
|
||||
Key: iam_model.MessageTextSearchKeyAggregateID,
|
||||
Value: aggregateID,
|
||||
Method: domain.SearchMethodEquals,
|
||||
},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.MessageTextSearchRequest{Queries: queries})
|
||||
_, err := query(db, &texts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return texts, nil
|
||||
}
|
||||
|
||||
func GetMessageTextByIDs(db *gorm.DB, table, aggregateID, textType, lang string) (*model.MessageTextView, error) {
|
||||
mailText := new(model.MessageTextView)
|
||||
aggregateIDQuery := &model.MessageTextSearchQuery{Key: iam_model.MessageTextSearchKeyAggregateID, Value: aggregateID, Method: domain.SearchMethodEquals}
|
||||
textTypeQuery := &model.MessageTextSearchQuery{Key: iam_model.MessageTextSearchKeyMessageTextType, Value: textType, Method: domain.SearchMethodEquals}
|
||||
languageQuery := &model.MessageTextSearchQuery{Key: iam_model.MessageTextSearchKeyLanguage, Value: lang, Method: domain.SearchMethodEquals}
|
||||
query := repository.PrepareGetByQuery(table, aggregateIDQuery, textTypeQuery, languageQuery)
|
||||
err := query(db, mailText)
|
||||
if caos_errs.IsNotFound(err) {
|
||||
return nil, caos_errs.ThrowNotFound(nil, "VIEW-IiJjm", "Errors.IAM.CustomMessageText.NotExisting")
|
||||
}
|
||||
return mailText, err
|
||||
}
|
||||
|
||||
func PutMessageText(db *gorm.DB, table string, mailText *model.MessageTextView) error {
|
||||
save := repository.PrepareSave(table)
|
||||
return save(db, mailText)
|
||||
}
|
||||
|
||||
func DeleteMessageText(db *gorm.DB, table, aggregateID, textType, lang string) error {
|
||||
aggregateIDSearch := repository.Key{Key: model.MessageTextSearchKey(iam_model.MessageTextSearchKeyAggregateID), Value: aggregateID}
|
||||
textTypeSearch := repository.Key{Key: model.MessageTextSearchKey(iam_model.MessageTextSearchKeyMessageTextType), Value: textType}
|
||||
languageSearch := repository.Key{Key: model.MessageTextSearchKey(iam_model.MessageTextSearchKeyLanguage), Value: lang}
|
||||
delete := repository.PrepareDeleteByKeys(table, aggregateIDSearch, textTypeSearch, languageSearch)
|
||||
return delete(db)
|
||||
}
|
@@ -1,80 +0,0 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/iam/repository/view/model"
|
||||
"github.com/caos/zitadel/internal/view/repository"
|
||||
)
|
||||
|
||||
func GetMetadataList(db *gorm.DB, table string, aggregateID string) ([]*model.MetadataView, error) {
|
||||
metadatas := make([]*model.MetadataView, 0)
|
||||
queries := []*domain.MetadataSearchQuery{
|
||||
{
|
||||
Key: domain.MetadataSearchKeyAggregateID,
|
||||
Value: aggregateID,
|
||||
Method: domain.SearchMethodEquals,
|
||||
},
|
||||
}
|
||||
query := repository.PrepareSearchQuery(table, model.MetadataSearchRequest{Queries: queries})
|
||||
_, err := query(db, &metadatas)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return metadatas, nil
|
||||
}
|
||||
|
||||
func MetadataByKey(db *gorm.DB, table, aggregateID, key string) (*model.MetadataView, error) {
|
||||
metadata := new(model.MetadataView)
|
||||
aggregateIDQuery := &model.MetadataSearchQuery{Key: domain.MetadataSearchKeyAggregateID, Value: aggregateID, Method: domain.SearchMethodEquals}
|
||||
keyQuery := &model.MetadataSearchQuery{Key: domain.MetadataSearchKeyKey, Value: key, Method: domain.SearchMethodEquals}
|
||||
query := repository.PrepareGetByQuery(table, aggregateIDQuery, keyQuery)
|
||||
err := query(db, metadata)
|
||||
if caos_errs.IsNotFound(err) {
|
||||
return nil, caos_errs.ThrowNotFound(nil, "VIEW-m0pes", "Errors.Metadata.NotExisting")
|
||||
}
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
func MetadataByKeyAndResourceOwner(db *gorm.DB, table, aggregateID, resourceOwner, key string) (*model.MetadataView, error) {
|
||||
metadata := new(model.MetadataView)
|
||||
aggregateIDQuery := &model.MetadataSearchQuery{Key: domain.MetadataSearchKeyAggregateID, Value: aggregateID, Method: domain.SearchMethodEquals}
|
||||
resourceOwnerQuery := &model.MetadataSearchQuery{Key: domain.MetadataSearchKeyResourceOwner, Value: resourceOwner, Method: domain.SearchMethodEquals}
|
||||
keyQuery := &model.MetadataSearchQuery{Key: domain.MetadataSearchKeyKey, Value: key, Method: domain.SearchMethodEquals}
|
||||
query := repository.PrepareGetByQuery(table, aggregateIDQuery, resourceOwnerQuery, keyQuery)
|
||||
err := query(db, metadata)
|
||||
if caos_errs.IsNotFound(err) {
|
||||
return nil, caos_errs.ThrowNotFound(nil, "VIEW-29kkd", "Errors.Metadata.NotExisting")
|
||||
}
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
func SearchMetadata(db *gorm.DB, table string, req *domain.MetadataSearchRequest) ([]*model.MetadataView, uint64, error) {
|
||||
metadata := make([]*model.MetadataView, 0)
|
||||
query := repository.PrepareSearchQuery(table, model.MetadataSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
||||
count, err := query(db, &metadata)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return metadata, count, nil
|
||||
}
|
||||
|
||||
func PutMetadata(db *gorm.DB, table string, customText *model.MetadataView) error {
|
||||
save := repository.PrepareSave(table)
|
||||
return save(db, customText)
|
||||
}
|
||||
|
||||
func DeleteMetadata(db *gorm.DB, table, aggregateID, key string) error {
|
||||
aggregateIDQuery := repository.Key{Key: model.MetadataSearchKey(domain.MetadataSearchKeyAggregateID), Value: aggregateID}
|
||||
keyQuery := repository.Key{Key: model.MetadataSearchKey(domain.MetadataSearchKeyKey), Value: key}
|
||||
deleteMD := repository.PrepareDeleteByKeys(table, aggregateIDQuery, keyQuery)
|
||||
return deleteMD(db)
|
||||
}
|
||||
|
||||
func DeleteMetadataByAggregateID(db *gorm.DB, table, aggregateID string) error {
|
||||
aggregateIDQuery := repository.Key{Key: model.MetadataSearchKey(domain.MetadataSearchKeyAggregateID), Value: aggregateID}
|
||||
deleteMD := repository.PrepareDeleteByKeys(table, aggregateIDQuery)
|
||||
return deleteMD(db)
|
||||
}
|
Reference in New Issue
Block a user