refactor: cleanup unused code (#7130)

* refactor: drop unused code

* refactor: drop unused code
This commit is contained in:
Silvan
2024-01-02 15:26:31 +01:00
committed by GitHub
parent 4e3936b5bf
commit 9892fd92b6
109 changed files with 0 additions and 6282 deletions

View File

@@ -7,7 +7,6 @@ import (
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
iam_model "github.com/zitadel/zitadel/internal/iam/model"
"github.com/zitadel/zitadel/internal/zerrors"
)
@@ -231,20 +230,6 @@ func (u *UserView) IsPasswordlessReady() bool {
return false
}
func (u *UserView) HasRequiredOrgMFALevel(policy *iam_model.LoginPolicyView) bool {
if !policy.ForceMFA {
return true
}
switch u.MFAMaxSetUp {
case domain.MFALevelSecondFactor:
return policy.HasSecondFactors()
case domain.MFALevelMultiFactor:
return policy.HasMultiFactors()
default:
return false
}
}
func (u *UserView) GetProfile() (*Profile, error) {
if u.HumanView == nil {
return nil, zerrors.ThrowPreconditionFailed(nil, "MODEL-WLTce", "Errors.User.NotHuman")

View File

@@ -1,147 +0,0 @@
package view
import (
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/view/repository"
"github.com/zitadel/zitadel/internal/zerrors"
"github.com/jinzhu/gorm"
usr_model "github.com/zitadel/zitadel/internal/user/model"
"github.com/zitadel/zitadel/internal/user/repository/view/model"
)
func ExternalIDPByExternalUserIDAndIDPConfigID(db *gorm.DB, table, externalUserID, idpConfigID, instanceID string) (*model.ExternalIDPView, error) {
user := new(model.ExternalIDPView)
userIDQuery := &model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyExternalUserID,
Method: domain.SearchMethodEquals,
Value: externalUserID,
}
idpConfigIDQuery := &model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyIdpConfigID,
Method: domain.SearchMethodEquals,
Value: idpConfigID,
}
instanceIDQuery := &model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyInstanceID,
Method: domain.SearchMethodEquals,
Value: instanceID,
}
ownerRemovedQuery := &model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyOwnerRemoved,
Method: domain.SearchMethodEquals,
Value: false,
}
query := repository.PrepareGetByQuery(table, userIDQuery, idpConfigIDQuery, instanceIDQuery, ownerRemovedQuery)
err := query(db, user)
if zerrors.IsNotFound(err) {
return nil, zerrors.ThrowNotFound(nil, "VIEW-Mso9f", "Errors.ExternalIDP.NotFound")
}
return user, err
}
func ExternalIDPByExternalUserIDAndIDPConfigIDAndResourceOwner(db *gorm.DB, table, externalUserID, idpConfigID, resourceOwner, instanceID string) (*model.ExternalIDPView, error) {
user := new(model.ExternalIDPView)
userIDQuery := &model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyExternalUserID,
Method: domain.SearchMethodEquals,
Value: externalUserID,
}
idpConfigIDQuery := &model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyIdpConfigID,
Method: domain.SearchMethodEquals,
Value: idpConfigID,
}
resourceOwnerQuery := &model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyResourceOwner,
Method: domain.SearchMethodEquals,
Value: resourceOwner,
}
instanceIDQuery := &model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyInstanceID,
Method: domain.SearchMethodEquals,
Value: instanceID,
}
ownerRemovedQuery := &model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyOwnerRemoved,
Method: domain.SearchMethodEquals,
Value: false,
}
query := repository.PrepareGetByQuery(table, userIDQuery, idpConfigIDQuery, resourceOwnerQuery, instanceIDQuery, ownerRemovedQuery)
err := query(db, user)
if zerrors.IsNotFound(err) {
return nil, zerrors.ThrowNotFound(nil, "VIEW-Sf8sd", "Errors.ExternalIDP.NotFound")
}
return user, err
}
func ExternalIDPsByIDPConfigID(db *gorm.DB, table, idpConfigID, instanceID string) ([]*model.ExternalIDPView, error) {
externalIDPs := make([]*model.ExternalIDPView, 0)
orgIDQuery := &usr_model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyIdpConfigID,
Method: domain.SearchMethodEquals,
Value: idpConfigID,
}
instanceIDQuery := &usr_model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyInstanceID,
Method: domain.SearchMethodEquals,
Value: instanceID,
}
ownerRemovedQuery := &usr_model.ExternalIDPSearchQuery{
Key: usr_model.ExternalIDPSearchKeyOwnerRemoved,
Method: domain.SearchMethodEquals,
Value: false,
}
query := repository.PrepareSearchQuery(table, model.ExternalIDPSearchRequest{
Queries: []*usr_model.ExternalIDPSearchQuery{orgIDQuery, instanceIDQuery, ownerRemovedQuery},
})
_, err := query(db, &externalIDPs)
return externalIDPs, err
}
func PutExternalIDPs(db *gorm.DB, table string, externalIDPs ...*model.ExternalIDPView) error {
save := repository.PrepareBulkSave(table)
u := make([]interface{}, len(externalIDPs))
for i, idp := range externalIDPs {
u[i] = idp
}
return save(db, u...)
}
func PutExternalIDP(db *gorm.DB, table string, idp *model.ExternalIDPView) error {
save := repository.PrepareSave(table)
return save(db, idp)
}
func DeleteExternalIDP(db *gorm.DB, table, externalUserID, idpConfigID, instanceID string) error {
delete := repository.PrepareDeleteByKeys(table,
repository.Key{Key: model.ExternalIDPSearchKey(usr_model.ExternalIDPSearchKeyExternalUserID), Value: externalUserID},
repository.Key{Key: model.ExternalIDPSearchKey(usr_model.ExternalIDPSearchKeyIdpConfigID), Value: idpConfigID},
repository.Key{Key: model.ExternalIDPSearchKey(usr_model.ExternalIDPSearchKeyInstanceID), Value: instanceID},
)
return delete(db)
}
func DeleteExternalIDPsByUserID(db *gorm.DB, table, userID, instanceID string) error {
delete := repository.PrepareDeleteByKeys(table,
repository.Key{model.ExternalIDPSearchKey(usr_model.ExternalIDPSearchKeyUserID), userID},
repository.Key{model.ExternalIDPSearchKey(usr_model.ExternalIDPSearchKeyInstanceID), instanceID},
)
return delete(db)
}
func DeleteInstanceExternalIDPs(db *gorm.DB, table, instanceID string) error {
delete := repository.PrepareDeleteByKey(table, model.ExternalIDPSearchKey(usr_model.ExternalIDPSearchKeyInstanceID), instanceID)
return delete(db)
}
func UpdateOrgOwnerRemovedExternalIDPs(db *gorm.DB, table, instanceID, aggID string) error {
update := repository.PrepareUpdateByKeys(table,
model.ExternalIDPSearchKey(usr_model.ExternalIDPSearchKeyOwnerRemoved),
true,
repository.Key{Key: model.ExternalIDPSearchKey(usr_model.ExternalIDPSearchKeyInstanceID), Value: instanceID},
repository.Key{Key: model.ExternalIDPSearchKey(usr_model.ExternalIDPSearchKeyResourceOwner), Value: aggID},
)
return update(db)
}

View File

@@ -1,69 +0,0 @@
package model
import (
"github.com/zitadel/zitadel/internal/domain"
usr_model "github.com/zitadel/zitadel/internal/user/model"
"github.com/zitadel/zitadel/internal/view/repository"
)
type ExternalIDPSearchRequest usr_model.ExternalIDPSearchRequest
type ExternalIDPSearchQuery usr_model.ExternalIDPSearchQuery
type ExternalIDPSearchKey usr_model.ExternalIDPSearchKey
func (req ExternalIDPSearchRequest) GetLimit() uint64 {
return req.Limit
}
func (req ExternalIDPSearchRequest) GetOffset() uint64 {
return req.Offset
}
func (req ExternalIDPSearchRequest) GetSortingColumn() repository.ColumnKey {
if req.SortingColumn == usr_model.ExternalIDPSearchKeyUnspecified {
return nil
}
return ExternalIDPSearchKey(req.SortingColumn)
}
func (req ExternalIDPSearchRequest) GetAsc() bool {
return req.Asc
}
func (req ExternalIDPSearchRequest) GetQueries() []repository.SearchQuery {
result := make([]repository.SearchQuery, len(req.Queries))
for i, q := range req.Queries {
result[i] = ExternalIDPSearchQuery{Key: q.Key, Value: q.Value, Method: q.Method}
}
return result
}
func (req ExternalIDPSearchQuery) GetKey() repository.ColumnKey {
return ExternalIDPSearchKey(req.Key)
}
func (req ExternalIDPSearchQuery) GetMethod() domain.SearchMethod {
return req.Method
}
func (req ExternalIDPSearchQuery) GetValue() interface{} {
return req.Value
}
func (key ExternalIDPSearchKey) ToColumnName() string {
switch usr_model.ExternalIDPSearchKey(key) {
case usr_model.ExternalIDPSearchKeyExternalUserID:
return ExternalIDPKeyExternalUserID
case usr_model.ExternalIDPSearchKeyUserID:
return ExternalIDPKeyUserID
case usr_model.ExternalIDPSearchKeyIdpConfigID:
return ExternalIDPKeyIDPConfigID
case usr_model.ExternalIDPSearchKeyResourceOwner:
return ExternalIDPKeyResourceOwner
case usr_model.ExternalIDPSearchKeyInstanceID:
return ExternalIDPKeyInstanceID
case usr_model.ExternalIDPSearchKeyOwnerRemoved:
return ExternalIDPKeyOwnerRemoved
default:
return ""
}
}

View File

@@ -1,59 +0,0 @@
package model
import (
"encoding/json"
"time"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
user_repo "github.com/zitadel/zitadel/internal/repository/user"
"github.com/zitadel/zitadel/internal/zerrors"
)
const (
ExternalIDPKeyExternalUserID = "external_user_id"
ExternalIDPKeyUserID = "user_id"
ExternalIDPKeyIDPConfigID = "idp_config_id"
ExternalIDPKeyResourceOwner = "resource_owner"
ExternalIDPKeyInstanceID = "instance_id"
ExternalIDPKeyOwnerRemoved = "owner_removed"
)
type ExternalIDPView struct {
ExternalUserID string `json:"userID" gorm:"column:external_user_id;primary_key"`
IDPConfigID string `json:"idpConfigID" gorm:"column:idp_config_id;primary_key"`
UserID string `json:"-" gorm:"column:user_id"`
IDPName string `json:"-" gorm:"column:idp_name"`
UserDisplayName string `json:"displayName" gorm:"column:user_display_name"`
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
InstanceID string `json:"instanceID" gorm:"column:instance_id;primary_key"`
}
func (i *ExternalIDPView) AppendEvent(event *models.Event) (err error) {
i.Sequence = event.Seq
i.ChangeDate = event.CreationDate
if event.Typ == user_repo.UserIDPLinkAddedType {
i.setRootData(event)
i.CreationDate = event.CreationDate
err = i.SetData(event)
}
return err
}
func (r *ExternalIDPView) setRootData(event *models.Event) {
r.UserID = event.AggregateID
r.ResourceOwner = event.ResourceOwner
r.InstanceID = event.InstanceID
}
func (r *ExternalIDPView) SetData(event *models.Event) error {
if err := json.Unmarshal(event.Data, r); err != nil {
logging.Log("EVEN-48sfs").WithError(err).Error("could not unmarshal event data")
return zerrors.ThrowInternal(err, "MODEL-Hs8uf", "Could not unmarshal data")
}
return nil
}

View File

@@ -1,132 +0,0 @@
package model
import (
"encoding/json"
"time"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
org_model "github.com/zitadel/zitadel/internal/org/model"
"github.com/zitadel/zitadel/internal/repository/user"
es_model "github.com/zitadel/zitadel/internal/user/repository/eventsourcing/model"
"github.com/zitadel/zitadel/internal/zerrors"
)
const (
NotifyUserKeyUserID = "id"
NotifyUserKeyResourceOwner = "resource_owner"
NotifyUserKeyInstanceID = "instance_id"
)
type NotifyUser struct {
ID string `json:"-" gorm:"column:id;primary_key"`
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
UserName string `json:"userName" gorm:"column:user_name"`
LoginNames database.TextArray[string] `json:"-" gorm:"column:login_names"`
PreferredLoginName string `json:"-" gorm:"column:preferred_login_name"`
FirstName string `json:"firstName" gorm:"column:first_name"`
LastName string `json:"lastName" gorm:"column:last_name"`
NickName string `json:"nickName" gorm:"column:nick_name"`
DisplayName string `json:"displayName" gorm:"column:display_name"`
PreferredLanguage string `json:"preferredLanguage" gorm:"column:preferred_language"`
Gender int32 `json:"gender" gorm:"column:gender"`
LastEmail string `json:"email" gorm:"column:last_email"`
VerifiedEmail string `json:"-" gorm:"column:verified_email"`
LastPhone string `json:"phone" gorm:"column:last_phone"`
VerifiedPhone string `json:"-" gorm:"column:verified_phone"`
PasswordSet bool `json:"-" gorm:"column:password_set"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
State int32 `json:"-" gorm:"-"`
InstanceID string `json:"instanceID" gorm:"column:instance_id;primary_key"`
}
func (u *NotifyUser) GenerateLoginName(domain string, appendDomain bool) string {
if !appendDomain {
return u.UserName
}
return u.UserName + "@" + domain
}
func (u *NotifyUser) SetLoginNames(userLoginMustBeDomain bool, domains []*org_model.OrgDomain) {
loginNames := make([]string, 0)
for _, d := range domains {
if d.Verified {
loginNames = append(loginNames, u.GenerateLoginName(d.Domain, true))
}
}
if !userLoginMustBeDomain {
loginNames = append(loginNames, u.UserName)
}
u.LoginNames = loginNames
}
func (u *NotifyUser) AppendEvent(event *models.Event) (err error) {
u.ChangeDate = event.CreationDate
u.Sequence = event.Seq
switch event.Type() {
case user.UserV1AddedType,
user.UserV1RegisteredType,
user.HumanRegisteredType,
user.HumanAddedType,
user.MachineAddedEventType:
u.CreationDate = event.CreationDate
u.setRootData(event)
err = u.setData(event)
if err != nil {
return err
}
err = u.setPasswordData(event)
case user.UserV1ProfileChangedType,
user.UserV1EmailChangedType,
user.UserV1PhoneChangedType,
user.HumanProfileChangedType,
user.HumanEmailChangedType,
user.HumanPhoneChangedType,
user.UserUserNameChangedType:
err = u.setData(event)
case user.UserV1EmailVerifiedType,
user.HumanEmailVerifiedType:
u.VerifiedEmail = u.LastEmail
case user.UserV1PhoneRemovedType,
user.HumanPhoneRemovedType:
u.VerifiedPhone = ""
u.LastPhone = ""
case user.UserV1PhoneVerifiedType,
user.HumanPhoneVerifiedType:
u.VerifiedPhone = u.LastPhone
case user.UserV1PasswordChangedType,
user.HumanPasswordChangedType:
err = u.setPasswordData(event)
case user.UserRemovedType:
u.State = int32(UserStateDeleted)
}
return err
}
func (u *NotifyUser) setRootData(event *models.Event) {
u.ID = event.AggregateID
u.ResourceOwner = event.ResourceOwner
u.InstanceID = event.InstanceID
}
func (u *NotifyUser) setData(event *models.Event) error {
if err := json.Unmarshal(event.Data, u); err != nil {
logging.Log("MODEL-lso9e").WithError(err).Error("could not unmarshal event data")
return zerrors.ThrowInternal(nil, "MODEL-8iows", "could not unmarshal data")
}
return nil
}
func (u *NotifyUser) setPasswordData(event *models.Event) error {
password := new(es_model.Password)
if err := json.Unmarshal(event.Data, password); err != nil {
logging.Log("MODEL-dfhw6").WithError(err).Error("could not unmarshal event data")
return zerrors.ThrowInternal(nil, "MODEL-BHFD2", "could not unmarshal data")
}
u.PasswordSet = password.Secret != nil || password.EncodedHash != ""
return nil
}

View File

@@ -1,63 +0,0 @@
package model
import (
"github.com/zitadel/zitadel/internal/domain"
usr_model "github.com/zitadel/zitadel/internal/user/model"
"github.com/zitadel/zitadel/internal/view/repository"
)
type NotifyUserSearchRequest usr_model.NotifyUserSearchRequest
type NotifyUserSearchQuery usr_model.NotifyUserSearchQuery
type NotifyUserSearchKey usr_model.NotifyUserSearchKey
func (req NotifyUserSearchRequest) GetLimit() uint64 {
return req.Limit
}
func (req NotifyUserSearchRequest) GetOffset() uint64 {
return req.Offset
}
func (req NotifyUserSearchRequest) GetSortingColumn() repository.ColumnKey {
if req.SortingColumn == usr_model.NotifyUserSearchKeyUnspecified {
return nil
}
return NotifyUserSearchKey(req.SortingColumn)
}
func (req NotifyUserSearchRequest) GetAsc() bool {
return req.Asc
}
func (req NotifyUserSearchRequest) GetQueries() []repository.SearchQuery {
result := make([]repository.SearchQuery, len(req.Queries))
for i, q := range req.Queries {
result[i] = NotifyUserSearchQuery{Key: q.Key, Value: q.Value, Method: q.Method}
}
return result
}
func (req NotifyUserSearchQuery) GetKey() repository.ColumnKey {
return NotifyUserSearchKey(req.Key)
}
func (req NotifyUserSearchQuery) GetMethod() domain.SearchMethod {
return req.Method
}
func (req NotifyUserSearchQuery) GetValue() interface{} {
return req.Value
}
func (key NotifyUserSearchKey) ToColumnName() string {
switch usr_model.NotifyUserSearchKey(key) {
case usr_model.NotifyUserSearchKeyUserID:
return NotifyUserKeyUserID
case usr_model.NotifyUserSearchKeyResourceOwner:
return NotifyUserKeyResourceOwner
case usr_model.NotifyUserSearchKeyInstanceID:
return NotifyUserKeyInstanceID
default:
return ""
}
}

View File

@@ -1,123 +0,0 @@
package model
import (
"testing"
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
"github.com/zitadel/zitadel/internal/repository/user"
es_model "github.com/zitadel/zitadel/internal/user/repository/eventsourcing/model"
)
func TestNotifyUserAppendEvent(t *testing.T) {
type args struct {
event *es_models.Event
user *NotifyUser
}
tests := []struct {
name string
args args
result *NotifyUser
}{
{
name: "append added user event",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: user.UserV1AddedType, ResourceOwner: "GrantedOrgID", Data: mockUserData(getFullHuman(nil))},
user: &NotifyUser{},
},
result: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone"},
},
{
name: "append added human event",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: user.HumanAddedType, ResourceOwner: "GrantedOrgID", Data: mockUserData(getFullHuman(nil))},
user: &NotifyUser{},
},
result: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone"},
},
{
name: "append change user profile event",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: user.UserV1ProfileChangedType, ResourceOwner: "GrantedOrgID", Data: mockProfileData(&es_model.Profile{FirstName: "FirstNameChanged"})},
user: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone"},
},
result: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstNameChanged", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone"},
},
{
name: "append change user email event",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: user.UserV1EmailChangedType, ResourceOwner: "GrantedOrgID", Data: mockEmailData(&es_model.Email{EmailAddress: "EmailChanged"})},
user: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone"},
},
result: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "EmailChanged", LastPhone: "Phone"},
},
{
name: "append change user email event, existing email",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: user.UserV1EmailChangedType, ResourceOwner: "GrantedOrgID", Data: mockEmailData(&es_model.Email{EmailAddress: "EmailChanged"})},
user: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", VerifiedEmail: "Email", LastPhone: "Phone"},
},
result: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "EmailChanged", VerifiedEmail: "Email", LastPhone: "Phone"},
},
{
name: "append verify user email event",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: user.UserV1EmailVerifiedType, ResourceOwner: "GrantedOrgID"},
user: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone"},
},
result: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", VerifiedEmail: "Email", LastPhone: "Phone"},
},
{
name: "append change user phone event",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: user.UserV1PhoneChangedType, ResourceOwner: "GrantedOrgID", Data: mockPhoneData(&es_model.Phone{PhoneNumber: "PhoneChanged"})},
user: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone"},
},
result: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "PhoneChanged"},
},
{
name: "append change user phone event, existing phone",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: user.UserV1PhoneChangedType, ResourceOwner: "GrantedOrgID", Data: mockPhoneData(&es_model.Phone{PhoneNumber: "PhoneChanged"})},
user: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone", VerifiedPhone: "Phone"},
},
result: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "PhoneChanged", VerifiedPhone: "Phone"},
},
{
name: "append verify user phone event",
args: args{
event: &es_models.Event{AggregateID: "AggregateID", Seq: 1, Typ: user.UserV1PhoneVerifiedType, ResourceOwner: "GrantedOrgID"},
user: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone"},
},
result: &NotifyUser{ID: "AggregateID", ResourceOwner: "GrantedOrgID", UserName: "UserName", FirstName: "FirstName", LastName: "LastName", LastEmail: "Email", LastPhone: "Phone", VerifiedPhone: "Phone"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.user.AppendEvent(tt.args.event)
if tt.args.user.ID != tt.result.ID {
t.Errorf("got wrong result ID: expected: %v, actual: %v ", tt.result.ID, tt.args.user.ID)
}
if tt.args.user.FirstName != tt.result.FirstName {
t.Errorf("got wrong result FirstName: expected: %v, actual: %v ", tt.result.FirstName, tt.args.user.FirstName)
}
if tt.args.user.LastName != tt.result.LastName {
t.Errorf("got wrong result FirstName: expected: %v, actual: %v ", tt.result.FirstName, tt.args.user.FirstName)
}
if tt.args.user.ResourceOwner != tt.result.ResourceOwner {
t.Errorf("got wrong result ResourceOwner: expected: %v, actual: %v ", tt.result.ResourceOwner, tt.args.user.ResourceOwner)
}
if tt.args.user.LastEmail != tt.result.LastEmail {
t.Errorf("got wrong result LastEmail: expected: %v, actual: %v ", tt.result.LastEmail, tt.args.user.LastEmail)
}
if tt.args.user.VerifiedEmail != tt.result.VerifiedEmail {
t.Errorf("got wrong result VerifiedEmail: expected: %v, actual: %v ", tt.result.VerifiedEmail, tt.args.user.VerifiedEmail)
}
if tt.args.user.LastPhone != tt.result.LastPhone {
t.Errorf("got wrong result LastPhone: expected: %v, actual: %v ", tt.result.LastPhone, tt.args.user.LastPhone)
}
if tt.args.user.VerifiedPhone != tt.result.VerifiedPhone {
t.Errorf("got wrong result VerifiedPhone: expected: %v, actual: %v ", tt.result.VerifiedPhone, tt.args.user.VerifiedPhone)
}
})
}
}

View File

@@ -1,135 +0,0 @@
package model
import (
"encoding/json"
"time"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
iam_es_model "github.com/zitadel/zitadel/internal/iam/repository/eventsourcing/model"
org_es_model "github.com/zitadel/zitadel/internal/org/repository/eventsourcing/model"
proj_es_model "github.com/zitadel/zitadel/internal/project/repository/eventsourcing/model"
"github.com/zitadel/zitadel/internal/repository/instance"
"github.com/zitadel/zitadel/internal/repository/org"
"github.com/zitadel/zitadel/internal/repository/project"
"github.com/zitadel/zitadel/internal/user/model"
"github.com/zitadel/zitadel/internal/zerrors"
)
const (
UserMembershipKeyUserID = "user_id"
UserMembershipKeyAggregateID = "aggregate_id"
UserMembershipKeyObjectID = "object_id"
UserMembershipKeyResourceOwner = "resource_owner"
UserMembershipKeyMemberType = "member_type"
UserMembershipKeyInstanceID = "instance_id"
)
type UserMembershipView struct {
UserID string `json:"-" gorm:"column:user_id;primary_key"`
MemberType int32 `json:"-" gorm:"column:member_type;primary_key"`
AggregateID string `json:"-" gorm:"column:aggregate_id;primary_key"`
ObjectID string `json:"-" gorm:"column:object_id;primary_key"`
Roles database.TextArray[string] `json:"-" gorm:"column:roles"`
DisplayName string `json:"-" gorm:"column:display_name"`
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
ResourceOwnerName string `json:"-" gorm:"column:resource_owner_name"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
InstanceID string `json:"instanceID" gorm:"column:instance_id;primary_key"`
}
func (u *UserMembershipView) AppendEvent(event *models.Event) (err error) {
u.ChangeDate = event.CreationDate
u.Sequence = event.Seq
switch event.Type() {
case instance.MemberAddedEventType:
u.setRootData(event, model.MemberTypeIam)
err = u.setIamMemberData(event)
case instance.MemberChangedEventType,
instance.MemberRemovedEventType,
instance.MemberCascadeRemovedEventType:
err = u.setIamMemberData(event)
case org.MemberAddedEventType:
u.setRootData(event, model.MemberTypeOrganisation)
err = u.setOrgMemberData(event)
case org.MemberChangedEventType,
org.MemberRemovedEventType,
org.MemberCascadeRemovedEventType:
err = u.setOrgMemberData(event)
case project.MemberAddedType:
u.setRootData(event, model.MemberTypeProject)
err = u.setProjectMemberData(event)
case project.MemberChangedType,
project.MemberRemovedType,
project.MemberCascadeRemovedType:
err = u.setProjectMemberData(event)
case project.GrantMemberAddedType:
u.setRootData(event, model.MemberTypeProjectGrant)
err = u.setProjectGrantMemberData(event)
case project.GrantMemberChangedType,
project.GrantMemberRemovedType,
project.GrantMemberCascadeRemovedType:
err = u.setProjectGrantMemberData(event)
}
return err
}
func (u *UserMembershipView) setRootData(event *models.Event, memberType model.MemberType) {
u.CreationDate = event.CreationDate
u.AggregateID = event.AggregateID
u.ObjectID = event.AggregateID
u.ResourceOwner = event.ResourceOwner
u.MemberType = int32(memberType)
u.InstanceID = event.InstanceID
}
func (u *UserMembershipView) setIamMemberData(event *models.Event) error {
member := new(iam_es_model.IAMMember)
if err := json.Unmarshal(event.Data, member); err != nil {
logging.New().WithError(err).Error("could not unmarshal event data")
return zerrors.ThrowInternal(nil, "MODEL-6jhsw", "could not unmarshal data")
}
u.UserID = member.UserID
u.Roles = member.Roles
return nil
}
func (u *UserMembershipView) setOrgMemberData(event *models.Event) error {
member := new(org_es_model.OrgMember)
if err := json.Unmarshal(event.Data, member); err != nil {
logging.New().WithError(err).Error("could not unmarshal event data")
return zerrors.ThrowInternal(nil, "MODEL-6jhsw", "could not unmarshal data")
}
u.UserID = member.UserID
u.Roles = member.Roles
return nil
}
func (u *UserMembershipView) setProjectMemberData(event *models.Event) error {
member := new(proj_es_model.ProjectMember)
if err := json.Unmarshal(event.Data, member); err != nil {
logging.New().WithError(err).Error("could not unmarshal event data")
return zerrors.ThrowInternal(nil, "MODEL-6jhsw", "could not unmarshal data")
}
u.UserID = member.UserID
u.Roles = member.Roles
return nil
}
func (u *UserMembershipView) setProjectGrantMemberData(event *models.Event) error {
member := new(proj_es_model.ProjectGrantMember)
if err := json.Unmarshal(event.Data, member); err != nil {
logging.New().WithError(err).Error("could not unmarshal event data")
return zerrors.ThrowInternal(nil, "MODEL-6jhsw", "could not unmarshal data")
}
u.UserID = member.UserID
u.ObjectID = member.GrantID
u.Roles = member.Roles
return nil
}

View File

@@ -1,70 +0,0 @@
package model
import (
"github.com/zitadel/zitadel/internal/domain"
usr_model "github.com/zitadel/zitadel/internal/user/model"
"github.com/zitadel/zitadel/internal/view/repository"
)
type UserMembershipSearchRequest usr_model.UserMembershipSearchRequest
type UserMembershipSearchQuery usr_model.UserMembershipSearchQuery
type UserMembershipSearchKey usr_model.UserMembershipSearchKey
func (req UserMembershipSearchRequest) GetLimit() uint64 {
return req.Limit
}
func (req UserMembershipSearchRequest) GetOffset() uint64 {
return req.Offset
}
func (req UserMembershipSearchRequest) GetSortingColumn() repository.ColumnKey {
if req.SortingColumn == usr_model.UserMembershipSearchKeyUnspecified {
return nil
}
return UserMembershipSearchKey(req.SortingColumn)
}
func (req UserMembershipSearchRequest) GetAsc() bool {
return req.Asc
}
func (req UserMembershipSearchRequest) GetQueries() []repository.SearchQuery {
result := make([]repository.SearchQuery, len(req.Queries))
for i, q := range req.Queries {
result[i] = UserMembershipSearchQuery{Key: q.Key, Value: q.Value, Method: q.Method}
}
return result
}
func (req UserMembershipSearchQuery) GetKey() repository.ColumnKey {
return UserMembershipSearchKey(req.Key)
}
func (req UserMembershipSearchQuery) GetMethod() domain.SearchMethod {
return req.Method
}
func (req UserMembershipSearchQuery) GetValue() interface{} {
return req.Value
}
func (key UserMembershipSearchKey) ToColumnName() string {
switch usr_model.UserMembershipSearchKey(key) {
case usr_model.UserMembershipSearchKeyUserID:
return UserMembershipKeyUserID
case usr_model.UserMembershipSearchKeyResourceOwner:
return UserMembershipKeyResourceOwner
case usr_model.UserMembershipSearchKeyMemberType:
return UserMembershipKeyMemberType
case usr_model.UserMembershipSearchKeyAggregateID:
return UserMembershipKeyAggregateID
case usr_model.UserMembershipSearchKeyObjectID:
return UserMembershipKeyObjectID
case usr_model.UserMembershipSearchKeyInstanceID:
return UserMembershipKeyInstanceID
default:
return ""
}
}

View File

@@ -1,56 +0,0 @@
package view
import (
"github.com/jinzhu/gorm"
"github.com/zitadel/zitadel/internal/domain"
usr_model "github.com/zitadel/zitadel/internal/user/model"
"github.com/zitadel/zitadel/internal/user/repository/view/model"
"github.com/zitadel/zitadel/internal/view/repository"
"github.com/zitadel/zitadel/internal/zerrors"
)
func NotifyUserByID(db *gorm.DB, table, userID, instanceID string) (*model.NotifyUser, error) {
user := new(model.NotifyUser)
query := repository.PrepareGetByQuery(table,
model.NotifyUserSearchQuery{Key: usr_model.NotifyUserSearchKeyUserID, Method: domain.SearchMethodEquals, Value: userID},
model.NotifyUserSearchQuery{Key: usr_model.NotifyUserSearchKeyInstanceID, Method: domain.SearchMethodEquals, Value: instanceID},
)
err := query(db, user)
if zerrors.IsNotFound(err) {
return nil, zerrors.ThrowNotFound(nil, "VIEW-Gad31", "Errors.User.NotFound")
}
return user, err
}
func NotifyUsersByOrgID(db *gorm.DB, table, orgID, instanceID string) ([]*model.NotifyUser, error) {
users := make([]*model.NotifyUser, 0)
orgIDQuery := &usr_model.NotifyUserSearchQuery{
Key: usr_model.NotifyUserSearchKeyResourceOwner,
Method: domain.SearchMethodEquals,
Value: orgID,
}
instanceIDQuery := &usr_model.NotifyUserSearchQuery{
Key: usr_model.NotifyUserSearchKeyInstanceID,
Method: domain.SearchMethodEquals,
Value: instanceID,
}
query := repository.PrepareSearchQuery(table, model.NotifyUserSearchRequest{
Queries: []*usr_model.NotifyUserSearchQuery{orgIDQuery, instanceIDQuery},
})
_, err := query(db, &users)
return users, err
}
func PutNotifyUser(db *gorm.DB, table string, project *model.NotifyUser) error {
save := repository.PrepareSave(table)
return save(db, project)
}
func DeleteNotifyUser(db *gorm.DB, table, userID, instanceID string) error {
delete := repository.PrepareDeleteByKeys(table,
repository.Key{model.UserSearchKey(usr_model.NotifyUserSearchKeyUserID), userID},
repository.Key{model.UserSearchKey(usr_model.NotifyUserSearchKeyInstanceID), instanceID},
)
return delete(db)
}

View File

@@ -36,89 +36,6 @@ func UserByID(db *gorm.DB, table, userID, instanceID string) (*model.UserView, e
return user, err
}
func UserByUserName(db *gorm.DB, table, userName, instanceID string) (*model.UserView, error) {
user := new(model.UserView)
userNameQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchKeyUserName,
Method: domain.SearchMethodEquals,
Value: userName,
}
instanceIDQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchKeyInstanceID,
Method: domain.SearchMethodEquals,
Value: instanceID,
}
ownerRemovedQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchOwnerRemoved,
Method: domain.SearchMethodEquals,
Value: false,
}
query := repository.PrepareGetByQuery(table, userNameQuery, instanceIDQuery, ownerRemovedQuery)
err := query(db, user)
if zerrors.IsNotFound(err) {
return nil, zerrors.ThrowNotFound(nil, "VIEW-Lso9s", "Errors.User.NotFound")
}
user.SetEmptyUserType()
return user, err
}
func UserByLoginName(db *gorm.DB, table, loginName, instanceID string) (*model.UserView, error) {
user := new(model.UserView)
loginNameQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchKeyLoginNames,
Method: domain.SearchMethodListContains,
Value: loginName,
}
instanceIDQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchKeyInstanceID,
Method: domain.SearchMethodEquals,
Value: instanceID,
}
ownerRemovedQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchOwnerRemoved,
Method: domain.SearchMethodEquals,
Value: false,
}
query := repository.PrepareGetByQuery(table, loginNameQuery, instanceIDQuery, ownerRemovedQuery)
err := query(db, user)
if zerrors.IsNotFound(err) {
return nil, zerrors.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFound")
}
user.SetEmptyUserType()
return user, err
}
func UserByLoginNameAndResourceOwner(db *gorm.DB, table, loginName, resourceOwner, instanceID string) (*model.UserView, error) {
user := new(model.UserView)
loginNameQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchKeyLoginNames,
Method: domain.SearchMethodListContains,
Value: loginName,
}
resourceOwnerQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchKeyResourceOwner,
Method: domain.SearchMethodEquals,
Value: resourceOwner,
}
instanceIDQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchKeyInstanceID,
Method: domain.SearchMethodEquals,
Value: instanceID,
}
ownerRemovedQuery := &model.UserSearchQuery{
Key: usr_model.UserSearchOwnerRemoved,
Method: domain.SearchMethodEquals,
Value: false,
}
query := repository.PrepareGetByQuery(table, loginNameQuery, resourceOwnerQuery, instanceIDQuery, ownerRemovedQuery)
err := query(db, user)
if zerrors.IsNotFound(err) {
return nil, zerrors.ThrowNotFound(nil, "VIEW-AD4qs", "Errors.User.NotFoundOnOrg")
}
user.SetEmptyUserType()
return user, err
}
func UsersByOrgID(db *gorm.DB, table, orgID, instanceID string) ([]*model.UserView, error) {
users := make([]*model.UserView, 0)
orgIDQuery := &usr_model.UserSearchQuery{