2023-04-25 07:02:29 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2024-07-26 20:39:55 +00:00
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/object/v2"
|
|
|
|
"github.com/zitadel/zitadel/pkg/grpc/user/v2"
|
2023-04-25 07:02:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) SetEmail(ctx context.Context, req *user.SetEmailRequest) (resp *user.SetEmailResponse, err error) {
|
|
|
|
var email *domain.Email
|
|
|
|
|
|
|
|
switch v := req.GetVerification().(type) {
|
|
|
|
case *user.SetEmailRequest_SendCode:
|
2024-02-14 07:22:55 +00:00
|
|
|
email, err = s.command.ChangeUserEmailURLTemplate(ctx, req.GetUserId(), req.GetEmail(), s.userCodeAlg, v.SendCode.GetUrlTemplate())
|
2023-04-25 07:02:29 +00:00
|
|
|
case *user.SetEmailRequest_ReturnCode:
|
2024-02-14 07:22:55 +00:00
|
|
|
email, err = s.command.ChangeUserEmailReturnCode(ctx, req.GetUserId(), req.GetEmail(), s.userCodeAlg)
|
2023-04-25 07:02:29 +00:00
|
|
|
case *user.SetEmailRequest_IsVerified:
|
2024-02-14 07:22:55 +00:00
|
|
|
email, err = s.command.ChangeUserEmailVerified(ctx, req.GetUserId(), req.GetEmail())
|
2023-04-25 07:02:29 +00:00
|
|
|
case nil:
|
2024-02-14 07:22:55 +00:00
|
|
|
email, err = s.command.ChangeUserEmail(ctx, req.GetUserId(), req.GetEmail(), s.userCodeAlg)
|
2023-04-25 07:02:29 +00:00
|
|
|
default:
|
2023-12-08 14:30:55 +00:00
|
|
|
err = zerrors.ThrowUnimplementedf(nil, "USERv2-Ahng0", "verification oneOf %T in method SetEmail not implemented", v)
|
2023-04-25 07:02:29 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user.SetEmailResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
Sequence: email.Sequence,
|
|
|
|
ChangeDate: timestamppb.New(email.ChangeDate),
|
|
|
|
ResourceOwner: email.ResourceOwner,
|
|
|
|
},
|
|
|
|
VerificationCode: email.PlainCode,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-02-14 07:22:55 +00:00
|
|
|
func (s *Server) ResendEmailCode(ctx context.Context, req *user.ResendEmailCodeRequest) (resp *user.ResendEmailCodeResponse, err error) {
|
|
|
|
var email *domain.Email
|
|
|
|
|
|
|
|
switch v := req.GetVerification().(type) {
|
|
|
|
case *user.ResendEmailCodeRequest_SendCode:
|
|
|
|
email, err = s.command.ResendUserEmailCodeURLTemplate(ctx, req.GetUserId(), s.userCodeAlg, v.SendCode.GetUrlTemplate())
|
|
|
|
case *user.ResendEmailCodeRequest_ReturnCode:
|
|
|
|
email, err = s.command.ResendUserEmailReturnCode(ctx, req.GetUserId(), s.userCodeAlg)
|
|
|
|
case nil:
|
|
|
|
email, err = s.command.ResendUserEmailCode(ctx, req.GetUserId(), s.userCodeAlg)
|
|
|
|
default:
|
|
|
|
err = zerrors.ThrowUnimplementedf(nil, "USERv2-faj0l0nj5x", "verification oneOf %T in method ResendEmailCode not implemented", v)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user.ResendEmailCodeResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
Sequence: email.Sequence,
|
|
|
|
ChangeDate: timestamppb.New(email.ChangeDate),
|
|
|
|
ResourceOwner: email.ResourceOwner,
|
|
|
|
},
|
|
|
|
VerificationCode: email.PlainCode,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 07:02:29 +00:00
|
|
|
func (s *Server) VerifyEmail(ctx context.Context, req *user.VerifyEmailRequest) (*user.VerifyEmailResponse, error) {
|
|
|
|
details, err := s.command.VerifyUserEmail(ctx,
|
|
|
|
req.GetUserId(),
|
|
|
|
req.GetVerificationCode(),
|
|
|
|
s.userCodeAlg,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &user.VerifyEmailResponse{
|
|
|
|
Details: &object.Details{
|
|
|
|
Sequence: details.Sequence,
|
|
|
|
ChangeDate: timestamppb.New(details.EventDate),
|
|
|
|
ResourceOwner: details.ResourceOwner,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|