member

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: 11 Imported by: 2

README

Member 用户系统

member 用户系统是一套基于 github.com/herb-go/herb 的用户系统。 系统主要是定义了用户系统的常用操作,用户驱动的接口,以相应的缓存操作。

功能

  • 用户的状态/帐号/密码/令牌/权限/档案的驱动支持。可以混合使用 sql/ldap/第三方方案。
  • 与缓存系统的良好配合
  • 登录/登出/跳转登录等中间件的配合

依赖

  • github.com/herb-go/usersystem/user 基本用户模块
  • github.com/herb-go/deprecated/httpuser HTTP 用户模块
  • github.com/herb-go/user/role 用户权限
  • github.com/herb-go/user/role/roleservice 权限服务模块
  • github.com/herb-go/herb/cache 缓存
  • github.com/herb-go/session 会话模块

Documentation

Index

Constants

View Source
const DefaultSessionMemberTokenFieldName = "herb-member-token"

DefaultSessionMemberTokenFieldName default member token session field name when create member service.

View Source
const DefaultSessionUIDFieldName = "herb-member-uid"

DefaultSessionUIDFieldName default user id session field name when create member service.

View Source
const StatusBanned = Status(1)

StatusBanned user status banned

View Source
const StatusExpired = Status(4)

StatusExpired user status expried

View Source
const StatusNormal = Status(0)

StatusNormal user status normal

View Source
const StatusPending = Status(3)

StatusPending user status pending

View Source
const StatusRevoked = Status(2)

StatusRevoked user status revoked

Variables

View Source
var DefaultContextName = ContextType("members")

DefaultContextName default member context name.

View Source
var ErrAccountKeywordNotRegistered = errors.New("account keyword not regietered")

ErrAccountKeywordNotRegistered errors rasied when account keyword not regietered.

View Source
var ErrAccountRegisterExists = errors.New("account registered exists")

ErrAccountRegisterExists errors rasied when registered account ecists.

View Source
var ErrFeatureNotSupported = errors.New("feature not supported")

ErrFeatureNotSupported errors rasied when feature not supported by provider.

View Source
var ErrPasswordNotChangeable = errors.New("password not changeable")

ErrPasswordNotChangeable errors raised when password provider not support change password.

View Source
var ErrRegisteredDataNotMap = errors.New("registered user data is not a map")

ErrRegisteredDataNotMap errors rasied when registered user data struct is not a map struct.

View Source
var ErrStatusNotSupport = errors.New("user status not  support")

ErrStatusNotSupport errors rasied when user status is not support by provider.

View Source
var ErrUserBanned = errors.New("user banned")

ErrUserBanned errors rasied when user status is banned.

View Source
var ErrUserNotFound = errors.New("user not found")

ErrUserNotFound errors rasied when user is not found.

View Source
var StatusMapMin = map[Status]bool{
	StatusNormal: true,
	StatusBanned: true,
}

Functions

func IsAvaliable

func IsAvaliable(s *Status) bool

IsAvaliable check if user status is normal status.

Types

type Accounts

type Accounts map[string]user.Accounts

Accounts user accounts map.

type AccountsProvider

type AccountsProvider interface {
	//Accounts return account map of given uid list.
	//Return account map and any error if raised.
	Accounts(uid ...string) (*Accounts, error)
	//AccountToUID query uid by user account.
	//Return user id and any error if raised.
	//Return empty string as userid if account not found.
	AccountToUID(account *user.Account) (uid string, err error)
	//Register create new user with given account.
	//Return created user id and any error if raised.
	//Privoder should return ErrAccountRegisterExists if account is used.
	Register(account *user.Account) (uid string, err error)
	//AccountToUIDOrRegister query uid by user account.Register user if account not found.
	//Return user id and any error if raised.
	AccountToUIDOrRegister(account *user.Account) (uid string, registerd bool, err error)
	//BindAccount bind account to user.
	//Return any error if raised.
	//If account exists,user.ErrAccountBindingExists should be rasied.
	BindAccount(uid string, account *user.Account) error
	//UnbindAccount unbind account from user.
	//Return any error if raised.
	//If account not exists,user.ErrAccountUnbindingNotExists should be rasied.
	UnbindAccount(uid string, account *user.Account) error
}

AccountsProvider member account provider interface

type AccountsStore

type AccountsStore struct {
	*datastore.SyncMapStore
}

AccountsStore accounts data store

func NewAccountsStore

func NewAccountsStore() *AccountsStore

NewAccountsStore create new account data store

func (*AccountsStore) Get

func (s *AccountsStore) Get(uid string) user.Accounts

Get get acounts by given user id

type Authorizer

type Authorizer struct {
	Service      *Service
	PolicyLoader protecter.PolicyLoader
}

Authorizer comonets to Authorize http request. Should be created by Service.Authorize

func (*Authorizer) Authorize

func (a *Authorizer) Authorize(r *http.Request) (bool, error)

Authorize Authorize http request.

type ContextType

type ContextType string

ContextType member context name type.

type Directive

type Directive interface {
	Execute(*Service) error
}

type DirectiveFactory

type DirectiveFactory func(loader func(v interface{}) error) (Directive, error)

DirectiveFactory member Directive create factory.

type Members

type Members struct {
	Service     *Service
	Accounts    *AccountsStore
	StatusStore *StatusStore
	Tokens      *TokensStore
	Roles       *RolesStore
	Profiles    *ProfilesStore
	Dataset     map[string]datastore.Store
}

Members members stand for cached user data.

func NewMembers

func NewMembers(s *Service) *Members

NewMembers return empty members with given service.

func (*Members) Data

func (m *Members) Data(field string) datastore.Store

Data return named data field of members

func (*Members) LoadAccount

func (m *Members) LoadAccount(keys ...string) (*AccountsStore, error)

LoadAccount load accounts for users. loaded tokens will stored in members Accounts field. Return Accounts and any error if rased.

func (*Members) LoadData

func (m *Members) LoadData(field string, keys ...string) (datastore.Store, error)

LoadData load named data for users. loaded datas will stored in members Dataset field. Return datas and any error if rased.

func (*Members) LoadProfiles

func (m *Members) LoadProfiles(keys ...string) (*ProfilesStore, error)

LoadProfiles load profiles for users. loaded profiles will stored in members Profiles field. Return Profiles and any error if rased.

func (*Members) LoadRoles

func (m *Members) LoadRoles(keys ...string) (*RolesStore, error)

LoadRoles load roles for users. loaded roles will stored in members Roles field. Return Roles and any error if rased.

func (*Members) LoadStatus

func (m *Members) LoadStatus(keys ...string) (*StatusStore, error)

LoadStatus load banned status for users. loaded status will stored in members StatusStore field. Return status map and any error if rased.

func (*Members) LoadTokens

func (m *Members) LoadTokens(keys ...string) (*TokensStore, error)

LoadTokens load tokens for users. loaded tokens will stored in members Tokens field. Return Tokens and any error if rased.

func (*Members) Status

func (m *Members) Status(key string) (*Status, error)

Status return user status. Return user status and any error if rased.

type Option

type Option interface {
	//ApplyTo  apply option to service.
	ApplyTo(*Service) error
}

Option member service option interface

type OptionFunc

type OptionFunc func(*Service) error

OptionFunc member service option function interface.

func OptionCommon

func OptionCommon(store *session.Store) OptionFunc

OptionCommon common member service option function with give session store.

func OptionSubCache

func OptionSubCache(store *session.Store, c cache.Cacheable) OptionFunc

OptionSubCache option use sub cache node of give cache as all modules's cache.

func (OptionFunc) ApplyTo

func (i OptionFunc) ApplyTo(s *Service) error

ApplyTo apply option function to service.

type PasswordProvider

type PasswordProvider interface {
	VerifyPassword(uid string, password string) (bool, error)
	//PasswordChangeable return password changeable
	PasswordChangeable() bool
	//UpdatePassword update user password
	//Return any error if raised
	UpdatePassword(uid string, password string) error
}

PasswordProvider member password provider interface

type Profiles

type Profiles map[string]*profile.Profile

Profiles user profiles map type

func (*Profiles) Chain

func (p *Profiles) Chain(profiles *Profiles)

type ProfilesProvider

type ProfilesProvider interface {
	Profiles(uid ...string) (*Profiles, error)
	UpdateProfile(uid string, profile *profile.Profile) error
}

ProfilesProvider member role provider interface

type ProfilesStore

type ProfilesStore struct {
	*datastore.SyncMapStore
}

ProfilesStore user profiles data store

func NewProfilesStore

func NewProfilesStore() *ProfilesStore

NewProfilesStore create new user profiles data store

func (*ProfilesStore) Get

func (s *ProfilesStore) Get(uid string) *profile.Profile

Get get user profiles by given user id

type Roles

type Roles map[string]*role.Roles

Roles user role map type

type RolesProvider

type RolesProvider interface {
	Roles(uid ...string) (*Roles, error)
}

RolesProvider member role provider interface

type RolesStore

type RolesStore struct {
	*datastore.SyncMapStore
}

RolesStore user roles data store

func NewRolesStore

func NewRolesStore() *RolesStore

NewRolesStore create new user rolees data store

func (*RolesStore) Get

func (s *RolesStore) Get(uid string) *role.Roles

Get get user roles by given user id

type Service

type Service struct {
	//SessionStore session store which stores member data.
	SessionStore *session.Store
	//SessionUIDFieldName session field which stores user id.
	SessionUIDFieldName string
	//SessionMemberFieldName session field which stores member token.
	SessionMemberFieldName string
	//ContextName context name stores members data.
	ContextName ContextType
	//BannedProvider user banned status provider.
	//DON'T use this provider directly,use Service.Banned() instead.
	StatusProvider StatusProvider
	//BannedCache data stores banned status.
	StatusCache cache.Cacheable
	//AccountsProvider user accounts provider.
	//DON'T use this provider directly,use Service.Accounts() instead.
	AccountsProvider AccountsProvider
	//AccountsCache data stores user accounts.
	AccountsCache cache.Cacheable
	//TokenProvider user token provider.
	//DON'T use this provider directly,use Service.Tokens() instead.
	TokenProvider TokenProvider
	//TokenCache data stores user tokens.
	TokenCache cache.Cacheable
	//PasswordProvider user password provider.
	//DON'T use this provider directly,use Service.Password() instead.
	PasswordProvider PasswordProvider
	//RoleProvider user roles provider.
	//DON'T use this provider directly,use Service.Roles() instead.
	RoleProvider RolesProvider
	//RoleCache data stores user roles.
	RoleCache cache.Cacheable
	//DataProviders user data provider.
	//A map of registered data map type.
	DataProviders map[string]*datastore.DataSource
	//DataCache data stores user dataset.
	DataCache cache.Cacheable
	//User Profiles Providers
	ProfilesProviders []ProfilesProvider
	//AccountProviders registered account provider map.
	AccountProviders map[string]user.AccountProvider
}

Service member service main interafce.

func New

func New() *Service

New create new member service with given session store.

func (*Service) Accounts

func (s *Service) Accounts() *ServiceAccounts

Accounts return Accounts module.

func (*Service) AuthorizeMiddleware

func (s *Service) AuthorizeMiddleware(rs protecter.PolicyLoader, unauthorizedAction http.HandlerFunc) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

AuthorizeMiddleware return Authorize Middleware with special rule provider. Middleware will check user banned status if banned status provider is installed.

func (*Service) Authorizer

func (s *Service) Authorizer(rs protecter.PolicyLoader) httpuser.Authorizer

Authorizer create Authorizer with given rule provider.

func (*Service) BannedMiddleware

func (s *Service) BannedMiddleware() func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

BannedMiddleware return Authorize Middleware check only if user is banned

func (*Service) Data

func (s *Service) Data() *ServiceData

Data return Data modules. DEPRECATED

func (*Service) GetMembersFromRequest

func (s *Service) GetMembersFromRequest(r *http.Request) (members *Members)

GetMembersFromRequest get members data in http request context. Create new members data and bind to context if not exist. Return members data.

func (*Service) IdentifyRequest

func (s *Service) IdentifyRequest(r *http.Request) (uid string, err error)

IdentifyRequest Identify user in http request. Return user id and any error raised. If user is not logged in,returned user id will by empty string.

func (*Service) Init

func (s *Service) Init(option Option) error

Init servcei with given option.

func (*Service) Login

func (s *Service) Login(w http.ResponseWriter, r *http.Request, id string) error

Login login giver user to http request

func (*Service) LoginRequiredMiddleware

func (s *Service) LoginRequiredMiddleware(unauthorizedAction http.HandlerFunc) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

LoginRequiredMiddleware return login requred middleware with given unauthorizedAction.

func (*Service) Logout

func (s *Service) Logout(w http.ResponseWriter, r *http.Request) error

Logout Logout user in http request.

func (*Service) LogoutMiddleware

func (s *Service) LogoutMiddleware() func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

LogoutMiddleware return logout middleware.

func (*Service) MemberTokenField

func (s *Service) MemberTokenField() *session.Field

MemberTokenField return member token session field

func (*Service) NewAccount

func (s *Service) NewAccount(keyword string, account string) (*user.Account, error)

NewAccount create new account by given registered keyword and account name. Return created user account and any error if raised. Return ErrAccountKeywordNotRegistered if account keyword is not registered by Service.RegisterAccountType .

func (*Service) NewMembers

func (s *Service) NewMembers() *Members

NewMembers return new members data.

func (*Service) Password

func (s *Service) Password() *ServicePassword

Password return Password modules.

func (*Service) Profiles

func (s *Service) Profiles() *ServiceProfiles

Profiles return profiles modules.

func (*Service) RegisterAccountProvider

func (s *Service) RegisterAccountProvider(keyword string, t user.AccountProvider)

RegisterAccountProvider register account provider as keyword.

func (*Service) RegisterData

func (s *Service) RegisterData(key string, p *datastore.DataSource) error

RegisterData register data type as named data field. data type should implement DataProvider interface so that data module can create and load user data. Return any error if raised. DEPRECATED

func (*Service) RequestProfiles

func (s *Service) RequestProfiles(r *http.Request) (*profile.Profile, error)

func (*Service) Reset

func (s *Service) Reset()

func (*Service) Roles

func (s *Service) Roles() *ServiceRole

Roles return Roles modules.

func (*Service) RolesAuthorizeMiddleware

func (s *Service) RolesAuthorizeMiddleware(ruleNames ...string) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

RolesAuthorizeMiddleware return Authorize Middleware with roles as rule provider. Middleware will check user banned status if banned status provider is installed.

func (*Service) Status

func (s *Service) Status() *ServiceStatus

Status return Status modules.

func (*Service) Token

func (s *Service) Token() *ServiceToken

Token return Token modules.

func (*Service) UIDField

func (s *Service) UIDField() *session.Field

UIDField return user id session field

type ServiceAccounts

type ServiceAccounts struct {
	// contains filtered or unexported fields
}

ServiceAccounts Member accounts module.

func (*ServiceAccounts) AccountToUID

func (s *ServiceAccounts) AccountToUID(account *user.Account) (uid string, err error)

AccountToUID query uid by user account. Return user id and any error if raised. Return empty string as userid if account not found.

func (*ServiceAccounts) AccountToUIDOrRegister

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

AccountToUIDOrRegister query uid by user account.Register user if account not found. Return user id ,whether registered and any error if raised.

func (*ServiceAccounts) BindAccount

func (s *ServiceAccounts) BindAccount(uid string, account *user.Account) error

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

func (*ServiceAccounts) Cache

func (s *ServiceAccounts) Cache() cache.Cacheable

Cache Return member accounts cache.

func (*ServiceAccounts) Clean

func (s *ServiceAccounts) Clean(uid string) error

Clean clean accounts cache by uid. Return any error if raised.

func (*ServiceAccounts) Load

func (s *ServiceAccounts) Load(accounts datastore.Store, keys ...string) error

Load load and cache accounts from provider. Return any error if raised.

func (*ServiceAccounts) Register

func (s *ServiceAccounts) Register(account *user.Account) (uid string, err error)

Register create new user with given account. Return created user id and any error if raised.

func (*ServiceAccounts) UnbindAccount

func (s *ServiceAccounts) UnbindAccount(uid string, account *user.Account) error

UnbindAccount unbind account from user. user account cache will be cleand. Return any error if raised. If account not exists,user.ErrAccountUnbindingNotExists should be rasied.

type ServiceData

type ServiceData struct {
	// contains filtered or unexported fields
}

ServiceData member user data module. DEPRECATED

func (*ServiceData) Cache

func (s *ServiceData) Cache(field string) cache.Cacheable

Cache Return member user data cache. DEPRECATED

func (*ServiceData) Clean

func (s *ServiceData) Clean(field string, uid string) error

Clean clean member user data cache by uid. DEPRECATED

func (*ServiceData) Load

func (s *ServiceData) Load(field string, data datastore.Store, keys ...string) error

Load load and cache user data map from provider. Return any error if raised. DEPRECATED

type ServicePassword

type ServicePassword struct {
	// contains filtered or unexported fields
}

ServicePassword Member password module.

func (*ServicePassword) PasswordChangeable

func (s *ServicePassword) PasswordChangeable() bool

PasswordChangeable return password changeable

func (*ServicePassword) UpdatePassword

func (s *ServicePassword) UpdatePassword(uid string, password string) error

UpdatePassword update user password Return any error if raised

func (*ServicePassword) VerifyPassword

func (s *ServicePassword) VerifyPassword(uid string, password string) (bool, error)

VerifyPassword Verify user password. Return verify result and any error if raised

type ServiceProfiles

type ServiceProfiles struct {
	// contains filtered or unexported fields
}

ServiceProfiles member profile module.

func (*ServiceProfiles) Load

func (s *ServiceProfiles) Load(store datastore.Store, keys ...string) error

Load load user profiles from provider. Return any error if raised.

func (*ServiceProfiles) UpdateProfile

func (s *ServiceProfiles) UpdateProfile(uid string, profile *profile.Profile) error

type ServiceRole

type ServiceRole struct {
	// contains filtered or unexported fields
}

ServiceRole member role module.

func (*ServiceRole) Cache

func (s *ServiceRole) Cache() cache.Cacheable

Cache Return member role cache.

func (*ServiceRole) Clean

func (s *ServiceRole) Clean(uid string) error

Clean clean role cache by uid.

func (*ServiceRole) Load

func (s *ServiceRole) Load(store datastore.Store, keys ...string) error

Load load and cache user roles from provider. Return any error if raised.

type ServiceStatus

type ServiceStatus struct {
	// contains filtered or unexported fields
}

ServiceStatus Member status module.

func (*ServiceStatus) Cache

func (s *ServiceStatus) Cache() cache.Cacheable

Cache Return member status cache.

func (*ServiceStatus) Clean

func (s *ServiceStatus) Clean(uid string) error

Clean clean status cache by uid.

func (*ServiceStatus) Load

func (s *ServiceStatus) Load(statusMap datastore.Store, keys ...string) error

Load load and cache user status from provider. Return any error if raised.

func (*ServiceStatus) SetStatus

func (s *ServiceStatus) SetStatus(uid string, status Status) error

SetStatus set user status. user status cache will be cleand. Return any error if raised.

type ServiceToken

type ServiceToken struct {
	// contains filtered or unexported fields
}

ServiceToken member token module.

func (*ServiceToken) Cache

func (s *ServiceToken) Cache() cache.Cacheable

Cache Return member token cache.

func (*ServiceToken) Clean

func (s *ServiceToken) Clean(uid string) error

Clean clean token cache by uid.

func (*ServiceToken) Load

func (s *ServiceToken) Load(Tokens datastore.Store, keys ...string) error

Load load and cache token from provider. Return any error if raised.

func (*ServiceToken) Revoke

func (s *ServiceToken) Revoke(uid string) (string, error)

Revoke revoke user token and regenerate new token. user revoke cache will be cleand. Return new token and any error if resied.

type Status

type Status int

Status user status type

func (*Status) IsAvaliable

func (s *Status) IsAvaliable() bool

IsAvaliable check if user status is normal status.

type StatusMap

type StatusMap map[string]Status

StatusMap user status map User is banned if if map data of user id is true

type StatusProvider

type StatusProvider interface {
	//Statuses return  status  map of given uid list.
	//Return status  map and any error if raised.
	Statuses(uid ...string) (StatusMap, error)
	//SetStatus set user status.
	//Return any error if raised.
	SetStatus(uid string, status Status) error
	//SupportedStatus return supported status map
	SupportedStatus() map[Status]bool
}

StatusProvider member status provider interface

type StatusStore

type StatusStore struct {
	*datastore.SyncMapStore
}

StatusStore user status data store

func NewStatusStore

func NewStatusStore() *StatusStore

NewStatusStore create new status data store

func (*StatusStore) Get

func (s *StatusStore) Get(uid string) *Status

Get get user status by given user id

type TokenProvider

type TokenProvider interface {
	Tokens(uid ...string) (Tokens, error)
	Revoke(uid string) (string, error)
}

TokenProvider member token provider interface

type Tokens

type Tokens map[string]string

Tokens user token map type

type TokensStore

type TokensStore struct {
	*datastore.SyncMapStore
}

TokensStore user token data store

func NewTokensStore

func NewTokensStore() *TokensStore

NewTokensStore create new user token data store.

func (*TokensStore) Get

func (s *TokensStore) Get(uid string) string

Get get user token by given user id

Directories

Path Synopsis
drivers

Jump to

Keyboard shortcuts

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