github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/users.go (about)

     1  package database
     2  
     3  import (
     4  	"time"
     5  
     6  	. "github.com/drone/drone/pkg/model"
     7  	"github.com/russross/meddler"
     8  )
     9  
    10  // Name of the User table in the database
    11  const userTable = "users"
    12  
    13  // SQL Queries to retrieve a user by their unique database key
    14  const userFindIdStmt = `
    15  SELECT id, email, password, token, name, gravatar, created, updated, admin,
    16  github_login, github_token, bitbucket_login, bitbucket_token, bitbucket_secret,
    17  gitlab_token
    18  FROM users WHERE id = ?
    19  `
    20  
    21  // SQL Queries to retrieve a user by their email address
    22  const userFindEmailStmt = `
    23  SELECT id, email, password, token, name, gravatar, created, updated, admin,
    24  github_login, github_token, bitbucket_login, bitbucket_token, bitbucket_secret,
    25  gitlab_token
    26  FROM users WHERE email = ?
    27  `
    28  
    29  // SQL Queries to retrieve a list of all users
    30  const userStmt = `
    31  SELECT id, email, password, token, name, gravatar, created, updated, admin,
    32  github_login, github_token, bitbucket_login, bitbucket_token, bitbucket_secret,
    33  gitlab_token
    34  FROM users
    35  ORDER BY name ASC
    36  `
    37  
    38  // Returns the User with the given ID.
    39  func GetUser(id int64) (*User, error) {
    40  	user := User{}
    41  	err := meddler.QueryRow(db, &user, userFindIdStmt, id)
    42  	return &user, err
    43  }
    44  
    45  // Returns the User with the given email address.
    46  func GetUserEmail(email string) (*User, error) {
    47  	user := User{}
    48  	err := meddler.QueryRow(db, &user, userFindEmailStmt, email)
    49  	return &user, err
    50  }
    51  
    52  // Returns the User Password Hash for the given
    53  // email address.
    54  func GetPassEmail(email string) ([]byte, error) {
    55  	user, err := GetUserEmail(email)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return []byte(user.Password), nil
    61  }
    62  
    63  // Saves the User account.
    64  func SaveUser(user *User) error {
    65  	if user.ID == 0 {
    66  		user.Created = time.Now().UTC()
    67  	}
    68  	user.Updated = time.Now().UTC()
    69  	return meddler.Save(db, userTable, user)
    70  }
    71  
    72  // Deletes an existing User account.
    73  func DeleteUser(id int64) error {
    74  	db.Exec("DELETE FROM members WHERE user_id = ?", id)
    75  	db.Exec("DELETE FROM users WHERE id = ?", id)
    76  	// TODO delete all projects
    77  	return nil
    78  }
    79  
    80  // Returns a list of all Users.
    81  func ListUsers() ([]*User, error) {
    82  	var users []*User
    83  	err := meddler.QueryAll(db, &users, userStmt)
    84  	return users, err
    85  }
    86  
    87  // Returns a list of Users within the specified
    88  // range (for pagination purposes).
    89  func ListUsersRange(limit, offset int) ([]*User, error) {
    90  	var users []*User
    91  	err := meddler.QueryAll(db, &users, userStmt)
    92  	return users, err
    93  }