github.com/topsteplocal/gophish@v0.6.0/models/user.go (about)

     1  package models
     2  
     3  // User represents the user model for gophish.
     4  type User struct {
     5  	Id       int64  `json:"id"`
     6  	Username string `json:"username" sql:"not null;unique"`
     7  	Hash     string `json:"-"`
     8  	ApiKey   string `json:"api_key" sql:"not null;unique"`
     9  }
    10  
    11  // GetUser returns the user that the given id corresponds to. If no user is found, an
    12  // error is thrown.
    13  func GetUser(id int64) (User, error) {
    14  	u := User{}
    15  	err := db.Where("id=?", id).First(&u).Error
    16  	return u, err
    17  }
    18  
    19  // GetUserByAPIKey returns the user that the given API Key corresponds to. If no user is found, an
    20  // error is thrown.
    21  func GetUserByAPIKey(key string) (User, error) {
    22  	u := User{}
    23  	err := db.Where("api_key = ?", key).First(&u).Error
    24  	return u, err
    25  }
    26  
    27  // GetUserByUsername returns the user that the given username corresponds to. If no user is found, an
    28  // error is thrown.
    29  func GetUserByUsername(username string) (User, error) {
    30  	u := User{}
    31  	err := db.Where("username = ?", username).First(&u).Error
    32  	return u, err
    33  }
    34  
    35  // PutUser updates the given user
    36  func PutUser(u *User) error {
    37  	err := db.Save(u).Error
    38  	return err
    39  }