2024-09-23 20:05:04 +02:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-09-24 20:42:20 +02:00
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
|
2024-09-23 20:05:04 +02:00
|
|
|
resource_object "github.com/zitadel/zitadel/internal/api/grpc/resources/object/v3alpha"
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2024-09-24 20:42:20 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2024-09-23 20:05:04 +02:00
|
|
|
object "github.com/zitadel/zitadel/pkg/grpc/object/v3alpha"
|
|
|
|
user "github.com/zitadel/zitadel/pkg/grpc/resources/user/v3alpha"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) SetPassword(ctx context.Context, req *user.SetPasswordRequest) (_ *user.SetPasswordResponse, err error) {
|
|
|
|
if err := checkUserSchemaEnabled(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
details, err := s.command.SetSchemaUserPassword(ctx, setPasswordRequestToSetSchemaUserPassword(req))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &user.SetPasswordResponse{
|
|
|
|
Details: resource_object.DomainToDetailsPb(details, object.OwnerType_OWNER_TYPE_ORG, details.ResourceOwner),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setPasswordRequestToSetSchemaUserPassword(req *user.SetPasswordRequest) *command.SetSchemaUserPassword {
|
|
|
|
return &command.SetSchemaUserPassword{
|
|
|
|
ResourceOwner: organizationToUpdateResourceOwner(req.Organization),
|
|
|
|
UserID: req.GetId(),
|
|
|
|
Password: req.GetNewPassword().GetPassword(),
|
|
|
|
EncodedPasswordHash: req.GetNewPassword().GetHash(),
|
|
|
|
ChangeRequired: req.GetNewPassword().GetChangeRequired(),
|
2024-09-24 20:42:20 +02:00
|
|
|
VerificationCode: req.GetNewPassword().GetVerificationCode(),
|
|
|
|
CurrentPassword: req.GetNewPassword().GetCurrentPassword(),
|
2024-09-23 20:05:04 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-24 14:26:48 +02:00
|
|
|
|
|
|
|
func (s *Server) RemovePassword(ctx context.Context, req *user.RemovePasswordRequest) (_ *user.RemovePasswordResponse, err error) {
|
|
|
|
if err := checkUserSchemaEnabled(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
details, err := s.command.DeleteSchemaUserPassword(ctx, organizationToUpdateResourceOwner(req.Organization), req.GetId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &user.RemovePasswordResponse{
|
|
|
|
Details: resource_object.DomainToDetailsPb(details, object.OwnerType_OWNER_TYPE_ORG, details.ResourceOwner),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-09-24 20:42:20 +02:00
|
|
|
func (s *Server) RequestPasswordReset(ctx context.Context, req *user.RequestPasswordResetRequest) (_ *user.RequestPasswordResetResponse, err error) {
|
2024-09-24 14:26:48 +02:00
|
|
|
if err := checkUserSchemaEnabled(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-24 20:42:20 +02:00
|
|
|
schemauser := requestPasswordResetRequestToRequestSchemaUserPasswordReset(req)
|
|
|
|
details, err := s.command.RequestSchemaUserPasswordReset(ctx, schemauser)
|
2024-09-24 14:26:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-24 20:42:20 +02:00
|
|
|
return &user.RequestPasswordResetResponse{
|
|
|
|
Details: resource_object.DomainToDetailsPb(details, object.OwnerType_OWNER_TYPE_ORG, details.ResourceOwner),
|
|
|
|
VerificationCode: gu.Ptr(schemauser.PlainCode),
|
2024-09-24 14:26:48 +02:00
|
|
|
}, nil
|
|
|
|
}
|
2024-09-24 20:42:20 +02:00
|
|
|
|
|
|
|
func requestPasswordResetRequestToRequestSchemaUserPasswordReset(req *user.RequestPasswordResetRequest) *command.RequestSchemaUserPasswordReset {
|
|
|
|
var notificationType domain.NotificationType
|
|
|
|
if req.GetSendEmail() != nil {
|
|
|
|
notificationType = domain.NotificationTypeEmail
|
|
|
|
}
|
|
|
|
if req.GetSendSms() != nil {
|
|
|
|
notificationType = domain.NotificationTypeSms
|
|
|
|
}
|
|
|
|
return &command.RequestSchemaUserPasswordReset{
|
|
|
|
ResourceOwner: organizationToUpdateResourceOwner(req.Organization),
|
|
|
|
UserID: req.GetId(),
|
|
|
|
URLTemplate: req.GetSendEmail().GetUrlTemplate(),
|
|
|
|
ReturnCode: req.GetReturnCode() != nil,
|
|
|
|
NotificationType: notificationType,
|
|
|
|
}
|
|
|
|
}
|