mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 04:18:01 +00:00
b5564572bc
This implementation increases parallel write capabilities of the eventstore. Please have a look at the technical advisories: [05](https://zitadel.com/docs/support/advisory/a10005) and [06](https://zitadel.com/docs/support/advisory/a10006). The implementation of eventstore.push is rewritten and stored events are migrated to a new table `eventstore.events2`. If you are using cockroach: make sure that the database user of ZITADEL has `VIEWACTIVITY` grant. This is used to query events.
183 lines
5.9 KiB
Go
183 lines
5.9 KiB
Go
package view
|
|
|
|
import (
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
usr_model "github.com/zitadel/zitadel/internal/user/model"
|
|
"github.com/zitadel/zitadel/internal/user/repository/view/model"
|
|
)
|
|
|
|
func UserByID(db *gorm.DB, table, userID, instanceID string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
userIDQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyUserID,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: userID,
|
|
}
|
|
instanceIDQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: instanceID,
|
|
}
|
|
ownerRemovedQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: false,
|
|
}
|
|
query := repository.PrepareGetByQuery(table, userIDQuery, instanceIDQuery, ownerRemovedQuery)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-sj8Sw", "Errors.User.NotFound")
|
|
}
|
|
user.SetEmptyUserType()
|
|
return user, err
|
|
}
|
|
|
|
func UserByUserName(db *gorm.DB, table, userName, instanceID string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
userNameQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyUserName,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: userName,
|
|
}
|
|
instanceIDQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: instanceID,
|
|
}
|
|
ownerRemovedQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: false,
|
|
}
|
|
query := repository.PrepareGetByQuery(table, userNameQuery, instanceIDQuery, ownerRemovedQuery)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Lso9s", "Errors.User.NotFound")
|
|
}
|
|
user.SetEmptyUserType()
|
|
return user, err
|
|
}
|
|
|
|
func UserByLoginName(db *gorm.DB, table, loginName, instanceID string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
loginNameQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyLoginNames,
|
|
Method: domain.SearchMethodListContains,
|
|
Value: loginName,
|
|
}
|
|
instanceIDQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: instanceID,
|
|
}
|
|
ownerRemovedQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: false,
|
|
}
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery, instanceIDQuery, ownerRemovedQuery)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFound")
|
|
}
|
|
user.SetEmptyUserType()
|
|
return user, err
|
|
}
|
|
|
|
func UserByLoginNameAndResourceOwner(db *gorm.DB, table, loginName, resourceOwner, instanceID string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
loginNameQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyLoginNames,
|
|
Method: domain.SearchMethodListContains,
|
|
Value: loginName,
|
|
}
|
|
resourceOwnerQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: resourceOwner,
|
|
}
|
|
instanceIDQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: instanceID,
|
|
}
|
|
ownerRemovedQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: false,
|
|
}
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery, resourceOwnerQuery, instanceIDQuery, ownerRemovedQuery)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFoundOnOrg")
|
|
}
|
|
user.SetEmptyUserType()
|
|
return user, err
|
|
}
|
|
|
|
func UsersByOrgID(db *gorm.DB, table, orgID, instanceID string) ([]*model.UserView, error) {
|
|
users := make([]*model.UserView, 0)
|
|
orgIDQuery := &usr_model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: orgID,
|
|
}
|
|
instanceIDQuery := &usr_model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: instanceID,
|
|
}
|
|
ownerRemovedQuery := &usr_model.UserSearchQuery{
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: false,
|
|
}
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery, instanceIDQuery, ownerRemovedQuery},
|
|
})
|
|
_, err := query(db, &users)
|
|
return users, err
|
|
}
|
|
|
|
func PutUsers(db *gorm.DB, table string, users ...*model.UserView) error {
|
|
save := repository.PrepareBulkSave(table)
|
|
u := make([]interface{}, len(users))
|
|
for i, user := range users {
|
|
u[i] = user
|
|
}
|
|
return save(db, u...)
|
|
}
|
|
|
|
func PutUser(db *gorm.DB, table string, user *model.UserView) error {
|
|
save := repository.PrepareSave(table)
|
|
return save(db, user)
|
|
}
|
|
|
|
func DeleteUser(db *gorm.DB, table, userID, instanceID string) error {
|
|
delete := repository.PrepareDeleteByKeys(table,
|
|
repository.Key{model.UserSearchKey(usr_model.UserSearchKeyUserID), userID},
|
|
repository.Key{model.UserSearchKey(usr_model.UserSearchKeyInstanceID), instanceID},
|
|
)
|
|
return delete(db)
|
|
}
|
|
|
|
func DeleteInstanceUsers(db *gorm.DB, table, instanceID string) error {
|
|
delete := repository.PrepareDeleteByKey(table, model.UserSearchKey(usr_model.UserSearchKeyInstanceID), instanceID)
|
|
return delete(db)
|
|
}
|
|
|
|
func UpdateOrgOwnerRemovedUsers(db *gorm.DB, table, instanceID, aggID string) error {
|
|
update := repository.PrepareUpdateByKeys(table,
|
|
model.UserSearchKey(usr_model.UserSearchOwnerRemoved),
|
|
true,
|
|
repository.Key{Key: model.UserSearchKey(usr_model.UserSearchKeyInstanceID), Value: instanceID},
|
|
repository.Key{Key: model.UserSearchKey(usr_model.UserSearchKeyResourceOwner), Value: aggID},
|
|
)
|
|
return update(db)
|
|
}
|