3
0

Implement user login & search

Took 21 minutes
This commit is contained in:
Denis Arh
2018-07-06 09:33:18 +02:00
parent 68dcdae73c
commit 64b7b21437
2 changed files with 40 additions and 2 deletions
+35 -2
View File
@@ -2,13 +2,46 @@ package sam
import (
"github.com/pkg/errors"
"github.com/titpetric/factory"
)
var _ = errors.Wrap
// User lookup & login
func (*User) Login(r *userLoginRequest) (interface{}, error) {
return nil, errors.New("Not implemented: User.login")
db, err := factory.Database.Get()
if err != nil {
return nil, err
}
u := &User{}
if err != db.Get(u, "SELECT * FROM users WHERE username = ?", r.username) {
return nil, err
}
if u.ID == 0 || !u.ValidatePassword(r.password) {
return nil, errors.New("Invalid username and password combination")
}
if !u.CanLogin() {
return nil, errors.New("User is not allowed to login")
}
return u, nil
}
// Searches the users table in the database to find users by matching (by-prefix) their username
func (*User) Search(r *userSearchRequest) (interface{}, error) {
return nil, errors.New("Not implemented: User.search")
db, err := factory.Database.Get()
if err != nil {
return nil, err
}
uu := []*User{}
if err != db.Get(uu, "SELECT * FROM users WHERE username LIKE ?", r.query+"%") {
return nil, err
}
return uu, nil
}
+5
View File
@@ -17,6 +17,11 @@ func (User) new() *User {
return &User{}
}
// Basic user validation
func (u *User) CanLogin() bool {
return u.ID > 0
}
func (u *User) GetID() uint64 {
return u.ID
}