2020-07-08 11:56:37 +00:00
|
|
|
package management
|
2020-05-11 08:16:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/logging"
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
|
2020-05-11 08:16:27 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
grant_model "github.com/caos/zitadel/internal/usergrant/model"
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/pkg/grpc/management"
|
2020-05-11 08:16:27 +00:00
|
|
|
)
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func usergrantFromModel(grant *grant_model.UserGrant) *management.UserGrant {
|
2020-05-11 08:16:27 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(grant.CreationDate)
|
|
|
|
logging.Log("GRPC-ki9ds").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(grant.ChangeDate)
|
|
|
|
logging.Log("GRPC-sl9ew").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &management.UserGrant{
|
2020-05-11 08:16:27 +00:00
|
|
|
Id: grant.AggregateID,
|
|
|
|
UserId: grant.UserID,
|
|
|
|
State: usergrantStateFromModel(grant.State),
|
|
|
|
CreationDate: creationDate,
|
|
|
|
ChangeDate: changeDate,
|
|
|
|
Sequence: grant.Sequence,
|
|
|
|
ProjectId: grant.ProjectID,
|
|
|
|
RoleKeys: grant.RoleKeys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantCreateToModel(u *management.UserGrantCreate) *grant_model.UserGrant {
|
2020-05-11 08:16:27 +00:00
|
|
|
return &grant_model.UserGrant{
|
|
|
|
ObjectRoot: models.ObjectRoot{AggregateID: u.UserId},
|
|
|
|
UserID: u.UserId,
|
|
|
|
ProjectID: u.ProjectId,
|
|
|
|
RoleKeys: u.RoleKeys,
|
2020-07-28 06:43:55 +00:00
|
|
|
GrantID: u.GrantId,
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantUpdateToModel(u *management.UserGrantUpdate) *grant_model.UserGrant {
|
2020-05-11 08:16:27 +00:00
|
|
|
return &grant_model.UserGrant{
|
|
|
|
ObjectRoot: models.ObjectRoot{AggregateID: u.Id},
|
|
|
|
RoleKeys: u.RoleKeys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantRemoveBulkToModel(u *management.UserGrantRemoveBulk) []string {
|
2020-06-19 13:32:03 +00:00
|
|
|
ids := make([]string, len(u.Ids))
|
|
|
|
for i, id := range u.Ids {
|
|
|
|
ids[i] = id
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantSearchRequestsToModel(project *management.UserGrantSearchRequest) *grant_model.UserGrantSearchRequest {
|
2020-05-12 04:30:53 +00:00
|
|
|
return &grant_model.UserGrantSearchRequest{
|
|
|
|
Offset: project.Offset,
|
|
|
|
Limit: project.Limit,
|
|
|
|
Queries: userGrantSearchQueriesToModel(project.Queries),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantSearchQueriesToModel(queries []*management.UserGrantSearchQuery) []*grant_model.UserGrantSearchQuery {
|
2020-05-12 04:30:53 +00:00
|
|
|
converted := make([]*grant_model.UserGrantSearchQuery, len(queries))
|
|
|
|
for i, q := range queries {
|
|
|
|
converted[i] = userGrantSearchQueryToModel(q)
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantSearchQueryToModel(query *management.UserGrantSearchQuery) *grant_model.UserGrantSearchQuery {
|
2020-05-12 04:30:53 +00:00
|
|
|
return &grant_model.UserGrantSearchQuery{
|
|
|
|
Key: userGrantSearchKeyToModel(query.Key),
|
|
|
|
Method: searchMethodToModel(query.Method),
|
|
|
|
Value: query.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantSearchKeyToModel(key management.UserGrantSearchKey) grant_model.UserGrantSearchKey {
|
2020-05-12 04:30:53 +00:00
|
|
|
switch key {
|
2020-07-08 11:56:37 +00:00
|
|
|
case management.UserGrantSearchKey_USERGRANTSEARCHKEY_ORG_ID:
|
2020-06-23 12:47:47 +00:00
|
|
|
return grant_model.UserGrantSearchKeyResourceOwner
|
2020-07-08 11:56:37 +00:00
|
|
|
case management.UserGrantSearchKey_USERGRANTSEARCHKEY_PROJECT_ID:
|
2020-06-23 12:47:47 +00:00
|
|
|
return grant_model.UserGrantSearchKeyProjectID
|
2020-07-08 11:56:37 +00:00
|
|
|
case management.UserGrantSearchKey_USERGRANTSEARCHKEY_USER_ID:
|
2020-06-23 12:47:47 +00:00
|
|
|
return grant_model.UserGrantSearchKeyUserID
|
2020-07-08 11:56:37 +00:00
|
|
|
case management.UserGrantSearchKey_USERGRANTSEARCHKEY_ROLE_KEY:
|
2020-06-23 12:47:47 +00:00
|
|
|
return grant_model.UserGrantSearchKeyRoleKey
|
2020-07-30 12:37:55 +00:00
|
|
|
case management.UserGrantSearchKey_USERGRANTSEARCHKEY_GRANT_ID:
|
|
|
|
return grant_model.UserGrantSearchKeyGrantID
|
2020-05-12 04:30:53 +00:00
|
|
|
default:
|
2020-06-23 12:47:47 +00:00
|
|
|
return grant_model.UserGrantSearchKeyUnspecified
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantSearchResponseFromModel(response *grant_model.UserGrantSearchResponse) *management.UserGrantSearchResponse {
|
2020-07-15 11:24:36 +00:00
|
|
|
timestamp, err := ptypes.TimestampProto(response.Timestamp)
|
|
|
|
logging.Log("GRPC-Wd7hs").OnError(err).Debug("unable to parse timestamp")
|
2020-07-08 11:56:37 +00:00
|
|
|
return &management.UserGrantSearchResponse{
|
2020-07-15 11:24:36 +00:00
|
|
|
Offset: response.Offset,
|
|
|
|
Limit: response.Limit,
|
|
|
|
TotalResult: response.TotalResult,
|
|
|
|
Result: userGrantViewsFromModel(response.Result),
|
|
|
|
ProcessedSequence: response.Sequence,
|
|
|
|
ViewTimestamp: timestamp,
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantViewsFromModel(users []*grant_model.UserGrantView) []*management.UserGrantView {
|
|
|
|
converted := make([]*management.UserGrantView, len(users))
|
2020-05-12 04:30:53 +00:00
|
|
|
for i, user := range users {
|
|
|
|
converted[i] = userGrantViewFromModel(user)
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userGrantViewFromModel(grant *grant_model.UserGrantView) *management.UserGrantView {
|
2020-05-12 04:30:53 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(grant.CreationDate)
|
|
|
|
logging.Log("GRPC-dl9we").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(grant.ChangeDate)
|
|
|
|
logging.Log("GRPC-lpsg5").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &management.UserGrantView{
|
2020-05-12 04:30:53 +00:00
|
|
|
Id: grant.ID,
|
|
|
|
State: usergrantStateFromModel(grant.State),
|
|
|
|
CreationDate: creationDate,
|
|
|
|
ChangeDate: changeDate,
|
|
|
|
Sequence: grant.Sequence,
|
|
|
|
ResourceOwner: grant.ResourceOwner,
|
|
|
|
UserName: grant.UserName,
|
|
|
|
FirstName: grant.FirstName,
|
|
|
|
LastName: grant.LastName,
|
|
|
|
Email: grant.Email,
|
|
|
|
ProjectName: grant.ProjectName,
|
|
|
|
OrgName: grant.OrgName,
|
|
|
|
OrgDomain: grant.OrgDomain,
|
|
|
|
RoleKeys: grant.RoleKeys,
|
2020-07-01 10:46:46 +00:00
|
|
|
UserId: grant.UserID,
|
|
|
|
ProjectId: grant.ProjectID,
|
|
|
|
OrgId: grant.ResourceOwner,
|
|
|
|
DisplayName: grant.DisplayName,
|
2020-07-09 13:14:01 +00:00
|
|
|
GrantId: grant.GrantID,
|
2020-05-12 04:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func usergrantStateFromModel(state grant_model.UserGrantState) management.UserGrantState {
|
2020-05-11 08:16:27 +00:00
|
|
|
switch state {
|
2020-06-23 12:47:47 +00:00
|
|
|
case grant_model.UserGrantStateActive:
|
2020-07-08 11:56:37 +00:00
|
|
|
return management.UserGrantState_USERGRANTSTATE_ACTIVE
|
2020-06-23 12:47:47 +00:00
|
|
|
case grant_model.UserGrantStateInactive:
|
2020-07-08 11:56:37 +00:00
|
|
|
return management.UserGrantState_USERGRANTSTATE_INACTIVE
|
2020-05-11 08:16:27 +00:00
|
|
|
default:
|
2020-07-08 11:56:37 +00:00
|
|
|
return management.UserGrantState_USERGRANTSTATE_UNSPECIFIED
|
2020-05-11 08:16:27 +00:00
|
|
|
}
|
|
|
|
}
|