grafana/pkg/models/account.go

90 lines
2.2 KiB
Go
Raw Normal View History

2014-09-19 23:37:18 +08:00
package models
import (
"errors"
"time"
)
2014-10-06 03:13:01 +08:00
var (
CreateAccount func(acccount *Account) error
UpdateAccount func(acccount *Account) error
GetAccountByLogin func(emailOrName string) (*Account, error)
GetAccount func(accountId int64) (*Account, error)
GetOtherAccountsFor func(accountId int64) ([]*OtherAccount, error)
GetCollaboratorsForAccount func(accountId int64) ([]*CollaboratorInfo, error)
AddCollaborator func(collaborator *Collaborator) error
2014-10-06 03:13:01 +08:00
)
// Typed errors
var (
ErrAccountNotFound = errors.New("Account not found")
)
2014-09-19 23:37:18 +08:00
type CollaboratorLink struct {
2014-11-20 19:11:07 +08:00
AccountId int64
2014-09-19 23:37:18 +08:00
Role string
2014-09-21 21:01:59 +08:00
Email string
2014-09-19 23:37:18 +08:00
ModifiedOn time.Time
CreatedOn time.Time
}
2014-09-21 21:01:59 +08:00
type OtherAccount struct {
2014-11-24 15:04:41 +08:00
Id int64
Email string
Role string
2014-09-21 21:01:59 +08:00
}
2014-09-20 18:13:46 +08:00
type Account struct {
2014-11-20 19:11:07 +08:00
Id int64
Login string `xorm:"UNIQUE NOT NULL"`
Email string `xorm:"UNIQUE NOT NULL"`
Name string `xorm:"UNIQUE NOT NULL"`
FullName string
2014-09-19 23:37:18 +08:00
Password string
2014-11-20 19:11:07 +08:00
IsAdmin bool
Salt string `xorm:"VARCHAR(10)"`
2014-09-22 17:39:40 +08:00
Company string
2014-09-19 23:37:18 +08:00
NextDashboardId int
2014-11-20 19:11:07 +08:00
UsingAccountId int64
Collaborators []CollaboratorLink `xorm:"-"`
Created time.Time `xorm:"CREATED"`
Updated time.Time `xorm:"UPDATED"`
2014-09-19 23:37:18 +08:00
}
2014-09-21 21:01:59 +08:00
func (account *Account) AddCollaborator(newCollaborator *Account) error {
2014-09-19 23:37:18 +08:00
for _, collaborator := range account.Collaborators {
2014-09-21 21:01:59 +08:00
if collaborator.AccountId == newCollaborator.Id {
2014-09-19 23:37:18 +08:00
return errors.New("Collaborator already exists")
}
}
account.Collaborators = append(account.Collaborators, CollaboratorLink{
2014-09-21 21:01:59 +08:00
AccountId: newCollaborator.Id,
Email: newCollaborator.Email,
2014-09-19 23:37:18 +08:00
Role: "admin",
CreatedOn: time.Now(),
ModifiedOn: time.Now(),
})
return nil
}
2014-09-21 21:01:59 +08:00
2014-11-20 19:11:07 +08:00
func (account *Account) RemoveCollaborator(accountId int64) {
2014-09-21 21:01:59 +08:00
list := account.Collaborators
for i, collaborator := range list {
if collaborator.AccountId == accountId {
account.Collaborators = append(list[:i], list[i+1:]...)
break
}
}
}
2014-11-20 19:11:07 +08:00
func (account *Account) HasCollaborator(accountId int64) bool {
2014-09-21 21:01:59 +08:00
for _, collaborator := range account.Collaborators {
if collaborator.AccountId == accountId {
return true
}
}
return false
}