mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
f3e6f3b23b
* feat(command): remove org * refactor: imports, unused code, error handling * reduce org removed in action * add org deletion to projections * add org removal to projections * add org removal to projections * org removed projection * lint import * projections * fix: table names in tests * fix: table names in tests * logging * add org state * fix(domain): add Owner removed to object details * feat(ListQuery): add with owner removed * fix(org-delete): add bool to functions to select with owner removed * fix(org-delete): add bools to user grants with events to determine if dependencies lost owner * fix(org-delete): add unit tests for owner removed and org removed events * fix(org-delete): add handling of org remove for grants and members * fix(org-delete): correction of unit tests for owner removed * fix(org-delete): update projections, unit tests and get functions * fix(org-delete): add change date to authnkeys and owner removed to org metadata * fix(org-delete): include owner removed for login names * fix(org-delete): some column fixes in projections and build for queries with owner removed * indexes * fix(org-delete): include review changes * fix(org-delete): change user projection name after merge * fix(org-delete): include review changes for project grant where no project owner is necessary * fix(org-delete): include auth and adminapi tables with owner removed information * fix(org-delete): cleanup username and orgdomain uniqueconstraints when org is removed * fix(org-delete): add permissions for org.remove * remove unnecessary unique constraints * fix column order in primary keys * fix(org-delete): include review changes * fix(org-delete): add owner removed indexes and chang setup step to create tables * fix(org-delete): move PK order of instance_id and change added user_grant from review * fix(org-delete): no params for prepareUserQuery * change to step 6 * merge main * fix(org-delete): OldUserName rename to private * fix linting * cleanup * fix: remove org test * create prerelease * chore: delete org-delete as prerelease Co-authored-by: Stefan Benz <stefan@caos.ch> Co-authored-by: Livio Spring <livio.a@gmail.com> Co-authored-by: Fabi <38692350+hifabienne@users.noreply.github.com> Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
253 lines
8.3 KiB
Go
253 lines
8.3 KiB
Go
package view
|
|
|
|
import (
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
usr_model "github.com/zitadel/zitadel/internal/user/model"
|
|
"github.com/zitadel/zitadel/internal/user/repository/view/model"
|
|
)
|
|
|
|
func UserByID(db *gorm.DB, table, userID, instanceID string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
userIDQuery := &model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyUserID,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: userID,
|
|
}
|
|
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, userIDQuery, instanceIDQuery, ownerRemovedQuery)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-sj8Sw", "Errors.User.NotFound")
|
|
}
|
|
user.SetEmptyUserType()
|
|
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 caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.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 caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.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 caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.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{
|
|
Key: usr_model.UserSearchKeyResourceOwner,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: orgID,
|
|
}
|
|
instanceIDQuery := &usr_model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: instanceID,
|
|
}
|
|
ownerRemovedQuery := &usr_model.UserSearchQuery{
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: false,
|
|
}
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery, instanceIDQuery, ownerRemovedQuery},
|
|
})
|
|
_, err := query(db, &users)
|
|
return users, err
|
|
}
|
|
|
|
func UserIDsByDomain(db *gorm.DB, table, orgDomain, instanceID string) ([]string, error) {
|
|
type id struct {
|
|
Id string
|
|
}
|
|
ids := make([]id, 0)
|
|
orgIDQuery := &usr_model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyUserName,
|
|
Method: domain.SearchMethodEndsWithIgnoreCase,
|
|
Value: "%" + orgDomain,
|
|
}
|
|
instanceIDQuery := &usr_model.UserSearchQuery{
|
|
Key: usr_model.UserSearchKeyInstanceID,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: instanceID,
|
|
}
|
|
ownerRemovedQuery := &usr_model.UserSearchQuery{
|
|
Key: usr_model.UserSearchOwnerRemoved,
|
|
Method: domain.SearchMethodEquals,
|
|
Value: false,
|
|
}
|
|
query := repository.PrepareSearchQuery(table, model.UserSearchRequest{
|
|
Queries: []*usr_model.UserSearchQuery{orgIDQuery, instanceIDQuery, ownerRemovedQuery},
|
|
})
|
|
_, 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, instanceID string) (*model.UserView, error) {
|
|
user := new(model.UserView)
|
|
query := repository.PrepareGetByQuery(table,
|
|
&model.UserSearchQuery{Key: usr_model.UserSearchKeyLoginNames, Value: loginName, Method: domain.SearchMethodListContains},
|
|
&model.UserSearchQuery{Key: usr_model.UserSearchKeyInstanceID, Value: instanceID, Method: domain.SearchMethodEquals},
|
|
&model.UserSearchQuery{Key: usr_model.UserSearchOwnerRemoved, Value: false, Method: domain.SearchMethodEquals},
|
|
)
|
|
err := query(db, user)
|
|
if caos_errs.IsNotFound(err) {
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-8uWer", "Errors.User.NotFound")
|
|
}
|
|
user.SetEmptyUserType()
|
|
return user, err
|
|
}
|
|
|
|
func UserMFAs(db *gorm.DB, table, userID, instanceID string) ([]*usr_model.MultiFactor, error) {
|
|
user, err := UserByID(db, table, userID, instanceID)
|
|
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, instanceID string) error {
|
|
delete := repository.PrepareDeleteByKeys(table,
|
|
repository.Key{model.UserSearchKey(usr_model.UserSearchKeyUserID), userID},
|
|
repository.Key{model.UserSearchKey(usr_model.UserSearchKeyInstanceID), instanceID},
|
|
)
|
|
return delete(db)
|
|
}
|
|
|
|
func DeleteInstanceUsers(db *gorm.DB, table, instanceID string) error {
|
|
delete := repository.PrepareDeleteByKey(table, model.UserSearchKey(usr_model.UserSearchKeyInstanceID), instanceID)
|
|
return delete(db)
|
|
}
|
|
|
|
func UpdateOrgOwnerRemovedUsers(db *gorm.DB, table, instanceID, aggID string) error {
|
|
update := repository.PrepareUpdateByKeys(table,
|
|
model.UserSearchKey(usr_model.UserSearchOwnerRemoved),
|
|
true,
|
|
repository.Key{Key: model.UserSearchKey(usr_model.UserSearchKeyInstanceID), Value: instanceID},
|
|
repository.Key{Key: model.UserSearchKey(usr_model.UserSearchKeyResourceOwner), Value: aggID},
|
|
)
|
|
return update(db)
|
|
}
|