2020-06-25 06:12:29 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2021-03-01 07:48:50 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-07-06 11:18:10 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-06-25 06:12:29 +00:00
|
|
|
iam_model "github.com/caos/zitadel/internal/iam/model"
|
|
|
|
"github.com/caos/zitadel/internal/iam/repository/view/model"
|
|
|
|
"github.com/caos/zitadel/internal/view/repository"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func IAMMemberByIDs(db *gorm.DB, table, orgID, userID string) (*model.IAMMemberView, error) {
|
|
|
|
member := new(model.IAMMemberView)
|
2020-06-25 06:12:29 +00:00
|
|
|
|
2021-03-01 07:48:50 +00:00
|
|
|
iamIDQuery := &model.IAMMemberSearchQuery{Key: iam_model.IAMMemberSearchKeyIamID, Value: orgID, Method: domain.SearchMethodEquals}
|
|
|
|
userIDQuery := &model.IAMMemberSearchQuery{Key: iam_model.IAMMemberSearchKeyUserID, Value: userID, Method: domain.SearchMethodEquals}
|
2020-07-07 09:42:39 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, iamIDQuery, userIDQuery)
|
2020-06-25 06:12:29 +00:00
|
|
|
err := query(db, member)
|
2020-07-06 11:18:10 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
2020-08-26 07:56:23 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Ahq2s", "Errors.IAM.MemberNotExisting")
|
2020-07-06 11:18:10 +00:00
|
|
|
}
|
2020-06-25 06:12:29 +00:00
|
|
|
return member, err
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func SearchIAMMembers(db *gorm.DB, table string, req *iam_model.IAMMemberSearchRequest) ([]*model.IAMMemberView, uint64, error) {
|
|
|
|
members := make([]*model.IAMMemberView, 0)
|
|
|
|
query := repository.PrepareSearchQuery(table, model.IAMMemberSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
2020-06-25 06:12:29 +00:00
|
|
|
count, err := query(db, &members)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return members, count, nil
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
func IAMMembersByUserID(db *gorm.DB, table string, userID string) ([]*model.IAMMemberView, error) {
|
|
|
|
members := make([]*model.IAMMemberView, 0)
|
|
|
|
queries := []*iam_model.IAMMemberSearchQuery{
|
2020-06-25 06:12:29 +00:00
|
|
|
{
|
2020-08-26 07:56:23 +00:00
|
|
|
Key: iam_model.IAMMemberSearchKeyUserID,
|
2020-06-25 06:12:29 +00:00
|
|
|
Value: userID,
|
2021-03-01 07:48:50 +00:00
|
|
|
Method: domain.SearchMethodEquals,
|
2020-06-25 06:12:29 +00:00
|
|
|
},
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.IAMMemberSearchRequest{Queries: queries})
|
2020-06-25 06:12:29 +00:00
|
|
|
_, err := query(db, &members)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return members, nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func PutIAMMember(db *gorm.DB, table string, role *model.IAMMemberView) error {
|
2020-06-25 06:12:29 +00:00
|
|
|
save := repository.PrepareSave(table)
|
|
|
|
return save(db, role)
|
|
|
|
}
|
|
|
|
|
2020-08-26 11:01:37 +00:00
|
|
|
func PutIAMMembers(db *gorm.DB, table string, members ...*model.IAMMemberView) error {
|
|
|
|
save := repository.PrepareBulkSave(table)
|
|
|
|
m := make([]interface{}, len(members))
|
|
|
|
for i, member := range members {
|
|
|
|
m[i] = member
|
|
|
|
}
|
|
|
|
return save(db, m...)
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func DeleteIAMMember(db *gorm.DB, table, orgID, userID string) error {
|
|
|
|
member, err := IAMMemberByIDs(db, table, orgID, userID)
|
2020-06-25 06:12:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete := repository.PrepareDeleteByObject(table, member)
|
|
|
|
return delete(db)
|
|
|
|
}
|
2020-10-07 06:16:42 +00:00
|
|
|
|
|
|
|
func DeleteIAMMembersByUserID(db *gorm.DB, table, userID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKey(table, model.IAMMemberSearchKey(iam_model.IAMMemberSearchKeyUserID), userID)
|
|
|
|
return delete(db)
|
|
|
|
}
|