Files
hanko/backend/persistence/password_credential_persister.go
bjoern-m 601ffaae92 Introduce Flowpilot - integration (#1532)
This pull request introduces the new Flowpilot system along with several new features and various improvements. The key enhancements include configurable authorization, registration, and profile flows, as well as the ability to enable and disable user identifiers (e.g., email addresses and usernames) and login methods.

---------

Co-authored-by: Frederic Jahn <frederic.jahn@hanko.io>
Co-authored-by: Lennart Fleischmann <lennart.fleischmann@hanko.io>
Co-authored-by: lfleischmann <67686424+lfleischmann@users.noreply.github.com>
Co-authored-by: merlindru <hello@merlindru.com>
2024-08-06 16:07:29 +02:00

74 lines
1.9 KiB
Go

package persistence
import (
"database/sql"
"errors"
"fmt"
"github.com/gobuffalo/pop/v6"
"github.com/gofrs/uuid"
"github.com/teamhanko/hanko/backend/persistence/models"
)
type PasswordCredentialPersister interface {
Create(password models.PasswordCredential) error
GetByUserID(userId uuid.UUID) (*models.PasswordCredential, error)
Update(password models.PasswordCredential) error
Delete(password models.PasswordCredential) error
}
type passwordCredentialPersister struct {
db *pop.Connection
}
func NewPasswordCredentialPersister(db *pop.Connection) PasswordCredentialPersister {
return &passwordCredentialPersister{db: db}
}
func (p *passwordCredentialPersister) Create(password models.PasswordCredential) error {
vErr, err := p.db.ValidateAndCreate(&password)
if err != nil {
return fmt.Errorf("failed to store password credential: %w", err)
}
if vErr != nil && vErr.HasAny() {
return fmt.Errorf("password object validation failed: %w", vErr)
}
return nil
}
func (p *passwordCredentialPersister) GetByUserID(userId uuid.UUID) (*models.PasswordCredential, error) {
pw := models.PasswordCredential{}
query := p.db.Where("user_id = (?)", userId.String())
err := query.First(&pw)
if err != nil && errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("failed to get credential: %w", err)
}
return &pw, nil
}
func (p *passwordCredentialPersister) Update(password models.PasswordCredential) error {
vErr, err := p.db.ValidateAndUpdate(&password)
if err != nil {
return fmt.Errorf("failed to update password: %w", err)
}
if vErr != nil && vErr.HasAny() {
return fmt.Errorf("password object validation failed: %w", vErr)
}
return nil
}
func (p *passwordCredentialPersister) Delete(password models.PasswordCredential) error {
err := p.db.Destroy(&password)
if err != nil {
return fmt.Errorf("failed to delete user: %w", err)
}
return nil
}