2015-01-14 02:33:34 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2015-01-16 00:45:37 -06:00
|
|
|
"time"
|
|
|
|
|
2015-01-14 02:33:34 -06:00
|
|
|
"github.com/go-xorm/xorm"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-01-14 02:33:34 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-01-27 01:26:11 -06:00
|
|
|
bus.AddHandler("sql", GetApiKeys)
|
2015-04-08 01:59:12 -05:00
|
|
|
bus.AddHandler("sql", GetApiKeyById)
|
2015-02-26 10:23:28 -06:00
|
|
|
bus.AddHandler("sql", GetApiKeyByName)
|
2015-01-27 01:26:11 -06:00
|
|
|
bus.AddHandler("sql", DeleteApiKey)
|
|
|
|
bus.AddHandler("sql", AddApiKey)
|
2015-01-14 02:33:34 -06:00
|
|
|
}
|
|
|
|
|
2015-01-27 01:26:11 -06:00
|
|
|
func GetApiKeys(query *m.GetApiKeysQuery) error {
|
2015-02-23 13:07:49 -06:00
|
|
|
sess := x.Limit(100, 0).Where("org_id=?", query.OrgId).Asc("name")
|
2015-01-14 02:33:34 -06:00
|
|
|
|
2015-01-27 01:26:11 -06:00
|
|
|
query.Result = make([]*m.ApiKey, 0)
|
2015-01-14 02:33:34 -06:00
|
|
|
return sess.Find(&query.Result)
|
|
|
|
}
|
|
|
|
|
2015-01-27 01:26:11 -06:00
|
|
|
func DeleteApiKey(cmd *m.DeleteApiKeyCommand) error {
|
2015-01-14 02:33:34 -06:00
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
2015-02-23 13:07:49 -06:00
|
|
|
var rawSql = "DELETE FROM api_key WHERE id=? and org_id=?"
|
|
|
|
_, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId)
|
2015-01-14 02:33:34 -06:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-27 01:26:11 -06:00
|
|
|
func AddApiKey(cmd *m.AddApiKeyCommand) error {
|
2015-01-14 02:33:34 -06:00
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
2015-01-27 01:26:11 -06:00
|
|
|
t := m.ApiKey{
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
Name: cmd.Name,
|
|
|
|
Role: cmd.Role,
|
|
|
|
Key: cmd.Key,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
2015-01-14 02:33:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.Insert(&t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.Result = &t
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-08 01:59:12 -05:00
|
|
|
func GetApiKeyById(query *m.GetApiKeyByIdQuery) error {
|
|
|
|
var apikey m.ApiKey
|
|
|
|
has, err := x.Id(query.ApiKeyId).Get(&apikey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has == false {
|
|
|
|
return m.ErrInvalidApiKey
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = &apikey
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-26 10:23:28 -06:00
|
|
|
func GetApiKeyByName(query *m.GetApiKeyByNameQuery) error {
|
2015-01-27 01:26:11 -06:00
|
|
|
var apikey m.ApiKey
|
2015-02-26 10:23:28 -06:00
|
|
|
has, err := x.Where("org_id=? AND name=?", query.OrgId, query.KeyName).Get(&apikey)
|
2015-01-16 09:15:35 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if has == false {
|
2015-01-27 01:26:11 -06:00
|
|
|
return m.ErrInvalidApiKey
|
2015-01-16 09:15:35 -06:00
|
|
|
}
|
|
|
|
|
2015-01-27 01:26:11 -06:00
|
|
|
query.Result = &apikey
|
2015-01-16 09:15:35 -06:00
|
|
|
return nil
|
|
|
|
}
|