db

package
v0.0.0-...-8e21e68 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorNoEmail .
	ErrorNoEmail = errors.New("no email")
	// ErrorUserAlreadyExists .
	ErrorUserAlreadyExists = errors.New("user already exists")
	// ErrorFailedSetPassword .
	ErrorFailedSetPassword = errors.New("failed set password")
	// ErrorInvalidPassword .
	ErrorInvalidPassword = errors.New("invalid password")
)

Functions

func Connection

func Connection(dataSourceName string, echo bool) (*gorm.DB, error)

Connection .

func Reset

func Reset(dataSourceName string, dbname string) error

Reset is drop the database and create a new one.

func SyncModels

func SyncModels(dataSourceName string, echo bool) (*gorm.DB, error)

SyncModels is synchronize databases and models.

func Transaction

func Transaction(db *gorm.DB, do Do) error

Transaction apply work tied to one in 'Do' type function to DB.

Types

type Codes

type Codes []byte

Codes are slice that store code.

func (*Codes) Del

func (c *Codes) Del(code string) (bool, error)

Del deletes the parameter after checking whether it is in the stored value. It returns whether to delete afterwards. If an error occurs, an error is returned. In this case, it returns false whether to delete.

func (Codes) In

func (c Codes) In(code string) (int, bool)

In returns the index and whether the argument is included in the stored value.

func (*Codes) Set

func (c *Codes) Set(codes []string) error

Set converts the argument to a byte string and saves it.

func (Codes) Value

func (c Codes) Value() []string

Value is returned after converting the saved value to a string slice. Returns nil if there is no value.

type DateTimeFields

type DateTimeFields struct {
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time
}

DateTimeFields is base columns definition.

type Do

type Do func(tx *gorm.DB) error

Do is executed between begin and commit in a transaction.

type IDField

type IDField struct {
	ID uint `gorm:"primary_key"`
}

IDField is primary key definition.

type JSONUser

type JSONUser struct {
	Email          string `json:"email"`
	IsAdmin        bool   `json:"is_admin"`
	CreatedAt      int64  `json:"created_at"`
	UpdatedAt      int64  `json:"updated_at"`
	DeletedAt      *int64 `json:"deleted_at"`
	OTPConfirmedAt *int64 `json:"otp_confirmed_at"`
}

JSONUser is used when payload to a request. This is a structure with important information removed.

type User

type User struct {
	IDField
	Email          string `gorm:"index;not null" binding:"required,email"`
	HashedPassword string `gorm:"not null"`
	IsAdmin        bool   `gorm:"default:false"`

	OTPSecretKey    string `gorm:"size:16"`
	OTPBackupCodes  Codes
	OTPConfirmedAt  *time.Time
	PasswordResetTs int

	DateTimeFields
}

User is user ORM.

func (*User) ConfirmOTP

func (u *User) ConfirmOTP()

ConfirmOTP .

func (*User) ConfirmedOTP

func (u *User) ConfirmedOTP() bool

ConfirmedOTP .

func (*User) Create

func (u *User) Create(con *gorm.DB, password string) error

Create creates a new user and saves it in the DB. An error is returned if there are users with duplicate emails or if the password does not match the specified format.

func (*User) Delete

func (u *User) Delete(con *gorm.DB) error

Delete deletes the user data from the DB. If an error occurs while saving, rollback and return error.

func (*User) Fetch

func (u *User) Fetch(con *gorm.DB) (*User, error)

Fetch reads data from DB and synchronizes the user's data. If the user has been deleted return error.

func (*User) GenerateOTPSecretKey

func (u *User) GenerateOTPSecretKey(secretKeyLen int)

GenerateOTPSecretKey .

func (User) MarshalJSON

func (u User) MarshalJSON() ([]byte, error)

MarshalJSON .

func (*User) OTPProvisioningURI

func (u *User) OTPProvisioningURI(issuer string) (string, error)

OTPProvisioningURI .

func (*User) ResetOTP

func (u *User) ResetOTP()

ResetOTP initializes OTP registration data. Applied when calling Save.

func (*User) Save

func (u *User) Save(con *gorm.DB) error

Save stores each attribute of User in DB. If an error occurs while saving, rollback and return error.

func (*User) SetPassword

func (u *User) SetPassword(password string) error

SetPassword converts the passed password string into a hash string and saves it. If the password format is incorrect, an error is returned.

func (*User) TOTP

func (u *User) TOTP() (*gotp.TOTP, error)

TOTP .

func (*User) VerifyOTP

func (u *User) VerifyOTP(otp string) bool

VerifyOTP .

func (*User) VerifyPassword

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

VerifyPassword verifies that the given password is correct.

Jump to

Keyboard shortcuts

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