2020-05-12 06:30:53 +02:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2021-03-01 08:48:50 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-06-25 08:01:13 +02:00
|
|
|
"github.com/caos/zitadel/internal/view/repository"
|
2020-08-18 08:57:16 +02:00
|
|
|
|
2020-06-22 10:38:29 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2020-05-12 06:30:53 +02:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
usr_model "github.com/caos/zitadel/internal/user/model"
|
|
|
|
"github.com/caos/zitadel/internal/user/repository/view/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func UserByID(db *gorm.DB, table, userID string) (*model.UserView, error) {
|
|
|
|
user := new(model.UserView)
|
2020-06-25 08:01:13 +02:00
|
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserID), userID)
|
2020-05-12 06:30:53 +02:00
|
|
|
err := query(db, user)
|
2020-06-11 13:22:24 +02:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-sj8Sw", "Errors.User.NotFound")
|
|
|
|
}
|
2021-03-25 18:12:24 +01:00
|
|
|
user.SetEmptyUserType()
|
2020-05-12 06:30:53 +02:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:14:39 +02:00
|
|
|
func UserByIDAndResourceOwner(db *gorm.DB, table, userID, resourceOwner string) (*model.UserView, error) {
|
|
|
|
user := new(model.UserView)
|
|
|
|
userIDQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyUserID,
|
2021-09-09 13:10:06 +02:00
|
|
|
Method: domain.SearchMethodEquals,
|
2021-09-08 15:14:39 +02:00
|
|
|
Value: userID,
|
|
|
|
}
|
|
|
|
resourceOwnerQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
|
|
|
Method: domain.SearchMethodEquals,
|
|
|
|
Value: resourceOwner,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, userIDQuery, resourceOwnerQuery)
|
|
|
|
err := query(db, user)
|
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-fb93Fs", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
user.SetEmptyUserType()
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2020-05-12 06:30:53 +02:00
|
|
|
func UserByUserName(db *gorm.DB, table, userName string) (*model.UserView, error) {
|
|
|
|
user := new(model.UserView)
|
2020-06-25 08:01:13 +02:00
|
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserName), userName)
|
2020-05-12 06:30:53 +02:00
|
|
|
err := query(db, user)
|
2020-06-11 13:22:24 +02:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Lso9s", "Errors.User.NotFound")
|
|
|
|
}
|
2021-03-25 18:12:24 +01:00
|
|
|
user.SetEmptyUserType()
|
2020-05-12 06:30:53 +02:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:40:18 +02:00
|
|
|
func UserByLoginName(db *gorm.DB, table, loginName string) (*model.UserView, error) {
|
|
|
|
user := new(model.UserView)
|
|
|
|
loginNameQuery := &model.UserSearchQuery{
|
2020-06-23 14:47:47 +02:00
|
|
|
Key: usr_model.UserSearchKeyLoginNames,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodListContains,
|
2020-06-22 10:38:29 +02:00
|
|
|
Value: loginName,
|
2020-06-16 11:40:18 +02:00
|
|
|
}
|
2020-06-25 08:01:13 +02:00
|
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery)
|
2020-06-16 11:40:18 +02:00
|
|
|
err := query(db, user)
|
2020-07-06 13:18:10 +02:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFound")
|
|
|
|
}
|
2021-03-25 18:12:24 +01:00
|
|
|
user.SetEmptyUserType()
|
2020-06-16 11:40:18 +02:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:26:28 +02:00
|
|
|
func UserByLoginNameAndResourceOwner(db *gorm.DB, table, loginName, resourceOwner string) (*model.UserView, error) {
|
|
|
|
user := new(model.UserView)
|
|
|
|
loginNameQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyLoginNames,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodListContains,
|
2020-09-18 13:26:28 +02:00
|
|
|
Value: loginName,
|
|
|
|
}
|
|
|
|
resourceOwnerQuery := &model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-09-18 13:26:28 +02:00
|
|
|
Value: resourceOwner,
|
|
|
|
}
|
|
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery, resourceOwnerQuery)
|
|
|
|
err := query(db, user)
|
|
|
|
if caos_errs.IsNotFound(err) {
|
2020-12-14 10:54:29 +01:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFoundOnOrg")
|
2020-09-18 13:26:28 +02:00
|
|
|
}
|
2021-03-25 18:12:24 +01:00
|
|
|
user.SetEmptyUserType()
|
2020-09-18 13:26:28 +02:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2020-06-16 11:40:18 +02:00
|
|
|
func UsersByOrgID(db *gorm.DB, table, orgID string) ([]*model.UserView, error) {
|
|
|
|
users := make([]*model.UserView, 0)
|
|
|
|
orgIDQuery := &usr_model.UserSearchQuery{
|
2020-06-23 14:47:47 +02:00
|
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-06-16 11:40:18 +02:00
|
|
|
Value: orgID,
|
|
|
|
}
|
2020-06-25 08:01:13 +02:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
2020-06-16 11:40:18 +02:00
|
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery},
|
|
|
|
})
|
|
|
|
_, err := query(db, &users)
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2021-03-01 08:48:50 +01:00
|
|
|
func UserIDsByDomain(db *gorm.DB, table, orgDomain string) ([]string, error) {
|
2020-08-20 07:28:49 +02:00
|
|
|
type id struct {
|
|
|
|
Id string
|
|
|
|
}
|
|
|
|
ids := make([]id, 0)
|
2020-08-18 08:57:16 +02:00
|
|
|
orgIDQuery := &usr_model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyUserName,
|
2021-03-01 08:48:50 +01:00
|
|
|
Method: domain.SearchMethodEndsWithIgnoreCase,
|
|
|
|
Value: "%" + orgDomain,
|
2020-08-18 08:57:16 +02:00
|
|
|
}
|
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
|
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery},
|
|
|
|
})
|
2020-08-20 07:28:49 +02: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 08:57:16 +02:00
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2020-07-30 14:37:55 +02:00
|
|
|
func SearchUsers(db *gorm.DB, table string, req *usr_model.UserSearchRequest) ([]*model.UserView, uint64, error) {
|
2020-05-12 06:30:53 +02:00
|
|
|
users := make([]*model.UserView, 0)
|
2020-06-25 08:01:13 +02:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
2020-05-12 06:30:53 +02:00
|
|
|
count, err := query(db, &users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return users, count, nil
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:18:23 +02:00
|
|
|
func GetGlobalUserByLoginName(db *gorm.DB, table, loginName string) (*model.UserView, error) {
|
2020-05-12 06:30:53 +02:00
|
|
|
user := new(model.UserView)
|
2021-03-01 08:48:50 +01:00
|
|
|
query := repository.PrepareGetByQuery(table, &model.UserSearchQuery{Key: usr_model.UserSearchKeyLoginNames, Value: loginName, Method: domain.SearchMethodListContains})
|
2020-05-12 06:30:53 +02:00
|
|
|
err := query(db, user)
|
2020-06-11 13:22:24 +02:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-8uWer", "Errors.User.NotFound")
|
|
|
|
}
|
2021-03-25 18:12:24 +01:00
|
|
|
user.SetEmptyUserType()
|
2020-05-12 06:30:53 +02:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsUserUnique(db *gorm.DB, table, userName, email string) (bool, error) {
|
|
|
|
user := new(model.UserView)
|
2021-04-27 11:22:33 +02:00
|
|
|
|
|
|
|
emailUnique := email == ""
|
|
|
|
userNameUnique := userName == ""
|
|
|
|
if email != "" {
|
|
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyEmail), email)
|
|
|
|
err := query(db, user)
|
|
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
emailUnique = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if userName != "" {
|
|
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserName), userName)
|
|
|
|
err := query(db, user)
|
|
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
userNameUnique = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return emailUnique && userNameUnique, nil
|
2020-05-12 06:30:53 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 17:00:04 +01:00
|
|
|
func UserMFAs(db *gorm.DB, table, userID string) ([]*usr_model.MultiFactor, error) {
|
2020-05-12 06:30:53 +02:00
|
|
|
user, err := UserByID(db, table, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-02 17:00:04 +01:00
|
|
|
if user.OTPState == int32(usr_model.MFAStateUnspecified) {
|
2020-05-12 06:30:53 +02:00
|
|
|
return []*usr_model.MultiFactor{}, nil
|
|
|
|
}
|
2020-12-02 17:00:04 +01:00
|
|
|
return []*usr_model.MultiFactor{{Type: usr_model.MFATypeOTP, State: usr_model.MFAState(user.OTPState)}}, nil
|
2020-05-12 06:30:53 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 09:42:21 +02: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 08:01:13 +02:00
|
|
|
save := repository.PrepareSave(table)
|
2020-07-28 09:42:21 +02:00
|
|
|
return save(db, user)
|
2020-05-12 06:30:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteUser(db *gorm.DB, table, userID string) error {
|
2020-06-25 08:01:13 +02:00
|
|
|
delete := repository.PrepareDeleteByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserID), userID)
|
2020-05-12 06:30:53 +02:00
|
|
|
return delete(db)
|
|
|
|
}
|