ribbit/model_user.go

238 lines
4.8 KiB
Go
Raw Permalink Normal View History

2018-04-26 11:53:03 +00:00
package main
import (
"errors"
"fmt"
2018-04-26 11:53:03 +00:00
"strings"
"github.com/pborman/uuid"
2019-01-10 16:39:42 +00:00
"golang.org/x/crypto/bcrypt"
2018-04-26 11:53:03 +00:00
)
type User struct {
Username string `json:username`
Uuid string `json:uuid`
SubSlugs []string `json:subs`
2019-01-10 16:39:42 +00:00
IsAdmin bool `json:admin`
2018-04-26 11:53:03 +00:00
}
func NewUser(un string) *User {
u := new(User)
u.Username = un
u.Uuid = uuid.New()
return u
}
2019-01-10 16:39:42 +00:00
/*
2018-04-26 11:53:03 +00:00
func (u *User) UpdateFeed() error {
for _, slug := range u.SubSlugs {
pts := strings.Split(slug, ";")
if len(pts) != 2 {
continue
}
if comic, err := m.GetComic(pts[0], pts[1]); err == nil {
comic.Update()
}
}
return nil
}
2019-01-10 16:39:42 +00:00
*/
2018-04-26 11:53:03 +00:00
func (m *model) SaveUser(u *User) error {
var err error
2020-09-28 18:03:57 +00:00
if err = m.openDB(); err != nil {
2018-04-26 11:53:03 +00:00
return err
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2018-04-26 11:53:03 +00:00
bkt := []string{"users", u.Uuid}
if err = m.bolt.MkBucketPath(bkt); err != nil {
return err
}
if err = m.bolt.SetValue(bkt, "username", u.Username); err != nil {
return err
}
2019-01-10 16:39:42 +00:00
var adminVal = "false"
if u.IsAdmin {
adminVal = "true"
}
if err = m.bolt.SetValue(bkt, "admin", adminVal); err != nil {
return err
}
2018-04-26 11:53:03 +00:00
var newSubs []string
for _, v := range u.SubSlugs {
if strings.TrimSpace(v) != "" {
newSubs = append(newSubs, v)
}
}
slugs := strings.Join(newSubs, ",")
if err = m.bolt.SetValue(bkt, "subs", slugs); err != nil {
return err
}
return nil
}
2019-01-10 16:39:42 +00:00
func (m *model) isValidUser(uid string) bool {
var err error
2020-09-28 18:03:57 +00:00
if err = m.openDB(); err != nil {
2019-01-10 16:39:42 +00:00
return false
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2019-01-10 16:39:42 +00:00
ret := new(User)
bkt := []string{"users", uid}
ret.Uuid = uid
if ret.Username, err = m.bolt.GetValue(bkt, "username"); err != nil {
fmt.Println("Error getting username value:", err.Error())
return false
}
return true
}
func (m *model) GetUserByEmail(email string) (*User, error) {
if email == "" {
return nil, errors.New("No email given")
}
u := m.GetAllUsers()
for i := range u {
if u[i].Username == email {
return &u[i], nil
}
}
return nil, errors.New("No user found")
}
2018-04-26 11:53:03 +00:00
func (m *model) GetUser(uid string) (*User, error) {
2019-01-10 16:39:42 +00:00
if uid == "" {
return nil, errors.New("No user id given")
}
2018-04-26 11:53:03 +00:00
var err error
2020-09-28 18:03:57 +00:00
if err = m.openDB(); err != nil {
2018-04-26 11:53:03 +00:00
return nil, err
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2018-04-26 11:53:03 +00:00
ret := new(User)
bkt := []string{"users", uid}
ret.Uuid = uid
if ret.Username, err = m.bolt.GetValue(bkt, "username"); err != nil {
fmt.Println("Error getting username value:", err.Error())
2018-04-26 11:53:03 +00:00
return nil, err
}
2019-01-10 16:39:42 +00:00
admin, _ := m.bolt.GetValue(bkt, "admin")
ret.IsAdmin = (admin == "true")
2018-04-26 11:53:03 +00:00
var subs string
if subs, err = m.bolt.GetValue(bkt, "subs"); err != nil {
return nil, err
}
ret.SubSlugs = strings.Split(subs, ",")
return ret, nil
}
func (m *model) SaveAllUsers(users []User) {
var err error
2020-09-28 18:03:57 +00:00
if err = m.openDB(); err != nil {
2018-04-26 11:53:03 +00:00
return
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2018-04-26 11:53:03 +00:00
for i := range users {
m.SaveUser(&users[i])
}
}
func (m *model) LoadUsers() error {
m.Users = m.GetAllUsers()
return nil
}
func (m *model) GetAllUsers() []User {
var err error
var ret []User
2020-09-28 18:03:57 +00:00
if err = m.openDB(); err != nil {
2018-04-26 11:53:03 +00:00
return ret
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2018-04-26 11:53:03 +00:00
uids := m.GetUserIdList()
for _, uid := range uids {
if u, e := m.GetUser(uid); e == nil {
ret = append(ret, *u)
}
}
return ret
}
func (m *model) GetUserByName(nm string) (*User, error) {
var err error
2020-09-28 18:03:57 +00:00
if err = m.openDB(); err != nil {
2018-04-26 11:53:03 +00:00
return nil, err
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2018-04-26 11:53:03 +00:00
usrids := m.GetUserIdList()
for i := range usrids {
bkt := []string{"users", usrids[i]}
var tstuname string
if tstuname, _ = m.bolt.GetValue(bkt, "username"); tstuname == nm {
// Found it
return m.GetUser(usrids[i])
}
}
return nil, errors.New("No user with username " + nm + " found")
}
func (m *model) GetUserIdList() []string {
var ret []string
var err error
2020-09-28 18:03:57 +00:00
if err = m.openDB(); err != nil {
2018-04-26 11:53:03 +00:00
return ret
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2018-04-26 11:53:03 +00:00
bkt := []string{"users"}
ret, _ = m.bolt.GetBucketList(bkt)
return ret
}
2019-01-10 16:39:42 +00:00
// updateUserPassword
// Takes a user id and a password
// Fails if the user doesn't exist
func (m *model) updateUserPassword(uid, password string) error {
cryptPw, cryptError := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if cryptError != nil {
return cryptError
}
if _, err := m.GetUser(uid); err != nil {
return err
}
2020-09-28 18:03:57 +00:00
if err := m.openDB(); err != nil {
2019-01-10 16:39:42 +00:00
return err
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2019-01-10 16:39:42 +00:00
usrPath := []string{"users", uid}
return m.bolt.SetValue(usrPath, "password", string(cryptPw))
}
// Is the uid and pw given valid?
func (m *model) checkCredentials(uid, pw string) error {
var err error
2020-09-28 18:03:57 +00:00
if err = m.openDB(); err != nil {
2019-01-10 16:39:42 +00:00
return err
}
2020-09-28 18:03:57 +00:00
defer m.closeDB()
2019-01-10 16:39:42 +00:00
var uPw string
usrPath := []string{"users", uid}
if uPw, err = m.bolt.GetValue(usrPath, "password"); err != nil {
return err
}
return bcrypt.CompareHashAndPassword([]byte(uPw), []byte(pw))
}
func (m *model) hasAdminUser() bool {
users := m.GetAllUsers()
for i := range users {
if users[i].IsAdmin {
return true
}
}
return false
}