mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-28 08:58:38 +02:00
parent
d050610129
commit
9f4cde2934
@ -37,7 +37,7 @@ func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = ctx.LoadUser(key, Cfg.Storer)
|
||||
err = ctx.LoadUser(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
28
context.go
28
context.go
@ -86,12 +86,12 @@ func (c *Context) FirstPostFormValue(key string) (string, bool) {
|
||||
}
|
||||
|
||||
// LoadUser loads the user Attributes if they haven't already been loaded.
|
||||
func (c *Context) LoadUser(key string, storer Storer) error {
|
||||
func (c *Context) LoadUser(key string) error {
|
||||
if c.User != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
intf, err := storer.Get(key, ModuleAttrMeta)
|
||||
intf, err := Cfg.Storer.Get(key, ModuleAttrMeta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -100,13 +100,33 @@ func (c *Context) LoadUser(key string, storer Storer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadSessionUser loads the user from the session if the user has not already been
|
||||
// loaded.
|
||||
func (c *Context) LoadSessionUser() error {
|
||||
if c.User != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
key, ok := c.SessionStorer.Get(SessionKey)
|
||||
if !ok {
|
||||
return ErrUserNotFound
|
||||
}
|
||||
|
||||
return c.LoadUser(key)
|
||||
}
|
||||
|
||||
// SaveUser saves the user Attributes.
|
||||
func (c *Context) SaveUser(key string, storer Storer) error {
|
||||
func (c *Context) SaveUser() error {
|
||||
if c.User == nil {
|
||||
return errors.New("User not initialized.")
|
||||
}
|
||||
|
||||
return storer.Put(key, c.User)
|
||||
key, ok := c.User.String("username")
|
||||
if !ok {
|
||||
return errors.New("User improperly initialized, primary ID missing")
|
||||
}
|
||||
|
||||
return Cfg.Storer.Put(key, c.User)
|
||||
}
|
||||
|
||||
// Attributes converts the post form values into an attributes map.
|
||||
|
@ -46,11 +46,13 @@ func TestContext_Request(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContext_SaveUser(t *testing.T) {
|
||||
Cfg = NewConfig()
|
||||
ctx := NewContext()
|
||||
storer := mockStorer{}
|
||||
Cfg.Storer = storer
|
||||
ctx.User = Attributes{"username": "joe", "email": "hello@joe.com", "password": "mysticalhash"}
|
||||
|
||||
ctx.User = Attributes{"email": "hello@joe.com", "password": "mysticalhash"}
|
||||
err := ctx.SaveUser("joe", storer)
|
||||
err := ctx.SaveUser()
|
||||
if err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
}
|
||||
@ -68,12 +70,39 @@ func TestContext_SaveUser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContext_LoadUser(t *testing.T) {
|
||||
Cfg = NewConfig()
|
||||
ctx := NewContext()
|
||||
storer := mockStorer{
|
||||
"joe": Attributes{"email": "hello@joe.com", "password": "mysticalhash"},
|
||||
}
|
||||
Cfg.Storer = storer
|
||||
|
||||
err := ctx.LoadUser("joe", storer)
|
||||
err := ctx.LoadUser("joe")
|
||||
if err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
}
|
||||
|
||||
attr := storer["joe"]
|
||||
|
||||
for k, v := range attr {
|
||||
if v != ctx.User[k] {
|
||||
t.Error(v, "not equal to", ctx.User[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_LoadSessionUser(t *testing.T) {
|
||||
Cfg = NewConfig()
|
||||
ctx := NewContext()
|
||||
storer := mockStorer{
|
||||
"joe": Attributes{"email": "hello@joe.com", "password": "mysticalhash"},
|
||||
}
|
||||
Cfg.Storer = storer
|
||||
ctx.SessionStorer = mockClientStore{
|
||||
SessionKey: "joe",
|
||||
}
|
||||
|
||||
err := ctx.LoadSessionUser()
|
||||
if err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ func (m *RecoverModule) recover(ctx *authboss.Context, xsrfName, xsrfToken strin
|
||||
}
|
||||
|
||||
func (m *RecoverModule) makeAndSendToken(ctx *authboss.Context, username string) (err error, emailSent <-chan struct{}) {
|
||||
if err = ctx.LoadUser(username, authboss.Cfg.Storer); err != nil {
|
||||
if err = ctx.LoadUser(username); err != nil {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user