updated user object

This commit is contained in:
Brad Rydzewski 2015-06-18 16:37:40 -07:00
parent dc0e714992
commit 64715c390c
9 changed files with 57 additions and 171 deletions

View file

@ -74,7 +74,6 @@ func (g *GitHub) Login(token, secret string) (*common.User, error) {
user := common.User{}
user.Login = *login.Login
user.Email = *login.Email
user.Name = *login.Name
user.Token = token
user.Secret = secret
return &user, nil

View file

@ -84,9 +84,8 @@ func GetLogin(c *gin.Context) {
u.Login = login.Login
u.Token = login.Token
u.Secret = login.Secret
u.Name = login.Name
u.Email = login.Email
u.Gravatar = gravatar.Hash(u.Email)
u.Avatar = gravatar.Hash(u.Email)
// insert the user into the database
if err := store.AddUser(u); err != nil {
@ -106,9 +105,8 @@ func GetLogin(c *gin.Context) {
// data and cache in the datastore.
u.Token = login.Token
u.Secret = login.Secret
u.Name = login.Name
u.Email = login.Email
u.Gravatar = gravatar.Hash(u.Email)
u.Avatar = gravatar.Hash(u.Email)
if err := store.SetUser(u); err != nil {
log.Errorf("cannot update %s. %s", u.Login, err)

View file

@ -32,7 +32,7 @@ func PutUserCurr(c *gin.Context) {
return
}
user.Email = in.Email
user.Gravatar = gravatar.Hash(in.Email)
user.Avatar = gravatar.Hash(in.Email)
err := store.SetUser(user)
if err != nil {
c.Fail(400, err)

View file

@ -32,7 +32,7 @@ func GetUsers(c *gin.Context) {
func PostUser(c *gin.Context) {
store := ToDatastore(c)
name := c.Params.ByName("name")
user := &common.User{Login: name, Name: name}
user := &common.User{Login: name}
user.Token = c.Request.FormValue("token")
user.Secret = c.Request.FormValue("secret")
if err := store.AddUser(user); err != nil {
@ -80,7 +80,7 @@ func PutUser(c *gin.Context) {
return
}
user.Email = in.Email
user.Gravatar = gravatar.Hash(user.Email)
user.Avatar = gravatar.Hash(user.Email)
// an administrator must not be able to
// downgrade her own account.

View file

@ -37,13 +37,10 @@ CREATE TABLE IF NOT EXISTS users (
,user_login VARCHAR(255)
,user_token VARCHAR(255)
,user_secret VARCHAR(255)
,user_name VARCHAR(255)
,user_email VARCHAR(255)
,user_gravatar VARCHAR(255)
,user_avatar VARCHAR(255)
,user_admin BOOLEAN
,user_active BOOLEAN
,user_created INTEGER
,user_updated INTEGER
,UNIQUE(user_login)
);
`
@ -80,33 +77,6 @@ var repoUserIndex = `
CREATE INDEX repos_user_idx ON repos (repo_user_id);
`
// var repoKeyTable = `
// CREATE TABLE IF NOT EXISTS repo_keys (
// keys_id INTEGER PRIMARY KEY AUTOINCREMENT
// ,repo_id INTEGER
// ,keys_public BLOB
// ,keys_private BLOB
// ,UNIQUE(repo_id)
// );
// `
//
// var repoKeyIndex = `
// CREATE INDEX keys_repo_idx ON repo_keys (repo_id);
// `
// var repoParamTable = `
// CREATE TABLE IF NOT EXISTS repo_params (
// param_id INTEGER PRIMARY KEY AUTOINCREMENT
// ,repo_id INTEGER
// ,param_map BLOB
// ,UNIQUE(repo_id)
// );
// `
//
// var repoParamsIndex = `
// CREATE INDEX params_repo_idx ON repo_params (repo_id);
// `
var starTable = `
CREATE TABLE IF NOT EXISTS stars (
star_id INTEGER PRIMARY KEY AUTOINCREMENT

View file

@ -2,7 +2,6 @@ package builtin
import (
"database/sql"
"time"
"github.com/drone/drone/pkg/types"
)
@ -51,14 +50,11 @@ func (db *Userstore) UserCount() (int, error) {
// AddUser inserts a new user into the datastore.
// If the user login already exists an error is returned.
func (db *Userstore) AddUser(user *types.User) error {
user.Created = time.Now().UTC().Unix()
user.Updated = time.Now().UTC().Unix()
return createUser(db, rebind(stmtUserInsert), user)
}
// SetUser updates an existing user.
func (db *Userstore) SetUser(user *types.User) error {
user.Updated = time.Now().UTC().Unix()
return updateUser(db, rebind(stmtUserUpdate), user)
}

View file

@ -39,21 +39,15 @@ func createUser(db userDB, query string, v *User) error {
var v2 string
var v3 string
var v4 string
var v5 string
var v5 bool
var v6 bool
var v7 bool
var v8 int64
var v9 int64
v0 = v.Login
v1 = v.Token
v2 = v.Secret
v3 = v.Name
v4 = v.Email
v5 = v.Gravatar
v3 = v.Email
v4 = v.Avatar
v5 = v.Active
v6 = v.Admin
v7 = v.Active
v8 = v.Created
v9 = v.Updated
res, err := db.Exec(query,
&v0,
@ -63,9 +57,6 @@ func createUser(db userDB, query string, v *User) error {
&v4,
&v5,
&v6,
&v7,
&v8,
&v9,
)
if err != nil {
return err
@ -82,22 +73,16 @@ func updateUser(db userDB, query string, v *User) error {
var v3 string
var v4 string
var v5 string
var v6 string
var v6 bool
var v7 bool
var v8 bool
var v9 int64
var v10 int64
v0 = v.ID
v1 = v.Login
v2 = v.Token
v3 = v.Secret
v4 = v.Name
v5 = v.Email
v6 = v.Gravatar
v4 = v.Email
v5 = v.Avatar
v6 = v.Active
v7 = v.Admin
v8 = v.Active
v9 = v.Created
v10 = v.Updated
_, err := db.Exec(query,
&v1,
@ -107,9 +92,6 @@ func updateUser(db userDB, query string, v *User) error {
&v5,
&v6,
&v7,
&v8,
&v9,
&v10,
&v0,
)
return err
@ -122,11 +104,8 @@ func scanUser(row *sql.Row) (*User, error) {
var v3 string
var v4 string
var v5 string
var v6 string
var v6 bool
var v7 bool
var v8 bool
var v9 int64
var v10 int64
err := row.Scan(
&v0,
@ -137,9 +116,6 @@ func scanUser(row *sql.Row) (*User, error) {
&v5,
&v6,
&v7,
&v8,
&v9,
&v10,
)
if err != nil {
return nil, err
@ -150,13 +126,10 @@ func scanUser(row *sql.Row) (*User, error) {
v.Login = v1
v.Token = v2
v.Secret = v3
v.Name = v4
v.Email = v5
v.Gravatar = v6
v.Email = v4
v.Avatar = v5
v.Active = v6
v.Admin = v7
v.Active = v8
v.Created = v9
v.Updated = v10
return v, nil
}
@ -171,11 +144,8 @@ func scanUsers(rows *sql.Rows) ([]*User, error) {
var v3 string
var v4 string
var v5 string
var v6 string
var v6 bool
var v7 bool
var v8 bool
var v9 int64
var v10 int64
err = rows.Scan(
&v0,
&v1,
@ -185,9 +155,6 @@ func scanUsers(rows *sql.Rows) ([]*User, error) {
&v5,
&v6,
&v7,
&v8,
&v9,
&v10,
)
if err != nil {
return vv, err
@ -198,13 +165,10 @@ func scanUsers(rows *sql.Rows) ([]*User, error) {
v.Login = v1
v.Token = v2
v.Secret = v3
v.Name = v4
v.Email = v5
v.Gravatar = v6
v.Email = v4
v.Avatar = v5
v.Active = v6
v.Admin = v7
v.Active = v8
v.Created = v9
v.Updated = v10
vv = append(vv, v)
}
return vv, rows.Err()
@ -216,13 +180,10 @@ SELECT
,user_login
,user_token
,user_secret
,user_name
,user_email
,user_gravatar
,user_admin
,user_avatar
,user_active
,user_created
,user_updated
,user_admin
FROM users
`
@ -232,13 +193,10 @@ SELECT
,user_login
,user_token
,user_secret
,user_name
,user_email
,user_gravatar
,user_admin
,user_avatar
,user_active
,user_created
,user_updated
,user_admin
FROM users
LIMIT ? OFFSET ?
`
@ -249,13 +207,10 @@ SELECT
,user_login
,user_token
,user_secret
,user_name
,user_email
,user_gravatar
,user_admin
,user_avatar
,user_active
,user_created
,user_updated
,user_admin
FROM users
WHERE user_id = ?
`
@ -266,13 +221,10 @@ SELECT
,user_login
,user_token
,user_secret
,user_name
,user_email
,user_gravatar
,user_admin
,user_avatar
,user_active
,user_created
,user_updated
,user_admin
FROM users
WHERE user_login = ?
`
@ -287,14 +239,11 @@ INSERT INTO users (
user_login
,user_token
,user_secret
,user_name
,user_email
,user_gravatar
,user_admin
,user_avatar
,user_active
,user_created
,user_updated
) VALUES (?,?,?,?,?,?,?,?,?,?);
,user_admin
) VALUES (?,?,?,?,?,?,?);
`
const stmtUserUpdate = `
@ -302,13 +251,10 @@ UPDATE users SET
user_login = ?
,user_token = ?
,user_secret = ?
,user_name = ?
,user_email = ?
,user_gravatar = ?
,user_admin = ?
,user_avatar = ?
,user_active = ?
,user_created = ?
,user_updated = ?
,user_admin = ?
WHERE user_id = ?
`
@ -319,17 +265,14 @@ WHERE user_id = ?
const stmtUserTable = `
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY AUTOINCREMENT
,user_login VARCHAR
,user_token VARCHAR
user_id INTEGER PRIMARY KEY AUTOINCREMENT
,user_login VARCHAR
,user_token VARCHAR
,user_secret VARCHAR
,user_name VARCHAR
,user_email VARCHAR
,user_gravatar VARCHAR
,user_admin BOOLEAN
,user_email VARCHAR
,user_avatar VARCHAR
,user_active BOOLEAN
,user_created INTEGER
,user_updated INTEGER
,user_admin BOOLEAN
);
`

View file

@ -31,7 +31,6 @@ func TestUserstore(t *testing.T) {
g.It("Should Update a User", func() {
user := types.User{
Login: "joe",
Name: "Joe Sixpack",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
@ -47,7 +46,6 @@ func TestUserstore(t *testing.T) {
g.It("Should Add a new User", func() {
user := types.User{
Login: "joe",
Name: "Joe Sixpack",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
@ -58,16 +56,13 @@ func TestUserstore(t *testing.T) {
g.It("Should Get a User", func() {
user := types.User{
Login: "joe",
Token: "f0b461ca586c27872b43a0685cbc2847",
Secret: "976f22a5eef7caacb7e678d6c52f49b1",
Name: "Joe Sixpack",
Email: "foo@bar.com",
Gravatar: "b9015b0857e16ac4d94a0ffd9a0b79c8",
Active: true,
Admin: true,
Created: 1398065343,
Updated: 1398065344,
Login: "joe",
Token: "f0b461ca586c27872b43a0685cbc2847",
Secret: "976f22a5eef7caacb7e678d6c52f49b1",
Email: "foo@bar.com",
Avatar: "b9015b0857e16ac4d94a0ffd9a0b79c8",
Active: true,
Admin: true,
}
us.AddUser(&user)
getuser, err := us.User(user.ID)
@ -76,19 +71,15 @@ func TestUserstore(t *testing.T) {
g.Assert(user.Login).Equal(getuser.Login)
g.Assert(user.Token).Equal(getuser.Token)
g.Assert(user.Secret).Equal(getuser.Secret)
g.Assert(user.Name).Equal(getuser.Name)
g.Assert(user.Email).Equal(getuser.Email)
g.Assert(user.Gravatar).Equal(getuser.Gravatar)
g.Assert(user.Avatar).Equal(getuser.Avatar)
g.Assert(user.Active).Equal(getuser.Active)
g.Assert(user.Admin).Equal(getuser.Admin)
g.Assert(user.Created).Equal(getuser.Created)
g.Assert(user.Updated).Equal(getuser.Updated)
})
g.It("Should Get a User By Login", func() {
user := types.User{
Login: "joe",
Name: "Joe Sixpack",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
@ -102,13 +93,11 @@ func TestUserstore(t *testing.T) {
g.It("Should Enforce Unique User Login", func() {
user1 := types.User{
Login: "joe",
Name: "Joe Sixpack",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
user2 := types.User{
Login: "joe",
Name: "Joe Sixpack",
Email: "foo@bar.com",
Token: "ab20g0ddaf012c744e136da16aa21ad9",
}
@ -121,13 +110,11 @@ func TestUserstore(t *testing.T) {
g.It("Should Get a User List", func() {
user1 := types.User{
Login: "jane",
Name: "Jane Doe",
Email: "foo@bar.com",
Token: "ab20g0ddaf012c744e136da16aa21ad9",
}
user2 := types.User{
Login: "joe",
Name: "Joe Sixpack",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
@ -137,7 +124,6 @@ func TestUserstore(t *testing.T) {
g.Assert(err == nil).IsTrue()
g.Assert(len(users)).Equal(2)
g.Assert(users[0].Login).Equal(user1.Login)
g.Assert(users[0].Name).Equal(user1.Name)
g.Assert(users[0].Email).Equal(user1.Email)
g.Assert(users[0].Token).Equal(user1.Token)
})
@ -145,13 +131,11 @@ func TestUserstore(t *testing.T) {
g.It("Should Get a User Count", func() {
user1 := types.User{
Login: "jane",
Name: "Jane Doe",
Email: "foo@bar.com",
Token: "ab20g0ddaf012c744e136da16aa21ad9",
}
user2 := types.User{
Login: "joe",
Name: "Joe Sixpack",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
@ -171,7 +155,6 @@ func TestUserstore(t *testing.T) {
g.It("Should Del a User", func() {
user := types.User{
Login: "joe",
Name: "Joe Sixpack",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}

View file

@ -1,15 +1,12 @@
package types
type User struct {
ID int64 `meddler:"user_id,pk" json:"id"`
Login string `meddler:"user_login" json:"login,omitempty" sql:"unique:ux_user_login"`
Token string `meddler:"user_token" json:"-"`
Secret string `meddler:"user_secret" json:"-"`
Name string `meddler:"user_name" json:"name,omitempty"`
Email string `meddler:"user_email" json:"email,omitempty"`
Gravatar string `meddler:"user_gravatar" json:"gravatar_id,omitempty"`
Admin bool `meddler:"user_admin" json:"admin,omitempty"`
Active bool `meddler:"user_active" json:"active,omitempty"`
Created int64 `meddler:"user_created" json:"created_at,omitempty"`
Updated int64 `meddler:"user_updated" json:"updated_at,omitempty"`
ID int64 `meddler:"user_id,pk" json:"id"`
Login string `meddler:"user_login" json:"login,omitempty" sql:"unique:ux_user_login"`
Token string `meddler:"user_token" json:"-"`
Secret string `meddler:"user_secret" json:"-"`
Email string `meddler:"user_email" json:"email,omitempty"`
Avatar string `meddler:"user_gravatar" json:"gravatar_id,omitempty"`
Active bool `meddler:"user_active" json:"active,omitempty"`
Admin bool `meddler:"user_admin" json:"admin,omitempty"`
}