2020-05-12 04:30:53 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2020-06-22 08:38:29 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-26 23:01:45 +00:00
|
|
|
usr_model "github.com/zitadel/zitadel/internal/user/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/user/repository/view/model"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-05-12 04:30:53 +00:00
|
|
|
)
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func UserByID(db *gorm.DB, table, userID, instanceID string) (*model.UserView, error) {
|
2020-05-12 04:30:53 +00:00
|
|
|
user := new(model.UserView)
|
2022-04-19 06:26:12 +00:00
|
|
|
userIDQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyUserID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: userID,
|
|
|
|
}
|
|
|
|
instanceIDQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
ownerRemovedQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: false,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, userIDQuery, instanceIDQuery, ownerRemovedQuery)
|
2020-05-12 04:30:53 +00:00
|
|
|
err := query(db, user)
|
2023-12-08 14:30:55 +00:00
|
|
|
if zerrors.IsNotFound(err) {
|
|
|
|
return nil, zerrors.ThrowNotFound(nil, "VIEW-sj8Sw", "Errors.User.NotFound")
|
2020-06-11 11:22:24 +00:00
|
|
|
}
|
2021-03-25 17:12:24 +00:00
|
|
|
user.SetEmptyUserType()
|
2020-05-12 04:30:53 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func UserByUserName(db *gorm.DB, table, userName, instanceID string) (*model.UserView, error) {
|
2020-05-12 04:30:53 +00:00
|
|
|
user := new(model.UserView)
|
2022-04-19 06:26:12 +00:00
|
|
|
userNameQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyUserName,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: userName,
|
|
|
|
}
|
|
|
|
instanceIDQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
ownerRemovedQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: false,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, userNameQuery, instanceIDQuery, ownerRemovedQuery)
|
2020-05-12 04:30:53 +00:00
|
|
|
err := query(db, user)
|
2023-12-08 14:30:55 +00:00
|
|
|
if zerrors.IsNotFound(err) {
|
|
|
|
return nil, zerrors.ThrowNotFound(nil, "VIEW-Lso9s", "Errors.User.NotFound")
|
2020-06-11 11:22:24 +00:00
|
|
|
}
|
2021-03-25 17:12:24 +00:00
|
|
|
user.SetEmptyUserType()
|
2020-05-12 04:30:53 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func UserByLoginName(db *gorm.DB, table, loginName, instanceID string) (*model.UserView, error) {
|
2020-06-16 09:40:18 +00:00
|
|
|
user := new(model.UserView)
|
|
|
|
loginNameQuery := &model.UserSearchQuery{
|
2020-06-23 12:47:47 +00:00
|
|
|
Key: usr_model.UserSearchKeyLoginNames,
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodListContains,
|
2020-06-22 08:38:29 +00:00
|
|
|
Value: loginName,
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
instanceIDQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
ownerRemovedQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: false,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery, instanceIDQuery, ownerRemovedQuery)
|
2020-06-16 09:40:18 +00:00
|
|
|
err := query(db, user)
|
2023-12-08 14:30:55 +00:00
|
|
|
if zerrors.IsNotFound(err) {
|
|
|
|
return nil, zerrors.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFound")
|
2020-07-06 11:18:10 +00:00
|
|
|
}
|
2021-03-25 17:12:24 +00:00
|
|
|
user.SetEmptyUserType()
|
2020-06-16 09:40:18 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func UserByLoginNameAndResourceOwner(db *gorm.DB, table, loginName, resourceOwner, instanceID string) (*model.UserView, error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
user := new(model.UserView)
|
|
|
|
loginNameQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyLoginNames,
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodListContains,
|
2020-09-18 11:26:28 +00:00
|
|
|
Value: loginName,
|
|
|
|
}
|
|
|
|
resourceOwnerQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-09-18 11:26:28 +00:00
|
|
|
Value: resourceOwner,
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
instanceIDQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
ownerRemovedQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: false,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery, resourceOwnerQuery, instanceIDQuery, ownerRemovedQuery)
|
2020-09-18 11:26:28 +00:00
|
|
|
err := query(db, user)
|
2023-12-08 14:30:55 +00:00
|
|
|
if zerrors.IsNotFound(err) {
|
|
|
|
return nil, zerrors.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFoundOnOrg")
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
2021-03-25 17:12:24 +00:00
|
|
|
user.SetEmptyUserType()
|
2020-09-18 11:26:28 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func UsersByOrgID(db *gorm.DB, table, orgID, instanceID string) ([]*model.UserView, error) {
|
2020-06-16 09:40:18 +00:00
|
|
|
users := make([]*model.UserView, 0)
|
|
|
|
orgIDQuery := &usr_model.UserSearchQuery{
|
2020-06-23 12:47:47 +00:00
|
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-06-16 09:40:18 +00:00
|
|
|
Value: orgID,
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
instanceIDQuery := &usr_model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
ownerRemovedQuery := &usr_model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: false,
|
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
2022-11-30 16:01:17 +00:00
|
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery, instanceIDQuery, ownerRemovedQuery},
|
2020-06-16 09:40:18 +00:00
|
|
|
})
|
|
|
|
_, err := query(db, &users)
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2020-07-28 07:42:21 +00:00
|
|
|
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 {
|
2020-06-25 06:01:13 +00:00
|
|
|
save := repository.PrepareSave(table)
|
2020-07-28 07:42:21 +00:00
|
|
|
return save(db, user)
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
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},
|
|
|
|
)
|
2020-05-12 04:30:53 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|
2022-10-26 13:06:48 +00:00
|
|
|
|
|
|
|
func DeleteInstanceUsers(db *gorm.DB, table, instanceID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKey(table, model.UserSearchKey(usr_model.UserSearchKeyInstanceID), instanceID)
|
|
|
|
return delete(db)
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|