mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
300ade66a7
* at least registration prompt works * in memory test for login * buttons to start webauthn process * begin eventstore impl * begin eventstore impl * serialize into bytes * fix: u2f, passwordless types * fix for localhost * fix script * fix: u2f, passwordless types * fix: add u2f * fix: verify u2f * fix: session data in event store * fix: u2f credentials in eventstore * fix: webauthn pkg handles business models * feat: tests * feat: append events * fix: test * fix: check only ready webauthn creds * fix: move u2f methods to authrepo * frontend improvements * fix return * feat: add passwordless * feat: add passwordless * improve ui / error handling * separate call for login * fix login * js * feat: u2f login methods * feat: remove unused session id * feat: error handling * feat: error handling * feat: refactor user eventstore * feat: finish webauthn * feat: u2f and passwordlss in auth.proto * u2f step * passwordless step * cleanup js * EndpointPasswordLessLogin * migration * update mfaChecked test * next step test * token name * cleanup * attribute * passwordless as tokens * remove sms as otp type * add "user" to amr for webauthn * error handling * fixes * fix tests * naming * naming * fixes * session handler * i18n * error handling in login * Update internal/ui/login/static/i18n/de.yaml Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> * Update internal/ui/login/static/i18n/en.yaml Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> * improvements * merge fixes * fixes * fixes Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com> Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>
174 lines
5.6 KiB
Go
174 lines
5.6 KiB
Go
package view
|
|
|
|
import (
|
|
"github.com/caos/zitadel/internal/view/repository"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
global_model "github.com/caos/zitadel/internal/model"
|
|
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)
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserID), userID)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-sj8Sw", "Errors.User.NotFound")
|
|
}
|
|
return user, err
|
|
}
|
|
|
|
func UserByUserName(db *gorm.DB, table, userName string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
query := repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserName), userName)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Lso9s", "Errors.User.NotFound")
|
|
}
|
|
return user, err
|
|
}
|
|
|
|
func UserByLoginName(db *gorm.DB, table, loginName string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
loginNameQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyLoginNames,
|
|
Method: global_model.SearchMethodListContains,
|
|
Value: loginName,
|
|
}
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFound")
|
|
}
|
|
return user, err
|
|
}
|
|
|
|
func UserByLoginNameAndResourceOwner(db *gorm.DB, table, loginName, resourceOwner string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
loginNameQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyLoginNames,
|
|
Method: global_model.SearchMethodListContains,
|
|
Value: loginName,
|
|
}
|
|
resourceOwnerQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
|
Method: global_model.SearchMethodEquals,
|
|
Value: resourceOwner,
|
|
}
|
|
query := repository.PrepareGetByQuery(table, loginNameQuery, resourceOwnerQuery)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFound")
|
|
}
|
|
return user, err
|
|
}
|
|
|
|
func UsersByOrgID(db *gorm.DB, table, orgID string) ([]*model.UserView, error) {
|
|
users := make([]*model.UserView, 0)
|
|
orgIDQuery := &usr_model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
|
Method: global_model.SearchMethodEquals,
|
|
Value: orgID,
|
|
}
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery},
|
|
})
|
|
_, err := query(db, &users)
|
|
return users, err
|
|
}
|
|
|
|
func UserIDsByDomain(db *gorm.DB, table, domain string) ([]string, error) {
|
|
type id struct {
|
|
Id string
|
|
}
|
|
ids := make([]id, 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, &ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
users := make([]string, len(ids))
|
|
for i, id := range ids {
|
|
users[i] = id.Id
|
|
}
|
|
return users, err
|
|
}
|
|
|
|
func SearchUsers(db *gorm.DB, table string, req *usr_model.UserSearchRequest) ([]*model.UserView, uint64, error) {
|
|
users := make([]*model.UserView, 0)
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
|
count, err := query(db, &users)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return users, count, nil
|
|
}
|
|
|
|
func GetGlobalUserByLoginName(db *gorm.DB, table, loginName string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
query := repository.PrepareGetByQuery(table, &model.UserSearchQuery{Key: usr_model.UserSearchKeyLoginNames, Value: loginName, Method: global_model.SearchMethodListContains})
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-8uWer", "Errors.User.NotFound")
|
|
}
|
|
return user, err
|
|
}
|
|
|
|
func IsUserUnique(db *gorm.DB, table, userName, email string) (bool, error) {
|
|
user := new(model.UserView)
|
|
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 user.UserName != "" {
|
|
return false, nil
|
|
}
|
|
query = repository.PrepareGetByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserName), userName)
|
|
err = query(db, user)
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
|
return false, err
|
|
}
|
|
return user.UserName == "", 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
|
|
}
|
|
if user.OTPState == int32(usr_model.MFAStateUnspecified) {
|
|
return []*usr_model.MultiFactor{}, nil
|
|
}
|
|
return []*usr_model.MultiFactor{{Type: usr_model.MFATypeOTP, State: usr_model.MFAState(user.OTPState)}}, nil
|
|
}
|
|
|
|
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 string) error {
|
|
delete := repository.PrepareDeleteByKey(table, model.UserSearchKey(usr_model.UserSearchKeyUserID), userID)
|
|
return delete(db)
|
|
}
|