users

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package users implements user management and verification for a web application.

Index

Constants

View Source
const (
	// user status values
	UserSuspended = 0 // blocked from access or registration
	UserKnown     = 1 // allowed to register and set display name and password
	UserActive    = 2 // registered

	MaxName = 60 // maximum name characters
)

Variables

View Source
var ErrInvalidCredentials = errors.New("webparts/users: invalid credentials")
View Source
var WebFiles embed.FS

WebFiles are the package's web resources (templates and static files)

Functions

This section is empty.

Types

type App

type App interface {
	// Authenticated adds a logged-in user's ID to the session
	Authenticated(r *http.Request, id int64)

	// Flash adds a confirmation message to the next page, via the session
	Flash(r *http.Request, msg string)

	// GetRedirect returns the next page after log-in, probably from a session key
	GetRedirect(r *http.Request) string

	// Log optionally records an error
	Log(error)

	// LogThreat optionally records a rejected request to sign-up or log-in
	LogThreat(msg string, r *http.Request)

	// OnRemoveUser is called to delete any application data for a user
	OnRemoveUser(tx etx.TxId, user *User)

	// Render writes an HTTP response using the specified template and template field Users
	Render(w http.ResponseWriter, r *http.Request, template string, usersData interface{})

	// Rollback requests that the transaction started by Serialise be cancelled.
	Rollback()

	// Serialise optionally requests application-level serialisation.
	// If updates=true, the store is to be updated and a transaction might be started (especially if a user is to be added or deleted).
	// The returned function will be called at the end of the operation.
	Serialise(updates bool) func()

	// Token returns a token to be added to the form as the hidden field csrf_token.
	Token(r *http.Request) string
}

App is the interface to functions provided by the parent application.

type User

type User struct {
	Id       int64     // database ID
	Parent   int64     // parent ID, if multiple sets of user are supported
	Username string    // unique name for user, typically an email address
	Name     string    // display name for user
	Role     int       // user's role (normal, administrator, etc.)
	Status   int       // user's status
	Password []byte    // hashed password
	Created  time.Time // time of first registration
}

User struct holds the stored data for a user.

func (*User) SetPassword

func (us *User) SetPassword(pwd string) error

setPassword stores a password hash

type UserFormData

type UserFormData struct {
	multiforms.Child
	Username    string
	DisplayName string
	NUser       int64
	Role        int
	Status      int
}

type UserStore

type UserStore interface {
	ByName() []*User                         // all users, in name order
	DeleteId(id int64) error                 // delete user
	Get(id int64) (*User, error)             // get user by database ID
	GetNamed(username string) (*User, error) // get user for username (expected to be unique)
	IsNoRecord(error) bool                   // true if error is "record not found"
	Name(id int64) string                    // get display name for user by database ID
	Rollback()                               // (redundant)
	Update(s *User) error                    // add or update user
}

UserStore is the interface for storage and update of user information. To be implemented by the parent application. Id and Username are unique keys for a user.

type Users

type Users struct {
	App   App
	Roles []string
	Store UserStore
	TM    *etx.TM
}

Users holds the dependencies of this package on the parent application. It has no state of its own.

func (*Users) GetFormEdit

func (u *Users) GetFormEdit(w http.ResponseWriter, r *http.Request)

GetFormEdit renders the form to manage users.

func (*Users) GetFormLogin

func (u *Users) GetFormLogin(w http.ResponseWriter, r *http.Request)

GetFormLogin renders the form for a user to log in.

func (*Users) GetFormSignup

func (u *Users) GetFormSignup(w http.ResponseWriter, r *http.Request)

GetFormSignup renders the form for a pre-approved user to sign-up.

func (*Users) NewUsersForm

func (u *Users) NewUsersForm(data url.Values, token string) *UsersForm

NewUsersForm returns a form to edit users.

func (*Users) PostFormEdit

func (u *Users) PostFormEdit(w http.ResponseWriter, r *http.Request)

PostFormUsers processes the form with changes to users.

func (*Users) PostFormLogin

func (u *Users) PostFormLogin(w http.ResponseWriter, r *http.Request)

PostFormLogin processes the log-in form.

func (*Users) PostFormSignup

func (u *Users) PostFormSignup(w http.ResponseWriter, r *http.Request)

PostFormSignup processes the sign-up form.

func (*Users) UserDisplayName

func (u *Users) UserDisplayName(userId int64) string

UserDisplayName returns the display name for a user.

type UsersForm

type UsersForm struct {
	multiforms.Form
	RoleOpts   []string
	StatusOpts []string
	Children   []*UserFormData
	App        interface{}
}

func (*UsersForm) Add

func (f *UsersForm) Add(index int, u *User)

Add appends a user sub-form to the form.

func (*UsersForm) AddTemplate

func (f *UsersForm) AddTemplate()

AddTemplate appends the sub-form template for new users.

func (*UsersForm) GetUsers

func (f *UsersForm) GetUsers(nRoleOpts int) (items []*UserFormData, err error)

GetUsers returns the user data as an array of structs. They are sent in the HTML form as arrays of values for each field name.

Jump to

Keyboard shortcuts

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