users

package
v0.0.0-...-60192f8 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package users contains the models and functions to manage users.

Index

Constants

View Source
const (
	// TableName is the user table name in database.
	TableName = "user"
)

Variables

View Source
var (
	// Users is the user manager.
	Users = Manager{}

	// ErrNotFound is returned when a user record was not found.
	ErrNotFound = errors.New("not found")
)
View Source
var IsValidPassword = forms.StringValidator(func(v string) bool {
	if strings.TrimSpace(v) == "" {
		return false
	}
	return len(v) >= 8
}, errors.New("password must be at least 8 character long"))

IsValidPassword is the password validation rule.

View Source
var IsValidUsername = forms.StringValidator(func(v string) bool {
	return rxUsername.MatchString(v)
}, errors.New(`must contain English letters, digits, "_" and "-" only`))

IsValidUsername is the username validation rule.

Functions

func NewRolesField

func NewRolesField(tr forms.Translator, user *User) forms.Field

NewRolesField returns a forms.Field with user's role choices.

Types

type Manager

type Manager struct{}

Manager is a query helper for user entries.

func (*Manager) Count

func (m *Manager) Count() (int64, error)

Count returns the number of user in the database.

func (*Manager) Create

func (m *Manager) Create(user *User) error

Create insert a new user in the database. The password must be present. It will be hashed and updated before insertion.

func (*Manager) GetOne

func (m *Manager) GetOne(expressions ...goqu.Expression) (*User, error)

GetOne executes the a select query and returns the first result or an error when there's no result.

func (*Manager) Query

func (m *Manager) Query() *goqu.SelectDataset

Query returns a prepared goqu SelectDataset that can be extended later.

type ReaderSettings

type ReaderSettings struct {
	Font       string `json:"font"`
	FontSize   int    `json:"font_size"`
	LineHeight int    `json:"line_height"`
}

ReaderSettings contains the reader settings.

type User

type User struct {
	ID       int           `db:"id" goqu:"skipinsert,skipupdate"`
	Created  time.Time     `db:"created" goqu:"skipupdate"`
	Updated  time.Time     `db:"updated"`
	Username string        `db:"username"`
	Email    string        `db:"email"`
	Password string        `db:"password"`
	Group    string        `db:"group"`
	Settings *UserSettings `db:"settings"`
	Seed     int           `db:"seed"`
}

User is a user record in database.

func (*User) CheckPassword

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

CheckPassword checks if the given password matches the current user password.

func (*User) Delete

func (u *User) Delete() error

Delete removes a user from the database.

func (*User) GetLastModified

func (u *User) GetLastModified() []time.Time

GetLastModified returns the last modified times.

func (*User) GetSumStrings

func (u *User) GetSumStrings() []string

GetSumStrings returns the string used to generate the etag of the user.

func (*User) HasPermission

func (u *User) HasPermission(obj, act string) bool

HasPermission returns true if the user can perform "act" action on "obj" object.

func (*User) HashPassword

func (u *User) HashPassword(password string) (string, error)

HashPassword returns a new hashed password.

func (*User) IsAnonymous

func (u *User) IsAnonymous() bool

IsAnonymous returns true when the instance is not set to any existing user (when ID is 0).

func (*User) Permissions

func (u *User) Permissions() []string

Permissions returns all the user's implicit permissions.

func (*User) Save

func (u *User) Save() error

Save updates all the user values.

func (*User) SetPassword

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

SetPassword set a new user password.

func (*User) SetSeed

func (u *User) SetSeed() int

SetSeed sets a new seed to the user. It returns the seed as an integer value and does *not* save the data but the seed is accessible on the user instance.

func (*User) Update

func (u *User) Update(v interface{}) error

Update updates some user values.

type UserForm

type UserForm struct {
	*forms.Form
}

UserForm is the form used for user creation and update.

func NewUserForm

func NewUserForm(tr forms.Translator) (f *UserForm)

NewUserForm returns a UserForm instance.

func (*UserForm) Bind

func (f *UserForm) Bind()

Bind prepares the form before data binding. It changes some validators in case of user update.

func (*UserForm) CreateUser

func (f *UserForm) CreateUser() (*User, error)

CreateUser performs the user creation.

func (*UserForm) SetUser

func (f *UserForm) SetUser(u *User)

SetUser adds a user to the form's context.

func (*UserForm) UpdateUser

func (f *UserForm) UpdateUser(u *User) (res map[string]interface{}, err error)

UpdateUser performs a user update and returns a mapping of updated fields.

func (*UserForm) Validate

func (f *UserForm) Validate()

Validate performs extra form validation.

type UserSettings

type UserSettings struct {
	DebugInfo      bool           `json:"debug_info"`
	Lang           string         `json:"lang"`
	ReaderSettings ReaderSettings `json:"reader_settings"`
}

UserSettings contains some user settings.

func (*UserSettings) Scan

func (s *UserSettings) Scan(value interface{}) error

Scan loads a UserSettings instance from a column.

func (*UserSettings) Value

func (s *UserSettings) Value() (driver.Value, error)

Value encodes a UserSettings value for storage.

Jump to

Keyboard shortcuts

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