2020-05-11 10:16:29 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2020-07-06 11:18:10 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-05-11 10:16:29 +00:00
|
|
|
global_model "github.com/caos/zitadel/internal/model"
|
|
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
|
|
"github.com/caos/zitadel/internal/project/repository/view/model"
|
2020-06-25 06:01:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/view/repository"
|
2020-05-11 10:16:29 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ProjectRoleByIDs(db *gorm.DB, table, projectID, orgID, key string) (*model.ProjectRoleView, error) {
|
|
|
|
role := new(model.ProjectRoleView)
|
|
|
|
|
2020-06-23 12:47:47 +00:00
|
|
|
projectIDQuery := model.ProjectRoleSearchQuery{Key: proj_model.ProjectRoleSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals}
|
|
|
|
grantIDQuery := model.ProjectRoleSearchQuery{Key: proj_model.ProjectRoleSearchKeyOrgID, Value: orgID, Method: global_model.SearchMethodEquals}
|
2020-06-30 05:56:03 +00:00
|
|
|
keyQuery := model.ProjectRoleSearchQuery{Key: proj_model.ProjectRoleSearchKeyKey, Value: key, Method: global_model.SearchMethodEquals}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, projectIDQuery, grantIDQuery, keyQuery)
|
2020-05-11 10:16:29 +00:00
|
|
|
err := query(db, role)
|
2020-07-06 11:18:10 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Wtg72", "Errors.Project.Role.NotExisting")
|
2020-07-06 11:18:10 +00:00
|
|
|
}
|
2020-05-11 10:16:29 +00:00
|
|
|
return role, err
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:32:25 +00:00
|
|
|
func ProjectRolesByProjectID(db *gorm.DB, table, projectID string) ([]*model.ProjectRoleView, error) {
|
|
|
|
roles := make([]*model.ProjectRoleView, 0)
|
|
|
|
queries := []*proj_model.ProjectRoleSearchQuery{
|
2020-12-21 17:42:34 +00:00
|
|
|
{Key: proj_model.ProjectRoleSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
2020-08-05 16:32:25 +00:00
|
|
|
}
|
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectRoleSearchRequest{Queries: queries})
|
|
|
|
_, err := query(db, &roles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return roles, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
func ResourceOwnerProjectRolesByKey(db *gorm.DB, table, projectID, resourceOwner, key string) ([]*model.ProjectRoleView, error) {
|
|
|
|
roles := make([]*model.ProjectRoleView, 0)
|
|
|
|
queries := []*proj_model.ProjectRoleSearchQuery{
|
2020-12-21 17:42:34 +00:00
|
|
|
{Key: proj_model.ProjectRoleSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
|
|
|
{Key: proj_model.ProjectRoleSearchKeyResourceOwner, Value: resourceOwner, Method: global_model.SearchMethodEquals},
|
|
|
|
{Key: proj_model.ProjectRoleSearchKeyKey, Value: key, Method: global_model.SearchMethodEquals},
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectRoleSearchRequest{Queries: queries})
|
2020-05-11 10:16:29 +00:00
|
|
|
_, err := query(db, &roles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return roles, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ResourceOwnerProjectRoles(db *gorm.DB, table, projectID, resourceOwner string) ([]*model.ProjectRoleView, error) {
|
|
|
|
roles := make([]*model.ProjectRoleView, 0)
|
|
|
|
queries := []*proj_model.ProjectRoleSearchQuery{
|
2020-12-21 17:42:34 +00:00
|
|
|
{Key: proj_model.ProjectRoleSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
|
|
|
{Key: proj_model.ProjectRoleSearchKeyResourceOwner, Value: resourceOwner, Method: global_model.SearchMethodEquals},
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectRoleSearchRequest{Queries: queries})
|
2020-05-11 10:16:29 +00:00
|
|
|
_, err := query(db, &roles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return roles, nil
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:37:55 +00:00
|
|
|
func SearchProjectRoles(db *gorm.DB, table string, req *proj_model.ProjectRoleSearchRequest) ([]*model.ProjectRoleView, uint64, error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
roles := make([]*model.ProjectRoleView, 0)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectRoleSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
2020-05-11 10:16:29 +00:00
|
|
|
count, err := query(db, &roles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return roles, count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutProjectRole(db *gorm.DB, table string, role *model.ProjectRoleView) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
save := repository.PrepareSave(table)
|
2020-05-11 10:16:29 +00:00
|
|
|
return save(db, role)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteProjectRole(db *gorm.DB, table, projectID, orgID, key string) error {
|
|
|
|
role, err := ProjectRoleByIDs(db, table, projectID, orgID, key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
delete := repository.PrepareDeleteByObject(table, role)
|
2020-05-11 10:16:29 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|
2020-08-05 16:32:25 +00:00
|
|
|
|
|
|
|
func DeleteProjectRolesByProjectID(db *gorm.DB, table, projectID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKey(table, model.ProjectRoleSearchKey(proj_model.ProjectRoleSearchKeyProjectID), projectID)
|
|
|
|
return delete(db)
|
|
|
|
|
|
|
|
}
|