diff --git a/bolt/users.go b/bolt/users.go index 55758614..6189016b 100644 --- a/bolt/users.go +++ b/bolt/users.go @@ -14,8 +14,8 @@ type UsersStore struct { // Get gets a user with a certain id from the database. func (u UsersStore) Get(id int, builder fm.FSBuilder) (*fm.User, error) { - var us *fm.User - err := u.DB.One("ID", id, us) + var us fm.User + err := u.DB.One("ID", id, &us) if err == storm.ErrNotFound { return nil, fm.ErrNotExist } @@ -25,13 +25,33 @@ func (u UsersStore) Get(id int, builder fm.FSBuilder) (*fm.User, error) { } us.FileSystem = builder(us.Scope) - return us, nil + return &us, nil +} + +// GetByUsername gets a user with a certain username from the database. +func (u UsersStore) GetByUsername(username string, builder fm.FSBuilder) (*fm.User, error) { + var us fm.User + err := u.DB.One("Username", username, &us) + if err == storm.ErrNotFound { + return nil, fm.ErrNotExist + } + + if err != nil { + return nil, err + } + + us.FileSystem = builder(us.Scope) + return &us, nil } // Gets gets all the users from the database. func (u UsersStore) Gets(builder fm.FSBuilder) ([]*fm.User, error) { var us []*fm.User - err := u.DB.All(us) + err := u.DB.All(&us) + if err == storm.ErrNotFound { + return nil, fm.ErrNotExist + } + if err != nil { return us, err } @@ -43,7 +63,7 @@ func (u UsersStore) Gets(builder fm.FSBuilder) ([]*fm.User, error) { return us, err } -// Updates the whole user object or only certain fields. +// Update updates the whole user object or only certain fields. func (u UsersStore) Update(us *fm.User, fields ...string) error { if len(fields) == 0 { return u.Save(us) diff --git a/caddy/parser/parser.go b/caddy/parser/parser.go index 1f531136..697d5584 100644 --- a/caddy/parser/parser.go +++ b/caddy/parser/parser.go @@ -210,6 +210,11 @@ func Parse(c *caddy.Controller, plugin string) ([]*filemanager.FileManager, erro }, } + err = m.Setup() + if err != nil { + return nil, err + } + switch plugin { case "hugo": // Initialize the default settings for Hugo. diff --git a/filemanager.go b/filemanager.go index 5af085cf..a2803941 100644 --- a/filemanager.go +++ b/filemanager.go @@ -68,7 +68,6 @@ import ( "golang.org/x/crypto/bcrypt" rice "github.com/GeertJohan/go.rice" - "github.com/asdine/storm" "github.com/hacdias/fileutils" "github.com/mholt/caddy" "github.com/robfig/cron" @@ -165,7 +164,7 @@ func (m *FileManager) Setup() error { // Tries to get the event commands from the database. // If they don't exist, initialize them. err = m.Store.Config.Get("commands", &m.Commands) - if err != nil && err == storm.ErrNotFound { + if err != nil && err == ErrNotExist { m.Commands = map[string][]string{ "before_save": {}, "after_save": {}, @@ -181,7 +180,7 @@ func (m *FileManager) Setup() error { // Tries to fetch the users from the database. users, err := m.Store.Users.Gets(m.NewFS) - if err != nil { + if err != nil && err != ErrNotExist { return err } @@ -460,6 +459,7 @@ type Store struct { // UsersStore is the interface to manage users. type UsersStore interface { Get(id int, builder FSBuilder) (*User, error) + GetByUsername(username string, builder FSBuilder) (*User, error) Gets(builder FSBuilder) ([]*User, error) Save(u *User) error Update(u *User, fields ...string) error diff --git a/http/auth.go b/http/auth.go index cf23d07c..b2e559b2 100644 --- a/http/auth.go +++ b/http/auth.go @@ -30,7 +30,7 @@ func authHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int, er } // Checks if the user exists. - u, err := c.Store.Users.Get(cred.ID, c.NewFS) + u, err := c.Store.Users.GetByUsername(cred.Username, c.NewFS) if err != nil { return http.StatusForbidden, nil }