Documentation
¶
Index ¶
- Constants
- Variables
- func RandomBytes() (string, error)
- func Timestamp() (string, error)
- type AccountMapper
- func (a *AccountMapper) AccountToUID(account *user.Account) (uid string, err error)
- func (a *AccountMapper) AccountToUIDOrRegister(account *user.Account) (uid string, registerd bool, err error)
- func (a *AccountMapper) Accounts(uid ...string) (*member.Accounts, error)
- func (a *AccountMapper) Bind(uid string, account *user.Account) error
- func (a *AccountMapper) BindAccount(uid string, account *user.Account) error
- func (a *AccountMapper) Execute(service *member.Service)
- func (a *AccountMapper) Find(keyword string, account string) (AccountModel, error)
- func (a *AccountMapper) FindAllByUID(uids ...string) ([]AccountModel, error)
- func (a *AccountMapper) FindOrInsert(UIDGenerater func() (string, error), account *user.Account) (string, bool, error)
- func (a *AccountMapper) Insert(uid string, keyword string, account string) error
- func (a *AccountMapper) Register(account *user.Account) (uid string, err error)
- func (a *AccountMapper) Unbind(uid string, account *user.Account) error
- func (a *AccountMapper) UnbindAccount(uid string, account *user.Account) error
- type AccountModel
- type Config
- type HashFunc
- type PasswordMapper
- func (p *PasswordMapper) Execute(service *member.Service)
- func (p *PasswordMapper) Find(uid string) (PasswordModel, error)
- func (p *PasswordMapper) InsertOrUpdate(model *PasswordModel) error
- func (p *PasswordMapper) PasswordChangeable() bool
- func (p *PasswordMapper) UpdatePassword(uid string, password string) error
- func (p *PasswordMapper) VerifyPassword(uid string, password string) (bool, error)
- type PasswordModel
- type Tables
- type TokenMapper
- func (t *TokenMapper) Execute(service *member.Service)
- func (t *TokenMapper) FindAllByUID(uids ...string) ([]TokenModel, error)
- func (t *TokenMapper) InsertOrUpdate(uid string, token string) error
- func (t *TokenMapper) Revoke(uid string) (string, error)
- func (t *TokenMapper) Tokens(uid ...string) (member.Tokens, error)
- type TokenModel
- type User
- func (u *User) Account() *AccountMapper
- func (u *User) AccountTableName() string
- func (u *User) AddTablePrefix(prefix string)
- func (u *User) HasFlag(flag int) bool
- func (u *User) Password() *PasswordMapper
- func (u *User) PasswordTableName() string
- func (u *User) Token() *TokenMapper
- func (u *User) TokenTableName() string
- func (u *User) User() *UserMapper
- func (u *User) UserTableName() string
- type UserMapper
- func (u *UserMapper) Execute(service *member.Service)
- func (u *UserMapper) FindAllByUID(uids ...string) ([]UserModel, error)
- func (u *UserMapper) InsertOrUpdate(uid string, status member.Status) error
- func (u *UserMapper) SetStatus(uid string, status member.Status) error
- func (u *UserMapper) Statuses(uid ...string) (member.StatusMap, error)
- func (u *UserMapper) SupportedStatus() map[member.Status]bool
- type UserModel
Constants ¶
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 ¶
var DefaultAccountMapperName = "account"
DefaultAccountMapperName default database table name for module account.
var DefaultHashMethod = "sha256"
DefaultHashMethod default hash method when created password data.
var DefaultPasswordMapperName = "password"
DefaultPasswordMapperName default database table name for module password.
var DefaultTokenMapperName = "token"
DefaultTokenMapperName default database table name for module token.
var DefaultUserMapperName = "user"
DefaultUserMapperName default database table name for module user.
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 }
var ErrHashMethodNotFound = errors.New("password hash method not found")
ErrHashMethodNotFound error raised when password hash method not found.
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.
var RandomBytesLength = 32
RandomBytesLength bytes length for RandomBytes function.
Functions ¶
func RandomBytes ¶
RandomBytes string generater return random bytes. Default length is 32 byte.You can change default length by change sqluesr.RandomBytesLength .
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 ¶
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.
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 ¶
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) AccountTableName ¶
AccountTableName return actual account database table name.
func (*User) AddTablePrefix ¶
AddTablePrefix add prefix to user table names.
func (*User) PasswordTableName ¶
PasswordTableName return actual password database table name.
func (*User) TokenTableName ¶
TokenTableName return actual token database table name.
func (*User) UserTableName ¶
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