sqluser

package
v0.0.0-...-2c17daf Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: MIT, MIT Imports: 14 Imported by: 0

README

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

Documentation

Index

Constants

View Source
const (
	//FlagEmpty sql user create flag empty
	FlagEmpty = 0
	//FlagWithAccount sql user create flag with account module
	FlagWithAccount = 1
	//FlagWithPassword sql user create flag with password module
	FlagWithPassword = 2
	//FlagWithToken sql user create flag with token module
	FlagWithToken = 4
	//FlagWithUser sql user create flag with user module
	FlagWithUser = 8
)

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) (member.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
	Service *member.Service
}

AccountMapper account mapper

func (*AccountMapper) AccountToUID

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

AccountToUID find user by account. Return user id and any error if rasied. If user not found,a empty string will be returned.

func (*AccountMapper) AccountToUIDOrRegister

func (a *AccountMapper) AccountToUIDOrRegister(account *user.Account) (uid string, registerd bool, err error)

AccountToUIDOrRegister find a user by account.if user didnot exist,a new user will be created. Return user id and any error if raised.

func (*AccountMapper) Accounts

func (a *AccountMapper) Accounts(uid ...string) (*member.Accounts, error)

Accounts get member account map by user id list. Return account map and any error if rasied. User unfound in account map will be a nil value.

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) BindAccount

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

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

func (*AccountMapper) Execute

func (a *AccountMapper) Execute(service *member.Service)

Execute install account module to member service as provider

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) FindAllByUID

func (a *AccountMapper) FindAllByUID(uids ...string) ([]AccountModel, error)

FindAllByUID find account models by user id list. Retrun account models and any error if rased.

func (*AccountMapper) FindOrInsert

func (a *AccountMapper) FindOrInsert(UIDGenerater func() (string, error), account *user.Account) (string, bool, error)

FindOrInsert find user by account.if account did not exists,a new user with given account will be created. UIDGenerater used when create new user. Return user id and any error if raised.

func (*AccountMapper) Insert

func (a *AccountMapper) Insert(uid string, keyword string, account string) error

Insert create new user with given account. Return any error if raised. If account exists,member.ErrAccountRegisterExists will raise.

func (*AccountMapper) Register

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

Register register a user with special account. Return user id and any error if raised. If account exists,member.ErrAccountRegisterExists will raise.

func (*AccountMapper) Unbind

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

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

func (*AccountMapper) UnbindAccount

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

UnbindAccount unbind account from user. Return any error if rasied.

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 *member.Service) 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
	Service *member.Service
}

PasswordMapper password mapper

func (*PasswordMapper) Execute

func (p *PasswordMapper) Execute(service *member.Service)

Execute install passowrd module to member service as provider

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) PasswordChangeable

func (p *PasswordMapper) PasswordChangeable() bool

PasswordChangeable return password changeable

func (*PasswordMapper) UpdatePassword

func (p *PasswordMapper) UpdatePassword(uid string, password string) error

UpdatePassword update user password.If user password does not exist,new password record will be created. Return any error if raised.

func (*PasswordMapper) VerifyPassword

func (p *PasswordMapper) VerifyPassword(uid string, password string) (bool, error)

VerifyPassword Verify user password. Return verify and any error if raised. if user not found,error member.ErrUserNotFound will be raised.

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
	Service *member.Service
}

TokenMapper token mapper

func (*TokenMapper) Execute

func (t *TokenMapper) Execute(service *member.Service)

Execute install token module to member service as provider

func (*TokenMapper) FindAllByUID

func (t *TokenMapper) FindAllByUID(uids ...string) ([]TokenModel, error)

FindAllByUID find all token model by uid list. Return token models and any error if raised.

func (*TokenMapper) InsertOrUpdate

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

InsertOrUpdate insert or update user token record.

func (*TokenMapper) Revoke

func (t *TokenMapper) Revoke(uid string) (string, error)

Revoke revoke and regenerate a new token to user.if revoke record does not exist,a new record will be created. Return new user token and any error if raised.

func (*TokenMapper) Tokens

func (t *TokenMapper) Tokens(uid ...string) (member.Tokens, error)

Tokens get member token map by user id list. Return token map and any error if rasied. User unfound in token map will be a nil value.

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
	//Flag sqluser modules create flg.
	Flag int
	//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 passwrod 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(db db.Database, uidgenerater func() (string, error), flag int) *User

New create User framework with given database ,uidgeneraterand falg. flag is values combine with flags to special which modules used. For example ,New(db,FlagWithAccount | FlagWithToken)

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) HasFlag

func (u *User) HasFlag(flag int) bool

HasFlag check if sqluser module created with special flag.

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
	Service *member.Service
}

UserMapper user mapper

func (*UserMapper) Execute

func (u *UserMapper) Execute(service *member.Service)

Execute install user module to member service as provider

func (*UserMapper) FindAllByUID

func (u *UserMapper) FindAllByUID(uids ...string) ([]UserModel, error)

FindAllByUID find user models by user id list. Return User model list and any error if raised.

func (*UserMapper) InsertOrUpdate

func (u *UserMapper) InsertOrUpdate(uid string, status member.Status) error

InsertOrUpdate insert or update user model with status. Return any error if raised.

func (*UserMapper) SetStatus

func (u *UserMapper) SetStatus(uid string, status member.Status) error

SetStatus set user status. Return any error if raised.

func (*UserMapper) Statuses

func (u *UserMapper) Statuses(uid ...string) (member.StatusMap, error)

Statuses get member status map by user id list. Return status map and any error if rasied. User unfound in token map will be false.

func (*UserMapper) SupportedStatus

func (u *UserMapper) SupportedStatus() map[member.Status]bool

SupportedStatus return supported status map

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