2020-08-26 07:56:23 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2022-04-19 06:26:12 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
iam_model "github.com/zitadel/zitadel/internal/iam/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/iam/repository/view/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-08-26 07:56:23 +00:00
|
|
|
)
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func GetIDPProviderByAggregateIDAndConfigID(db *gorm.DB, table, aggregateID, idpConfigID, instanceID string) (*model.IDPProviderView, error) {
|
2020-08-26 07:56:23 +00:00
|
|
|
policy := new(model.IDPProviderView)
|
2021-03-01 07:48:50 +00:00
|
|
|
aggIDQuery := &model.IDPProviderSearchQuery{Key: iam_model.IDPProviderSearchKeyAggregateID, Value: aggregateID, Method: domain.SearchMethodEquals}
|
|
|
|
idpConfigIDQuery := &model.IDPProviderSearchQuery{Key: iam_model.IDPProviderSearchKeyIdpConfigID, Value: idpConfigID, Method: domain.SearchMethodEquals}
|
2022-04-19 06:26:12 +00:00
|
|
|
instanceIDQuery := &model.IDPProviderSearchQuery{Key: iam_model.IDPProviderSearchKeyInstanceID, Value: instanceID, Method: domain.SearchMethodEquals}
|
2022-11-30 16:01:17 +00:00
|
|
|
ownerRemovedQuery := &model.IDPProviderSearchQuery{Key: iam_model.IDPProviderSearchKeyOwnerRemoved, Value: false, Method: domain.SearchMethodEquals}
|
|
|
|
query := repository.PrepareGetByQuery(table, aggIDQuery, idpConfigIDQuery, instanceIDQuery, ownerRemovedQuery)
|
2020-08-26 07:56:23 +00:00
|
|
|
err := query(db, policy)
|
2023-12-08 14:30:55 +00:00
|
|
|
if zerrors.IsNotFound(err) {
|
|
|
|
return nil, zerrors.ThrowNotFound(nil, "VIEW-Skvi8", "Errors.IAM.LoginPolicy.IDP.NotExisting")
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
return policy, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func IDPProvidersByIdpConfigID(db *gorm.DB, table, idpConfigID, instanceID string) ([]*model.IDPProviderView, error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
providers := make([]*model.IDPProviderView, 0)
|
2020-08-26 07:56:23 +00:00
|
|
|
queries := []*iam_model.IDPProviderSearchQuery{
|
|
|
|
{
|
|
|
|
Key: iam_model.IDPProviderSearchKeyIdpConfigID,
|
|
|
|
Value: idpConfigID,
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-08-26 07:56:23 +00:00
|
|
|
},
|
2022-04-19 06:26:12 +00:00
|
|
|
{
|
|
|
|
Key: iam_model.IDPProviderSearchKeyInstanceID,
|
|
|
|
Value: instanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Key: iam_model.IDPProviderSearchKeyOwnerRemoved,
|
|
|
|
Value: false,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
},
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
query := repository.PrepareSearchQuery(table, model.IDPProviderSearchRequest{Queries: queries})
|
2020-09-18 11:26:28 +00:00
|
|
|
_, err := query(db, &providers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return providers, nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func IDPProvidersByAggregateIDAndState(db *gorm.DB, table string, aggregateID, instanceID string, idpConfigState iam_model.IDPConfigState) ([]*model.IDPProviderView, error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
providers := make([]*model.IDPProviderView, 0)
|
|
|
|
queries := []*iam_model.IDPProviderSearchQuery{
|
|
|
|
{
|
|
|
|
Key: iam_model.IDPProviderSearchKeyAggregateID,
|
|
|
|
Value: aggregateID,
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-09-18 11:26:28 +00:00
|
|
|
},
|
2020-09-23 14:52:19 +00:00
|
|
|
{
|
|
|
|
Key: iam_model.IDPProviderSearchKeyState,
|
|
|
|
Value: int(idpConfigState),
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-09-23 14:52:19 +00:00
|
|
|
},
|
2022-04-19 06:26:12 +00:00
|
|
|
{
|
|
|
|
Key: iam_model.IDPProviderSearchKeyInstanceID,
|
|
|
|
Value: instanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Key: iam_model.IDPProviderSearchKeyOwnerRemoved,
|
|
|
|
Value: false,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
},
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
query := repository.PrepareSearchQuery(table, model.IDPProviderSearchRequest{Queries: queries})
|
|
|
|
_, err := query(db, &providers)
|
2020-08-26 07:56:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-18 11:26:28 +00:00
|
|
|
return providers, nil
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SearchIDPProviders(db *gorm.DB, table string, req *iam_model.IDPProviderSearchRequest) ([]*model.IDPProviderView, uint64, error) {
|
|
|
|
providers := make([]*model.IDPProviderView, 0)
|
|
|
|
query := repository.PrepareSearchQuery(table, model.IDPProviderSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
|
|
|
count, err := query(db, &providers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return providers, count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutIDPProvider(db *gorm.DB, table string, provider *model.IDPProviderView) error {
|
|
|
|
save := repository.PrepareSave(table)
|
|
|
|
return save(db, provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutIDPProviders(db *gorm.DB, table string, providers ...*model.IDPProviderView) error {
|
|
|
|
save := repository.PrepareBulkSave(table)
|
|
|
|
p := make([]interface{}, len(providers))
|
|
|
|
for i, provider := range providers {
|
|
|
|
p[i] = provider
|
|
|
|
}
|
|
|
|
return save(db, p...)
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func DeleteIDPProvider(db *gorm.DB, table, aggregateID, idpConfigID, instanceID string) error {
|
2020-08-26 07:56:23 +00:00
|
|
|
delete := repository.PrepareDeleteByKeys(table,
|
|
|
|
repository.Key{Key: model.IDPProviderSearchKey(iam_model.IDPProviderSearchKeyAggregateID), Value: aggregateID},
|
|
|
|
repository.Key{Key: model.IDPProviderSearchKey(iam_model.IDPProviderSearchKeyIdpConfigID), Value: idpConfigID},
|
2022-04-19 06:26:12 +00:00
|
|
|
repository.Key{Key: model.IDPProviderSearchKey(iam_model.IDPProviderSearchKeyInstanceID), Value: instanceID},
|
2020-08-26 07:56:23 +00:00
|
|
|
)
|
|
|
|
return delete(db)
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func DeleteIDPProvidersByAggregateID(db *gorm.DB, table, aggregateID, instanceID string) error {
|
2020-08-26 07:56:23 +00:00
|
|
|
delete := repository.PrepareDeleteByKeys(table,
|
|
|
|
repository.Key{Key: model.IDPProviderSearchKey(iam_model.IDPProviderSearchKeyAggregateID), Value: aggregateID},
|
2022-04-19 06:26:12 +00:00
|
|
|
repository.Key{Key: model.IDPProviderSearchKey(iam_model.IDPProviderSearchKeyInstanceID), Value: instanceID},
|
2020-08-26 07:56:23 +00:00
|
|
|
)
|
|
|
|
return delete(db)
|
|
|
|
}
|
2022-10-26 13:06:48 +00:00
|
|
|
|
|
|
|
func DeleteInstanceIDPProviders(db *gorm.DB, table, instanceID string) error {
|
2022-11-30 16:01:17 +00:00
|
|
|
delete := repository.PrepareDeleteByKey(table,
|
|
|
|
model.IDPProviderSearchKey(iam_model.IDPProviderSearchKeyInstanceID),
|
|
|
|
instanceID,
|
|
|
|
)
|
2022-10-26 13:06:48 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
func UpdateOrgOwnerRemovedIDPProviders(db *gorm.DB, table, instanceID, aggID string) error {
|
|
|
|
update := repository.PrepareUpdateByKeys(table,
|
|
|
|
model.IDPProviderSearchKey(iam_model.IDPProviderSearchKeyOwnerRemoved),
|
|
|
|
true,
|
|
|
|
repository.Key{Key: model.IDPProviderSearchKey(iam_model.IDPProviderSearchKeyInstanceID), Value: instanceID},
|
|
|
|
repository.Key{Key: model.IDPProviderSearchKey(iam_model.IDPProviderSearchKeyAggregateID), Value: aggID},
|
|
|
|
)
|
|
|
|
return update(db)
|
|
|
|
}
|