2020-07-08 11:56:37 +00:00
|
|
|
package management
|
2020-04-07 11:23:04 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-15 14:50:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2020-04-07 11:23:04 +00:00
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2020-06-15 14:50:09 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
2020-07-08 11:56:37 +00:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
|
|
"github.com/caos/zitadel/pkg/grpc/management"
|
|
|
|
"github.com/caos/zitadel/pkg/grpc/message"
|
2020-04-07 11:23:04 +00:00
|
|
|
)
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectFromModel(project *proj_model.Project) *management.Project {
|
2020-04-07 11:23:04 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(project.CreationDate)
|
|
|
|
logging.Log("GRPC-iejs3").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(project.ChangeDate)
|
|
|
|
logging.Log("GRPC-di7rw").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &management.Project{
|
2020-10-16 05:49:38 +00:00
|
|
|
Id: project.AggregateID,
|
|
|
|
State: projectStateFromModel(project.State),
|
|
|
|
CreationDate: creationDate,
|
|
|
|
ChangeDate: changeDate,
|
|
|
|
Name: project.Name,
|
|
|
|
Sequence: project.Sequence,
|
|
|
|
ProjectRoleAssertion: project.ProjectRoleAssertion,
|
|
|
|
ProjectRoleCheck: project.ProjectRoleCheck,
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectSearchResponseFromModel(response *proj_model.ProjectViewSearchResponse) *management.ProjectSearchResponse {
|
2020-07-15 11:24:36 +00:00
|
|
|
timestamp, err := ptypes.TimestampProto(response.Timestamp)
|
|
|
|
logging.Log("GRPC-iejs3").OnError(err).Debug("unable to parse timestamp")
|
2020-07-08 11:56:37 +00:00
|
|
|
return &management.ProjectSearchResponse{
|
2020-07-15 11:24:36 +00:00
|
|
|
Offset: response.Offset,
|
|
|
|
Limit: response.Limit,
|
|
|
|
TotalResult: response.TotalResult,
|
|
|
|
Result: projectViewsFromModel(response.Result),
|
|
|
|
ProcessedSequence: response.Sequence,
|
|
|
|
ViewTimestamp: timestamp,
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectViewsFromModel(projects []*proj_model.ProjectView) []*management.ProjectView {
|
|
|
|
converted := make([]*management.ProjectView, len(projects))
|
2020-05-11 10:16:29 +00:00
|
|
|
for i, project := range projects {
|
2020-06-15 12:50:39 +00:00
|
|
|
converted[i] = projectViewFromModel(project)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectViewFromModel(project *proj_model.ProjectView) *management.ProjectView {
|
2020-05-11 10:16:29 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(project.CreationDate)
|
|
|
|
logging.Log("GRPC-dlso3").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(project.ChangeDate)
|
|
|
|
logging.Log("GRPC-sope3").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &management.ProjectView{
|
2020-10-16 05:49:38 +00:00
|
|
|
ProjectId: project.ProjectID,
|
|
|
|
State: projectStateFromModel(project.State),
|
|
|
|
CreationDate: creationDate,
|
|
|
|
ChangeDate: changeDate,
|
|
|
|
Name: project.Name,
|
|
|
|
Sequence: project.Sequence,
|
|
|
|
ResourceOwner: project.ResourceOwner,
|
|
|
|
ProjectRoleAssertion: project.ProjectRoleAssertion,
|
|
|
|
ProjectRoleCheck: project.ProjectRoleCheck,
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleSearchResponseFromModel(response *proj_model.ProjectRoleSearchResponse) *management.ProjectRoleSearchResponse {
|
2020-07-15 11:24:36 +00:00
|
|
|
timestamp, err := ptypes.TimestampProto(response.Timestamp)
|
|
|
|
logging.Log("GRPC-Lps0c").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &management.ProjectRoleSearchResponse{
|
2020-07-15 11:24:36 +00:00
|
|
|
Offset: response.Offset,
|
|
|
|
Limit: response.Limit,
|
|
|
|
TotalResult: response.TotalResult,
|
|
|
|
Result: projectRoleViewsFromModel(response.Result),
|
|
|
|
ProcessedSequence: response.Sequence,
|
|
|
|
ViewTimestamp: timestamp,
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleViewsFromModel(roles []*proj_model.ProjectRoleView) []*management.ProjectRoleView {
|
|
|
|
converted := make([]*management.ProjectRoleView, len(roles))
|
2020-05-11 10:16:29 +00:00
|
|
|
for i, role := range roles {
|
|
|
|
converted[i] = projectRoleViewFromModel(role)
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleViewFromModel(role *proj_model.ProjectRoleView) *management.ProjectRoleView {
|
2020-05-11 10:16:29 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(role.CreationDate)
|
|
|
|
logging.Log("GRPC-dlso3").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &management.ProjectRoleView{
|
2020-05-11 10:16:29 +00:00
|
|
|
ProjectId: role.ProjectID,
|
|
|
|
CreationDate: creationDate,
|
|
|
|
Key: role.Key,
|
|
|
|
Group: role.Group,
|
|
|
|
DisplayName: role.DisplayName,
|
|
|
|
Sequence: role.Sequence,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectStateFromModel(state proj_model.ProjectState) management.ProjectState {
|
2020-04-07 11:23:04 +00:00
|
|
|
switch state {
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.ProjectStateActive:
|
2020-07-08 11:56:37 +00:00
|
|
|
return management.ProjectState_PROJECTSTATE_ACTIVE
|
2020-06-23 12:47:47 +00:00
|
|
|
case proj_model.ProjectStateInactive:
|
2020-07-08 11:56:37 +00:00
|
|
|
return management.ProjectState_PROJECTSTATE_INACTIVE
|
2020-04-07 11:23:04 +00:00
|
|
|
default:
|
2020-07-08 11:56:37 +00:00
|
|
|
return management.ProjectState_PROJECTSTATE_UNSPECIFIED
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:49:38 +00:00
|
|
|
func projectCreateToModel(project *management.ProjectCreateRequest) *proj_model.Project {
|
|
|
|
return &proj_model.Project{
|
|
|
|
Name: project.Name,
|
|
|
|
ProjectRoleAssertion: project.ProjectRoleAssertion,
|
|
|
|
ProjectRoleCheck: project.ProjectRoleCheck,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectUpdateToModel(project *management.ProjectUpdateRequest) *proj_model.Project {
|
2020-04-07 11:23:04 +00:00
|
|
|
return &proj_model.Project{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
2020-04-21 15:00:32 +00:00
|
|
|
AggregateID: project.Id,
|
2020-04-07 11:23:04 +00:00
|
|
|
},
|
2020-10-16 05:49:38 +00:00
|
|
|
Name: project.Name,
|
|
|
|
ProjectRoleAssertion: project.ProjectRoleAssertion,
|
|
|
|
ProjectRoleCheck: project.ProjectRoleCheck,
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 15:00:32 +00:00
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleFromModel(role *proj_model.ProjectRole) *management.ProjectRole {
|
2020-04-21 15:00:32 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(role.CreationDate)
|
|
|
|
logging.Log("GRPC-due83").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(role.ChangeDate)
|
|
|
|
logging.Log("GRPC-id93s").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &management.ProjectRole{
|
2020-04-21 15:00:32 +00:00
|
|
|
CreationDate: creationDate,
|
|
|
|
ChangeDate: changeDate,
|
|
|
|
Sequence: role.Sequence,
|
|
|
|
Key: role.Key,
|
|
|
|
DisplayName: role.DisplayName,
|
|
|
|
Group: role.Group,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleAddBulkToModel(bulk *management.ProjectRoleAddBulk) []*proj_model.ProjectRole {
|
2020-06-19 13:32:03 +00:00
|
|
|
roles := make([]*proj_model.ProjectRole, len(bulk.ProjectRoles))
|
|
|
|
for i, role := range bulk.ProjectRoles {
|
|
|
|
roles[i] = &proj_model.ProjectRole{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: bulk.Id,
|
|
|
|
},
|
|
|
|
Key: role.Key,
|
|
|
|
DisplayName: role.DisplayName,
|
|
|
|
Group: role.Group,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return roles
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleAddToModel(role *management.ProjectRoleAdd) *proj_model.ProjectRole {
|
2020-04-21 15:00:32 +00:00
|
|
|
return &proj_model.ProjectRole{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: role.Id,
|
|
|
|
},
|
|
|
|
Key: role.Key,
|
|
|
|
DisplayName: role.DisplayName,
|
|
|
|
Group: role.Group,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleChangeToModel(role *management.ProjectRoleChange) *proj_model.ProjectRole {
|
2020-04-21 15:00:32 +00:00
|
|
|
return &proj_model.ProjectRole{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: role.Id,
|
|
|
|
},
|
|
|
|
Key: role.Key,
|
|
|
|
DisplayName: role.DisplayName,
|
|
|
|
Group: role.Group,
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 10:16:29 +00:00
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectSearchRequestsToModel(project *management.ProjectSearchRequest) *proj_model.ProjectViewSearchRequest {
|
2020-06-15 12:50:39 +00:00
|
|
|
return &proj_model.ProjectViewSearchRequest{
|
2020-05-11 10:16:29 +00:00
|
|
|
Offset: project.Offset,
|
|
|
|
Limit: project.Limit,
|
2020-06-15 12:50:39 +00:00
|
|
|
Queries: projectSearchQueriesToModel(project.Queries),
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 11:56:37 +00:00
|
|
|
func grantedProjectSearchRequestsToModel(request *management.GrantedProjectSearchRequest) *proj_model.ProjectGrantViewSearchRequest {
|
2020-06-15 12:50:39 +00:00
|
|
|
return &proj_model.ProjectGrantViewSearchRequest{
|
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
|
|
|
Queries: grantedPRojectSearchQueriesToModel(request.Queries),
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectSearchQueriesToModel(queries []*management.ProjectSearchQuery) []*proj_model.ProjectViewSearchQuery {
|
2020-06-15 12:50:39 +00:00
|
|
|
converted := make([]*proj_model.ProjectViewSearchQuery, len(queries))
|
2020-05-11 10:16:29 +00:00
|
|
|
for i, q := range queries {
|
2020-06-15 12:50:39 +00:00
|
|
|
converted[i] = projectSearchQueryToModel(q)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectSearchQueryToModel(query *management.ProjectSearchQuery) *proj_model.ProjectViewSearchQuery {
|
2020-06-15 12:50:39 +00:00
|
|
|
return &proj_model.ProjectViewSearchQuery{
|
2020-05-11 10:16:29 +00:00
|
|
|
Key: projectSearchKeyToModel(query.Key),
|
|
|
|
Method: searchMethodToModel(query.Method),
|
|
|
|
Value: query.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectSearchKeyToModel(key management.ProjectSearchKey) proj_model.ProjectViewSearchKey {
|
2020-06-15 12:50:39 +00:00
|
|
|
switch key {
|
2020-07-08 11:56:37 +00:00
|
|
|
case management.ProjectSearchKey_PROJECTSEARCHKEY_PROJECT_NAME:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.ProjectViewSearchKeyName
|
2020-06-15 12:50:39 +00:00
|
|
|
default:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.ProjectViewSearchKeyUnspecified
|
2020-06-15 12:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func grantedPRojectSearchQueriesToModel(queries []*management.ProjectSearchQuery) []*proj_model.ProjectGrantViewSearchQuery {
|
2020-06-15 12:50:39 +00:00
|
|
|
converted := make([]*proj_model.ProjectGrantViewSearchQuery, len(queries))
|
|
|
|
for i, q := range queries {
|
|
|
|
converted[i] = grantedProjectSearchQueryToModel(q)
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func grantedProjectSearchQueryToModel(query *management.ProjectSearchQuery) *proj_model.ProjectGrantViewSearchQuery {
|
2020-06-15 12:50:39 +00:00
|
|
|
return &proj_model.ProjectGrantViewSearchQuery{
|
|
|
|
Key: projectGrantSearchKeyToModel(query.Key),
|
|
|
|
Method: searchMethodToModel(query.Method),
|
|
|
|
Value: query.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectGrantSearchKeyToModel(key management.ProjectSearchKey) proj_model.ProjectGrantViewSearchKey {
|
2020-05-11 10:16:29 +00:00
|
|
|
switch key {
|
2020-07-08 11:56:37 +00:00
|
|
|
case management.ProjectSearchKey_PROJECTSEARCHKEY_PROJECT_NAME:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.GrantedProjectSearchKeyName
|
2020-05-11 10:16:29 +00:00
|
|
|
default:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.GrantedProjectSearchKeyUnspecified
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleSearchRequestsToModel(role *management.ProjectRoleSearchRequest) *proj_model.ProjectRoleSearchRequest {
|
2020-05-11 10:16:29 +00:00
|
|
|
return &proj_model.ProjectRoleSearchRequest{
|
|
|
|
Offset: role.Offset,
|
|
|
|
Limit: role.Limit,
|
|
|
|
Queries: projectRoleSearchQueriesToModel(role.Queries),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleSearchQueriesToModel(queries []*management.ProjectRoleSearchQuery) []*proj_model.ProjectRoleSearchQuery {
|
2020-05-11 10:16:29 +00:00
|
|
|
converted := make([]*proj_model.ProjectRoleSearchQuery, len(queries))
|
|
|
|
for i, q := range queries {
|
|
|
|
converted[i] = projectRoleSearchQueryToModel(q)
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleSearchQueryToModel(query *management.ProjectRoleSearchQuery) *proj_model.ProjectRoleSearchQuery {
|
2020-05-11 10:16:29 +00:00
|
|
|
return &proj_model.ProjectRoleSearchQuery{
|
|
|
|
Key: projectRoleSearchKeyToModel(query.Key),
|
|
|
|
Method: searchMethodToModel(query.Method),
|
|
|
|
Value: query.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectRoleSearchKeyToModel(key management.ProjectRoleSearchKey) proj_model.ProjectRoleSearchKey {
|
2020-05-11 10:16:29 +00:00
|
|
|
switch key {
|
2020-07-08 11:56:37 +00:00
|
|
|
case management.ProjectRoleSearchKey_PROJECTROLESEARCHKEY_KEY:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.ProjectRoleSearchKeyKey
|
2020-07-08 11:56:37 +00:00
|
|
|
case management.ProjectRoleSearchKey_PROJECTROLESEARCHKEY_DISPLAY_NAME:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.ProjectRoleSearchKeyDisplayName
|
2020-05-11 10:16:29 +00:00
|
|
|
default:
|
2020-06-23 12:47:47 +00:00
|
|
|
return proj_model.ProjectRoleSearchKeyUnspecified
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:50:09 +00:00
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectChangesToResponse(response *proj_model.ProjectChanges, offset uint64, limit uint64) (_ *management.Changes) {
|
|
|
|
return &management.Changes{
|
2020-06-15 14:50:09 +00:00
|
|
|
Limit: limit,
|
|
|
|
Offset: offset,
|
|
|
|
Changes: projectChangesToMgtAPI(response),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func projectChangesToMgtAPI(changes *proj_model.ProjectChanges) (_ []*management.Change) {
|
|
|
|
result := make([]*management.Change, len(changes.Changes))
|
2020-06-15 14:50:09 +00:00
|
|
|
|
|
|
|
for i, change := range changes.Changes {
|
|
|
|
b, err := json.Marshal(change.Data)
|
|
|
|
data := &structpb.Struct{}
|
|
|
|
err = protojson.Unmarshal(b, data)
|
|
|
|
if err != nil {
|
|
|
|
}
|
2020-07-08 11:56:37 +00:00
|
|
|
result[i] = &management.Change{
|
2020-06-15 14:50:09 +00:00
|
|
|
ChangeDate: change.ChangeDate,
|
2020-07-08 07:48:11 +00:00
|
|
|
EventType: message.NewLocalizedEventType(change.EventType),
|
2020-06-15 14:50:09 +00:00
|
|
|
Sequence: change.Sequence,
|
2020-06-29 07:37:10 +00:00
|
|
|
Editor: change.ModifierName,
|
|
|
|
EditorId: change.ModifierId,
|
2020-06-15 14:50:09 +00:00
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|