Some bug fixes

Former-commit-id: 54fc2a2869dd625e55881818e0022f3c4ae45bd1 [formerly ddba8f0dc58999a6f483fe61fda9391da251d49b] [formerly f76423c629f671538e6c008365c8d6dc1a5460d7 [formerly 82b161cfb2]]
Former-commit-id: 34601615e2beb773bf266cdb503e3c9fd8ead09b [formerly 0f39bbd2d66c789219785b4a726297a7c00a7f1e]
Former-commit-id: 1c2e33c56af3f57f8e8751b4c43b05967f87c587
pull/726/head
Henrique Dias 2017-08-20 09:55:45 +01:00
parent 67dbf88eb6
commit 1b0f67c0f6
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. // Get gets a user with a certain id from the database.
func (u UsersStore) Get(id int, builder fm.FSBuilder) (*fm.User, error) { func (u UsersStore) Get(id int, builder fm.FSBuilder) (*fm.User, error) {
var us *fm.User var us fm.User
err := u.DB.One("ID", id, us) err := u.DB.One("ID", id, &us)
if err == storm.ErrNotFound { if err == storm.ErrNotFound {
return nil, fm.ErrNotExist 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) 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. // Gets gets all the users from the database.
func (u UsersStore) Gets(builder fm.FSBuilder) ([]*fm.User, error) { func (u UsersStore) Gets(builder fm.FSBuilder) ([]*fm.User, error) {
var us []*fm.User 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 { if err != nil {
return us, err return us, err
} }
@ -43,7 +63,7 @@ func (u UsersStore) Gets(builder fm.FSBuilder) ([]*fm.User, error) {
return us, err 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 { func (u UsersStore) Update(us *fm.User, fields ...string) error {
if len(fields) == 0 { if len(fields) == 0 {
return u.Save(us) 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 { switch plugin {
case "hugo": case "hugo":
// Initialize the default settings for Hugo. // Initialize the default settings for Hugo.

View File

@ -68,7 +68,6 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
rice "github.com/GeertJohan/go.rice" rice "github.com/GeertJohan/go.rice"
"github.com/asdine/storm"
"github.com/hacdias/fileutils" "github.com/hacdias/fileutils"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/robfig/cron" "github.com/robfig/cron"
@ -165,7 +164,7 @@ func (m *FileManager) Setup() error {
// Tries to get the event commands from the database. // Tries to get the event commands from the database.
// If they don't exist, initialize them. // If they don't exist, initialize them.
err = m.Store.Config.Get("commands", &m.Commands) err = m.Store.Config.Get("commands", &m.Commands)
if err != nil && err == storm.ErrNotFound { if err != nil && err == ErrNotExist {
m.Commands = map[string][]string{ m.Commands = map[string][]string{
"before_save": {}, "before_save": {},
"after_save": {}, "after_save": {},
@ -181,7 +180,7 @@ func (m *FileManager) Setup() error {
// Tries to fetch the users from the database. // Tries to fetch the users from the database.
users, err := m.Store.Users.Gets(m.NewFS) users, err := m.Store.Users.Gets(m.NewFS)
if err != nil { if err != nil && err != ErrNotExist {
return err return err
} }
@ -460,6 +459,7 @@ type Store struct {
// UsersStore is the interface to manage users. // UsersStore is the interface to manage users.
type UsersStore interface { type UsersStore interface {
Get(id int, builder FSBuilder) (*User, error) Get(id int, builder FSBuilder) (*User, error)
GetByUsername(username string, builder FSBuilder) (*User, error)
Gets(builder FSBuilder) ([]*User, error) Gets(builder FSBuilder) ([]*User, error)
Save(u *User) error Save(u *User) error
Update(u *User, fields ...string) 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. // 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 { if err != nil {
return http.StatusForbidden, nil return http.StatusForbidden, nil
} }