2020-03-24 10:14:39 +01:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-15 16:50:09 +02:00
|
|
|
|
2020-05-12 06:30:53 +02:00
|
|
|
"github.com/caos/zitadel/internal/api"
|
|
|
|
grpc_util "github.com/caos/zitadel/internal/api/grpc"
|
2020-03-24 10:14:39 +01:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
|
|
)
|
|
|
|
|
2020-06-17 07:25:04 +02:00
|
|
|
func (s *Server) GetUserByID(ctx context.Context, id *UserID) (*UserView, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
user, err := s.user.UserByID(ctx, id.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-17 07:25:04 +02:00
|
|
|
return userViewFromModel(user), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:09:30 +02:00
|
|
|
func (s *Server) GetUserByEmailGlobal(ctx context.Context, email *Email) (*UserView, error) {
|
2020-05-12 06:30:53 +02:00
|
|
|
user, err := s.user.GetGlobalUserByEmail(ctx, email.Email)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userViewFromModel(user), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-12 06:30:53 +02:00
|
|
|
func (s *Server) SearchUsers(ctx context.Context, in *UserSearchRequest) (*UserSearchResponse, error) {
|
|
|
|
request := userSearchRequestsToModel(in)
|
|
|
|
orgID := grpc_util.GetHeader(ctx, api.ZitadelOrgID)
|
|
|
|
request.AppendMyOrgQuery(orgID)
|
|
|
|
response, err := s.user.SearchUsers(ctx, request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userSearchResponseFromModel(response), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) UserChanges(ctx context.Context, changesRequest *ChangeRequest) (*Changes, error) {
|
2020-06-25 11:25:38 +02:00
|
|
|
response, err := s.user.UserChanges(ctx, changesRequest.Id, changesRequest.SequenceOffset, changesRequest.Limit, changesRequest.Asc)
|
2020-06-15 16:50:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userChangesToResponse(response, changesRequest.GetSequenceOffset(), changesRequest.GetLimit()), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) IsUserUnique(ctx context.Context, request *UniqueUserRequest) (*UniqueUserResponse, error) {
|
2020-05-12 06:30:53 +02:00
|
|
|
unique, err := s.user.IsUserUnique(ctx, request.UserName, request.Email)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &UniqueUserResponse{IsUnique: unique}, nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func (s *Server) CreateUser(ctx context.Context, in *CreateUserRequest) (*User, error) {
|
|
|
|
user, err := s.user.CreateUser(ctx, userCreateToModel(in))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userFromModel(user), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func (s *Server) DeactivateUser(ctx context.Context, in *UserID) (*User, error) {
|
|
|
|
user, err := s.user.DeactivateUser(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userFromModel(user), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func (s *Server) ReactivateUser(ctx context.Context, in *UserID) (*User, error) {
|
|
|
|
user, err := s.user.ReactivateUser(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userFromModel(user), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func (s *Server) LockUser(ctx context.Context, in *UserID) (*User, error) {
|
|
|
|
user, err := s.user.LockUser(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userFromModel(user), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func (s *Server) UnlockUser(ctx context.Context, in *UserID) (*User, error) {
|
|
|
|
user, err := s.user.UnlockUser(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userFromModel(user), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func (s *Server) DeleteUser(ctx context.Context, in *UserID) (*empty.Empty, error) {
|
2020-03-24 10:14:39 +01:00
|
|
|
return nil, errors.ThrowUnimplemented(nil, "GRPC-as4fg", "Not implemented")
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:25:04 +02:00
|
|
|
func (s *Server) GetUserProfile(ctx context.Context, in *UserID) (*UserProfileView, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
profile, err := s.user.ProfileByID(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-17 07:25:04 +02:00
|
|
|
return profileViewFromModel(profile), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) UpdateUserProfile(ctx context.Context, request *UpdateUserProfileRequest) (*UserProfile, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
profile, err := s.user.ChangeProfile(ctx, updateProfileToModel(request))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return profileFromModel(profile), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-06-17 07:25:04 +02:00
|
|
|
func (s *Server) GetUserEmail(ctx context.Context, in *UserID) (*UserEmailView, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
email, err := s.user.EmailByID(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-17 07:25:04 +02:00
|
|
|
return emailViewFromModel(email), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ChangeUserEmail(ctx context.Context, request *UpdateUserEmailRequest) (*UserEmail, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
email, err := s.user.ChangeEmail(ctx, updateEmailToModel(request))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return emailFromModel(email), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func (s *Server) ResendEmailVerificationMail(ctx context.Context, in *UserID) (*empty.Empty, error) {
|
|
|
|
err := s.user.CreateEmailVerificationCode(ctx, in.Id)
|
|
|
|
return &empty.Empty{}, err
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-06-17 07:25:04 +02:00
|
|
|
func (s *Server) GetUserPhone(ctx context.Context, in *UserID) (*UserPhoneView, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
phone, err := s.user.PhoneByID(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-17 07:25:04 +02:00
|
|
|
return phoneViewFromModel(phone), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ChangeUserPhone(ctx context.Context, request *UpdateUserPhoneRequest) (*UserPhone, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
phone, err := s.user.ChangePhone(ctx, updatePhoneToModel(request))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return phoneFromModel(phone), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func (s *Server) ResendPhoneVerificationCode(ctx context.Context, in *UserID) (*empty.Empty, error) {
|
|
|
|
err := s.user.CreatePhoneVerificationCode(ctx, in.Id)
|
|
|
|
return &empty.Empty{}, err
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
2020-06-17 07:25:04 +02:00
|
|
|
func (s *Server) GetUserAddress(ctx context.Context, in *UserID) (*UserAddressView, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
address, err := s.user.AddressByID(ctx, in.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-17 07:25:04 +02:00
|
|
|
return addressViewFromModel(address), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) UpdateUserAddress(ctx context.Context, request *UpdateUserAddressRequest) (*UserAddress, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
address, err := s.user.ChangeAddress(ctx, updateAddressToModel(request))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return addressFromModel(address), nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SendSetPasswordNotification(ctx context.Context, request *SetPasswordNotificationRequest) (*empty.Empty, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
err := s.user.RequestSetPassword(ctx, request.Id, notifyTypeToModel(request.Type))
|
|
|
|
return &empty.Empty{}, err
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SetInitialPassword(ctx context.Context, request *PasswordRequest) (*empty.Empty, error) {
|
2020-05-11 10:16:27 +02:00
|
|
|
_, err := s.user.SetOneTimePassword(ctx, passwordRequestToModel(request))
|
|
|
|
return &empty.Empty{}, err
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetUserMfas(ctx context.Context, userID *UserID) (*MultiFactors, error) {
|
2020-05-12 06:30:53 +02:00
|
|
|
mfas, err := s.user.UserMfas(ctx, userID.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &MultiFactors{Mfas: mfasFromModel(mfas)}, nil
|
2020-03-24 10:14:39 +01:00
|
|
|
}
|