2020-05-12 04:30:53 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
2020-08-18 06:57:16 +00:00
|
|
|
|
2020-06-22 08:38:29 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
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"
|
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,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, userIDQuery, instanceIDQuery)
|
2020-05-12 04:30:53 +00:00
|
|
|
err := query(db, user)
|
2020-06-11 11:22:24 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-sj8Sw", "Errors.User.NotFound")
|
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, userNameQuery, instanceIDQuery)
|
2020-05-12 04:30:53 +00:00
|
|
|
err := query(db, user)
|
2020-06-11 11:22:24 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Lso9s", "Errors.User.NotFound")
|
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery, instanceIDQuery)
|
2020-06-16 09:40:18 +00:00
|
|
|
err := query(db, user)
|
2020-07-06 11:18:10 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFound")
|
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery, resourceOwnerQuery, instanceIDQuery)
|
2020-09-18 11:26:28 +00:00
|
|
|
err := query(db, user)
|
|
|
|
if caos_errs.IsNotFound(err) {
|
2020-12-14 09:54:29 +00:00
|
|
|
return nil, caos_errs.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,
|
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
2022-04-19 06:26:12 +00:00
|
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery, instanceIDQuery},
|
2020-06-16 09:40:18 +00:00
|
|
|
})
|
|
|
|
_, err := query(db, &users)
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func UserIDsByDomain(db *gorm.DB, table, orgDomain, instanceID string) ([]string, error) {
|
2020-08-20 05:28:49 +00:00
|
|
|
type id struct {
|
|
|
|
Id string
|
|
|
|
}
|
|
|
|
ids := make([]id, 0)
|
2020-08-18 06:57:16 +00:00
|
|
|
orgIDQuery := &usr_model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyUserName,
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodEndsWithIgnoreCase,
|
|
|
|
Value: "%" + orgDomain,
|
2020-08-18 06:57:16 +00:00
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
instanceIDQuery := &usr_model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: instanceID,
|
|
|
|
}
|
2020-08-18 06:57:16 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
2022-04-19 06:26:12 +00:00
|
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery, instanceIDQuery},
|
2020-08-18 06:57:16 +00:00
|
|
|
})
|
2020-08-20 05:28:49 +00:00
|
|
|
_, err := query(db, &ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
users := make([]string, len(ids))
|
|
|
|
for i, id := range ids {
|
|
|
|
users[i] = id.Id
|
|
|
|
}
|
2020-08-18 06:57:16 +00:00
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:37:55 +00:00
|
|
|
func SearchUsers(db *gorm.DB, table string, req *usr_model.UserSearchRequest) ([]*model.UserView, uint64, error) {
|
2020-05-12 04:30:53 +00:00
|
|
|
users := make([]*model.UserView, 0)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
2020-05-12 04:30:53 +00:00
|
|
|
count, err := query(db, &users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return users, count, nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func GetGlobalUserByLoginName(db *gorm.DB, table, loginName, 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
|
|
|
query := repository.PrepareGetByQuery(table,
|
|
|
|
&model.UserSearchQuery{Key: usr_model.UserSearchKeyLoginNames, Value: loginName, Method: domain.SearchMethodListContains},
|
|
|
|
&model.UserSearchQuery{Key: usr_model.UserSearchKeyInstanceID, Value: instanceID, Method: domain.SearchMethodEquals},
|
|
|
|
)
|
2020-05-12 04:30:53 +00:00
|
|
|
err := query(db, user)
|
2020-06-11 11:22:24 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-8uWer", "Errors.User.NotFound")
|
|
|
|
}
|
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 UserMFAs(db *gorm.DB, table, userID, instanceID string) ([]*usr_model.MultiFactor, error) {
|
|
|
|
user, err := UserByID(db, table, userID, instanceID)
|
2020-05-12 04:30:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-02 16:00:04 +00:00
|
|
|
if user.OTPState == int32(usr_model.MFAStateUnspecified) {
|
2020-05-12 04:30:53 +00:00
|
|
|
return []*usr_model.MultiFactor{}, nil
|
|
|
|
}
|
2020-12-02 16:00:04 +00:00
|
|
|
return []*usr_model.MultiFactor{{Type: usr_model.MFATypeOTP, State: usr_model.MFAState(user.OTPState)}}, nil
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|