models

package
v0.0.0-...-0f031a4 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAdminRoleRequired = errors.New("admin or greater role required")

	ErrBanSelf           = errors.New("cannot ban self")
	ErrBanExist          = errors.New("user already banned")
	ErrBanMorePrivileged = errors.New("cannot ban user with higher permissions")

	ErrUnbanSelf           = errors.New("cannot unban self")
	ErrUnbanExist          = errors.New("user already unbanned")
	ErrUnbanMorePrivileged = errors.New("cannot unban user with higher permissions")

	ErrLockSelf           = errors.New("cannot lock self")
	ErrLockExist          = errors.New("user already locked")
	ErrLockMorePrivileged = errors.New("cannot lock user with higher permissions")

	ErrUnlockSelf           = errors.New("cannot unlock self")
	ErrUnlockExist          = errors.New("user already unlocked")
	ErrUnlockMorePrivileged = errors.New("cannot unlock user with higher permissions")

	ErrRoleSelf = errors.New("cannot modify own roles")

	ErrRoleAddExist          = errors.New("user already has role")
	ErrRoleAddMorePrivileged = errors.New("cannot add a more privileged role")

	ErrRoleRemoveNotExist       = errors.New("user doesn't have role")
	ErrRoleRemoveMorePrivileged = errors.New("cannot remove a more privileged role")
)
View Source
var ErrExpiresAtPast = errors.New("expires_at cannot be in the past")
View Source
var RolesMap = map[string]Role{
	"user":  UserRole,
	"admin": AdminRole,
	"super": SuperRole,
}

Functions

func NewError

func NewError(err error, kind Kind) error

NewError instantiates a new error.

Types

type Error

type Error struct {
	Kind    Kind
	Message string
}

Error represents a model error

func (*Error) Error

func (e *Error) Error() string

Error returns the message.

type Kind

type Kind uint8

Kind defines supported error types.

const (
	Other Kind = iota + 1 // Unclassified error.
	Conflict
	Permission
)

func (Kind) String

func (k Kind) String() string

type Model

type Model struct {
	Id        string     `bson:"id"`
	CreatedAt *time.Time `bson:"created_at"`
	CreatedBy any        `bson:"created_by"`
	DeletedAt *time.Time `bson:"deleted_at"`
	DeletedBy any        `bson:"deleted_by"`
	UpdatedAt *time.Time `bson:"updated_at"`
	UpdatedBy any        `bson:"updated_by"`
}

Model is the base model for all models. NewModel should be used unless you know what you're doing.

func NewModel

func NewModel() *Model

func (*Model) Create

func (m *Model) Create(id string)

func (*Model) Delete

func (m *Model) Delete(id string)

func (*Model) Update

func (m *Model) Update(id string)

type PersonalAccessToken

type PersonalAccessToken struct {
	Id        string     `bson:"id"`
	CreatedAt *time.Time `bson:"created_at"`
	ExpiresAt *time.Time `bson:"expires_at"`
	IsRevoked bool       `bson:"is_revoked"`
	Name      string     `bson:"name"`
	Token     string     `bson:"token"`
	UserId    string     `bson:"user_id"`
}

func NewPersonalAccessToken

func NewPersonalAccessToken(userId string, name string, expiresAt string) (*PersonalAccessToken, error)

func (*PersonalAccessToken) CreateResponse

func (*PersonalAccessToken) Encrypt

func (pat *PersonalAccessToken) Encrypt() error

func (*PersonalAccessToken) Response

func (*PersonalAccessToken) Validate

func (pat *PersonalAccessToken) Validate(s string) error

type PersonalAccessTokenCreateResponse

type PersonalAccessTokenCreateResponse struct {
	PersonalAccessTokenResponse
	Token string `json:"token"`
}

type PersonalAccessTokenResponse

type PersonalAccessTokenResponse struct {
	Id        string     `json:"id" bson:"id"`
	CreatedAt *time.Time `json:"created_at"`
	ExpiresAt *time.Time `json:"expires_at"`
	IsRevoked bool       `json:"is_revoked"`
	Name      string     `json:"name"`
	UserId    string     `json:"user_id"`
}

type PersonalAccessTokens

type PersonalAccessTokens []PersonalAccessToken

func (PersonalAccessTokens) Response

type PersonalAccessTokensResponse

type PersonalAccessTokensResponse struct {
	Tokens []PersonalAccessTokenResponse `json:"personal_access_tokens"`
}

type Ref

type Ref struct {
	Id string `json:"id" bson:"id"`
}

Ref is a reference to another document.

type Role

type Role int8
const (
	UserRole Role = iota + 1
	AdminRole
	SuperRole
)

func (Role) String

func (r Role) String() string

type Task

type Task struct {
	*Model      `bson:",inline"`
	Completed   bool       `bson:"completed"`
	CompletedAt *time.Time `bson:"completed_at"`
	CompletedBy any        `bson:"completed_by"`
	Title       string     `bson:"title"`
}

func NewTask

func NewTask() *Task

func (*Task) Complete

func (t *Task) Complete(id string)

func (*Task) Incomplete

func (t *Task) Incomplete()

func (*Task) MarshalBSON

func (t *Task) MarshalBSON() ([]byte, error)

func (*Task) Response

func (t *Task) Response() *TaskResponse

func (*Task) UnmarshalBSON

func (t *Task) UnmarshalBSON(data []byte) error

type TaskResponse

type TaskResponse struct {
	Id          string     `json:"id"`
	Completed   bool       `json:"completed"`
	CompletedAt *time.Time `json:"completed_at"`
	CompletedBy *UserRef   `json:"completed_by"`
	CreatedAt   *time.Time `json:"created_at"`
	CreatedBy   *UserRef   `json:"created_by"`
	DeletedAt   *time.Time `json:"-"`
	DeletedBy   *UserRef   `json:"-"`
	Title       string     `json:"title"`
	UpdatedAt   *time.Time `json:"updated_at"`
	UpdatedBy   *UserRef   `json:"updated_by"`
}

type TaskSearchParams

type TaskSearchParams struct {
	Completed []string
	CreatedBy string
	Queries   []string
	Limit     int
	Skip      int
}

type Tasks

type Tasks []Task

func (Tasks) Response

func (t Tasks) Response() *TasksResponse

type TasksResponse

type TasksResponse struct {
	Tasks []TaskResponse `json:"tasks"`
}

type User

type User struct {
	*Model        `bson:",inline"`
	BannedAt      *time.Time `bson:"banned_at"`
	BannedBy      any        `bson:"banned_by"`
	Bio           string     `bson:"bio"`
	Email         string     `bson:"email"`
	IsBanned      bool       `bson:"is_banned"`
	IsLocked      bool       `bson:"is_locked"`
	LastLoginAt   *time.Time `bson:"last_login_at"`
	LastLogoutAt  *time.Time `bson:"last_logout_at"`
	LastRefreshAt *time.Time `bson:"last_refresh_at"`
	LockedAt      *time.Time `bson:"locked_at"`
	LockedBy      any        `bson:"locked_by"`
	Name          string     `bson:"name"`
	Password      string     `bson:"password"`
	RefreshToken  string     `bson:"refresh_token"`
	Roles         []string   `bson:"roles"`
	UnbannedAt    *time.Time `bson:"unbanned_at"`
	UnbannedBy    any        `bson:"unbanned_by"`
	UnlockedAt    *time.Time `bson:"unlocked_at"`
	UnlockedBy    any        `bson:"unlocked_by"`
	Username      string     `bson:"username"`
}

func NewUser

func NewUser(email string, username string) *User

func NewUserWithRole

func NewUserWithRole(email string, username string, role Role) *User

func (*User) AddRole

func (u *User) AddRole(user *User, role Role) error

func (*User) AdminResponse

func (u *User) AdminResponse() *UserAdminResponse

func (*User) Ban

func (u *User) Ban(user *User) error

func (*User) HasRoleOrHigher

func (u *User) HasRoleOrHigher(role Role) bool

func (*User) Lock

func (u *User) Lock(user *User) error

func (*User) Login

func (u *User) Login() ([]byte, []byte, error)

func (*User) Logout

func (u *User) Logout()

func (*User) Ref

func (u *User) Ref() *UserRef

func (*User) Refresh

func (u *User) Refresh() ([]byte, []byte, error)

func (*User) RemoveRole

func (u *User) RemoveRole(user *User, role Role) error

func (*User) Response

func (u *User) Response() *UserResponse

func (*User) SetPassword

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

func (*User) Unban

func (u *User) Unban(user *User) error

func (*User) Unlock

func (u *User) Unlock(user *User) error

func (*User) ValidatePassword

func (u *User) ValidatePassword(s string) error

func (*User) ValidateRefreshToken

func (u *User) ValidateRefreshToken(s string) error

type UserAdminResponse

type UserAdminResponse struct {
	UserResponse
	IsBanned      bool       `json:"is_banned"`
	IsLocked      bool       `json:"is_locked"`
	LastLoginAt   *time.Time `json:"last_login_at"`
	LastLogoutAt  *time.Time `json:"last_logout_at"`
	LastRefreshAt *time.Time `json:"last_refresh_at"`
}

type UserRef

type UserRef struct {
	Ref
	Username string `json:"username" bson:"username"`
	Name     string `json:"name" bson:"name"`
}

type UserResponse

type UserResponse struct {
	Id        string     `json:"id"`
	Bio       string     `json:"bio"`
	CreatedAt *time.Time `json:"created_at"`
	Email     string     `json:"email,omitempty"`
	Name      string     `json:"name"`
	Roles     []string   `json:"-"`
	UpdatedAt *time.Time `json:"updated_at"`
	Username  string     `json:"username"`
}

type UserSearchParams

type UserSearchParams struct {
	Limit int
	Skip  int
}

type Users

type Users []User

func (Users) AdminResponse

func (users Users) AdminResponse() *UsersAdminResponse

func (Users) Response

func (users Users) Response() *UsersResponse

type UsersAdminResponse

type UsersAdminResponse struct {
	Users []UserAdminResponse `json:"users"`
}

type UsersResponse

type UsersResponse struct {
	Users []UserResponse `json:"users"`
}

Jump to

Keyboard shortcuts

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