store

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2024 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

All the error codes can be found here: https://www.postgresql.org/docs/current/errcodes-appendix.html

Index

Constants

View Source
const (
	O_NO_PERMS          int = 0b0000_0000_0000_0000
	O_WRITE             int = 0b0000_0000_0000_0001
	O_SHARE             int = 0b0000_0000_0000_0010
	O_GRANT_SHARE       int = 0b0000_0000_0000_0100
	O_DELETE            int = 0b0000_0000_0000_1000
	O_WRITE_INGRIDIENT  int = 0b0000_0000_0001_0000
	O_DELETE_INGRIDIENT int = 0b0000_0000_0010_0000
	O_RENAME_INGRIDIENT int = 0b0000_0000_0100_0000
	O_RENAME_BENTO      int = 0b0000_0000_1000_0000
	O_REVOKE_SHARE      int = 0b0000_0001_0000_0000
	O_OWNER             int = 0b1000_0000_0000_0000

	// This represents all the permissions a requesting user can grant to a target user.
	S_ALL               string = "all"
	S_WRITE             string = "write"
	S_DELETE            string = "delete"
	S_SHARE             string = "share"
	S_RENAME_BENTO      string = "rename_bento"
	S_RENAME_INGRIDIENT string = "rename_ingridient"
	S_WRITE_INGRIDIENT  string = "write_ingridient"
	S_DELETE_INGRIDIENT string = "delete_ingridient"
	S_REVOKE_SHARE      string = "revoke_share"
)
View Source
const (
	EMAIL_VERIFICATION_CODE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
	EMAIL_VERIFICATION_CODE_LENGTH   = 20
)
View Source
const (
	PG_ERR_UNIQUE_VIOLATION = "unique_violation"
)

Class 23 - Integrity Constraint Violation

Variables

An map to take the integer permission value using text

Functions

func Close

func Close() error

Close closes the database connection. Not intended to be used in production, but makes testing easier.

func Connect

func Connect(dbUrl string) error

Connect establishes a connection with the given postgresql database with the given url.

func DeleteBentoPermissionById

func DeleteBentoPermissionById(permsId int64) error

Deletes an entry in bento_permissions by matching the given id.

IMPORTANT: THIS PROCESS IS NOT REVERSABLE!

func DeleteEmailVerificationWithUserId

func DeleteEmailVerificationWithUserId(uid string) (sql.Result, error)

DeleteEmailVerificationWithUserId tries to removes any email verification code that has the given user id. IMPORTANT: this method does not use a transaction so deletion are UNSAFE.

func DeleteIngridient

func DeleteIngridient(bentoId, name string) error

Deletes an ingridient from a bento. IMPORTANT: This method does not check for permissions before executing. Make sure to check before calling.

func DeleteTokensOwnedByUser

func DeleteTokensOwnedByUser(tx *sql.Tx, userId string) (sql.Result, error)

Deletes all the tokens, access and refresh tokens owned by the given user with the userId.

func ExistsBentoPermissionByUserBentoId

func ExistsBentoPermissionByUserBentoId(userId, bentoId string) (bool, error)

Checks if there already exists a bento permission for the given user and bento.

func ExistsUserWithEmail

func ExistsUserWithEmail(email string) (bool, error)

ExistsUserWithEmail checks if there is a user with the given email stored in the database.

func IsTokenValid

func IsTokenValid(tokenId, tokenType string) error

Checks if the token with the given tokenId exists and its valid in the database. This is because users can invalidate all access tokens for a given account and when resetting passwords, the tokens are invalidated too.

func Ping

func Ping() error

Ping makes sure that connection is still alives. It context.Background and timeouts in 5 seconds.

func RenameIngridient

func RenameIngridient(bentoId, oldName, newName string) error

Rename an ingridient in a bento. The ingridient and bento must exists or an error will be returned.

func ReseasonIngridient

func ReseasonIngridient(bentoId, name, value string) error

Re-seaons, aka, change the value of the ingridient. IMPORTANT: This method does not check for permissions before executing. Make sure to check before calling it.

func Rollback

func Rollback(tx *sql.Tx, requestId string) error

Rollback is a helper function to rollback if tx.Commit() returns an error and will log if rollback results in an error.

func SaveBentoEntryBatch

func SaveBentoEntryBatch(entries []BentoEntry) error

SaveBentoEntryBatch will save a batch of BentoEntry at once. The entries will be updated in place with the corresponding id, created_at and updated_at from the database.

func StartTx

func StartTx() (*sql.Tx, error)

StartTx begins a new transaction

Types

type Bento

type Bento struct {
	Id        string
	Name      string
	OwnerId   string
	PubKey    string
	CreatedAt time.Time
	UpdatedAt time.Time
}

Bento represents a bento object that has a slice with all the entries as well.

func GetBentoWithId

func GetBentoWithId(id string) (*Bento, error)

GetBentoWithId retrieves a bento with the given id.

func NewBentoTx

func NewBentoTx(tx *sql.Tx, name, ownerId, pubKey string) (*Bento, error)

NewBentoTx will create and save a new bento into the database with the given information. This method will return an error if there is another bento with the same name from the same user. All bentos belonging to one user should have unique names.

IMPORTANT: You must call tx.Commit() afterwards for changes to take effect.

func (*Bento) Delete

func (b *Bento) Delete(tx *sql.Tx) (sql.Result, error)

Delete removes the bento from the database.

You must call tx.Commit for it to take effect.

func (*Bento) Rename

func (b *Bento) Rename(newName string) error

Rename will rename the bento by updating the database record. If the new name is the same as the current name, it won't perform a database query. After a successful rename, it will auto assigned the new name to the Bento instance.

func (*Bento) VerifySignature

func (b *Bento) VerifySignature(signature string, challenge string) error

VerifySignature verifies the given hex encoded signature with the given challenge.

type BentoEntry

type BentoEntry struct {
	Id        int64
	Name      string
	Value     string
	BentoId   string
	CreatedAt time.Time
	UpdatedAt time.Time
}

BentoEntry represents an entry (secret) of a already preprared bento in the database.

func GetEntriesForBento

func GetEntriesForBento(bentoId string) ([]BentoEntry, error)

GetEntriesForBento will get all the entries for a given bento.

func NewBentoEntry

func NewBentoEntry(name, value, bentoId string) BentoEntry

NewBentoEntry just creates a new BentoEntry struct and it DOES NOT saves it in the database. This allows any procedure to create multiple BentoEntry if needed and save them in a batch.

type BentoPermission

type BentoPermission struct {
	Id          int64
	UserId      string
	BentoId     string
	Permissions int
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

BentoPermission represents an entry for a bento permission in the database. It has a set of methods that help with the retrieval and manipulation of the bento permission.

func GetBentoPermissionByUserBentoId

func GetBentoPermissionByUserBentoId(userId, bentoId string) (*BentoPermission, error)

GetBentoPermissionByUserBentoId gets a bento permission that matches the given user and bento id.

func NewBentoPermissionTx

func NewBentoPermissionTx(tx *sql.Tx, userId, bentoId string, permissions int) (*BentoPermission, error)

This will create a new bento permission in the database using the transaction provided.

IMPORTANT: You must call tx.Commit() afterwards for changes to take effect.

type EmailVerification

type EmailVerification struct {
	Id        int64
	Code      string
	UserId    string
	ExpiresAt time.Time
	CreatedAt time.Time
}

EmailVerification represents an email verification record in the database.

func GetEmailVerificationWithCode

func GetEmailVerificationWithCode(code string) (*EmailVerification, error)

GetEmailVerificationWithCode tries to retrieves an email verification record from the database with the given code.

func NewEmailVerification

func NewEmailVerification(userId string) (*EmailVerification, error)

NewEmailVerification creates a new email verification record.

func (*EmailVerification) Delete

func (ev *EmailVerification) Delete(tx *sql.Tx) (sql.Result, error)

Delete removes the email verification from the database. You must call tx.Commit for the deletion to take effect.

func (*EmailVerification) Update

func (ev *EmailVerification) Update(tx *sql.Tx) (sql.Result, error)

Update is not implemented, but its defined to satisfy the Model interface.

type Model

type Model interface {
	// Delete is used to delete the row in the database.
	Delete(tx *sql.Tx) (sql.Result, error)
	// Update will save the current model values into the database.
	Update(tx *sql.Tx) (sql.Result, error)
}

type PasswordResetCode

type PasswordResetCode struct {
	Id        int64
	Code      string
	UserId    string
	ExpiresAt time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
}

Represents a row in the db for password reset codes.

func GetPasswordResetCodeByUserId

func GetPasswordResetCodeByUserId(uid string) (*PasswordResetCode, error)

Get a password reset code by using the given user id.

func NewOrUpdatePasswordResetCode

func NewOrUpdatePasswordResetCode(uid string) (*PasswordResetCode, error)

func (*PasswordResetCode) Delete

func (p *PasswordResetCode) Delete(tx *sql.Tx) (sql.Result, error)

Delete the password reset code from the database.

type User

type User struct {
	Id            string
	Email         string
	Password      string
	Name          string
	EmailVerified bool
	CreatedAt     time.Time
	UpdatedAt     time.Time
}

User represents a real user store in the database.

func GetUserWithEmail

func GetUserWithEmail(email string) (*User, error)

GetUserWithEmail retrieves a user in the database with the given email.

func GetUserWithEmailAndPassword

func GetUserWithEmailAndPassword(email, password string) (*User, error)

GetUserWithEmailAndPassword tries to match a user with the given email and password. Usage for signining a user. Its better to use psql to match the password in the query.

func GetUserWithId

func GetUserWithId(id string) (*User, error)

GetUserWithId retrieves a user with the given id. An error is returned if nothing is found.

func NewUser

func NewUser(email, password, name string) (*User, error)

NewUser creates a new user with the given information. This function will save the new user in the database.

func (*User) Delete

func (u *User) Delete(tx *sql.Tx) (sql.Result, error)

Delete removes a user from the database using the id.

You must call tx.Commit for it to take effect.

func (*User) SetEmailVerifiedTx

func (u *User) SetEmailVerifiedTx(tx *sql.Tx, v bool) (sql.Result, error)

Sets the value for the email_verified column in the users table. Will set the User.EmailVerified field to the boolean value 'v'.

You must call tx.Commit() for it to take effect.

func (*User) Update

func (u *User) Update(tx *sql.Tx) (sql.Result, error)

DEPRACATED: this method will try to update the columns in users and rehash the password while doing so, which causes issues since the password is not the same anymore. This leads to users unable to access their accounts unless they reset their password.

Update updates the user in the database with the current field values of the struct.

This does not update the Id, CreatedAt, and UpdatedAt fields.

You must call tx.Commit for it to take effect.

Jump to

Keyboard shortcuts

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