2020-05-11 12:16:29 +02:00
|
|
|
package eventstore
|
2020-04-07 13:23:04 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-22 14:08:47 +01:00
|
|
|
"encoding/json"
|
2020-06-25 11:25:38 +02:00
|
|
|
"strings"
|
2021-03-25 17:26:21 +01:00
|
|
|
"time"
|
2020-06-25 11:25:38 +02:00
|
|
|
|
2020-06-30 07:54:39 +02:00
|
|
|
"github.com/caos/logging"
|
2021-06-10 11:29:34 +02:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
|
2020-07-08 13:56:37 +02:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2022-01-20 15:40:25 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-06-19 15:32:03 +02:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-04-20 22:17:37 +02:00
|
|
|
v1 "github.com/caos/zitadel/internal/eventstore/v1"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-07-08 13:56:37 +02:00
|
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
2021-02-22 14:08:47 +01:00
|
|
|
proj_view "github.com/caos/zitadel/internal/project/repository/view"
|
2022-01-13 08:58:14 +01:00
|
|
|
"github.com/caos/zitadel/internal/query"
|
2020-04-07 13:23:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectRepo struct {
|
2021-02-23 15:13:04 +01:00
|
|
|
v1.Eventstore
|
2021-06-11 13:20:39 +02:00
|
|
|
Roles []string
|
|
|
|
IAMID string
|
|
|
|
PrefixAvatarURL string
|
2022-01-13 08:58:14 +01:00
|
|
|
Query *query.Queries
|
2020-05-11 12:16:29 +02:00
|
|
|
}
|
|
|
|
|
2021-03-25 17:26:21 +01:00
|
|
|
func (repo *ProjectRepo) ProjectChanges(ctx context.Context, id string, lastSequence uint64, limit uint64, sortAscending bool, retention time.Duration) (*proj_model.ProjectChanges, error) {
|
|
|
|
changes, err := repo.getProjectChanges(ctx, id, lastSequence, limit, sortAscending, retention)
|
2020-06-15 16:50:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-29 09:37:10 +02:00
|
|
|
for _, change := range changes.Changes {
|
|
|
|
change.ModifierName = change.ModifierId
|
2021-06-10 11:29:34 +02:00
|
|
|
change.ModifierLoginName = change.ModifierId
|
2022-01-20 15:40:25 +01:00
|
|
|
user, _ := repo.Query.GetUserByID(ctx, change.ModifierId)
|
2020-06-29 09:37:10 +02:00
|
|
|
if user != nil {
|
2021-06-10 11:29:34 +02:00
|
|
|
change.ModifierLoginName = user.PreferredLoginName
|
2022-01-20 15:40:25 +01:00
|
|
|
if user.Human != nil {
|
|
|
|
change.ModifierName = user.Human.DisplayName
|
|
|
|
change.ModifierAvatarURL = domain.AvatarURL(repo.PrefixAvatarURL, user.ResourceOwner, user.Human.AvatarKey)
|
2020-11-12 17:01:30 +01:00
|
|
|
}
|
2022-01-20 15:40:25 +01:00
|
|
|
if user.Machine != nil {
|
|
|
|
change.ModifierName = user.Machine.Name
|
2020-11-12 17:01:30 +01:00
|
|
|
}
|
2020-06-29 09:37:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 16:50:09 +02:00
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:26:21 +01:00
|
|
|
func (repo *ProjectRepo) ApplicationChanges(ctx context.Context, projectID string, appID string, lastSequence uint64, limit uint64, sortAscending bool, retention time.Duration) (*proj_model.ApplicationChanges, error) {
|
|
|
|
changes, err := repo.getApplicationChanges(ctx, projectID, appID, lastSequence, limit, sortAscending, retention)
|
2020-06-15 16:50:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-29 09:37:10 +02:00
|
|
|
for _, change := range changes.Changes {
|
|
|
|
change.ModifierName = change.ModifierId
|
2021-06-10 11:29:34 +02:00
|
|
|
change.ModifierLoginName = change.ModifierId
|
2022-01-20 15:40:25 +01:00
|
|
|
user, _ := repo.Query.GetUserByID(ctx, change.ModifierId)
|
2020-06-29 09:37:10 +02:00
|
|
|
if user != nil {
|
2021-06-10 11:29:34 +02:00
|
|
|
change.ModifierLoginName = user.PreferredLoginName
|
2022-01-20 15:40:25 +01:00
|
|
|
if user.Human != nil {
|
|
|
|
change.ModifierName = user.Human.DisplayName
|
|
|
|
change.ModifierAvatarURL = domain.AvatarURL(repo.PrefixAvatarURL, user.ResourceOwner, user.Human.AvatarKey)
|
2020-11-12 17:01:30 +01:00
|
|
|
}
|
2022-01-20 15:40:25 +01:00
|
|
|
if user.Machine != nil {
|
|
|
|
change.ModifierName = user.Machine.Name
|
2020-11-12 17:01:30 +01:00
|
|
|
}
|
2020-06-29 09:37:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 16:50:09 +02:00
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 16:38:34 +02:00
|
|
|
func (repo *ProjectRepo) GetProjectMemberRoles(ctx context.Context) ([]string, error) {
|
2022-01-13 08:58:14 +01:00
|
|
|
iam, err := repo.Query.IAMByID(ctx, repo.IAMID)
|
2020-09-01 16:38:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-26 16:46:16 +02:00
|
|
|
roles := make([]string, 0)
|
2020-09-01 16:38:34 +02:00
|
|
|
global := authz.GetCtxData(ctx).OrgID == iam.GlobalOrgID
|
2020-05-26 16:46:16 +02:00
|
|
|
for _, roleMap := range repo.Roles {
|
|
|
|
if strings.HasPrefix(roleMap, "PROJECT") && !strings.HasPrefix(roleMap, "PROJECT_GRANT") {
|
2020-09-01 16:38:34 +02:00
|
|
|
if global && !strings.HasSuffix(roleMap, "GLOBAL") {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-26 16:46:16 +02:00
|
|
|
roles = append(roles, roleMap)
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 16:38:34 +02:00
|
|
|
return roles, nil
|
2020-05-26 16:46:16 +02: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
|
|
|
|
}
|
2021-02-22 14:08:47 +01:00
|
|
|
|
2021-03-25 17:26:21 +01:00
|
|
|
func (repo *ProjectRepo) getProjectChanges(ctx context.Context, id string, lastSequence uint64, limit uint64, sortAscending bool, retention time.Duration) (*proj_model.ProjectChanges, error) {
|
|
|
|
query := proj_view.ChangesQuery(id, lastSequence, limit, sortAscending, retention)
|
2021-02-22 14:08:47 +01:00
|
|
|
|
|
|
|
events, err := repo.Eventstore.FilterEvents(context.Background(), query)
|
|
|
|
if err != nil {
|
|
|
|
logging.Log("EVENT-ZRffs").WithError(err).Warn("eventstore unavailable")
|
|
|
|
return nil, caos_errs.ThrowInternal(err, "EVENT-328b1", "Errors.Internal")
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-FpQqK", "Errors.Changes.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
changes := make([]*proj_model.ProjectChange, len(events))
|
|
|
|
|
|
|
|
for i, event := range events {
|
|
|
|
creationDate, err := ptypes.TimestampProto(event.CreationDate)
|
|
|
|
logging.Log("EVENT-qxIR7").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
change := &proj_model.ProjectChange{
|
|
|
|
ChangeDate: creationDate,
|
|
|
|
EventType: event.Type.String(),
|
|
|
|
ModifierId: event.EditorUser,
|
|
|
|
Sequence: event.Sequence,
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Data != nil {
|
|
|
|
var data interface{}
|
|
|
|
if strings.Contains(change.EventType, "application") {
|
|
|
|
data = new(proj_model.Application)
|
|
|
|
} else {
|
|
|
|
data = new(proj_model.Project)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(event.Data, data)
|
|
|
|
logging.Log("EVENT-NCkpN").OnError(err).Debug("unable to unmarshal data")
|
|
|
|
change.Data = data
|
|
|
|
}
|
|
|
|
|
|
|
|
changes[i] = change
|
|
|
|
if lastSequence < event.Sequence {
|
|
|
|
lastSequence = event.Sequence
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proj_model.ProjectChanges{
|
|
|
|
Changes: changes,
|
|
|
|
LastSequence: lastSequence,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *ProjectRepo) getProjectEvents(ctx context.Context, id string, sequence uint64) ([]*models.Event, error) {
|
|
|
|
query, err := proj_view.ProjectByIDQuery(id, sequence)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.Eventstore.FilterEvents(ctx, query)
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:26:21 +01:00
|
|
|
func (repo *ProjectRepo) getApplicationChanges(ctx context.Context, projectID string, appID string, lastSequence uint64, limit uint64, sortAscending bool, retention time.Duration) (*proj_model.ApplicationChanges, error) {
|
|
|
|
query := proj_view.ChangesQuery(projectID, lastSequence, limit, sortAscending, retention)
|
2021-02-22 14:08:47 +01:00
|
|
|
|
|
|
|
events, err := repo.Eventstore.FilterEvents(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
logging.Log("EVENT-ZRffs").WithError(err).Warn("eventstore unavailable")
|
|
|
|
return nil, caos_errs.ThrowInternal(err, "EVENT-sw6Ku", "Errors.Internal")
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "EVENT-9IHLP", "Errors.Changes.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*proj_model.ApplicationChange, 0)
|
|
|
|
for _, event := range events {
|
|
|
|
if !strings.Contains(event.Type.String(), "application") || event.Data == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
app := new(proj_model.Application)
|
|
|
|
err := json.Unmarshal(event.Data, app)
|
|
|
|
logging.Log("EVENT-GIiKD").OnError(err).Debug("unable to unmarshal data")
|
|
|
|
if app.AppID != appID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
creationDate, err := ptypes.TimestampProto(event.CreationDate)
|
|
|
|
logging.Log("EVENT-MJzeN").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
result = append(result, &proj_model.ApplicationChange{
|
|
|
|
ChangeDate: creationDate,
|
|
|
|
EventType: event.Type.String(),
|
|
|
|
ModifierId: event.EditorUser,
|
|
|
|
Sequence: event.Sequence,
|
|
|
|
Data: app,
|
|
|
|
})
|
|
|
|
if lastSequence < event.Sequence {
|
|
|
|
lastSequence = event.Sequence
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proj_model.ApplicationChanges{
|
|
|
|
Changes: result,
|
|
|
|
LastSequence: lastSequence,
|
|
|
|
}, nil
|
|
|
|
}
|