2014-09-26 07:10:48 +00:00
|
|
|
package database
|
2014-09-25 08:07:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/drone/drone/shared/model"
|
|
|
|
"github.com/russross/meddler"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Permstore struct {
|
|
|
|
meddler.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPermstore(db meddler.DB) *Permstore {
|
|
|
|
return &Permstore{db}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPerm retrieves the User's permission from
|
|
|
|
// the datastore for the given repository.
|
|
|
|
func (db *Repostore) GetPerm(user *model.User, repo *model.Repo) (*model.Perm, error) {
|
|
|
|
var perm = new(model.Perm)
|
2014-09-26 07:10:48 +00:00
|
|
|
var err = meddler.QueryRow(db, perm, rebind(permQuery), user.ID, repo.ID)
|
2014-09-25 08:07:40 +00:00
|
|
|
return perm, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostPerm saves permission in the datastore.
|
|
|
|
func (db *Repostore) PostPerm(perm *model.Perm) error {
|
|
|
|
return meddler.Save(db, permTable, perm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutPerm saves permission in the datastore.
|
|
|
|
func (db *Repostore) PutPerm(perm *model.Perm) error {
|
|
|
|
return meddler.Save(db, permTable, perm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DelPerm removes permission from the datastore.
|
|
|
|
func (db *Repostore) DelPerm(perm *model.Perm) error {
|
2014-09-26 07:10:48 +00:00
|
|
|
var _, err = db.Exec(rebind(permDeleteStmt), perm.ID)
|
2014-09-25 08:07:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permission table name in database.
|
|
|
|
const permTable = "perms"
|
|
|
|
|
|
|
|
// SQL query to retrieve a user's permission to
|
|
|
|
// access a repository.
|
|
|
|
const permQuery = `
|
|
|
|
SELECT *
|
|
|
|
FROM perms
|
|
|
|
WHERE user_id=?
|
|
|
|
AND repo_id=?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL statement to delete a User by ID.
|
|
|
|
const permDeleteStmt = `
|
|
|
|
DELETE FROM perms
|
|
|
|
WHERE perm_id=?
|
|
|
|
`
|