2020-05-12 04:30:53 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2020-06-25 06:01:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/view/repository"
|
2020-08-18 06:57:16 +00:00
|
|
|
|
2020-06-22 08:38:29 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
|
2020-05-12 04:30:53 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-06-16 09:40:18 +00:00
|
|
|
global_model "github.com/caos/zitadel/internal/model"
|
2020-05-12 04:30:53 +00:00
|
|
|
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 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserID), userID)
|
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")
|
|
|
|
}
|
2020-05-12 04:30:53 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserByUserName(db *gorm.DB, table, userName string) (*model.UserView, error) {
|
|
|
|
user := new(model.UserView)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserName), userName)
|
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")
|
|
|
|
}
|
2020-05-12 04:30:53 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
func UserByLoginName(db *gorm.DB, table, loginName string) (*model.UserView, error) {
|
|
|
|
user := new(model.UserView)
|
|
|
|
loginNameQuery := &model.UserSearchQuery{
|
2020-06-23 12:47:47 +00:00
|
|
|
Key: usr_model.UserSearchKeyLoginNames,
|
|
|
|
Method: global_model.SearchMethodListContains,
|
2020-06-22 08:38:29 +00:00
|
|
|
Value: loginName,
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery)
|
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")
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func UsersByOrgID(db *gorm.DB, table, orgID string) ([]*model.UserView, error) {
|
|
|
|
users := make([]*model.UserView, 0)
|
|
|
|
orgIDQuery := &usr_model.UserSearchQuery{
|
2020-06-23 12:47:47 +00:00
|
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
|
|
|
Method: global_model.SearchMethodEquals,
|
2020-06-16 09:40:18 +00:00
|
|
|
Value: orgID,
|
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
2020-06-16 09:40:18 +00:00
|
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery},
|
|
|
|
})
|
|
|
|
_, err := query(db, &users)
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:57:16 +00:00
|
|
|
func UserIDsByDomain(db *gorm.DB, table, domain string) ([]string, error) {
|
|
|
|
users := make([]string, 0)
|
|
|
|
orgIDQuery := &usr_model.UserSearchQuery{
|
|
|
|
Key: usr_model.UserSearchKeyUserName,
|
|
|
|
Method: global_model.SearchMethodEndsWithIgnoreCase,
|
|
|
|
Value: "%" + domain,
|
|
|
|
}
|
|
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
|
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery},
|
|
|
|
})
|
|
|
|
_, err := query(db, &users)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetGlobalUserByEmail(db *gorm.DB, table, email string) (*model.UserView, error) {
|
|
|
|
user := new(model.UserView)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyEmail), email)
|
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")
|
|
|
|
}
|
2020-05-12 04:30:53 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsUserUnique(db *gorm.DB, table, userName, email string) (bool, error) {
|
|
|
|
user := new(model.UserView)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyEmail), email)
|
2020-05-12 04:30:53 +00:00
|
|
|
err := query(db, user)
|
|
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if user != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query = repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserName), email)
|
2020-05-12 04:30:53 +00:00
|
|
|
err = query(db, user)
|
|
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return user == nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserMfas(db *gorm.DB, table, userID string) ([]*usr_model.MultiFactor, error) {
|
|
|
|
user, err := UserByID(db, table, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-23 12:47:47 +00:00
|
|
|
if user.OTPState == int32(usr_model.MfaStateUnspecified) {
|
2020-05-12 04:30:53 +00:00
|
|
|
return []*usr_model.MultiFactor{}, nil
|
|
|
|
}
|
2020-07-28 07:42:21 +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
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteUser(db *gorm.DB, table, userID string) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
delete := repository.PrepareDeleteByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserID), userID)
|
2020-05-12 04:30:53 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|