1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-24 08:42:17 +02:00

Clean up save/load user on context.

- Fix #16
This commit is contained in:
Aaron 2015-02-18 08:45:27 -08:00
parent d050610129
commit 9f4cde2934
4 changed files with 58 additions and 9 deletions

View File

@ -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
}

View File

@ -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.

View File

@ -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)
}

View File

@ -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
}