DB Updates :)

Former-commit-id: e9795cfca39aab57f4a4b604c65633958ff22e46 [formerly 7dd93e46eb6915a387dc64500a3fe7f6f955643b] [formerly ffa277f1605e46bb8c914464b1223fe029d579d8 [formerly a04ff87bf9271d0e4eabbbe096ce0704d6cf61ea]]
Former-commit-id: ffa472fd3b1534f64a1c343864564bbc0290714b [formerly 7074f824d7d7dd3cd74b884c9f3e96834f662394]
Former-commit-id: 4b1e0324de6065fd4daec25d463d3756588b92d8
This commit is contained in:
Henrique Dias
2017-08-19 12:35:44 +01:00
parent 4b602be5e3
commit 764289e52f
15 changed files with 337 additions and 297 deletions

66
user.go
View File

@@ -1,12 +1,28 @@
package filemanager
import (
"crypto/rand"
"errors"
"regexp"
"strings"
"time"
"golang.org/x/crypto/bcrypt"
"github.com/hacdias/fileutils"
)
var (
ErrExist = errors.New("the resource already exists")
ErrNotExist = errors.New("the resource does not exist")
ErrEmptyRequest = errors.New("request body is empty")
ErrEmptyPassword = errors.New("password is empty")
ErrEmptyUsername = errors.New("username is empty")
ErrEmptyScope = errors.New("scope is empty")
ErrWrongDataType = errors.New("wrong data type")
ErrInvalidUpdateField = errors.New("invalid field to update")
)
// DefaultUser is used on New, when no 'base' user is provided.
var DefaultUser = User{
AllowCommands: true,
@@ -110,10 +126,24 @@ func (r *Regexp) MatchString(s string) bool {
return r.regexp.MatchString(s)
}
type ShareLink struct {
Hash string `json:"hash" storm:"id,index"`
Path string `json:"path" storm:"index"`
Expires bool `json:"expires"`
ExpireDate time.Time `json:"expireDate"`
}
type Store struct {
Users UsersStore
Config ConfigStore
Share ShareStore
}
type UsersStore interface {
Get(id int) (*User, error)
Gets() ([]*User, error)
Save(u *User, fields ...string) error
Save(u *User) error
Update(u *User, fields ...string) error
Delete(id int) error
}
@@ -123,6 +153,36 @@ type ConfigStore interface {
}
type ShareStore interface {
Get(hash string)
Save()
Get(hash string) (*ShareLink, error)
GetByPath(path string) ([]*ShareLink, error)
Gets() ([]*ShareLink, error)
Save(s *ShareLink) error
Delete(hash string) error
}
// HashPassword generates an hash from a password using bcrypt.
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes), err
}
// CheckPasswordHash compares a password with an hash to check if they match.
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
// GenerateRandomBytes returns securely generated random bytes.
// It will return an fm.Error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}