user

package
v0.0.0-...-6b846f9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2021 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrZeroUserID                      = errors.New("user id is zero")
	ErrNoInputData                     = errors.New("no input data")
	ErrNilManager                      = errors.New("user manager is nil")
	ErrUserExists                      = errors.New("user already exists")
	ErrNilUser                         = errors.New("user is nil")
	ErrNilUserStore                    = errors.New("user store is nil")
	ErrUsernameTaken                   = errors.New("username is already taken")
	ErrEmptyEmailAddr                  = errors.New("empty email address")
	ErrDuplicateEmailAddr              = errors.New("duplicate email address")
	ErrEmptyPhoneNumber                = errors.New("empty phone number")
	ErrDuplicatePhoneNumber            = errors.New("duplicate phone number")
	ErrEmailTaken                      = errors.New("email is already taken")
	ErrDuplicatePhone                  = errors.New("duplicate phone")
	ErrPhoneNotFound                   = errors.New("phone not found")
	ErrUserNotFound                    = errors.New("user not found")
	ErrEmailNotFound                   = errors.New("email not found")
	ErrUnknownUser                     = errors.New("unknown user")
	ErrInsufficientDataToHashKey       = errors.New("insufficient data to hash a key")
	ErrUserAlreadyConfirmed            = errors.New("user is already confirmed")
	ErrNilPasswordManager              = errors.New("password manager is nil")
	ErrUserPasswordNotEligible         = errors.New("user is not eligible for password assignment")
	ErrDuplicateProfile                = errors.New("duplicate profile")
	ErrProfileNotFound                 = errors.New("profile not found")
	ErrNoParentPolicy                  = errors.New("no parent accesspolicy policy")
	ErrNoPolicyBackup                  = errors.New("no policy backup")
	ErrNoParentGroup                   = errors.New("no parent group")
	ErrZeroGroupID                     = errors.New("role group id is zero")
	ErrZeroRoleID                      = errors.New("role id is zero")
	ErrZeroAssignorID                  = errors.New("assignor id is zero")
	ErrZeroAssigneeID                  = errors.New("assignee id is zero")
	ErrNothingChanged                  = errors.New("nothing has changed")
	ErrZeroID                          = errors.New("id is zero")
	ErrNonZeroID                       = errors.New("id is non-zero")
	ErrInvalidSuspensionExpirationTime = errors.New("suspension expiration time is invalid")
	ErrUserAlreadySuspended            = errors.New("user is already suspended")
)

errors

Functions

This section is empty.

Types

type ContextKey

type ContextKey uint16
const (
	CKUserManager ContextKey = iota
	CKUser
	CKGroupManager
	CKAccessPolicyManager
)

type Email

type Email struct {
	UserID uuid.UUID `db:"user_id" json:"user_id"`

	EmailEssential
	EmailMetadata
}

Email represents certain emails which are custom and are handled by the customer

func (*Email) ApplyChangelog

func (email *Email) ApplyChangelog(changelog diff.Changelog) (err error)

ApplyChangelog applies changes described by a diff.Diff()'s changelog NOTE: doing a manual update to avoid using reflection

func (Email) Validate

func (email Email) Validate() (err error)

SanitizeAndValidate validates the object

type EmailEssential

type EmailEssential struct {
	Addr      string `db:"addr" json:"addr"`
	IsPrimary bool   `db:"is_primary" json:"is_primary"`
}

EmailEssential represents an essential part of the primary object

type EmailMetadata

type EmailMetadata struct {
	CreatedAt   time.Time `db:"created_at" json:"created_at"`
	ConfirmedAt time.Time `db:"confirmed_at" json:"confirmed_at"`
	UpdatedAt   time.Time `db:"updated_at" json:"updated_at"`
	// contains filtered or unexported fields
}

EmailMetadata contains generic metadata of the primary object

type Essential

type Essential struct {
	Username    string `db:"username" json:"username"`
	DisplayName string `db:"display_name" json:"display_name"`
}

Essential represents an essential part of the primary object

type Manager

type Manager struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

userManager handles business logic of its underlying objects TODO: consider naming first release `Lidia`

func ManagerForTesting

func ManagerForTesting(db *pgx.Conn) (*Manager, context.Context, error)

ManagerForTesting returns a fully initialized user manager for testing

func NewManager

func NewManager(s Store) (*Manager, error)

NewManager returns a new user manager instance also initializing by loading necessary data from a given store

func (*Manager) AccessPolicyManager

func (m *Manager) AccessPolicyManager() *accesspolicy.Manager

func (*Manager) CheckAvailability

func (m *Manager) CheckAvailability(ctx context.Context, username string, email string) error

CheckAvailability tests whether someone with such username or email is already registered

func (*Manager) ConfirmEmail

func (m *Manager) ConfirmEmail(ctx context.Context, addr string) (err error)

ConfirmEmail this function is used only when user's email is confirmed

func (*Manager) CreateEmail

func (m *Manager) CreateEmail(ctx context.Context, fn func(ctx context.Context) (NewEmailObject, error)) (email Email, err error)

UpsertEmail creates a new email

func (*Manager) CreatePhone

func (m *Manager) CreatePhone(ctx context.Context, fn func(ctx context.Context) (NewPhoneObject, error)) (phone Phone, err error)

CreatePhone creates a new phone

func (*Manager) CreateProfile

func (m *Manager) CreateProfile(ctx context.Context, fn func(ctx context.Context) (NewProfileObject, error)) (profile Profile, err error)

CreateProfile creates a new profile

func (*Manager) CreateUser

func (m *Manager) CreateUser(ctx context.Context, fn func(ctx context.Context) (NewUserObject, error)) (u User, err error)

CreateUser creates a new user

func (*Manager) DeleteEmailByAddr

func (m *Manager) DeleteEmailByAddr(ctx context.Context, userID uuid.UUID, addr string) (err error)

DeleteEmailByAddr deletes an object and returns an object, which is an updated object if it's soft deleted, or nil otherwise

func (*Manager) DeletePhoneByNumber

func (m *Manager) DeletePhoneByNumber(ctx context.Context, userID uuid.UUID, number string) (phone Phone, err error)

DeletePhoneByNumber deletes an object and returns an object, which is an updated object if it's soft deleted, or nil otherwise

func (*Manager) DeleteProfileByUserID

func (m *Manager) DeleteProfileByUserID(ctx context.Context, userID uuid.UUID) (err error)

DeleteProfileByUserID deletes an object and returns an object, which is an updated object if it's soft deleted, or nil otherwise

func (*Manager) DeleteUserByID

func (m *Manager) DeleteUserByID(ctx context.Context, id uuid.UUID, isHard bool) (u User, err error)

DeleteUserByID deletes an object and returns an object, which is an updated object if it's soft deleted, or nil otherwise

func (*Manager) EmailByAddr

func (m *Manager) EmailByAddr(ctx context.Context, addr string) (email Email, err error)

EmailByAddr obtains an email by a given address

func (*Manager) GetProfileByID

func (m *Manager) GetProfileByID(ctx context.Context, id uuid.UUID) (profile Profile, err error)

GetProfileByID returns a profile if found by ObjectID

func (*Manager) GroupManager

func (m *Manager) GroupManager() *group.Manager

func (*Manager) Logger

func (m *Manager) Logger() *zap.Logger

Logger returns primary logger if is set, otherwise initializing and returning a new default emergency logger NOTE: will panic if it finally fails to obtain a logger

func (*Manager) PasswordManager

func (m *Manager) PasswordManager() password.Manager

func (*Manager) PrimaryEmailByUserID

func (m *Manager) PrimaryEmailByUserID(ctx context.Context, userID uuid.UUID) (email Email, err error)

PrimaryEmailByUserID obtains the primary email by user id

func (*Manager) PrimaryPhoneByUserID

func (m *Manager) PrimaryPhoneByUserID(ctx context.Context, userID uuid.UUID) (phone Phone, err error)

PrimaryPhoneByUserID obtains the primary phone by user id

func (*Manager) SetAccessPolicyManager

func (m *Manager) SetAccessPolicyManager(apm *accesspolicy.Manager) error

SetAccessPolicyManager assigns accesspolicy policy manager

func (*Manager) SetGroupManager

func (m *Manager) SetGroupManager(gm *group.Manager) error

SetGroupManager assigns a group manager

func (*Manager) SetLogger

func (m *Manager) SetLogger(logger *zap.Logger) error

SetLogger assigns a primary logger for the manager

func (*Manager) SetPassword

func (m *Manager) SetPassword(ctx context.Context, userID uuid.UUID, p password.Password) (err error)

SetPassword sets a new password for the user

func (*Manager) SetPasswordManager

func (m *Manager) SetPasswordManager(pm password.Manager) error

SetPasswordManager assigns a password manager for this container

func (*Manager) SetTokenManager

func (m *Manager) SetTokenManager(tm *token.Manager) error

SetTokenManager assigns a token manager

func (*Manager) Store

func (m *Manager) Store() (Store, error)

Store returns store if set

func (*Manager) SuspendUser

func (m *Manager) SuspendUser(
	ctx context.Context,
	userID uuid.UUID,
	reason string,
	expireAt time.Time,
) (err error)

SuspendUser suspends user by ID with a reason and expiration time

func (*Manager) TokenManager

func (m *Manager) TokenManager() *token.Manager

func (*Manager) UpdateEmail

func (m *Manager) UpdateEmail(ctx context.Context, addr string, fn func(ctx context.Context, email Email) (_ Email, err error)) (email Email, essentialChangelog diff.Changelog, err error)

UpdateEmail updates an existing object NOTE: be very cautious about how you deal with metadata inside the user function

func (*Manager) UpdatePhone

func (m *Manager) UpdatePhone(ctx context.Context, number string, fn func(ctx context.Context, phone Phone) (_ Phone, err error)) (phone Phone, essentialChangelog diff.Changelog, err error)

UpdatePhone updates an existing object NOTE: be very cautious about how you deal with metadata inside the user function

func (*Manager) UpdateProfile

func (m *Manager) UpdateProfile(ctx context.Context, id uuid.UUID, fn func(context.Context, Profile) (Profile, error)) (profile Profile, essentialChangelog diff.Changelog, err error)

UpdateProfile updates an existing object NOTE: be very cautious about how you deal with metadata inside the user function

func (*Manager) UpdateUser

func (m *Manager) UpdateUser(ctx context.Context, id uuid.UUID, fn func(ctx context.Context, u User) (_ User, err error)) (u User, essentialChangelog diff.Changelog, err error)

UpdateUser updates an existing object NOTE: be very cautious about how you deal with metadata inside the user function

func (*Manager) UserByEmailAddr

func (m *Manager) UserByEmailAddr(ctx context.Context, addr string) (u User, err error)

UserByEmailAddr returns a user if found by username

func (*Manager) UserByID

func (m *Manager) UserByID(ctx context.Context, id uuid.UUID) (u User, err error)

UserByID returns a user if found by ObjectID

func (*Manager) UserByUsername

func (m *Manager) UserByUsername(ctx context.Context, username string) (u User, err error)

UserByUsername returns a user if found by username

func (*Manager) Validate

func (m *Manager) Validate() error

type Metadata

type Metadata struct {
	Checksum uint64 `db:"checksum" json:"checksum"`

	// timestamps
	CreatedAt   time.Time `db:"created_at" json:"created_at"`
	UpdatedAt   time.Time `db:"updated_at" json:"updated_at"`
	ConfirmedAt time.Time `db:"confirmed_at" json:"confirmed_at"`
	DeletedAt   time.Time `db:"deleted_at" json:"deleted_at"`

	// the most recent authentication information
	LastLoginAt       time.Time `db:"last_login_at" json:"last_login_at"`
	LastLoginIP       net.IP    `db:"last_login_ip" json:"last_login_ip"`
	LastLoginFailedAt time.Time `db:"last_login_failed_at" json:"last_login_failed_at"`
	LastLoginFailedIP net.IP    `db:"last_login_failed_ip" json:"last_login_failed_ip"`
	LastLoginAttempts uint8     `db:"last_login_attempts" json:"last_login_attempts"`

	// account suspension
	IsSuspended         bool      `db:"is_suspended" json:"is_suspended"`
	SuspendedAt         time.Time `db:"suspended_at" json:"suspended_at"`
	SuspensionExpiresAt time.Time `db:"suspension_expires_at" json:"suspension_expires_at"`
	SuspensionReason    string    `db:"suspension_reason" json:"suspension_reason"`
}

TODO: solve net.IPAddr field problem

type NewEmailObject

type NewEmailObject struct {
	EmailEssential
	UserID      uuid.UUID
	IsConfirmed bool
}

EmailNewObject contains fields sufficient to create a new object

type NewPhoneObject

type NewPhoneObject struct {
	PhoneEssential
	UserID      uuid.UUID
	IsConfirmed bool
}

NewPhoneObject contains fields sufficient to create a new object

type NewProfileObject

type NewProfileObject struct {
	ProfileEssential
	UserID uuid.UUID `db:"user_id" json:"user_id"`
}

NewProfileObject contains fields sufficient to create a new object

type NewUserObject

type NewUserObject struct {
	Essential
	ProfileEssential
	EmailAddr   string `json:"email_addr"`
	PhoneNumber string `json:"phone_number"`
	Password    []byte `json:"password"`
}

UserNewObject contains fields sufficient to create a new object

type Phone

type Phone struct {
	UserID uuid.UUID `db:"user_id" json:"user_id"`

	PhoneEssential
	PhoneMetadata
}

Phone represents certain emails which are custom and are handled by the customer

func (*Phone) ApplyChangelog

func (p *Phone) ApplyChangelog(changelog diff.Changelog) (err error)

ApplyChangelog applies changes described by a diff.Diff()'s changelog NOTE: doing a manual update to avoid using reflection

func (Phone) Validate

func (p Phone) Validate() (err error)

SanitizeAndValidate validates the object

type PhoneEssential

type PhoneEssential struct {
	Number    string `db:"number" json:"number"`
	IsPrimary bool   `db:"is_primary" json:"is_primary"`
}

PhoneEssential represents an essential part of the primary object

type PhoneMetadata

type PhoneMetadata struct {
	CreatedAt   time.Time `db:"created_at" json:"created_at"`
	ConfirmedAt time.Time `db:"confirmed_at" json:"confirmed_at"`
	UpdatedAt   time.Time `db:"updated_at" json:"updated_at"`
	// contains filtered or unexported fields
}

PhoneMetadata contains generic metadata of the primary object

type PostgreSQLStore

type PostgreSQLStore struct {
	// contains filtered or unexported fields
}

func (*PostgreSQLStore) DeleteEmailByAddr

func (s *PostgreSQLStore) DeleteEmailByAddr(ctx context.Context, userID uuid.UUID, addr string) (err error)

func (*PostgreSQLStore) DeletePhoneByNumber

func (s *PostgreSQLStore) DeletePhoneByNumber(ctx context.Context, userID uuid.UUID, number string) (err error)

func (*PostgreSQLStore) DeleteProfileByUserID

func (s *PostgreSQLStore) DeleteProfileByUserID(ctx context.Context, userID uuid.UUID) (err error)

func (*PostgreSQLStore) DeleteUserByID

func (s *PostgreSQLStore) DeleteUserByID(ctx context.Context, id uuid.UUID) (err error)

func (*PostgreSQLStore) FetchEmailByAddr

func (s *PostgreSQLStore) FetchEmailByAddr(ctx context.Context, addr string) (e Email, err error)

func (*PostgreSQLStore) FetchEmailsByUserID

func (s *PostgreSQLStore) FetchEmailsByUserID(ctx context.Context, userID uuid.UUID) ([]Email, error)

func (*PostgreSQLStore) FetchPhoneByNumber

func (s *PostgreSQLStore) FetchPhoneByNumber(ctx context.Context, number string) (e Phone, err error)

func (*PostgreSQLStore) FetchPhonesByUserID

func (s *PostgreSQLStore) FetchPhonesByUserID(ctx context.Context, userID uuid.UUID) ([]Phone, error)

func (*PostgreSQLStore) FetchPrimaryEmailByUserID

func (s *PostgreSQLStore) FetchPrimaryEmailByUserID(ctx context.Context, userID uuid.UUID) (e Email, err error)

func (*PostgreSQLStore) FetchPrimaryPhoneByUserID

func (s *PostgreSQLStore) FetchPrimaryPhoneByUserID(ctx context.Context, userID uuid.UUID) (e Phone, err error)

func (*PostgreSQLStore) FetchProfileByUserID

func (s *PostgreSQLStore) FetchProfileByUserID(ctx context.Context, userID uuid.UUID) (profile Profile, err error)

func (*PostgreSQLStore) FetchUserByEmailAddr

func (s *PostgreSQLStore) FetchUserByEmailAddr(ctx context.Context, addr string) (u User, err error)

func (*PostgreSQLStore) FetchUserByID

func (s *PostgreSQLStore) FetchUserByID(ctx context.Context, id uuid.UUID) (u User, err error)

func (*PostgreSQLStore) FetchUserByPhoneNumber

func (s *PostgreSQLStore) FetchUserByPhoneNumber(ctx context.Context, number string) (u User, err error)

func (*PostgreSQLStore) FetchUserByUsername

func (s *PostgreSQLStore) FetchUserByUsername(ctx context.Context, username string) (u User, err error)

func (*PostgreSQLStore) UpsertEmail

func (s *PostgreSQLStore) UpsertEmail(ctx context.Context, e Email) (_ Email, err error)

UpsertEmail creates a new entry in the storage backend

func (*PostgreSQLStore) UpsertPhone

func (s *PostgreSQLStore) UpsertPhone(ctx context.Context, p Phone) (_ Phone, err error)

func (*PostgreSQLStore) UpsertProfile

func (s *PostgreSQLStore) UpsertProfile(ctx context.Context, p Profile) (_ Profile, err error)

func (*PostgreSQLStore) UpsertUser

func (s *PostgreSQLStore) UpsertUser(ctx context.Context, u User) (_ User, err error)

CreateUser creates a new entry in the storage backend

type Profile

type Profile struct {
	UserID uuid.UUID `db:"user_id" json:"-"`

	ProfileEssential
	ProfileMetadata
}

Profile represents certain profiles which are custom and are handled by the customer

func (*Profile) ApplyChangelog

func (p *Profile) ApplyChangelog(changelog diff.Changelog) (err error)

ApplyChangelog applies changes described by a diff.Diff()'s changelog NOTE: doing a manual update to avoid using reflection

func (Profile) Validate

func (p Profile) Validate() (err error)

SanitizeAndValidate validates the object

type ProfileEssential

type ProfileEssential struct {
	Firstname  string `db:"firstname" json:"firstname"`
	Lastname   string `db:"lastname" json:"lastname"`
	Middlename string `db:"middlename" json:"middlename"`
}

ProfileEssential represents an essential part of the primary object

type ProfileMetadata

type ProfileMetadata struct {
	Checksum  uint64    `db:"checksum" json:"checksum"`
	CreatedAt time.Time `db:"created_at" json:"created_at"`
	UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
	// contains filtered or unexported fields
}

ProfileMetadata contains generic metadata of the primary object

type Store

type Store interface {
	// user
	UpsertUser(ctx context.Context, u User) (_ User, err error)
	FetchUserByID(ctx context.Context, id uuid.UUID) (u User, err error)
	FetchUserByUsername(ctx context.Context, username string) (u User, err error)
	FetchUserByEmailAddr(ctx context.Context, addr string) (u User, err error)
	FetchUserByPhoneNumber(ctx context.Context, number string) (u User, err error)
	DeleteUserByID(ctx context.Context, id uuid.UUID) (err error)

	// emails
	UpsertEmail(ctx context.Context, e Email) (_ Email, err error)
	FetchPrimaryEmailByUserID(ctx context.Context, userID uuid.UUID) (e Email, err error)
	FetchEmailByAddr(ctx context.Context, addr string) (e Email, err error)
	FetchEmailsByUserID(ctx context.Context, userID uuid.UUID) (es []Email, err error)
	DeleteEmailByAddr(ctx context.Context, userID uuid.UUID, addr string) (err error)

	// phones
	UpsertPhone(ctx context.Context, p Phone) (_ Phone, err error)
	FetchPrimaryPhoneByUserID(ctx context.Context, userID uuid.UUID) (p Phone, err error)
	FetchPhoneByNumber(ctx context.Context, number string) (p Phone, err error)
	FetchPhonesByUserID(ctx context.Context, userID uuid.UUID) (ps []Phone, err error)
	DeletePhoneByNumber(ctx context.Context, userID uuid.UUID, number string) (err error)

	// profile
	UpsertProfile(ctx context.Context, p Profile) (_ Profile, err error)
	FetchProfileByUserID(ctx context.Context, userID uuid.UUID) (p Profile, err error)
	DeleteProfileByUserID(ctx context.Context, userID uuid.UUID) (err error)
}

Store represents a user storage backend contract

func NewPostgreSQLStore

func NewPostgreSQLStore(conn *pgx.Conn) (Store, error)

type User

type User struct {
	ID uuid.UUID `db:"id" json:"id"`

	Essential
	Metadata
	// contains filtered or unexported fields
}

User represents certain users which are custom and are handled by the customer

func CreateTestUser

func CreateTestUser(ctx context.Context, m *Manager, username string, email string, pass []byte) (User, error)

func (*User) ApplyChangelog

func (u *User) ApplyChangelog(changelog diff.Changelog) (err error)

ApplyChangelog applies changes described by a diff.Diff()'s changelog NOTE: doing a manual update to avoid using reflection

func (User) Validate

func (u User) Validate() error

SanitizeAndValidate user object

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL