2020-05-11 10:16:29 +00:00
|
|
|
package eventstore
|
2020-04-07 11:23:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-25 09:25:38 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-06-30 05:54:39 +00:00
|
|
|
"github.com/caos/logging"
|
2020-07-08 11:56:37 +00:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2020-06-19 13:32:03 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
es_int "github.com/caos/zitadel/internal/eventstore"
|
2020-09-01 14:38:34 +00:00
|
|
|
iam_event "github.com/caos/zitadel/internal/iam/repository/eventsourcing"
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/internal/management/repository/eventsourcing/view"
|
|
|
|
global_model "github.com/caos/zitadel/internal/model"
|
|
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
|
|
proj_event "github.com/caos/zitadel/internal/project/repository/eventsourcing"
|
|
|
|
"github.com/caos/zitadel/internal/project/repository/view/model"
|
2020-06-29 07:37:10 +00:00
|
|
|
usr_event "github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
2020-06-19 13:32:03 +00:00
|
|
|
usr_grant_event "github.com/caos/zitadel/internal/usergrant/repository/eventsourcing"
|
2020-04-07 11:23:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectRepo struct {
|
2020-06-19 13:32:03 +00:00
|
|
|
es_int.Eventstore
|
|
|
|
SearchLimit uint64
|
|
|
|
ProjectEvents *proj_event.ProjectEventstore
|
|
|
|
UserGrantEvents *usr_grant_event.UserGrantEventStore
|
2020-06-29 07:37:10 +00:00
|
|
|
UserEvents *usr_event.UserEventstore
|
2020-09-01 14:38:34 +00:00
|
|
|
IAMEvents *iam_event.IAMEventstore
|
2020-06-19 13:32:03 +00:00
|
|
|
View *view.View
|
|
|
|
Roles []string
|
2020-09-01 14:38:34 +00:00
|
|
|
IAMID string
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 05:06:07 +00:00
|
|
|
func (repo *ProjectRepo) ProjectByID(ctx context.Context, id string) (*proj_model.ProjectView, error) {
|
2020-07-08 12:26:35 +00:00
|
|
|
project, viewErr := repo.View.ProjectByID(id)
|
|
|
|
if viewErr != nil && !caos_errs.IsNotFound(viewErr) {
|
|
|
|
return nil, viewErr
|
2020-06-23 05:06:07 +00:00
|
|
|
}
|
2020-07-08 12:26:35 +00:00
|
|
|
if caos_errs.IsNotFound(viewErr) {
|
2020-07-07 17:28:19 +00:00
|
|
|
project = new(model.ProjectView)
|
|
|
|
}
|
2020-06-30 05:54:39 +00:00
|
|
|
|
2020-07-08 12:26:35 +00:00
|
|
|
events, esErr := repo.ProjectEvents.ProjectEventsByID(ctx, id, project.Sequence)
|
|
|
|
if caos_errs.IsNotFound(viewErr) && len(events) == 0 {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-8yfKu", "Errors.Project.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
if esErr != nil {
|
|
|
|
logging.Log("EVENT-V9x1V").WithError(viewErr).Debug("error retrieving new events")
|
2020-06-30 05:54:39 +00:00
|
|
|
return model.ProjectToModel(project), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
viewProject := *project
|
|
|
|
for _, event := range events {
|
|
|
|
err := project.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return model.ProjectToModel(&viewProject), nil
|
|
|
|
}
|
|
|
|
}
|
2020-10-07 06:16:42 +00:00
|
|
|
if viewProject.State == int32(proj_model.ProjectStateRemoved) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-3Mo0s", "Errors.Project.NotFound")
|
|
|
|
}
|
2020-06-23 05:06:07 +00:00
|
|
|
return model.ProjectToModel(project), nil
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (repo *ProjectRepo) SearchProjects(ctx context.Context, request *proj_model.ProjectViewSearchRequest) (*proj_model.ProjectViewSearchResponse, error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, sequenceErr := repo.View.GetLatestProjectSequence("")
|
2020-08-26 07:56:23 +00:00
|
|
|
logging.Log("EVENT-Edc56").OnError(sequenceErr).Warn("could not read latest project sequence")
|
2020-06-05 05:50:04 +00:00
|
|
|
|
2020-07-22 12:00:29 +00:00
|
|
|
permissions := authz.GetRequestPermissionsFromCtx(ctx)
|
2020-07-08 11:56:37 +00:00
|
|
|
if !authz.HasGlobalPermission(permissions) {
|
2020-07-22 12:00:29 +00:00
|
|
|
ids := authz.GetAllPermissionCtxIDs(permissions)
|
|
|
|
if _, q := request.GetSearchQuery(proj_model.ProjectViewSearchKeyProjectID); q != nil {
|
|
|
|
containsID := false
|
|
|
|
for _, id := range ids {
|
|
|
|
if id == q.Value {
|
|
|
|
containsID = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !containsID {
|
|
|
|
result := &proj_model.ProjectViewSearchResponse{
|
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
|
|
|
TotalResult: uint64(0),
|
|
|
|
Result: []*proj_model.ProjectView{},
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
if sequenceErr == nil {
|
2020-07-22 12:00:29 +00:00
|
|
|
result.Sequence = sequence.CurrentSequence
|
2020-12-02 07:50:59 +00:00
|
|
|
result.Timestamp = sequence.LastSuccessfulSpoolerRun
|
2020-07-22 12:00:29 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request.Queries = append(request.Queries, &proj_model.ProjectViewSearchQuery{Key: proj_model.ProjectViewSearchKeyProjectID, Method: global_model.SearchMethodIsOneOf, Value: ids})
|
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
projects, count, err := repo.View.SearchProjects(request)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &proj_model.ProjectViewSearchResponse{
|
2020-05-11 10:16:29 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
|
|
|
TotalResult: uint64(count),
|
2020-06-15 12:50:39 +00:00
|
|
|
Result: model.ProjectsToModel(projects),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
if sequenceErr == nil {
|
2020-07-15 11:24:36 +00:00
|
|
|
result.Sequence = sequence.CurrentSequence
|
2020-12-02 07:50:59 +00:00
|
|
|
result.Timestamp = sequence.LastSuccessfulSpoolerRun
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (repo *ProjectRepo) ProjectGrantViewByID(ctx context.Context, grantID string) (project *proj_model.ProjectGrantView, err error) {
|
|
|
|
p, err := repo.View.ProjectGrantByID(grantID)
|
2020-06-10 12:28:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-15 12:50:39 +00:00
|
|
|
return model.ProjectGrantToModel(p), nil
|
2020-06-10 12:28:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 05:06:07 +00:00
|
|
|
func (repo *ProjectRepo) ProjectMemberByID(ctx context.Context, projectID, userID string) (*proj_model.ProjectMemberView, error) {
|
|
|
|
member, err := repo.View.ProjectMemberByIDs(projectID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return model.ProjectMemberToModel(member), nil
|
2020-04-15 15:11:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
func (repo *ProjectRepo) SearchProjectMembers(ctx context.Context, request *proj_model.ProjectMemberSearchRequest) (*proj_model.ProjectMemberSearchResponse, error) {
|
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, sequenceErr := repo.View.GetLatestProjectMemberSequence("")
|
2020-08-26 07:56:23 +00:00
|
|
|
logging.Log("EVENT-3dgt6").OnError(sequenceErr).Warn("could not read latest project member sequence")
|
2020-05-11 10:16:29 +00:00
|
|
|
members, count, err := repo.View.SearchProjectMembers(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &proj_model.ProjectMemberSearchResponse{
|
2020-05-11 10:16:29 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
|
|
|
TotalResult: uint64(count),
|
|
|
|
Result: model.ProjectMembersToModel(members),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
if sequenceErr == nil {
|
2020-07-15 11:24:36 +00:00
|
|
|
result.Sequence = sequence.CurrentSequence
|
2020-12-02 07:50:59 +00:00
|
|
|
result.Timestamp = sequence.LastSuccessfulSpoolerRun
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 11:24:36 +00:00
|
|
|
func (repo *ProjectRepo) SearchProjectRoles(ctx context.Context, projectID string, request *proj_model.ProjectRoleSearchRequest) (*proj_model.ProjectRoleSearchResponse, error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-07-15 11:24:36 +00:00
|
|
|
request.AppendProjectQuery(projectID)
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, sequenceErr := repo.View.GetLatestProjectRoleSequence("")
|
2020-08-26 07:56:23 +00:00
|
|
|
logging.Log("LSp0d-47suf").OnError(sequenceErr).Warn("could not read latest project role sequence")
|
2020-05-11 10:16:29 +00:00
|
|
|
roles, count, err := repo.View.SearchProjectRoles(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
|
|
|
|
result := &proj_model.ProjectRoleSearchResponse{
|
2020-05-11 10:16:29 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
2020-08-26 07:56:23 +00:00
|
|
|
TotalResult: count,
|
2020-05-11 10:16:29 +00:00
|
|
|
Result: model.ProjectRolesToModel(roles),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
if sequenceErr == nil {
|
2020-07-15 11:24:36 +00:00
|
|
|
result.Sequence = sequence.CurrentSequence
|
2020-12-02 07:50:59 +00:00
|
|
|
result.Timestamp = sequence.LastSuccessfulSpoolerRun
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:25:38 +00:00
|
|
|
func (repo *ProjectRepo) ProjectChanges(ctx context.Context, id string, lastSequence uint64, limit uint64, sortAscending bool) (*proj_model.ProjectChanges, error) {
|
|
|
|
changes, err := repo.ProjectEvents.ProjectChanges(ctx, id, lastSequence, limit, sortAscending)
|
2020-06-15 14:50:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-29 07:37:10 +00:00
|
|
|
for _, change := range changes.Changes {
|
|
|
|
change.ModifierName = change.ModifierId
|
|
|
|
user, _ := repo.UserEvents.UserByID(ctx, change.ModifierId)
|
|
|
|
if user != nil {
|
2020-11-12 16:01:30 +00:00
|
|
|
if user.Human != nil {
|
|
|
|
change.ModifierName = user.DisplayName
|
|
|
|
}
|
|
|
|
if user.Machine != nil {
|
|
|
|
change.ModifierName = user.Machine.Name
|
|
|
|
}
|
2020-06-29 07:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:50:09 +00:00
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 07:34:56 +00:00
|
|
|
func (repo *ProjectRepo) ApplicationByID(ctx context.Context, projectID, appID string) (*proj_model.ApplicationView, error) {
|
2020-08-13 06:28:18 +00:00
|
|
|
app, viewErr := repo.View.ApplicationByID(projectID, appID)
|
2020-08-10 07:34:56 +00:00
|
|
|
if viewErr != nil && !caos_errs.IsNotFound(viewErr) {
|
|
|
|
return nil, viewErr
|
|
|
|
}
|
|
|
|
if caos_errs.IsNotFound(viewErr) {
|
|
|
|
app = new(model.ApplicationView)
|
2020-08-13 06:28:18 +00:00
|
|
|
app.ID = appID
|
2020-08-10 07:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
events, esErr := repo.ProjectEvents.ProjectEventsByID(ctx, projectID, app.Sequence)
|
|
|
|
if caos_errs.IsNotFound(viewErr) && len(events) == 0 {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-Fshu8", "Errors.Application.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
if esErr != nil {
|
|
|
|
logging.Log("EVENT-SLCo9").WithError(viewErr).Debug("error retrieving new events")
|
|
|
|
return model.ApplicationViewToModel(app), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
viewApp := *app
|
|
|
|
for _, event := range events {
|
2020-08-13 06:28:18 +00:00
|
|
|
err := app.AppendEventIfMyApp(event)
|
2020-08-10 07:34:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return model.ApplicationViewToModel(&viewApp), nil
|
|
|
|
}
|
2020-08-13 06:28:18 +00:00
|
|
|
if app.State == int32(proj_model.AppStateRemoved) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-Msl96", "Errors.Application.NotFound")
|
|
|
|
}
|
2020-06-23 05:06:07 +00:00
|
|
|
}
|
|
|
|
return model.ApplicationViewToModel(app), nil
|
2020-04-21 15:00:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
func (repo *ProjectRepo) SearchApplications(ctx context.Context, request *proj_model.ApplicationSearchRequest) (*proj_model.ApplicationSearchResponse, error) {
|
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, sequenceErr := repo.View.GetLatestApplicationSequence("")
|
2020-08-26 07:56:23 +00:00
|
|
|
logging.Log("EVENT-SKe8s").OnError(sequenceErr).Warn("could not read latest application sequence")
|
2020-05-11 10:16:29 +00:00
|
|
|
apps, count, err := repo.View.SearchApplications(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &proj_model.ApplicationSearchResponse{
|
2020-05-11 10:16:29 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
2020-08-26 07:56:23 +00:00
|
|
|
TotalResult: count,
|
2020-05-11 10:16:29 +00:00
|
|
|
Result: model.ApplicationViewsToModel(apps),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
if sequenceErr == nil {
|
2020-07-15 11:24:36 +00:00
|
|
|
result.Sequence = sequence.CurrentSequence
|
2020-12-02 07:50:59 +00:00
|
|
|
result.Timestamp = sequence.LastSuccessfulSpoolerRun
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:25:38 +00:00
|
|
|
func (repo *ProjectRepo) ApplicationChanges(ctx context.Context, id string, appId string, lastSequence uint64, limit uint64, sortAscending bool) (*proj_model.ApplicationChanges, error) {
|
|
|
|
changes, err := repo.ProjectEvents.ApplicationChanges(ctx, id, appId, lastSequence, limit, sortAscending)
|
2020-06-15 14:50:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-29 07:37:10 +00:00
|
|
|
for _, change := range changes.Changes {
|
|
|
|
change.ModifierName = change.ModifierId
|
|
|
|
user, _ := repo.UserEvents.UserByID(ctx, change.ModifierId)
|
|
|
|
if user != nil {
|
2020-11-12 16:01:30 +00:00
|
|
|
if user.Human != nil {
|
|
|
|
change.ModifierName = user.DisplayName
|
|
|
|
}
|
|
|
|
if user.Machine != nil {
|
|
|
|
change.ModifierName = user.Machine.Name
|
|
|
|
}
|
2020-06-29 07:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:50:09 +00:00
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
2020-04-21 15:00:32 +00:00
|
|
|
func (repo *ProjectRepo) ChangeOIDConfigSecret(ctx context.Context, projectID, appID string) (*proj_model.OIDCConfig, error) {
|
|
|
|
return repo.ProjectEvents.ChangeOIDCConfigSecret(ctx, projectID, appID)
|
|
|
|
}
|
2020-04-23 05:54:40 +00:00
|
|
|
|
2020-06-23 05:06:07 +00:00
|
|
|
func (repo *ProjectRepo) ProjectGrantByID(ctx context.Context, grantID string) (*proj_model.ProjectGrantView, error) {
|
|
|
|
grant, err := repo.View.ProjectGrantByID(grantID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return model.ProjectGrantToModel(grant), nil
|
2020-04-23 05:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (repo *ProjectRepo) SearchProjectGrants(ctx context.Context, request *proj_model.ProjectGrantViewSearchRequest) (*proj_model.ProjectGrantViewSearchResponse, error) {
|
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, sequenceErr := repo.View.GetLatestProjectGrantSequence("")
|
2020-08-26 07:56:23 +00:00
|
|
|
logging.Log("EVENT-Skw9f").OnError(sequenceErr).Warn("could not read latest project grant sequence")
|
2020-06-15 12:50:39 +00:00
|
|
|
projects, count, err := repo.View.SearchProjectGrants(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &proj_model.ProjectGrantViewSearchResponse{
|
2020-06-15 12:50:39 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
2020-08-26 07:56:23 +00:00
|
|
|
TotalResult: count,
|
2020-06-15 12:50:39 +00:00
|
|
|
Result: model.ProjectGrantsToModel(projects),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
if sequenceErr == nil {
|
2020-07-15 11:24:36 +00:00
|
|
|
result.Sequence = sequence.CurrentSequence
|
2020-12-02 07:50:59 +00:00
|
|
|
result.Timestamp = sequence.LastSuccessfulSpoolerRun
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
2020-06-15 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 12:00:29 +00:00
|
|
|
func (repo *ProjectRepo) SearchGrantedProjects(ctx context.Context, request *proj_model.ProjectGrantViewSearchRequest) (*proj_model.ProjectGrantViewSearchResponse, error) {
|
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, sequenceErr := repo.View.GetLatestProjectGrantSequence("")
|
2020-08-26 07:56:23 +00:00
|
|
|
logging.Log("EVENT-Skw9f").OnError(sequenceErr).Warn("could not read latest project grant sequence")
|
2020-07-22 12:00:29 +00:00
|
|
|
|
|
|
|
permissions := authz.GetRequestPermissionsFromCtx(ctx)
|
|
|
|
if !authz.HasGlobalPermission(permissions) {
|
|
|
|
ids := authz.GetAllPermissionCtxIDs(permissions)
|
|
|
|
if _, q := request.GetSearchQuery(proj_model.GrantedProjectSearchKeyGrantID); q != nil {
|
|
|
|
containsID := false
|
|
|
|
for _, id := range ids {
|
|
|
|
if id == q.Value {
|
|
|
|
containsID = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !containsID {
|
|
|
|
result := &proj_model.ProjectGrantViewSearchResponse{
|
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
|
|
|
TotalResult: uint64(0),
|
|
|
|
Result: []*proj_model.ProjectGrantView{},
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
if sequenceErr == nil {
|
2020-07-22 12:00:29 +00:00
|
|
|
result.Sequence = sequence.CurrentSequence
|
2020-12-02 07:50:59 +00:00
|
|
|
result.Timestamp = sequence.LastSuccessfulSpoolerRun
|
2020-07-22 12:00:29 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request.Queries = append(request.Queries, &proj_model.ProjectGrantViewSearchQuery{Key: proj_model.GrantedProjectSearchKeyGrantID, Method: global_model.SearchMethodIsOneOf, Value: ids})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
projects, count, err := repo.View.SearchProjectGrants(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := &proj_model.ProjectGrantViewSearchResponse{
|
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
2020-08-26 07:56:23 +00:00
|
|
|
TotalResult: count,
|
2020-07-22 12:00:29 +00:00
|
|
|
Result: model.ProjectGrantsToModel(projects),
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
if sequenceErr == nil {
|
2020-07-22 12:00:29 +00:00
|
|
|
result.Sequence = sequence.CurrentSequence
|
2020-12-02 07:50:59 +00:00
|
|
|
result.Timestamp = sequence.LastSuccessfulSpoolerRun
|
2020-07-22 12:00:29 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 05:06:07 +00:00
|
|
|
func (repo *ProjectRepo) ProjectGrantMemberByID(ctx context.Context, projectID, userID string) (*proj_model.ProjectGrantMemberView, error) {
|
|
|
|
member, err := repo.View.ProjectGrantMemberByIDs(projectID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return model.ProjectGrantMemberToModel(member), nil
|
2020-04-23 05:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
func (repo *ProjectRepo) SearchProjectGrantMembers(ctx context.Context, request *proj_model.ProjectGrantMemberSearchRequest) (*proj_model.ProjectGrantMemberSearchResponse, error) {
|
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-12-18 15:47:45 +00:00
|
|
|
sequence, sequenceErr := repo.View.GetLatestProjectGrantMemberSequence("")
|
2020-08-26 07:56:23 +00:00
|
|
|
logging.Log("EVENT-Du8sk").OnError(sequenceErr).Warn("could not read latest project grant sequence")
|
2020-05-11 10:16:29 +00:00
|
|
|
members, count, err := repo.View.SearchProjectGrantMembers(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &proj_model.ProjectGrantMemberSearchResponse{
|
2020-05-11 10:16:29 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
|
|
|
TotalResult: uint64(count),
|
|
|
|
Result: model.ProjectGrantMembersToModel(members),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
if sequenceErr == nil {
|
2020-07-15 11:24:36 +00:00
|
|
|
result.Sequence = sequence.CurrentSequence
|
2020-12-02 07:50:59 +00:00
|
|
|
result.Timestamp = sequence.LastSuccessfulSpoolerRun
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
2020-05-26 14:46:16 +00:00
|
|
|
|
2020-09-01 14:38:34 +00:00
|
|
|
func (repo *ProjectRepo) GetProjectMemberRoles(ctx context.Context) ([]string, error) {
|
|
|
|
iam, err := repo.IAMEvents.IAMByID(ctx, repo.IAMID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-26 14:46:16 +00:00
|
|
|
roles := make([]string, 0)
|
2020-09-01 14:38:34 +00:00
|
|
|
global := authz.GetCtxData(ctx).OrgID == iam.GlobalOrgID
|
2020-05-26 14:46:16 +00:00
|
|
|
for _, roleMap := range repo.Roles {
|
|
|
|
if strings.HasPrefix(roleMap, "PROJECT") && !strings.HasPrefix(roleMap, "PROJECT_GRANT") {
|
2020-09-01 14:38:34 +00:00
|
|
|
if global && !strings.HasSuffix(roleMap, "GLOBAL") {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-26 14:46:16 +00:00
|
|
|
roles = append(roles, roleMap)
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 14:38:34 +00:00
|
|
|
return roles, nil
|
2020-05-26 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *ProjectRepo) GetProjectGrantMemberRoles() []string {
|
|
|
|
roles := make([]string, 0)
|
|
|
|
for _, roleMap := range repo.Roles {
|
|
|
|
if strings.HasPrefix(roleMap, "PROJECT_GRANT") {
|
|
|
|
roles = append(roles, roleMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return roles
|
|
|
|
}
|