2020-05-11 10:16:29 +00:00
|
|
|
package eventstore
|
2020-05-11 08:16:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-25 09:25:38 +00:00
|
|
|
|
2020-06-17 05:25:04 +00:00
|
|
|
"github.com/caos/logging"
|
2020-06-15 14:50:09 +00:00
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2020-05-12 04:30:53 +00:00
|
|
|
"github.com/caos/zitadel/internal/management/repository/eventsourcing/view"
|
2020-06-16 09:40:18 +00:00
|
|
|
org_event "github.com/caos/zitadel/internal/org/repository/eventsourcing"
|
2020-05-29 06:44:01 +00:00
|
|
|
policy_event "github.com/caos/zitadel/internal/policy/repository/eventsourcing"
|
2020-05-11 08:16:27 +00:00
|
|
|
usr_model "github.com/caos/zitadel/internal/user/model"
|
|
|
|
usr_event "github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
2020-05-12 04:30:53 +00:00
|
|
|
"github.com/caos/zitadel/internal/user/repository/view/model"
|
2020-05-11 08:16:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserRepo struct {
|
2020-05-29 06:44:01 +00:00
|
|
|
SearchLimit uint64
|
|
|
|
UserEvents *usr_event.UserEventstore
|
|
|
|
PolicyEvents *policy_event.PolicyEventstore
|
2020-06-16 09:40:18 +00:00
|
|
|
OrgEvents *org_event.OrgEventstore
|
2020-05-29 06:44:01 +00:00
|
|
|
View *view.View
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 05:25:04 +00:00
|
|
|
func (repo *UserRepo) UserByID(ctx context.Context, id string) (*usr_model.UserView, error) {
|
|
|
|
user, err := repo.View.UserByID(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := repo.UserEvents.UserEventsByID(ctx, id, user.Sequence)
|
|
|
|
if err != nil {
|
|
|
|
logging.Log("EVENT-PSoc3").WithError(err).Debug("error retrieving new events")
|
|
|
|
return model.UserToModel(user), nil
|
|
|
|
}
|
|
|
|
userCopy := *user
|
|
|
|
for _, event := range events {
|
|
|
|
if err := userCopy.AppendEvent(event); err != nil {
|
|
|
|
return model.UserToModel(user), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return model.UserToModel(&userCopy), nil
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) CreateUser(ctx context.Context, user *usr_model.User) (*usr_model.User, error) {
|
2020-07-08 11:56:37 +00:00
|
|
|
pwPolicy, err := repo.PolicyEvents.GetPasswordComplexityPolicy(ctx, authz.GetCtxData(ctx).OrgID)
|
2020-05-29 06:44:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-08 11:56:37 +00:00
|
|
|
orgPolicy, err := repo.OrgEvents.GetOrgIamPolicy(ctx, authz.GetCtxData(ctx).OrgID)
|
2020-06-16 09:40:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.UserEvents.CreateUser(ctx, user, pwPolicy, orgPolicy)
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) RegisterUser(ctx context.Context, user *usr_model.User, resourceOwner string) (*usr_model.User, error) {
|
2020-07-08 11:56:37 +00:00
|
|
|
policyResourceOwner := authz.GetCtxData(ctx).OrgID
|
2020-05-29 06:44:01 +00:00
|
|
|
if resourceOwner != "" {
|
|
|
|
policyResourceOwner = resourceOwner
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
pwPolicy, err := repo.PolicyEvents.GetPasswordComplexityPolicy(ctx, policyResourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-08 11:56:37 +00:00
|
|
|
orgPolicy, err := repo.OrgEvents.GetOrgIamPolicy(ctx, authz.GetCtxData(ctx).OrgID)
|
2020-05-29 06:44:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
return repo.UserEvents.RegisterUser(ctx, user, pwPolicy, orgPolicy, resourceOwner)
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) DeactivateUser(ctx context.Context, id string) (*usr_model.User, error) {
|
|
|
|
return repo.UserEvents.DeactivateUser(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) ReactivateUser(ctx context.Context, id string) (*usr_model.User, error) {
|
|
|
|
return repo.UserEvents.ReactivateUser(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) LockUser(ctx context.Context, id string) (*usr_model.User, error) {
|
|
|
|
return repo.UserEvents.LockUser(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) UnlockUser(ctx context.Context, id string) (*usr_model.User, error) {
|
|
|
|
return repo.UserEvents.UnlockUser(ctx, id)
|
|
|
|
}
|
|
|
|
|
2020-05-12 04:30:53 +00:00
|
|
|
func (repo *UserRepo) SearchUsers(ctx context.Context, request *usr_model.UserSearchRequest) (*usr_model.UserSearchResponse, error) {
|
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-07-15 11:24:36 +00:00
|
|
|
sequence, err := repo.View.GetLatestUserSequence()
|
|
|
|
logging.Log("EVENT-Lcn7d").OnError(err).Warn("could not read latest user sequence")
|
2020-05-12 04:30:53 +00:00
|
|
|
projects, count, err := repo.View.SearchUsers(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &usr_model.UserSearchResponse{
|
2020-05-12 04:30:53 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
|
|
|
TotalResult: uint64(count),
|
|
|
|
Result: model.UsersToModel(projects),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
result.Sequence = sequence.CurrentSequence
|
|
|
|
result.Timestamp = sequence.CurrentTimestamp
|
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:25:38 +00:00
|
|
|
func (repo *UserRepo) UserChanges(ctx context.Context, id string, lastSequence uint64, limit uint64, sortAscending bool) (*usr_model.UserChanges, error) {
|
|
|
|
changes, err := repo.UserEvents.UserChanges(ctx, id, lastSequence, limit, sortAscending)
|
2020-06-15 14:50:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-29 07:37:10 +00:00
|
|
|
for _, change := range changes.Changes {
|
|
|
|
change.ModifierName = change.ModifierId
|
|
|
|
user, _ := repo.UserEvents.UserByID(ctx, change.ModifierId)
|
|
|
|
if user != nil {
|
|
|
|
change.ModifierName = user.DisplayName
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:50:09 +00:00
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
2020-05-12 04:30:53 +00:00
|
|
|
func (repo *UserRepo) GetGlobalUserByEmail(ctx context.Context, email string) (*usr_model.UserView, error) {
|
|
|
|
user, err := repo.View.GetGlobalUserByEmail(email)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return model.UserToModel(user), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) IsUserUnique(ctx context.Context, userName, email string) (bool, error) {
|
|
|
|
return repo.View.IsUserUnique(userName, email)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) UserMfas(ctx context.Context, userID string) ([]*usr_model.MultiFactor, error) {
|
|
|
|
return repo.View.UserMfas(userID)
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:16:27 +00:00
|
|
|
func (repo *UserRepo) SetOneTimePassword(ctx context.Context, password *usr_model.Password) (*usr_model.Password, error) {
|
2020-07-08 11:56:37 +00:00
|
|
|
policy, err := repo.PolicyEvents.GetPasswordComplexityPolicy(ctx, authz.GetCtxData(ctx).OrgID)
|
2020-05-29 06:44:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.UserEvents.SetOneTimePassword(ctx, policy, password)
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) RequestSetPassword(ctx context.Context, id string, notifyType usr_model.NotificationType) error {
|
|
|
|
return repo.UserEvents.RequestSetPassword(ctx, id, notifyType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) ProfileByID(ctx context.Context, userID string) (*usr_model.Profile, error) {
|
2020-06-17 05:25:04 +00:00
|
|
|
user, err := repo.UserByID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return user.GetProfile(), nil
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) ChangeProfile(ctx context.Context, profile *usr_model.Profile) (*usr_model.Profile, error) {
|
|
|
|
return repo.UserEvents.ChangeProfile(ctx, profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) EmailByID(ctx context.Context, userID string) (*usr_model.Email, error) {
|
2020-06-17 05:25:04 +00:00
|
|
|
user, err := repo.UserByID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return user.GetEmail(), nil
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) ChangeEmail(ctx context.Context, email *usr_model.Email) (*usr_model.Email, error) {
|
|
|
|
return repo.UserEvents.ChangeEmail(ctx, email)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) CreateEmailVerificationCode(ctx context.Context, userID string) error {
|
|
|
|
return repo.UserEvents.CreateEmailVerificationCode(ctx, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) PhoneByID(ctx context.Context, userID string) (*usr_model.Phone, error) {
|
2020-06-17 05:25:04 +00:00
|
|
|
user, err := repo.UserByID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return user.GetPhone(), nil
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) ChangePhone(ctx context.Context, email *usr_model.Phone) (*usr_model.Phone, error) {
|
|
|
|
return repo.UserEvents.ChangePhone(ctx, email)
|
|
|
|
}
|
|
|
|
|
2020-07-06 13:48:24 +00:00
|
|
|
func (repo *UserRepo) RemovePhone(ctx context.Context, userID string) error {
|
|
|
|
return repo.UserEvents.RemovePhone(ctx, userID)
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:16:27 +00:00
|
|
|
func (repo *UserRepo) CreatePhoneVerificationCode(ctx context.Context, userID string) error {
|
|
|
|
return repo.UserEvents.CreatePhoneVerificationCode(ctx, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) AddressByID(ctx context.Context, userID string) (*usr_model.Address, error) {
|
2020-06-17 05:25:04 +00:00
|
|
|
user, err := repo.UserByID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return user.GetAddress(), nil
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) ChangeAddress(ctx context.Context, address *usr_model.Address) (*usr_model.Address, error) {
|
|
|
|
return repo.UserEvents.ChangeAddress(ctx, address)
|
|
|
|
}
|