anthropoi

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2019 License: MIT Imports: 9 Imported by: 0

README

Anthropoi

A simple accounts package and management tool.

What is it

This package sets up and manages user accounts with multi-site support and per-site groups.

Requirements

This package was made for use with PostgreSQL. CockroachDB probably won't work because of triggers.

Installing

Run this command to get the package:

go get -u github.com/Urethramancer/anthropoi

And run this to compile and install the management command:

go get -u github.com/Urethramancer/anthropoi/cmd/anthro

Using the package

(See built-in documentation for parameter information.)

  • New() creates a DBM structure to use for all further calls.
  • ConnectionString() rebuilds and returns a string based on the internally stored parameters.
  • Connect() opens the connection to the specified host, or localhost.
  • DatabaseExists() checks if there is a database of the specified name.
  • Create() creates a new account database with the specified name.
  • InitDatabase() sets up a new database with tables and triggers.

New() will use reasonable defaults for its connection string:

  • host: localhost
  • port: 5432
  • user: postgres
  • password: unused if blank
  • name: unused if blank
  • mode: disable if blank

Documentation

Index

Constants

View Source
const (
	DefaultName = "accounts"
)

Variables

This section is empty.

Functions

func GenString

func GenString(size int) string

GenString generates a random string, usable for passwords.

Types

type DBM

type DBM struct {
	*sql.DB
	// contains filtered or unexported fields
}

DBM is a DB manager for user accounts and groups.

func New

func New(host, port, user, password, mode string) *DBM

New DBM setup.

func (*DBM) AddProfile

func (db *DBM) AddProfile(id, user int64, domain, data string) *Profile

AddProfile creates a new profile.

func (*DBM) AddUser

func (db *DBM) AddUser(username, password, email, first, last, data, tokens string, cost int) (*User, error)

AddUser creates a new User. This may fail.

func (*DBM) Connect

func (db *DBM) Connect(name string) error

OpenDB and set the pointer in the DBM struct.

func (*DBM) ConnectionString

func (db *DBM) ConnectionString() string

func (*DBM) Create

func (db *DBM) Create(name string) error

Create the database and retain the name.

func (*DBM) DatabaseExists

func (db *DBM) DatabaseExists(name string) bool

DatabaseExists checks for the existence of the actual database.

func (*DBM) DeleteUser

func (db *DBM) DeleteUser(id int64) error

DeleteUser by ID.

func (*DBM) DeleteUserByName

func (db *DBM) DeleteUserByName(name string) error

DeleteUserByName for when that's needed.

func (*DBM) Drop

func (db *DBM) Drop(name string) error

Drop a named database.

func (*DBM) GetUser

func (db *DBM) GetUser(id int64) (*User, error)

GetUser returns a User based on an ID.

func (*DBM) GetUserByName

func (db *DBM) GetUserByName(name string) (*User, error)

GetUserByName for when you don't have an id.

func (*DBM) InitDatabase

func (db *DBM) InitDatabase() error

InitDatabase creates the tables, functions and triggers required for the full account system.

func (*DBM) SaveUser

func (db *DBM) SaveUser(u *User) error

UpdateUser saves an existing user by ID.

type Group

type Group struct {
	// ID of group in the database.
	ID int64
	// Name of group.
	Name string
	// Permissions which can be set in this group.
	Permissions []string
}

Group structures contain the ID, name and a list of possible permissions.

func NewGroup

func NewGroup(id int64, name string, permissions []string) *Group

NewGroup creates an initialised group structure.

func (*Group) AddPermissions

func (g *Group) AddPermissions(p ...string)

AddPermissions and sort.

func (*Group) RemovePermission

func (g *Group) RemovePermission(p string)

RemovePermission from group.

type Profile

type Profile struct {
	// ID of profile in the database.
	ID int64
	// User ID this profile belongs to.
	User int64
	// Domain this profile is for. This may be an actual Internet
	// domain or the display name for a site/business.
	Domain string
	// Groups the user belongs to on this site, with permissions.
	Groups map[string]string
	// Data for the site. Usually a custom JSON structure.
	Data string
}

Profile for a site or business. One or more of these exist per user account.

func (*Profile) RemoveGroup

func (p *Profile) RemoveGroup(group string)

RemoveGroup if it exists in the profile.

func (*Profile) SetGroup

func (p *Profile) SetGroup(group string, permissions []string)

SetGroup adds or creates a group permissions entry to the profile.

type User

type User struct {

	// ID of user in the database.
	ID int64
	// Username to log in with.
	Usermame string
	// Password for user account.
	Password string
	// Salt for the password.
	Salt string
	// Email to verify account or reset password.
	Email string
	// Created timestamp.
	Created time.Time
	// Locked accounts can't log in.
	Locked bool

	// Profiles for specific sites.
	Profiles []*Profile

	// First name of user (optional).
	First string
	// Last name of user (optional).
	Last string
	// Data for the account. JSON field for all the customising you need.
	Data string
	// Tokens is meant to store any authentication tokens required for external sites.
	Tokens string
}

User account structure holds basic login and personal information.

func (*User) CheckPassword

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

CheckPassword against the account's hash.

func (*User) SetPassword

func (u *User) SetPassword(password string, cost int) error

SetPassword generates a new salt and sets the password.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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