2020-05-11 08:16:27 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2020-06-16 09:40:18 +00:00
|
|
|
caos_errors "github.com/caos/zitadel/internal/errors"
|
|
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
2020-06-15 14:50:09 +00:00
|
|
|
policy_model "github.com/caos/zitadel/internal/policy/model"
|
2020-06-22 11:51:44 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2020-06-16 09:40:18 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-06-15 14:50:09 +00:00
|
|
|
|
2020-05-11 08:16:27 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
|
|
|
State UserState
|
|
|
|
*Password
|
|
|
|
*Profile
|
|
|
|
*Email
|
|
|
|
*Phone
|
|
|
|
*Address
|
|
|
|
InitCode *InitUserCode
|
|
|
|
EmailCode *EmailCode
|
|
|
|
PhoneCode *PhoneCode
|
|
|
|
PasswordCode *PasswordCode
|
|
|
|
OTP *OTP
|
|
|
|
}
|
2020-06-15 14:50:09 +00:00
|
|
|
type UserChanges struct {
|
|
|
|
Changes []*UserChange
|
|
|
|
LastSequence uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserChange struct {
|
2020-06-29 07:37:10 +00:00
|
|
|
ChangeDate *timestamp.Timestamp `json:"changeDate,omitempty"`
|
|
|
|
EventType string `json:"eventType,omitempty"`
|
|
|
|
Sequence uint64 `json:"sequence,omitempty"`
|
|
|
|
ModifierId string `json:"modifierUser,omitempty"`
|
|
|
|
ModifierName string `json:"-"`
|
|
|
|
Data interface{} `json:"data,omitempty"`
|
2020-06-15 14:50:09 +00:00
|
|
|
}
|
2020-05-11 08:16:27 +00:00
|
|
|
|
|
|
|
type InitUserCode struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
|
|
|
|
Code *crypto.CryptoValue
|
|
|
|
Expiry time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserState int32
|
|
|
|
|
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
UserStateUnspecified UserState = iota
|
|
|
|
UserStateActive
|
|
|
|
UserStateInactive
|
|
|
|
UserStateDeleted
|
|
|
|
UserStateLocked
|
|
|
|
UserStateSuspend
|
|
|
|
UserStateInitial
|
2020-05-11 08:16:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Gender int32
|
|
|
|
|
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
GenderUnspecified Gender = iota
|
|
|
|
GenderFemale
|
|
|
|
GenderMale
|
|
|
|
GenderDiverse
|
2020-05-11 08:16:27 +00:00
|
|
|
)
|
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
func (u *User) CheckOrgIamPolicy(policy *org_model.OrgIamPolicy) error {
|
|
|
|
if policy == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return caos_errors.ThrowPreconditionFailed(nil, "MODEL-zSH7j", "Errors.Users.OrgIamPolicyNil")
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
if policy.UserLoginMustBeDomain && strings.Contains(u.UserName, "@") {
|
2020-06-22 11:51:44 +00:00
|
|
|
return caos_errors.ThrowPreconditionFailed(nil, "MODEL-se4sJ", "Errors.User.EmailAsUsernameNotAllowed")
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
if !policy.UserLoginMustBeDomain && u.Profile != nil && u.UserName == "" && u.Email != nil {
|
2020-05-11 08:16:27 +00:00
|
|
|
u.UserName = u.EmailAddress
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
return nil
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 06:06:40 +00:00
|
|
|
func (u *User) SetNamesAsDisplayname() {
|
|
|
|
if u.Profile != nil && u.DisplayName == "" && u.FirstName != "" && u.LastName != "" {
|
|
|
|
u.DisplayName = u.FirstName + " " + u.LastName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:16:27 +00:00
|
|
|
func (u *User) IsValid() bool {
|
2020-05-20 12:28:08 +00:00
|
|
|
return u.Profile != nil && u.FirstName != "" && u.LastName != "" && u.UserName != "" && u.Email != nil && u.Email.IsValid() && u.Phone == nil || (u.Phone != nil && u.Phone.IsValid())
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) IsInitialState() bool {
|
|
|
|
return u.Email == nil || !u.IsEmailVerified || u.Password == nil || u.SecretString == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) IsActive() bool {
|
2020-06-23 12:47:47 +00:00
|
|
|
return u.State == UserStateActive
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) IsInitial() bool {
|
2020-06-23 12:47:47 +00:00
|
|
|
return u.State == UserStateInitial
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) IsInactive() bool {
|
2020-06-23 12:47:47 +00:00
|
|
|
return u.State == UserStateInactive
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) IsLocked() bool {
|
2020-06-23 12:47:47 +00:00
|
|
|
return u.State == UserStateLocked
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) IsOTPReady() bool {
|
2020-06-23 12:47:47 +00:00
|
|
|
return u.OTP != nil && u.OTP.State == MfaStateReady
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 06:44:01 +00:00
|
|
|
func (u *User) HashPasswordIfExisting(policy *policy_model.PasswordComplexityPolicy, passwordAlg crypto.HashAlgorithm, onetime bool) error {
|
2020-05-11 08:16:27 +00:00
|
|
|
if u.Password != nil {
|
2020-05-29 06:44:01 +00:00
|
|
|
return u.Password.HashPasswordIfExisting(policy, passwordAlg, onetime)
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GenerateInitCodeIfNeeded(initGenerator crypto.Generator) error {
|
|
|
|
if !u.IsInitialState() {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-13 12:22:29 +00:00
|
|
|
u.InitCode = new(InitUserCode)
|
2020-05-11 08:16:27 +00:00
|
|
|
return u.InitCode.GenerateInitUserCode(initGenerator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GeneratePhoneCodeIfNeeded(phoneGenerator crypto.Generator) error {
|
|
|
|
if u.Phone == nil || u.IsPhoneVerified {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-13 12:22:29 +00:00
|
|
|
u.PhoneCode = new(PhoneCode)
|
2020-05-11 08:16:27 +00:00
|
|
|
return u.PhoneCode.GeneratePhoneCode(phoneGenerator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GenerateEmailCodeIfNeeded(emailGenerator crypto.Generator) error {
|
|
|
|
if u.Email == nil || u.IsEmailVerified {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-13 12:22:29 +00:00
|
|
|
u.EmailCode = new(EmailCode)
|
2020-05-11 08:16:27 +00:00
|
|
|
return u.EmailCode.GenerateEmailCode(emailGenerator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (init *InitUserCode) GenerateInitUserCode(generator crypto.Generator) error {
|
|
|
|
initCodeCrypto, _, err := crypto.NewCode(generator)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
init.Code = initCodeCrypto
|
|
|
|
init.Expiry = generator.Expiry()
|
|
|
|
return nil
|
|
|
|
}
|