grafana/pkg/models/account.go
2014-10-05 21:13:07 +02:00

88 lines
1.9 KiB
Go

package models
import (
"errors"
"time"
)
var (
CreateAccount func(acccount *Account) error
UpdateAccount func(acccount *Account) error
GetAccountByLogin func(emailOrName string) (*Account, error)
GetAccount func(accountId int) (*Account, error)
GetOtherAccountsFor func(accountId int) ([]*OtherAccount, error)
)
// Typed errors
var (
ErrAccountNotFound = errors.New("Account not found")
)
type CollaboratorLink struct {
AccountId int
Role string
Email string
ModifiedOn time.Time
CreatedOn time.Time
}
type OtherAccount struct {
Id int `gorethink:"id"`
Name string
Role string
}
type Account struct {
Id int `gorethink:"id"`
Version int
Login string
Email string
AccountName string
Password string
Name string
Company string
NextDashboardId int
UsingAccountId int
Collaborators []CollaboratorLink
CreatedOn time.Time
ModifiedOn time.Time
LastLoginOn time.Time
}
func (account *Account) AddCollaborator(newCollaborator *Account) error {
for _, collaborator := range account.Collaborators {
if collaborator.AccountId == newCollaborator.Id {
return errors.New("Collaborator already exists")
}
}
account.Collaborators = append(account.Collaborators, CollaboratorLink{
AccountId: newCollaborator.Id,
Email: newCollaborator.Email,
Role: "admin",
CreatedOn: time.Now(),
ModifiedOn: time.Now(),
})
return nil
}
func (account *Account) RemoveCollaborator(accountId int) {
list := account.Collaborators
for i, collaborator := range list {
if collaborator.AccountId == accountId {
account.Collaborators = append(list[:i], list[i+1:]...)
break
}
}
}
func (account *Account) HasCollaborator(accountId int) bool {
for _, collaborator := range account.Collaborators {
if collaborator.AccountId == accountId {
return true
}
}
return false
}