2020-07-08 11:56:37 +00:00
|
|
|
package admin
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/logging"
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2020-05-13 12:22:29 +00:00
|
|
|
admin_model "github.com/caos/zitadel/internal/admin/model"
|
2020-06-16 09:40:18 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/model"
|
2020-05-13 12:22:29 +00:00
|
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
|
|
usr_model "github.com/caos/zitadel/internal/user/model"
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/pkg/grpc/admin"
|
2020-05-13 12:22:29 +00:00
|
|
|
)
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func setUpRequestToModel(setUp *admin.OrgSetUpRequest) *admin_model.SetupOrg {
|
2020-05-13 12:22:29 +00:00
|
|
|
return &admin_model.SetupOrg{
|
|
|
|
Org: orgCreateRequestToModel(setUp.Org),
|
|
|
|
User: userCreateRequestToModel(setUp.User),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgCreateRequestToModel(org *admin.CreateOrgRequest) *org_model.Org {
|
2020-07-07 10:57:45 +00:00
|
|
|
o := &org_model.Org{
|
|
|
|
Domains: []*org_model.OrgDomain{},
|
2020-06-16 09:40:18 +00:00
|
|
|
Name: org.Name,
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
2020-07-07 10:57:45 +00:00
|
|
|
if org.Domain != "" {
|
|
|
|
o.Domains = append(o.Domains, &org_model.OrgDomain{Domain: org.Domain})
|
|
|
|
}
|
|
|
|
|
|
|
|
return o
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userCreateRequestToModel(user *admin.CreateUserRequest) *usr_model.User {
|
2020-05-13 12:22:29 +00:00
|
|
|
preferredLanguage, err := language.Parse(user.PreferredLanguage)
|
|
|
|
logging.Log("GRPC-30hwz").OnError(err).Debug("unable to parse language")
|
2020-06-26 13:42:34 +00:00
|
|
|
result := &usr_model.User{
|
2020-05-13 12:22:29 +00:00
|
|
|
Profile: &usr_model.Profile{
|
|
|
|
UserName: user.UserName,
|
|
|
|
FirstName: user.FirstName,
|
|
|
|
LastName: user.LastName,
|
|
|
|
NickName: user.NickName,
|
|
|
|
PreferredLanguage: preferredLanguage,
|
|
|
|
Gender: genderToModel(user.Gender),
|
|
|
|
},
|
|
|
|
Password: &usr_model.Password{
|
|
|
|
SecretString: user.Password,
|
|
|
|
},
|
|
|
|
Email: &usr_model.Email{
|
|
|
|
EmailAddress: user.Email,
|
|
|
|
IsEmailVerified: user.IsEmailVerified,
|
|
|
|
},
|
|
|
|
Address: &usr_model.Address{
|
|
|
|
Country: user.Country,
|
|
|
|
Locality: user.Locality,
|
|
|
|
PostalCode: user.PostalCode,
|
|
|
|
Region: user.Region,
|
|
|
|
StreetAddress: user.StreetAddress,
|
|
|
|
},
|
|
|
|
}
|
2020-06-26 13:42:34 +00:00
|
|
|
if user.Phone != "" {
|
|
|
|
result.Phone = &usr_model.Phone{PhoneNumber: user.Phone, IsPhoneVerified: user.IsPhoneVerified}
|
|
|
|
}
|
|
|
|
return result
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func setUpOrgResponseFromModel(setUp *admin_model.SetupOrg) *admin.OrgSetUpResponse {
|
|
|
|
return &admin.OrgSetUpResponse{
|
2020-05-13 12:22:29 +00:00
|
|
|
Org: orgFromModel(setUp.Org),
|
|
|
|
User: userFromModel(setUp.User),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:24:36 +00:00
|
|
|
func orgSearchResponseFromModel(request *org_model.OrgSearchResult) *admin.OrgSearchResponse {
|
|
|
|
timestamp, err := ptypes.TimestampProto(request.Timestamp)
|
|
|
|
logging.Log("GRPC-shu7s").OnError(err).Debug("unable to get timestamp from time")
|
|
|
|
return &admin.OrgSearchResponse{
|
|
|
|
Result: orgViewsFromModel(request.Result),
|
|
|
|
Limit: request.Limit,
|
|
|
|
Offset: request.Offset,
|
|
|
|
TotalResult: request.TotalResult,
|
|
|
|
ProcessedSequence: request.Sequence,
|
|
|
|
ViewTimestamp: timestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgViewsFromModel(orgs []*org_model.OrgView) []*admin.Org {
|
|
|
|
result := make([]*admin.Org, len(orgs))
|
2020-05-13 12:22:29 +00:00
|
|
|
for i, org := range orgs {
|
2020-05-26 14:46:16 +00:00
|
|
|
result[i] = orgViewFromModel(org)
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgFromModel(org *org_model.Org) *admin.Org {
|
2020-05-13 12:22:29 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(org.CreationDate)
|
|
|
|
logging.Log("GRPC-GTHsZ").OnError(err).Debug("unable to get timestamp from time")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(org.ChangeDate)
|
|
|
|
logging.Log("GRPC-dVnoj").OnError(err).Debug("unable to get timestamp from time")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &admin.Org{
|
2020-05-13 12:22:29 +00:00
|
|
|
ChangeDate: changeDate,
|
|
|
|
CreationDate: creationDate,
|
|
|
|
Id: org.AggregateID,
|
|
|
|
Name: org.Name,
|
|
|
|
State: orgStateFromModel(org.State),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgViewFromModel(org *org_model.OrgView) *admin.Org {
|
2020-05-26 14:46:16 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(org.CreationDate)
|
|
|
|
logging.Log("GRPC-GTHsZ").OnError(err).Debug("unable to get timestamp from time")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(org.ChangeDate)
|
|
|
|
logging.Log("GRPC-dVnoj").OnError(err).Debug("unable to get timestamp from time")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &admin.Org{
|
2020-05-26 14:46:16 +00:00
|
|
|
ChangeDate: changeDate,
|
|
|
|
CreationDate: creationDate,
|
|
|
|
Id: org.ID,
|
|
|
|
Name: org.Name,
|
|
|
|
State: orgStateFromModel(org.State),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userFromModel(user *usr_model.User) *admin.User {
|
2020-05-13 12:22:29 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(user.CreationDate)
|
|
|
|
logging.Log("GRPC-8duwe").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(user.ChangeDate)
|
|
|
|
logging.Log("GRPC-ckoe3d").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
converted := &admin.User{
|
2020-05-13 12:22:29 +00:00
|
|
|
Id: user.AggregateID,
|
|
|
|
State: userStateFromModel(user.State),
|
|
|
|
CreationDate: creationDate,
|
|
|
|
ChangeDate: changeDate,
|
|
|
|
Sequence: user.Sequence,
|
|
|
|
UserName: user.UserName,
|
|
|
|
FirstName: user.FirstName,
|
|
|
|
LastName: user.LastName,
|
|
|
|
DisplayName: user.DisplayName,
|
|
|
|
NickName: user.NickName,
|
|
|
|
PreferredLanguage: user.PreferredLanguage.String(),
|
|
|
|
Gender: genderFromModel(user.Gender),
|
|
|
|
}
|
|
|
|
if user.Email != nil {
|
|
|
|
converted.Email = user.EmailAddress
|
|
|
|
converted.IsEmailVerified = user.IsEmailVerified
|
|
|
|
}
|
|
|
|
if user.Phone != nil {
|
|
|
|
converted.Phone = user.PhoneNumber
|
|
|
|
converted.IsPhoneVerified = user.IsPhoneVerified
|
|
|
|
}
|
|
|
|
if user.Address != nil {
|
|
|
|
converted.Country = user.Country
|
|
|
|
converted.Locality = user.Locality
|
|
|
|
converted.PostalCode = user.PostalCode
|
|
|
|
converted.Region = user.Region
|
|
|
|
converted.StreetAddress = user.StreetAddress
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgStateFromModel(state org_model.OrgState) admin.OrgState {
|
2020-05-13 12:22:29 +00:00
|
|
|
switch state {
|
2020-06-23 12:47:47 +00:00
|
|
|
case org_model.OrgStateActive:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.OrgState_ORGSTATE_ACTIVE
|
2020-06-23 12:47:47 +00:00
|
|
|
case org_model.OrgStateInactive:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.OrgState_ORGSTATE_INACTIVE
|
2020-05-13 12:22:29 +00:00
|
|
|
default:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.OrgState_ORGSTATE_UNSPECIFIED
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func genderFromModel(gender usr_model.Gender) admin.Gender {
|
2020-05-13 12:22:29 +00:00
|
|
|
switch gender {
|
2020-06-23 12:47:47 +00:00
|
|
|
case usr_model.GenderFemale:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.Gender_GENDER_FEMALE
|
2020-06-23 12:47:47 +00:00
|
|
|
case usr_model.GenderMale:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.Gender_GENDER_MALE
|
2020-06-23 12:47:47 +00:00
|
|
|
case usr_model.GenderDiverse:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.Gender_GENDER_DIVERSE
|
2020-05-13 12:22:29 +00:00
|
|
|
default:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.Gender_GENDER_UNSPECIFIED
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func genderToModel(gender admin.Gender) usr_model.Gender {
|
2020-05-13 12:22:29 +00:00
|
|
|
switch gender {
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.Gender_GENDER_FEMALE:
|
2020-06-23 12:47:47 +00:00
|
|
|
return usr_model.GenderFemale
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.Gender_GENDER_MALE:
|
2020-06-23 12:47:47 +00:00
|
|
|
return usr_model.GenderMale
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.Gender_GENDER_DIVERSE:
|
2020-06-23 12:47:47 +00:00
|
|
|
return usr_model.GenderDiverse
|
2020-05-13 12:22:29 +00:00
|
|
|
default:
|
2020-06-23 12:47:47 +00:00
|
|
|
return usr_model.GenderUnspecified
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func userStateFromModel(state usr_model.UserState) admin.UserState {
|
2020-05-13 12:22:29 +00:00
|
|
|
switch state {
|
2020-06-23 12:47:47 +00:00
|
|
|
case usr_model.UserStateActive:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.UserState_USERSTATE_ACTIVE
|
2020-06-23 12:47:47 +00:00
|
|
|
case usr_model.UserStateInactive:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.UserState_USERSTATE_INACTIVE
|
2020-06-23 12:47:47 +00:00
|
|
|
case usr_model.UserStateLocked:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.UserState_USERSTATE_LOCKED
|
2020-05-13 12:22:29 +00:00
|
|
|
default:
|
2020-07-08 11:56:37 +00:00
|
|
|
return admin.UserState_USERSTATE_UNSPECIFIED
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgSearchRequestToModel(req *admin.OrgSearchRequest) *org_model.OrgSearchRequest {
|
2020-06-16 09:40:18 +00:00
|
|
|
return &org_model.OrgSearchRequest{
|
|
|
|
Limit: req.Limit,
|
|
|
|
Asc: req.Asc,
|
|
|
|
Offset: req.Offset,
|
|
|
|
Queries: orgQueriesToModel(req.Queries),
|
|
|
|
SortingColumn: orgQueryKeyToModel(req.SortingColumn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgQueriesToModel(queries []*admin.OrgSearchQuery) []*org_model.OrgSearchQuery {
|
2020-06-16 09:40:18 +00:00
|
|
|
modelQueries := make([]*org_model.OrgSearchQuery, len(queries))
|
|
|
|
|
|
|
|
for i, query := range queries {
|
|
|
|
modelQueries[i] = orgQueryToModel(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
return modelQueries
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgQueryToModel(query *admin.OrgSearchQuery) *org_model.OrgSearchQuery {
|
2020-06-16 09:40:18 +00:00
|
|
|
return &org_model.OrgSearchQuery{
|
|
|
|
Key: orgQueryKeyToModel(query.Key),
|
|
|
|
Value: query.Value,
|
|
|
|
Method: orgQueryMethodToModel(query.Method),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgQueryKeyToModel(key admin.OrgSearchKey) org_model.OrgSearchKey {
|
2020-06-16 09:40:18 +00:00
|
|
|
switch key {
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.OrgSearchKey_ORGSEARCHKEY_DOMAIN:
|
2020-06-23 12:47:47 +00:00
|
|
|
return org_model.OrgSearchKeyOrgDomain
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.OrgSearchKey_ORGSEARCHKEY_ORG_NAME:
|
2020-06-23 12:47:47 +00:00
|
|
|
return org_model.OrgSearchKeyOrgName
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.OrgSearchKey_ORGSEARCHKEY_STATE:
|
2020-06-23 12:47:47 +00:00
|
|
|
return org_model.OrgSearchKeyState
|
2020-06-16 09:40:18 +00:00
|
|
|
default:
|
2020-06-23 12:47:47 +00:00
|
|
|
return org_model.OrgSearchKeyUnspecified
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgQueryMethodToModel(method admin.OrgSearchMethod) model.SearchMethod {
|
2020-06-16 09:40:18 +00:00
|
|
|
switch method {
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.OrgSearchMethod_ORGSEARCHMETHOD_CONTAINS:
|
2020-06-23 12:47:47 +00:00
|
|
|
return model.SearchMethodContains
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.OrgSearchMethod_ORGSEARCHMETHOD_EQUALS:
|
2020-06-23 12:47:47 +00:00
|
|
|
return model.SearchMethodEquals
|
2020-07-08 11:56:37 +00:00
|
|
|
case admin.OrgSearchMethod_ORGSEARCHMETHOD_STARTS_WITH:
|
2020-06-23 12:47:47 +00:00
|
|
|
return model.SearchMethodStartsWith
|
2020-06-16 09:40:18 +00:00
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgIamPolicyFromModel(policy *org_model.OrgIamPolicy) *admin.OrgIamPolicy {
|
2020-06-16 09:40:18 +00:00
|
|
|
creationDate, err := ptypes.TimestampProto(policy.CreationDate)
|
|
|
|
logging.Log("GRPC-ush36").OnError(err).Debug("unable to get timestamp from time")
|
|
|
|
|
|
|
|
changeDate, err := ptypes.TimestampProto(policy.ChangeDate)
|
|
|
|
logging.Log("GRPC-Ps9fW").OnError(err).Debug("unable to get timestamp from time")
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
return &admin.OrgIamPolicy{
|
2020-06-16 09:40:18 +00:00
|
|
|
OrgId: policy.AggregateID,
|
|
|
|
Description: policy.Description,
|
|
|
|
UserLoginMustBeDomain: policy.UserLoginMustBeDomain,
|
|
|
|
Default: policy.Default,
|
|
|
|
CreationDate: creationDate,
|
|
|
|
ChangeDate: changeDate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
func orgIamPolicyRequestToModel(policy *admin.OrgIamPolicyRequest) *org_model.OrgIamPolicy {
|
2020-06-16 09:40:18 +00:00
|
|
|
return &org_model.OrgIamPolicy{
|
|
|
|
ObjectRoot: models.ObjectRoot{
|
|
|
|
AggregateID: policy.OrgId,
|
|
|
|
},
|
|
|
|
Description: policy.Description,
|
|
|
|
UserLoginMustBeDomain: policy.UserLoginMustBeDomain,
|
|
|
|
}
|
|
|
|
}
|