ictgj-voting/model_users.go

83 lines
1.9 KiB
Go
Raw Normal View History

2017-04-21 18:17:18 +00:00
package main
import "golang.org/x/crypto/bcrypt"
2017-10-11 23:03:27 +00:00
// These are all model functions that have to do with users
2017-10-19 19:53:07 +00:00
// Unlike gamejam functions, we manipulate the DB directly
// We want to make sure that we always use the most up-to-date user
// information.
2017-10-11 23:03:27 +00:00
2017-04-21 18:17:18 +00:00
// Returns true if there are any users in the database
2017-10-11 23:03:27 +00:00
func (m *model) hasUser() bool {
return len(m.getAllUsers()) > 0
2017-04-21 18:17:18 +00:00
}
2017-10-11 23:03:27 +00:00
func (m *model) getAllUsers() []string {
if err := m.openDB(); err != nil {
2017-04-21 18:17:18 +00:00
return []string{}
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-04-21 18:17:18 +00:00
2017-10-11 23:03:27 +00:00
usrs, err := m.bolt.GetBucketList([]string{"users"})
2017-04-21 18:17:18 +00:00
if err != nil {
return []string{}
}
return usrs
}
2017-10-11 23:03:27 +00:00
// Is the given email one that is in our DB?
func (m *model) isValidUserEmail(email string) bool {
if err := m.openDB(); err != nil {
2017-04-21 18:17:18 +00:00
return false
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-04-21 18:17:18 +00:00
usrPath := []string{"users", email}
2017-10-11 23:03:27 +00:00
_, err := m.bolt.GetValue(usrPath, "password")
2017-04-21 18:17:18 +00:00
return err == nil
}
2017-10-11 23:03:27 +00:00
// Is the email and pw given valid?
func (m *model) checkCredentials(email, pw string) error {
2017-04-21 18:17:18 +00:00
var err error
2017-10-11 23:03:27 +00:00
if err = m.openDB(); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-04-21 18:17:18 +00:00
var uPw string
usrPath := []string{"users", email}
2017-10-11 23:03:27 +00:00
if uPw, err = m.bolt.GetValue(usrPath, "password"); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
return bcrypt.CompareHashAndPassword([]byte(uPw), []byte(pw))
}
// updateUserPassword
2017-04-21 18:17:18 +00:00
// Takes an email address and a password
// Creates the user if it doesn't exist, encrypts the password
// and updates it in the db
2017-10-11 23:03:27 +00:00
func (m *model) updateUserPassword(email, password string) error {
2017-04-21 18:17:18 +00:00
cryptPw, cryptError := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if cryptError != nil {
return cryptError
}
2017-10-11 23:03:27 +00:00
if err := m.openDB(); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-04-21 18:17:18 +00:00
usrPath := []string{"users", email}
2017-10-11 23:03:27 +00:00
return m.bolt.SetValue(usrPath, "password", string(cryptPw))
2017-04-21 18:17:18 +00:00
}
2017-10-11 23:03:27 +00:00
func (m *model) deleteUser(email string) error {
2017-04-21 18:17:18 +00:00
var err error
2017-10-11 23:03:27 +00:00
if err = m.openDB(); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-04-21 18:17:18 +00:00
2017-10-11 23:03:27 +00:00
return m.bolt.DeleteBucket([]string{"users"}, email)
2017-04-21 18:17:18 +00:00
}