models

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const CreatedAt = "created_at"
View Source
const SystemUserID = "0"

Variables

View Source
var SystemUserUUID = uuid.Nil

Functions

func CountOtherUsers

func CountOtherUsers(tx *storage.Connection, instanceID, id uuid.UUID) (int, error)

CountOtherUsers counts how many other users exist besides the one provided

func DeleteInstance

func DeleteInstance(conn *storage.Connection, instance *Instance) error

func FindUserWithRefreshToken

func FindUserWithRefreshToken(tx *storage.Connection, token string) (*User, *RefreshToken, error)

FindUserWithRefreshToken finds a user from the provided refresh token.

func IsDuplicatedEmail

func IsDuplicatedEmail(tx *storage.Connection, instanceID uuid.UUID, email, aud string) (bool, error)

IsDuplicatedEmail returns whether a user exists with a matching email and audience.

func IsNotFoundError

func IsNotFoundError(err error) bool

IsNotFoundError returns whether an error represents a "not found" error.

func Logout

func Logout(tx *storage.Connection, instanceID uuid.UUID, id uuid.UUID) error

Logout deletes all refresh tokens for a user.

func NewAuditLogEntry

func NewAuditLogEntry(tx *storage.Connection, instanceID uuid.UUID, actor *User, action AuditAction, traits map[string]interface{}) error

Types

type AuditAction

type AuditAction string
const (
	LoginAction                 AuditAction = "login"
	LogoutAction                AuditAction = "logout"
	InviteAcceptedAction        AuditAction = "invite_accepted"
	UserSignedUpAction          AuditAction = "user_signedup"
	UserInvitedAction           AuditAction = "user_invited"
	UserDeletedAction           AuditAction = "user_deleted"
	UserModifiedAction          AuditAction = "user_modified"
	UserRecoveryRequestedAction AuditAction = "user_recovery_requested"
	TokenRevokedAction          AuditAction = "token_revoked"
	TokenRefreshedAction        AuditAction = "token_refreshed"
)

type AuditLogEntry

type AuditLogEntry struct {
	InstanceID uuid.UUID `json:"-" gorm:"index:audit_logs_instance_id_idx;type:varchar(255) DEFAULT NULL"`
	ID         uuid.UUID `json:"id" gorm:"primaryKey;type:varchar(255) NOT NULL"`

	Payload JSONMap `json:"payload" gorm:"type:JSON NULL DEFAULT NULL"`

	CreatedAt time.Time `json:"created_at" gorm:"type:timestamp NULL DEFAULT NULL"`
}

AuditLogEntry is the database model for audit log entries.

func FindAuditLogEntries

func FindAuditLogEntries(tx *storage.Connection, instanceID uuid.UUID, filterColumns []string, filterValue string, pageParams *Pagination) ([]*AuditLogEntry, error)

type Instance

type Instance struct {
	ID uuid.UUID `json:"id" gorm:"primaryKey;type:varchar(255) NOT NULL"`
	// Gothic UUID
	UUID uuid.UUID `json:"uuid,omitempty" gorm:"type:varchar(255) DEFAULT NULL"`

	BaseConfig *conf.Configuration `json:"config" gorm:"column:raw_base_config"`

	CreatedAt time.Time `json:"created_at" gorm:"type:timestamp NULL DEFAULT NULL"`
	UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamp NULL DEFAULT NULL"`
}

func GetInstance

func GetInstance(tx *storage.Connection, instanceID uuid.UUID) (*Instance, error)

GetInstance finds an instance by ID

func GetInstanceByUUID

func GetInstanceByUUID(tx *storage.Connection, uuid uuid.UUID) (*Instance, error)

func (*Instance) Config

func (i *Instance) Config() (*conf.Configuration, error)

Config loads the the base configuration values with defaults.

func (*Instance) UpdateConfig

func (i *Instance) UpdateConfig(tx *storage.Connection, config *conf.Configuration) error

UpdateConfig updates the base config

type InstanceNotFoundError

type InstanceNotFoundError struct{}

InstanceNotFoundError represents when an instance is not found.

func (InstanceNotFoundError) Error

func (e InstanceNotFoundError) Error() string

type JSONMap

type JSONMap map[string]interface{}

func (*JSONMap) Scan

func (j *JSONMap) Scan(src interface{}) error

func (JSONMap) Value

func (j JSONMap) Value() (driver.Value, error)

type Pagination

type Pagination struct {
	Page    uint64
	PerPage uint64
	Count   uint64
}

func (*Pagination) Offset

func (p *Pagination) Offset() uint64

type RefreshToken

type RefreshToken struct {
	InstanceID uuid.UUID `` /* 128-byte string literal not displayed */
	ID         int64     `gorm:"primaryKey"`

	Token string `gorm:"index:refresh_tokens_token_idx;type:varchar(255) DEFAULT NULL"`

	UserID uuid.UUID `gorm:"index:refresh_tokens_instance_id_user_id_idx;type:varchar(255) DEFAULT NULL"`

	Revoked   bool      `gorm:"type:tinyint(1) DEFAULT NULL"`
	CreatedAt time.Time `gorm:"type:timestamp NULL DEFAULT NULL"`
	UpdatedAt time.Time `gorm:"type:timestamp NULL DEFAULT NULL"`
}

RefreshToken is the database model for refresh tokens.

func GrantAuthenticatedUser

func GrantAuthenticatedUser(tx *storage.Connection, user *User) (*RefreshToken, error)

GrantAuthenticatedUser creates a refresh token for the provided user.

func GrantRefreshTokenSwap

func GrantRefreshTokenSwap(tx *storage.Connection, user *User, token *RefreshToken) (*RefreshToken, error)

GrantRefreshTokenSwap swaps a refresh token for a new one, revoking the provided token.

type RefreshTokenNotFoundError

type RefreshTokenNotFoundError struct{}

RefreshTokenNotFoundError represents when a refresh token is not found.

func (RefreshTokenNotFoundError) Error

type SortDirection

type SortDirection string
const Ascending SortDirection = "ASC"
const Descending SortDirection = "DESC"

type SortField

type SortField struct {
	Name string
	Dir  SortDirection
}

type SortParams

type SortParams struct {
	Fields []SortField
}

type User

type User struct {
	InstanceID uuid.UUID `json:"-" gorm:"index:users_instance_id_idx;index:users_instance_id_email_idx;type:varchar(255) DEFAULT NULL"`
	ID         uuid.UUID `json:"id" gorm:"primaryKey;type:varchar(255) NOT NULL"`

	Aud               string     `json:"aud" gorm:"type:varchar(255) DEFAULT NULL"`
	Role              string     `json:"role" gorm:"type:varchar(255) DEFAULT NULL"`
	Email             string     `json:"email" gorm:"index:users_instance_id_email_idx;type:varchar(255) DEFAULT NULL"`
	EncryptedPassword string     `json:"-" gorm:"type:varchar(255) DEFAULT NULL"`
	ConfirmedAt       *time.Time `json:"confirmed_at,omitempty" gorm:"type:timestamp NULL DEFAULT NULL"`
	InvitedAt         *time.Time `json:"invited_at,omitempty" gorm:"type:timestamp NULL DEFAULT NULL"`

	ConfirmationToken  string     `json:"-" gorm:"type:varchar(255) DEFAULT NULL"`
	ConfirmationSentAt *time.Time `json:"confirmation_sent_at,omitempty" gorm:"type:timestamp NULL DEFAULT NULL"`

	RecoveryToken  string     `json:"-" gorm:"type:varchar(255) DEFAULT NULL"`
	RecoverySentAt *time.Time `json:"recovery_sent_at,omitempty" gorm:"type:timestamp NULL DEFAULT NULL"`

	EmailChangeToken  string     `json:"-" gorm:"type:varchar(255) DEFAULT NULL"`
	EmailChange       string     `json:"new_email,omitempty" gorm:"type:varchar(255) DEFAULT NULL"`
	EmailChangeSentAt *time.Time `json:"email_change_sent_at,omitempty" gorm:"type:timestamp NULL DEFAULT NULL"`

	LastSignInAt *time.Time `json:"last_sign_in_at,omitempty" gorm:"type:timestamp NULL DEFAULT NULL"`

	AppMetaData  JSONMap `json:"app_metadata" gorm:"column:raw_app_meta_data;type:JSON NULL DEFAULT NULL"`
	UserMetaData JSONMap `json:"user_metadata" gorm:"column:raw_user_meta_data;type:JSON NULL DEFAULT NULL"`

	IsSuperAdmin bool `json:"-" gorm:"type:tinyint(1) DEFAULT NULL"`

	CreatedAt time.Time `json:"created_at" gorm:"type:timestamp NULL DEFAULT NULL"`
	UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamp NULL DEFAULT NULL"`
}

User represents a registered user with email/password authentication

func FindUserByConfirmationToken

func FindUserByConfirmationToken(tx *storage.Connection, token string) (*User, error)

FindUserByConfirmationToken finds users with the matching confirmation token.

func FindUserByEmailAndAudience

func FindUserByEmailAndAudience(tx *storage.Connection, instanceID uuid.UUID, email, aud string) (*User, error)

FindUserByEmailAndAudience finds a user with the matching email and audience.

func FindUserByID

func FindUserByID(tx *storage.Connection, id uuid.UUID) (*User, error)

FindUserByID finds a user matching the provided ID.

func FindUserByInstanceIDAndID

func FindUserByInstanceIDAndID(tx *storage.Connection, instanceID, id uuid.UUID) (*User, error)

FindUserByInstanceIDAndID finds a user matching the provided ID.

func FindUserByRecoveryToken

func FindUserByRecoveryToken(tx *storage.Connection, token string) (*User, error)

FindUserByRecoveryToken finds a user with the matching recovery token.

func FindUsersInAudience

func FindUsersInAudience(tx *storage.Connection, instanceID uuid.UUID, aud string, pageParams *Pagination, sortParams *SortParams, filter string) ([]*User, error)

FindUsersInAudience finds users with the matching audience.

func NewSystemUser

func NewSystemUser(instanceID uuid.UUID, aud string) *User

func NewUser

func NewUser(instanceID uuid.UUID, email, password, aud string, userData map[string]interface{}) (*User, error)

NewUser initializes a new user from an email, password and user data.

func (*User) Authenticate

func (u *User) Authenticate(password string) bool

Authenticate a user from a password

func (*User) BeforeCreate

func (u *User) BeforeCreate(tx *gorm.DB) error

func (*User) BeforeSave

func (u *User) BeforeSave(tx *gorm.DB) error

func (*User) BeforeUpdate

func (u *User) BeforeUpdate(tx *gorm.DB) error

func (*User) Confirm

func (u *User) Confirm(tx *storage.Connection) error

Confirm resets the confimation token and the confirm timestamp

func (*User) ConfirmEmailChange

func (u *User) ConfirmEmailChange(tx *storage.Connection) error

ConfirmEmailChange confirm the change of email for a user

func (*User) HasRole

func (u *User) HasRole(roleName string) bool

HasRole returns true when the users role is set to roleName

func (*User) IsConfirmed

func (u *User) IsConfirmed() bool

IsConfirmed checks if a user has already being registered and confirmed.

func (*User) Recover

func (u *User) Recover(tx *storage.Connection) error

Recover resets the recovery token

func (*User) SetEmail

func (u *User) SetEmail(tx *storage.Connection, email string) error

func (*User) SetRole

func (u *User) SetRole(tx *storage.Connection, roleName string) error

SetRole sets the users Role to roleName

func (*User) UpdateAppMetaData

func (u *User) UpdateAppMetaData(tx *storage.Connection, updates map[string]interface{}) error

UpdateAppMetaData updates all app data from a map of updates

func (*User) UpdatePassword

func (u *User) UpdatePassword(tx *storage.Connection, password string) error

func (*User) UpdateUserMetaData

func (u *User) UpdateUserMetaData(tx *storage.Connection, updates map[string]interface{}) error

UpdateUserMetaData sets all user data from a map of updates, ensuring that it doesn't override attributes that are not in the provided map.

type UserNotFoundError

type UserNotFoundError struct{}

UserNotFoundError represents when a user is not found.

func (UserNotFoundError) Error

func (e UserNotFoundError) Error() string

Jump to

Keyboard shortcuts

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