2015-05-01 04:08:42 +00:00
|
|
|
package builtin
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
import (
|
2015-06-13 23:48:06 +00:00
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/drone/drone/pkg/types"
|
2015-04-07 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
type Tokenstore struct {
|
2015-06-13 23:48:06 +00:00
|
|
|
*sql.DB
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 23:48:06 +00:00
|
|
|
func NewTokenstore(db *sql.DB) *Tokenstore {
|
2015-05-11 07:45:31 +00:00
|
|
|
return &Tokenstore{db}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token returns a token by ID.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Tokenstore) Token(id int64) (*types.Token, error) {
|
|
|
|
return getToken(db, rebind(stmtTokenSelect), id)
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
2015-04-15 05:04:38 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// TokenLabel returns a token by label
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Tokenstore) TokenLabel(user *types.User, label string) (*types.Token, error) {
|
|
|
|
return getToken(db, rebind(stmtTokenSelectTokenUserLabel), user.ID, label)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// TokenList returns a list of all user tokens.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Tokenstore) TokenList(user *types.User) ([]*types.Token, error) {
|
|
|
|
return getTokens(db, rebind(stmtTokenSelectTokenUserId), user.ID)
|
2015-04-15 05:04:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// AddToken inserts a new token into the datastore.
|
|
|
|
// If the token label already exists for the user
|
|
|
|
// an error is returned.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Tokenstore) AddToken(token *types.Token) error {
|
|
|
|
return createToken(db, rebind(stmtTokenInsert), token)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// DelToken removes the DelToken from the datastore.
|
2015-06-13 23:48:06 +00:00
|
|
|
func (db *Tokenstore) DelToken(token *types.Token) error {
|
|
|
|
var _, err = db.Exec(rebind(stmtTokenDelete), token.ID)
|
2015-05-11 07:45:31 +00:00
|
|
|
return err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|