2020-06-15 12:50:39 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2020-07-06 11:18:10 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-06-15 12:50:39 +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-06-15 12:50:39 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ProjectGrantByProjectAndOrg(db *gorm.DB, table, projectID, orgID string) (*model.ProjectGrantView, error) {
|
2020-07-01 05:18:05 +00:00
|
|
|
projectGrant := new(model.ProjectGrantView)
|
2020-06-15 12:50:39 +00:00
|
|
|
|
2020-06-23 12:47:47 +00:00
|
|
|
projectIDQuery := model.ProjectGrantSearchQuery{Key: proj_model.GrantedProjectSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals}
|
|
|
|
orgIDQuery := model.ProjectGrantSearchQuery{Key: proj_model.GrantedProjectSearchKeyOrgID, Value: orgID, Method: global_model.SearchMethodEquals}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, projectIDQuery, orgIDQuery)
|
2020-07-01 05:18:05 +00:00
|
|
|
err := query(db, projectGrant)
|
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-WR3z2", "Errors.Project.Grant.NotExisting")
|
2020-07-06 11:18:10 +00:00
|
|
|
}
|
2020-07-01 05:18:05 +00:00
|
|
|
return projectGrant, err
|
2020-06-15 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectGrantByID(db *gorm.DB, table, grantID string) (*model.ProjectGrantView, error) {
|
2020-07-01 05:18:05 +00:00
|
|
|
projectGrant := new(model.ProjectGrantView)
|
2020-06-23 12:47:47 +00:00
|
|
|
grantIDQuery := model.ProjectGrantSearchQuery{Key: proj_model.GrantedProjectSearchKeyGrantID, Value: grantID, Method: global_model.SearchMethodEquals}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, grantIDQuery)
|
2020-07-01 05:18:05 +00:00
|
|
|
err := query(db, projectGrant)
|
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-EGdh4", "Errors.Project.Grant.NotFound")
|
2020-07-06 11:18:10 +00:00
|
|
|
}
|
2020-07-01 05:18:05 +00:00
|
|
|
return projectGrant, err
|
2020-06-15 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectGrantsByProjectID(db *gorm.DB, table, projectID string) ([]*model.ProjectGrantView, error) {
|
2020-07-01 05:18:05 +00:00
|
|
|
projectGrants := make([]*model.ProjectGrantView, 0)
|
2020-06-15 12:50:39 +00:00
|
|
|
queries := []*proj_model.ProjectGrantViewSearchQuery{
|
2020-07-01 05:18:05 +00:00
|
|
|
{Key: proj_model.GrantedProjectSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
2020-06-15 12:50:39 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectGrantSearchRequest{Queries: queries})
|
2020-07-01 05:18:05 +00:00
|
|
|
_, err := query(db, &projectGrants)
|
|
|
|
return projectGrants, err
|
2020-06-15 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 13:32:03 +00:00
|
|
|
func ProjectGrantsByProjectIDAndRoleKey(db *gorm.DB, table, projectID, roleKey string) ([]*model.ProjectGrantView, error) {
|
2020-07-01 05:18:05 +00:00
|
|
|
projectGrants := make([]*model.ProjectGrantView, 0)
|
2020-06-19 13:32:03 +00:00
|
|
|
queries := []*proj_model.ProjectGrantViewSearchQuery{
|
2020-07-01 05:18:05 +00:00
|
|
|
{Key: proj_model.GrantedProjectSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
|
|
|
{Key: proj_model.GrantedProjectSearchKeyRoleKeys, Value: roleKey, Method: global_model.SearchMethodListContains},
|
2020-06-19 13:32:03 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectGrantSearchRequest{Queries: queries})
|
2020-07-01 05:18:05 +00:00
|
|
|
_, err := query(db, &projectGrants)
|
|
|
|
|
|
|
|
return projectGrants, err
|
2020-06-19 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 12:37:55 +00:00
|
|
|
func SearchProjectGrants(db *gorm.DB, table string, req *proj_model.ProjectGrantViewSearchRequest) ([]*model.ProjectGrantView, uint64, error) {
|
2020-07-01 05:18:05 +00:00
|
|
|
projectGrants := make([]*model.ProjectGrantView, 0)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.ProjectGrantSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
2020-07-01 05:18:05 +00:00
|
|
|
count, err := query(db, &projectGrants)
|
|
|
|
|
|
|
|
return projectGrants, count, err
|
2020-06-15 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 11:01:37 +00:00
|
|
|
func PutProjectGrant(db *gorm.DB, table string, grant *model.ProjectGrantView) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
save := repository.PrepareSave(table)
|
2020-08-26 11:01:37 +00:00
|
|
|
return save(db, grant)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutProjectGrants(db *gorm.DB, table string, grants ...*model.ProjectGrantView) error {
|
|
|
|
save := repository.PrepareBulkSave(table)
|
|
|
|
g := make([]interface{}, len(grants))
|
|
|
|
for i, grant := range grants {
|
|
|
|
g[i] = grant
|
|
|
|
}
|
|
|
|
return save(db, g...)
|
2020-06-15 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteProjectGrant(db *gorm.DB, table, grantID string) error {
|
2020-07-09 11:05:02 +00:00
|
|
|
delete := repository.PrepareDeleteByKey(table, model.ProjectGrantSearchKey(proj_model.GrantedProjectSearchKeyGrantID), grantID)
|
2020-06-15 12:50:39 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|
2020-08-05 16:32:25 +00:00
|
|
|
|
|
|
|
func DeleteProjectGrantsByProjectID(db *gorm.DB, table, projectID string) error {
|
|
|
|
delete := repository.PrepareDeleteByKey(table, model.ProjectGrantSearchKey(proj_model.GrantedProjectSearchKeyProjectID), projectID)
|
|
|
|
return delete(db)
|
|
|
|
}
|