2020-05-12 04:30:53 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2020-07-06 11:18:10 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-05-12 04:30:53 +00:00
|
|
|
global_model "github.com/caos/zitadel/internal/model"
|
|
|
|
grant_model "github.com/caos/zitadel/internal/usergrant/model"
|
|
|
|
"github.com/caos/zitadel/internal/usergrant/repository/view/model"
|
2020-06-25 06:01:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/view/repository"
|
2020-05-12 04:30:53 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func UserGrantByID(db *gorm.DB, table, grantID string) (*model.UserGrantView, error) {
|
|
|
|
user := new(model.UserGrantView)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByKey(table, model.UserGrantSearchKey(grant_model.UserGrantSearchKeyGrantID), grantID)
|
2020-05-12 04:30:53 +00:00
|
|
|
err := query(db, user)
|
2020-07-06 11:18:10 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Nqwf1", "Errors.Token.NotFound")
|
|
|
|
}
|
2020-05-12 04:30:53 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
func UserGrantByIDs(db *gorm.DB, table, resourceOwnerID, projectID, userID string) (*model.UserGrantView, error) {
|
|
|
|
user := new(model.UserGrantView)
|
|
|
|
|
2020-06-23 12:47:47 +00:00
|
|
|
resourceOwnerIDQuery := model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyResourceOwner, Value: resourceOwnerID, Method: global_model.SearchMethodEquals}
|
|
|
|
projectIDQuery := model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals}
|
|
|
|
userIDQuery := model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyUserID, Value: userID, Method: global_model.SearchMethodEquals}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareGetByQuery(table, resourceOwnerIDQuery, projectIDQuery, userIDQuery)
|
2020-06-05 05:50:04 +00:00
|
|
|
err := query(db, user)
|
2020-07-06 11:18:10 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "VIEW-Q1tq2", "Errors.UserGrant.NotFound")
|
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2020-05-12 04:30:53 +00:00
|
|
|
func SearchUserGrants(db *gorm.DB, table string, req *grant_model.UserGrantSearchRequest) ([]*model.UserGrantView, int, error) {
|
|
|
|
users := make([]*model.UserGrantView, 0)
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserGrantSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries})
|
2020-05-12 04:30:53 +00:00
|
|
|
count, err := query(db, &users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return users, count, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantsByUserID(db *gorm.DB, table, userID string) ([]*model.UserGrantView, error) {
|
|
|
|
users := make([]*model.UserGrantView, 0)
|
|
|
|
queries := []*grant_model.UserGrantSearchQuery{
|
2020-06-23 12:47:47 +00:00
|
|
|
&grant_model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyUserID, Value: userID, Method: global_model.SearchMethodEquals},
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserGrantSearchRequest{Queries: queries})
|
2020-05-12 04:30:53 +00:00
|
|
|
_, err := query(db, &users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantsByProjectID(db *gorm.DB, table, projectID string) ([]*model.UserGrantView, error) {
|
|
|
|
users := make([]*model.UserGrantView, 0)
|
|
|
|
queries := []*grant_model.UserGrantSearchQuery{
|
2020-06-23 12:47:47 +00:00
|
|
|
&grant_model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserGrantSearchRequest{Queries: queries})
|
2020-06-19 13:32:03 +00:00
|
|
|
_, err := query(db, &users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantsByProjectIDAndRole(db *gorm.DB, table, projectID, roleKey string) ([]*model.UserGrantView, error) {
|
|
|
|
users := make([]*model.UserGrantView, 0)
|
|
|
|
queries := []*grant_model.UserGrantSearchQuery{
|
2020-06-23 12:47:47 +00:00
|
|
|
&grant_model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
|
|
|
&grant_model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyRoleKey, 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.UserGrantSearchRequest{Queries: queries})
|
2020-06-19 13:32:03 +00:00
|
|
|
_, err := query(db, &users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantsByOrgIDAndProjectID(db *gorm.DB, table, orgID, projectID string) ([]*model.UserGrantView, error) {
|
|
|
|
users := make([]*model.UserGrantView, 0)
|
|
|
|
queries := []*grant_model.UserGrantSearchQuery{
|
2020-06-23 12:47:47 +00:00
|
|
|
&grant_model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyResourceOwner, Value: orgID, Method: global_model.SearchMethodEquals},
|
|
|
|
&grant_model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyProjectID, Value: projectID, Method: global_model.SearchMethodEquals},
|
2020-06-19 13:32:03 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserGrantSearchRequest{Queries: queries})
|
2020-05-12 04:30:53 +00:00
|
|
|
_, err := query(db, &users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserGrantsByOrgID(db *gorm.DB, table, orgID string) ([]*model.UserGrantView, error) {
|
|
|
|
users := make([]*model.UserGrantView, 0)
|
|
|
|
queries := []*grant_model.UserGrantSearchQuery{
|
2020-06-23 12:47:47 +00:00
|
|
|
&grant_model.UserGrantSearchQuery{Key: grant_model.UserGrantSearchKeyResourceOwner, Value: orgID, Method: global_model.SearchMethodEquals},
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
query := repository.PrepareSearchQuery(table, model.UserGrantSearchRequest{Queries: queries})
|
2020-05-12 04:30:53 +00:00
|
|
|
_, err := query(db, &users)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PutUserGrant(db *gorm.DB, table string, grant *model.UserGrantView) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
save := repository.PrepareSave(table)
|
2020-05-12 04:30:53 +00:00
|
|
|
return save(db, grant)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteUserGrant(db *gorm.DB, table, grantID string) error {
|
2020-06-25 06:01:13 +00:00
|
|
|
delete := repository.PrepareDeleteByKey(table, model.UserGrantSearchKey(grant_model.UserGrantSearchKeyGrantID), grantID)
|
2020-05-12 04:30:53 +00:00
|
|
|
return delete(db)
|
|
|
|
}
|