Rename identity profile on membership profile
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
@@ -125,14 +125,9 @@ func (s AccountService) ChangeEmail(ctx context.Context, identityID gid.GID, req
|
||||
return fmt.Errorf("cannot update identity: %w", err)
|
||||
}
|
||||
|
||||
profile := &coredata.IdentityProfile{}
|
||||
if err := profile.LoadDefaultByIdentityID(ctx, tx, identityID); err != nil {
|
||||
return fmt.Errorf("cannot load default profile: %w", err)
|
||||
}
|
||||
|
||||
subject, textBody, htmlBody, err := emails.RenderConfirmEmail(
|
||||
s.baseURL,
|
||||
profile.FullName,
|
||||
identity.FullName,
|
||||
confirmationUrl,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -140,7 +135,7 @@ func (s AccountService) ChangeEmail(ctx context.Context, identityID gid.GID, req
|
||||
}
|
||||
|
||||
confirmationEmail := coredata.NewEmail(
|
||||
profile.FullName,
|
||||
identity.FullName,
|
||||
identity.EmailAddress,
|
||||
subject,
|
||||
textBody,
|
||||
@@ -720,10 +715,10 @@ func (s AccountService) ListOrganizations(ctx context.Context, identityID gid.GI
|
||||
return organizations, nil
|
||||
}
|
||||
|
||||
func (s AccountService) GetProfileForMembership(ctx context.Context, membershipID gid.GID) (*coredata.IdentityProfile, error) {
|
||||
func (s AccountService) GetProfileForMembership(ctx context.Context, membershipID gid.GID) (*coredata.MembershipProfile, error) {
|
||||
var (
|
||||
scope = coredata.NewScopeFromObjectID(membershipID)
|
||||
profile = &coredata.IdentityProfile{}
|
||||
profile = &coredata.MembershipProfile{}
|
||||
)
|
||||
|
||||
err := s.pg.WithConn(
|
||||
@@ -758,118 +753,3 @@ func (s AccountService) GetProfileForMembership(ctx context.Context, membershipI
|
||||
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
func (s AccountService) GetDefaultProfile(ctx context.Context, identityID gid.GID) (*coredata.IdentityProfile, error) {
|
||||
var profile = &coredata.IdentityProfile{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
err := profile.LoadDefaultByIdentityID(ctx, conn, identityID)
|
||||
if err != nil {
|
||||
if err == coredata.ErrResourceNotFound {
|
||||
return NewProfileNotFoundError(identityID)
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load default profile: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
type UpdateIdentityProfileRequest struct {
|
||||
MembershipID gid.GID
|
||||
FullName *string
|
||||
}
|
||||
|
||||
func (s AccountService) UpdateIdentityProfile(
|
||||
ctx context.Context,
|
||||
identityID gid.GID,
|
||||
req *UpdateIdentityProfileRequest,
|
||||
) (*coredata.IdentityProfile, error) {
|
||||
// var (
|
||||
// scope = coredata.NewScopeFromObjectID(req.MembershipID)
|
||||
// profile = &coredata.IdentityProfile{}
|
||||
// )
|
||||
|
||||
// err := s.pg.WithTx(
|
||||
// ctx,
|
||||
// func(tx pg.Conn) error {
|
||||
// // First verify the membership belongs to the identity
|
||||
// membership := &coredata.Membership{}
|
||||
// err := membership.LoadByID(ctx, tx, scope, req.MembershipID)
|
||||
// if err != nil {
|
||||
// if err == coredata.ErrResourceNotFound {
|
||||
// return NewMembershipNotFoundError(req.MembershipID)
|
||||
// }
|
||||
|
||||
// return fmt.Errorf("cannot load membership: %w", err)
|
||||
// }
|
||||
|
||||
// if membership.IdentityID != identityID {
|
||||
// return NewMembershipNotFoundError(req.MembershipID)
|
||||
// }
|
||||
|
||||
// // Try to load existing membership profile
|
||||
// err = profile.LoadByMembershipID(ctx, tx, scope, req.MembershipID)
|
||||
// if err != nil && err != coredata.ErrResourceNotFound {
|
||||
// return fmt.Errorf("cannot load identity profile: %w", err)
|
||||
// }
|
||||
|
||||
// now := time.Now()
|
||||
|
||||
// if err == coredata.ErrResourceNotFound {
|
||||
// // Create new membership profile, optionally inheriting from default
|
||||
// tenantID := req.MembershipID.TenantID()
|
||||
// membershipID := req.MembershipID
|
||||
|
||||
// // Try to get default profile to inherit FullName
|
||||
// defaultProfile := &coredata.IdentityProfile{}
|
||||
// defaultFullName := ""
|
||||
// if loadErr := defaultProfile.LoadDefaultByIdentityID(ctx, tx, identityID); loadErr == nil {
|
||||
// defaultFullName = defaultProfile.FullName
|
||||
// }
|
||||
|
||||
// tenantIDStr := tenantID.String()
|
||||
// profile = &coredata.IdentityProfile{
|
||||
// ID: gid.New(tenantID, coredata.IdentityProfileEntityType),
|
||||
// TenantID: &tenantIDStr,
|
||||
// IdentityID: identityID,
|
||||
// MembershipID: &membershipID,
|
||||
// FullName: defaultFullName,
|
||||
// CreatedAt: now,
|
||||
// UpdatedAt: now,
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Apply updates
|
||||
// if req.FullName != nil {
|
||||
// profile.FullName = *req.FullName
|
||||
// }
|
||||
// profile.UpdatedAt = now
|
||||
|
||||
// // Upsert the membership profile
|
||||
// err = profile.UpsertMembership(ctx, tx)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("cannot upsert identity profile: %w", err)
|
||||
// }
|
||||
|
||||
// return nil
|
||||
// },
|
||||
// )
|
||||
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// return profile, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -156,6 +156,7 @@ func (s *AuthService) CreateIdentityFromInvitation(
|
||||
identity = &coredata.Identity{
|
||||
ID: gid.New(gid.NilTenant, coredata.IdentityEntityType),
|
||||
EmailAddress: invitation.Email,
|
||||
FullName: invitation.FullName,
|
||||
HashedPassword: hashedPassword,
|
||||
EmailAddressVerified: true,
|
||||
CreatedAt: now,
|
||||
@@ -171,19 +172,6 @@ func (s *AuthService) CreateIdentityFromInvitation(
|
||||
return fmt.Errorf("cannot insert identity: %w", err)
|
||||
}
|
||||
|
||||
defaultProfile := &coredata.IdentityProfile{
|
||||
ID: gid.New(gid.NilTenant, coredata.IdentityProfileEntityType),
|
||||
IdentityID: identity.ID,
|
||||
FullName: invitation.FullName,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
err = defaultProfile.Insert(ctx, tx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert default profile: %w", err)
|
||||
}
|
||||
|
||||
session = coredata.NewRootSession(identity.ID, coredata.AuthMethodPassword, s.sessionDuration)
|
||||
err = session.Insert(ctx, tx)
|
||||
if err != nil {
|
||||
@@ -285,14 +273,9 @@ func (s AuthService) SendPasswordResetInstructionByEmail(
|
||||
return fmt.Errorf("cannot load identity: %w", err)
|
||||
}
|
||||
|
||||
profile := &coredata.IdentityProfile{}
|
||||
if err := profile.LoadDefaultByIdentityID(ctx, tx, identity.ID); err != nil {
|
||||
return fmt.Errorf("cannot load default profile: %w", err)
|
||||
}
|
||||
|
||||
subject, textBody, htmlBody, err := emails.RenderPasswordReset(
|
||||
s.baseURL,
|
||||
profile.FullName,
|
||||
identity.FullName,
|
||||
resetPasswordUrl,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -300,7 +283,7 @@ func (s AuthService) SendPasswordResetInstructionByEmail(
|
||||
}
|
||||
|
||||
passwordResetEmail := coredata.NewEmail(
|
||||
profile.FullName,
|
||||
identity.FullName,
|
||||
identity.EmailAddress,
|
||||
subject,
|
||||
textBody,
|
||||
@@ -340,20 +323,13 @@ func (s AuthService) CreateIdentityWithPassword(
|
||||
identity = &coredata.Identity{
|
||||
ID: gid.New(gid.NilTenant, coredata.IdentityEntityType),
|
||||
EmailAddress: req.Email,
|
||||
FullName: req.FullName,
|
||||
HashedPassword: hashedPassword,
|
||||
EmailAddressVerified: false,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
defaultProfile = &coredata.IdentityProfile{
|
||||
ID: gid.New(gid.NilTenant, coredata.IdentityProfileEntityType),
|
||||
IdentityID: identity.ID,
|
||||
FullName: req.FullName,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
session = coredata.NewRootSession(identity.ID, coredata.AuthMethodPassword, 24*time.Hour*7)
|
||||
)
|
||||
|
||||
@@ -409,11 +385,6 @@ func (s AuthService) CreateIdentityWithPassword(
|
||||
return fmt.Errorf("cannot insert identity: %w", err)
|
||||
}
|
||||
|
||||
err = defaultProfile.Insert(ctx, tx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert default profile: %w", err)
|
||||
}
|
||||
|
||||
if err := confirmationEmail.Insert(ctx, tx); err != nil {
|
||||
return fmt.Errorf("cannot insert email: %w", err)
|
||||
}
|
||||
|
||||
@@ -258,6 +258,7 @@ func (s *Service) HandleAssertion(
|
||||
*identity = coredata.Identity{
|
||||
ID: gid.New(gid.NilTenant, coredata.IdentityEntityType),
|
||||
EmailAddress: email,
|
||||
FullName: fullname,
|
||||
HashedPassword: nil,
|
||||
EmailAddressVerified: true,
|
||||
CreatedAt: now,
|
||||
@@ -268,24 +269,12 @@ func (s *Service) HandleAssertion(
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert identity: %w", err)
|
||||
}
|
||||
|
||||
defaultProfile := &coredata.IdentityProfile{
|
||||
ID: gid.New(gid.NilTenant, coredata.IdentityProfileEntityType),
|
||||
IdentityID: identity.ID,
|
||||
FullName: fullname,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
err = defaultProfile.Insert(ctx, tx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert default profile: %w", err)
|
||||
}
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("cannot load identity: %w", err)
|
||||
} else {
|
||||
identity.SAMLSubject = &assertion.Subject.NameID.Value
|
||||
identity.EmailAddress = email
|
||||
identity.FullName = fullname
|
||||
identity.EmailAddressVerified = true
|
||||
identity.UpdatedAt = now
|
||||
|
||||
@@ -316,10 +305,9 @@ func (s *Service) HandleAssertion(
|
||||
return fmt.Errorf("cannot insert membership: %w", err)
|
||||
}
|
||||
|
||||
membershipProfile := &coredata.IdentityProfile{
|
||||
ID: gid.New(membership.ID.TenantID(), coredata.IdentityProfileEntityType),
|
||||
IdentityID: identity.ID,
|
||||
MembershipID: &membership.ID,
|
||||
membershipProfile := &coredata.MembershipProfile{
|
||||
ID: gid.New(membership.ID.TenantID(), coredata.MembershipProfileEntityType),
|
||||
MembershipID: membership.ID,
|
||||
FullName: fullname,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
@@ -341,7 +329,7 @@ func (s *Service) HandleAssertion(
|
||||
}
|
||||
}
|
||||
|
||||
memberProfile := &coredata.IdentityProfile{}
|
||||
memberProfile := &coredata.MembershipProfile{}
|
||||
err = memberProfile.LoadByMembershipID(ctx, tx, coredata.NewNoScope(), membership.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load membership profile: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user