2020-07-08 11:56:37 +00:00
|
|
|
package admin
|
2020-06-25 06:12:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/logging"
|
2021-01-05 08:33:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2021-01-20 10:06:52 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2020-07-08 11:56:37 +00:00
|
|
|
|
2020-06-25 06:12:29 +00:00
|
|
|
iam_model "github.com/caos/zitadel/internal/iam/model"
|
|
|
|
"github.com/caos/zitadel/internal/model"
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/pkg/grpc/admin"
|
2020-06-25 06:12:29 +00:00
|
|
|
)
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
func addIamMemberToDomain(member *admin.AddIamMemberRequest) *domain.Member {
|
2021-01-18 10:24:15 +00:00
|
|
|
return domain.NewMember(domain.IAMID, member.UserId, member.Roles...)
|
2020-06-25 06:12:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
func changeIamMemberToDomain(member *admin.ChangeIamMemberRequest) *domain.Member {
|
2021-01-18 10:24:15 +00:00
|
|
|
return domain.NewMember(domain.IAMID, member.UserId, member.Roles...)
|
2020-06-25 06:12:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
func iamMemberFromDomain(member *domain.Member) *admin.IamMember {
|
2020-07-08 11:56:37 +00:00
|
|
|
return &admin.IamMember{
|
2021-01-20 10:06:52 +00:00
|
|
|
UserId: member.UserID,
|
|
|
|
ChangeDate: timestamppb.New(member.ChangeDate),
|
|
|
|
Roles: member.Roles,
|
|
|
|
Sequence: member.Sequence,
|
2020-06-25 06:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func iamMemberSearchRequestToModel(request *admin.IamMemberSearchRequest) *iam_model.IAMMemberSearchRequest {
|
|
|
|
return &iam_model.IAMMemberSearchRequest{
|
2020-06-25 06:12:29 +00:00
|
|
|
Limit: request.Limit,
|
|
|
|
Offset: request.Offset,
|
|
|
|
Queries: iamMemberSearchQueriesToModel(request.Queries),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func iamMemberSearchQueriesToModel(queries []*admin.IamMemberSearchQuery) []*iam_model.IAMMemberSearchQuery {
|
|
|
|
modelQueries := make([]*iam_model.IAMMemberSearchQuery, len(queries))
|
2020-06-25 06:12:29 +00:00
|
|
|
for i, query := range queries {
|
|
|
|
modelQueries[i] = iamMemberSearchQueryToModel(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
return modelQueries
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func iamMemberSearchQueryToModel(query *admin.IamMemberSearchQuery) *iam_model.IAMMemberSearchQuery {
|
|
|
|
return &iam_model.IAMMemberSearchQuery{
|
2020-06-25 06:12:29 +00:00
|
|
|
Key: iamMemberSearchKeyToModel(query.Key),
|
2020-08-26 07:56:23 +00:00
|
|
|
Method: searchMethodToModel(query.Method),
|
2020-06-25 06:12:29 +00:00
|
|
|
Value: query.Value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func iamMemberSearchKeyToModel(key admin.IamMemberSearchKey) iam_model.IAMMemberSearchKey {
|
2020-06-25 06:12:29 +00:00
|
|
|
switch key {
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.IamMemberSearchKey_IAMMEMBERSEARCHKEY_EMAIL:
|
2020-08-26 07:56:23 +00:00
|
|
|
return iam_model.IAMMemberSearchKeyEmail
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.IamMemberSearchKey_IAMMEMBERSEARCHKEY_FIRST_NAME:
|
2020-08-26 07:56:23 +00:00
|
|
|
return iam_model.IAMMemberSearchKeyFirstName
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.IamMemberSearchKey_IAMMEMBERSEARCHKEY_LAST_NAME:
|
2020-08-26 07:56:23 +00:00
|
|
|
return iam_model.IAMMemberSearchKeyLastName
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.IamMemberSearchKey_IAMMEMBERSEARCHKEY_USER_ID:
|
2020-08-26 07:56:23 +00:00
|
|
|
return iam_model.IAMMemberSearchKeyUserID
|
2020-06-25 06:12:29 +00:00
|
|
|
default:
|
2020-08-26 07:56:23 +00:00
|
|
|
return iam_model.IAMMemberSearchKeyUnspecified
|
2020-06-25 06:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func searchMethodToModel(key admin.SearchMethod) model.SearchMethod {
|
2020-06-25 06:12:29 +00:00
|
|
|
switch key {
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.SearchMethod_SEARCHMETHOD_CONTAINS:
|
2020-06-25 06:12:29 +00:00
|
|
|
return model.SearchMethodContains
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.SearchMethod_SEARCHMETHOD_CONTAINS_IGNORE_CASE:
|
2020-06-25 06:12:29 +00:00
|
|
|
return model.SearchMethodContainsIgnoreCase
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.SearchMethod_SEARCHMETHOD_EQUALS:
|
2020-06-25 06:12:29 +00:00
|
|
|
return model.SearchMethodEquals
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.SearchMethod_SEARCHMETHOD_EQUALS_IGNORE_CASE:
|
2020-06-25 06:12:29 +00:00
|
|
|
return model.SearchMethodEqualsIgnoreCase
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.SearchMethod_SEARCHMETHOD_STARTS_WITH:
|
2020-06-25 06:12:29 +00:00
|
|
|
return model.SearchMethodStartsWith
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.SearchMethod_SEARCHMETHOD_STARTS_WITH_IGNORE_CASE:
|
2020-06-25 06:12:29 +00:00
|
|
|
return model.SearchMethodStartsWithIgnoreCase
|
|
|
|
default:
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func iamMemberSearchResponseFromModel(resp *iam_model.IAMMemberSearchResponse) *admin.IamMemberSearchResponse {
|
2020-07-15 11:24:36 +00:00
|
|
|
timestamp, err := ptypes.TimestampProto(resp.Timestamp)
|
|
|
|
logging.Log("GRPC-5shu8").OnError(err).Debug("date parse failed")
|
2020-07-08 11:56:37 +00:00
|
|
|
return &admin.IamMemberSearchResponse{
|
2020-07-15 11:24:36 +00:00
|
|
|
Limit: resp.Limit,
|
|
|
|
Offset: resp.Offset,
|
|
|
|
TotalResult: resp.TotalResult,
|
|
|
|
Result: iamMembersFromView(resp.Result),
|
|
|
|
ProcessedSequence: resp.Sequence,
|
|
|
|
ViewTimestamp: timestamp,
|
2020-06-25 06:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
func iamMembersFromView(viewMembers []*iam_model.IAMMemberView) []*admin.IamMemberView {
|
2020-07-08 11:56:37 +00:00
|
|
|
members := make([]*admin.IamMemberView, len(viewMembers))
|
2020-06-25 06:12:29 +00:00
|
|
|
|
|
|
|
for i, member := range viewMembers {
|
|
|
|
members[i] = iamMemberFromView(member)
|
|
|
|
}
|
|
|
|
|
|
|
|
return members
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func iamMemberFromView(member *iam_model.IAMMemberView) *admin.IamMemberView {
|
2020-06-25 06:12:29 +00:00
|
|
|
changeDate, err := ptypes.TimestampProto(member.ChangeDate)
|
|
|
|
logging.Log("GRPC-Lso9c").OnError(err).Debug("unable to parse changedate")
|
|
|
|
creationDate, err := ptypes.TimestampProto(member.CreationDate)
|
|
|
|
logging.Log("GRPC-6szE").OnError(err).Debug("unable to parse creation date")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &admin.IamMemberView{
|
2020-06-25 06:12:29 +00:00
|
|
|
ChangeDate: changeDate,
|
|
|
|
CreationDate: creationDate,
|
|
|
|
Roles: member.Roles,
|
|
|
|
Sequence: member.Sequence,
|
|
|
|
UserId: member.UserID,
|
|
|
|
UserName: member.UserName,
|
|
|
|
Email: member.Email,
|
|
|
|
FirstName: member.FirstName,
|
|
|
|
LastName: member.LastName,
|
2020-07-22 12:21:41 +00:00
|
|
|
DisplayName: member.DisplayName,
|
2020-06-25 06:12:29 +00:00
|
|
|
}
|
|
|
|
}
|