Some bug fixes

pull/211/head
Henrique Dias 2017-08-20 09:55:45 +01:00
parent e4d345b7e5
commit 82b161cfb2
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
4 changed files with 34 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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