2014-03-25 03:31:33 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import "database/sql"
|
|
|
|
|
|
|
|
// User represents the user model for gophish.
|
|
|
|
type User struct {
|
|
|
|
Id int64 `json:"id"`
|
2014-03-26 20:01:49 +00:00
|
|
|
Username string `json:"username" sql:"not null;unique"`
|
2014-03-25 03:31:33 +00:00
|
|
|
Hash string `json:"-"`
|
2014-03-26 20:01:49 +00:00
|
|
|
ApiKey string `json:"api_key" sql:"not null;unique"`
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUser returns the user that the given id corresponds to. If no user is found, an
|
|
|
|
// error is thrown.
|
|
|
|
func GetUser(id int64) (User, error) {
|
|
|
|
u := User{}
|
2014-03-26 04:53:51 +00:00
|
|
|
err := db.Where("id=?", id).First(&u).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByAPIKey returns the user that the given API Key corresponds to. If no user is found, an
|
|
|
|
// error is thrown.
|
2014-03-26 04:53:51 +00:00
|
|
|
func GetUserByAPIKey(key string) (User, error) {
|
2014-03-25 03:31:33 +00:00
|
|
|
u := User{}
|
2014-03-26 20:01:49 +00:00
|
|
|
err := db.Where("api_key = ?", key).First(&u).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByUsername returns the user that the given username corresponds to. If no user is found, an
|
|
|
|
// error is thrown.
|
|
|
|
func GetUserByUsername(username string) (User, error) {
|
|
|
|
u := User{}
|
2014-03-26 04:53:51 +00:00
|
|
|
err := db.Where("username = ?", username).First(&u).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
return u, ErrUsernameTaken
|
|
|
|
} else if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutUser updates the given user
|
|
|
|
func PutUser(u *User) error {
|
2014-03-26 20:01:49 +00:00
|
|
|
err := db.Update(&u).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|