sqlusersystem

package
v0.0.0-...-be25ffa Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2021 License: MIT Imports: 19 Imported by: 0

README

sqluser 基于 SQL 数据库的用户实现

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultAccountMapperName = "account"

DefaultAccountMapperName default database table name for module account.

View Source
var DefaultHashMethod = "sha256"

DefaultHashMethod default hash method when created password data.

View Source
var DefaultPasswordMapperName = "password"

DefaultPasswordMapperName default database table name for module password.

View Source
var DefaultTokenMapperName = "token"

DefaultTokenMapperName default database table name for module token.

View Source
var DefaultUserMapperName = "user"

DefaultUserMapperName default database table name for module user.

View Source
var DirectiveFactory = func(loader func(v interface{}) error) (usersystem.Directive, error) {
	c := &Config{}
	err := loader(c)

	if err != nil {
		return nil, err
	}

	return c, nil
}
View Source
var ErrHashMethodNotFound = errors.New("password hash method not found")

ErrHashMethodNotFound error raised when password hash method not found.

View Source
var HashFuncMap = map[string]HashFunc{
	"sha256": func(key string, salt string, password string) ([]byte, error) {
		var val = []byte(key + salt + password)
		var s256 = sha256.New()
		s256.Write(val)
		val = s256.Sum(nil)
		s256.Write(val)
		return []byte(hex.EncodeToString(s256.Sum(nil))), nil
	},
}

HashFuncMap all available password hash func. You can insert custom hash func into this map.

View Source
var RandomBytesLength = 32

RandomBytesLength bytes length for RandomBytes function.

Functions

func RandomBytes

func RandomBytes() (string, error)

RandomBytes string generater return random bytes. Default length is 32 byte.You can change default length by change sqluesr.RandomBytesLength .

func Timestamp

func Timestamp() (string, error)

Timestamp string generater return timestamp in nano.

Types

type AccountMapper

type AccountMapper struct {
	*modelmapper.ModelMapper
	User *User
}

AccountMapper account mapper

func (*AccountMapper) Bind

func (a *AccountMapper) Bind(uid string, account *user.Account) error

Bind bind account to user. Return any error if raised. If account exists, error user.ErrAccountBindingExists will raised.

func (*AccountMapper) Find

func (a *AccountMapper) Find(keyword string, account string) (*AccountModel, error)

Find find account by given keyword and account. Return account model and any error if raised.

func (*AccountMapper) MustAccountToUID

func (a *AccountMapper) MustAccountToUID(account *user.Account) (uid string)

MustAccountToUID query uid by user account. Return user id .

func (*AccountMapper) MustAccounts

func (a *AccountMapper) MustAccounts(uid string) *user.Accounts

MustAccounts return accounts of give uid.

func (*AccountMapper) MustBindAccount

func (a *AccountMapper) MustBindAccount(uid string, account *user.Account)

MustBindAccount bind account to user. If account exists, error user.ErrAccountBindingExists will raised.

func (*AccountMapper) MustUnbindAccount

func (a *AccountMapper) MustUnbindAccount(uid string, account *user.Account)

MustUnbindAccount unbind account from user.

func (*AccountMapper) Purge

func (a *AccountMapper) Purge(string) error

Purge purge user data cache

func (*AccountMapper) Start

func (a *AccountMapper) Start() error

func (*AccountMapper) Stop

func (a *AccountMapper) Stop() error

Stop stop service

func (*AccountMapper) Unbind

func (a *AccountMapper) Unbind(uid string, account *user.Account) error

Unbind unbind account from user. Return any error if raised.

type AccountModel

type AccountModel struct {
	//UID user id.
	UID string
	//Keyword account keyword.
	Keyword string
	//Account account name.
	Account string
	//CreatedTime created timestamp in second.
	CreatedTime int64
}

AccountModel account data model

type Config

type Config struct {
	Database      *db.Config
	TableAccount  string
	TablePassword string
	TableToken    string
	TableUser     string
	Prefix        string
}

func (*Config) ApplyToUser

func (c *Config) ApplyToUser(u *User) error

func (*Config) Execute

func (c *Config) Execute(s *usersystem.UserSystem) error

type HashFunc

type HashFunc func(key string, salt string, password string) ([]byte, error)

HashFunc interaface of pasword hash func

type PasswordMapper

type PasswordMapper struct {
	*modelmapper.ModelMapper
	User *User
}

PasswordMapper password mapper

func (*PasswordMapper) Find

func (p *PasswordMapper) Find(uid string) (PasswordModel, error)

Find find password model by userd id. Return any error if raised.

func (*PasswordMapper) InsertOrUpdate

func (p *PasswordMapper) InsertOrUpdate(model *PasswordModel) error

InsertOrUpdate insert or update password model. Return any error if raised.

func (*PasswordMapper) MustUpdatePassword

func (p *PasswordMapper) MustUpdatePassword(uid string, password string)

UpdatePassword update user password.If user password does not exist,new password record will be created.

func (*PasswordMapper) MustVerifyPassword

func (p *PasswordMapper) MustVerifyPassword(uid string, password string) bool

VerifyPassword Verify user password. if user not found,error user.ErrUserNotExists will be raised.

func (*PasswordMapper) PasswordChangeable

func (p *PasswordMapper) PasswordChangeable() bool

PasswordChangeable return password changeable

func (*PasswordMapper) Purge

func (p *PasswordMapper) Purge(string) error

Purge purge user data cache

func (*PasswordMapper) Start

func (p *PasswordMapper) Start() error

Start start service

func (*PasswordMapper) Stop

func (p *PasswordMapper) Stop() error

Stop stop service

type PasswordModel

type PasswordModel struct {
	//UID user id.
	UID string
	//HashMethod hash method to verify this password.
	HashMethod string
	//Salt random salt.
	Salt string
	//Password hashed password data.
	Password []byte
	//UpdatedTime updated timestamp in second.
	UpdatedTime int64
}

PasswordModel password data model

type Tables

type Tables struct {
	AccountMapperName  string
	PasswordMapperName string
	TokenMapperName    string
	UserMapperName     string
}

Tables struct stores table info.

type TokenMapper

type TokenMapper struct {
	*modelmapper.ModelMapper
	User *User
}

TokenMapper token mapper

func (*TokenMapper) InsertOrUpdate

func (t *TokenMapper) InsertOrUpdate(uid string, token string) error

InsertOrUpdate insert or update user token record.

func (*TokenMapper) MustCurrentTerm

func (t *TokenMapper) MustCurrentTerm(uid string) string

func (*TokenMapper) MustStartNewTerm

func (t *TokenMapper) MustStartNewTerm(uid string) string

func (*TokenMapper) Purge

func (t *TokenMapper) Purge(string) error

Purge purge user data cache

func (*TokenMapper) Start

func (t *TokenMapper) Start() error

Start start service

func (*TokenMapper) Stop

func (t *TokenMapper) Stop() error

Stop stop service

type TokenModel

type TokenModel struct {
	//UID user id
	UID string
	//Token current user token
	Token string
	//UpdatedTime updated timestamp in second.
	UpdatedTime string
}

TokenModel token data model

type User

type User struct {
	//DB database used.
	DB db.Database
	//Tables table name info.
	Tables Tables
	//UIDGenerater string generater for uid
	//default value is uuid
	UIDGenerater func() (string, error)
	//TokenGenerater string generater for usertoken
	//default value is timestamp
	TokenGenerater func() (string, error)
	//SaltGenerater string generater for salt
	//default value is 32 byte length random bytes.
	SaltGenerater func() (string, error)
	//HashMethod hash method which used to generate new salt.
	//default value is sha256
	HashMethod string
	//PasswordKey static key used in password hash generater.
	//default value is empty.
	//You can change this value after sqluser init.
	PasswordKey string
	//QueryBuilder sql query builder
	QueryBuilder *querybuilder.Builder
}

User main struct of sqluser module.

func New

func New() *User

New create User framework

func (*User) Account

func (u *User) Account() *AccountMapper

Account return account mapper

func (*User) AccountTableName

func (u *User) AccountTableName() string

AccountTableName return actual account database table name.

func (*User) AddTablePrefix

func (u *User) AddTablePrefix(prefix string)

AddTablePrefix add prefix to user table names.

func (*User) Password

func (u *User) Password() *PasswordMapper

Password return password mapper

func (*User) PasswordTableName

func (u *User) PasswordTableName() string

PasswordTableName return actual password database table name.

func (*User) Token

func (u *User) Token() *TokenMapper

Token return token mapper

func (*User) TokenTableName

func (u *User) TokenTableName() string

TokenTableName return actual token database table name.

func (*User) User

func (u *User) User() *UserMapper

User return user mapper

func (*User) UserTableName

func (u *User) UserTableName() string

UserTableName return actual user database table name.

type UserMapper

type UserMapper struct {
	*modelmapper.ModelMapper
	User *User
}

UserMapper user mapper

func (*UserMapper) IsAvailable

func (u *UserMapper) IsAvailable(userstats status.Status) (bool, error)

IsAvailable check is status available

func (*UserMapper) Label

func (u *UserMapper) Label(userstats status.Status) (string, error)

Label get status label Empty string will be returned if status invalid

func (*UserMapper) MustCreateStatus

func (u *UserMapper) MustCreateStatus(uid string)

func (*UserMapper) MustListUsersByStatus

func (u *UserMapper) MustListUsersByStatus(last string, limit int, reverse bool, statuses ...status.Status) []string

func (*UserMapper) MustLoadStatus

func (u *UserMapper) MustLoadStatus(uid string) (status.Status, bool)

func (*UserMapper) MustRemoveStatus

func (u *UserMapper) MustRemoveStatus(uid string)

func (*UserMapper) MustUpdateStatus

func (u *UserMapper) MustUpdateStatus(uid string, userstatus status.Status)

func (*UserMapper) Purge

func (u *UserMapper) Purge(uid string) error

func (*UserMapper) Start

func (u *UserMapper) Start() error

func (*UserMapper) Stop

func (u *UserMapper) Stop() error

type UserModel

type UserModel struct {
	//UID user id
	UID string
	//CreatedTime created timestamp in second
	CreatedTime int64
	//UpdateTIme updated timestamp in second
	UpdateTIme int64
	//Status user status
	Status int
}

UserModel user data model

Jump to

Keyboard shortcuts

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