mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(invite): worked on db & domain model for temp users, #2353
This commit is contained in:
parent
c3a5822a40
commit
444807c35b
58
pkg/models/temp_user.go
Normal file
58
pkg/models/temp_user.go
Normal file
@ -0,0 +1,58 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Typed errors
|
||||
var (
|
||||
ErrTempUserNotFound = errors.New("User not found")
|
||||
)
|
||||
|
||||
// TempUser holds data for org invites and new sign ups
|
||||
type TempUser struct {
|
||||
Id int64
|
||||
OrgId int64
|
||||
Version int
|
||||
Email string
|
||||
Name string
|
||||
Role string
|
||||
IsInvite bool
|
||||
|
||||
EmailSent bool
|
||||
EmailSentOn time.Time
|
||||
Code string
|
||||
|
||||
Created time.Time
|
||||
Updated time.Time
|
||||
}
|
||||
|
||||
// ---------------------
|
||||
// COMMANDS
|
||||
|
||||
type CreateTempUserCommand struct {
|
||||
Email string
|
||||
Name string
|
||||
OrgId int64
|
||||
IsInvite bool
|
||||
Code string
|
||||
|
||||
Result *TempUser
|
||||
}
|
||||
|
||||
type GetTempUsersForOrgQuery struct {
|
||||
OrgId int64
|
||||
|
||||
Result []*TempUserDTO
|
||||
}
|
||||
|
||||
type TempUserDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Role string `json:"role"`
|
||||
EmailSent bool `json:"emailSent"`
|
||||
EmailSentOn time.Time `json:"emailSentOn"`
|
||||
Created time.Time `json:"createdOn"`
|
||||
}
|
@ -10,6 +10,7 @@ import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
func AddMigrations(mg *Migrator) {
|
||||
addMigrationLogMigrations(mg)
|
||||
addUserMigrations(mg)
|
||||
addTempUserMigrations(mg)
|
||||
addStarMigrations(mg)
|
||||
addOrgMigrations(mg)
|
||||
addDashboardMigration(mg)
|
||||
|
34
pkg/services/sqlstore/migrations/temp_user.go
Normal file
34
pkg/services/sqlstore/migrations/temp_user.go
Normal file
@ -0,0 +1,34 @@
|
||||
package migrations
|
||||
|
||||
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
|
||||
func addTempUserMigrations(mg *Migrator) {
|
||||
tempUserV1 := Table{
|
||||
Name: "temp_user",
|
||||
Columns: []*Column{
|
||||
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
||||
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
||||
{Name: "version", Type: DB_Int, Nullable: false},
|
||||
{Name: "email", Type: DB_NVarchar, Length: 255},
|
||||
{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
|
||||
{Name: "role", Type: DB_NVarchar, Length: 20, Nullable: true},
|
||||
{Name: "code", Type: DB_NVarchar, Length: 255},
|
||||
{Name: "is_invite", Type: DB_Bool},
|
||||
{Name: "invited_by", Type: DB_NVarchar, Length: 255, Nullable: true},
|
||||
{Name: "email_sent", Type: DB_Bool},
|
||||
{Name: "email_sent_on", Type: DB_DateTime, Nullable: true},
|
||||
{Name: "created", Type: DB_DateTime},
|
||||
{Name: "updated", Type: DB_DateTime},
|
||||
},
|
||||
Indices: []*Index{
|
||||
{Cols: []string{"email"}, Type: IndexType},
|
||||
{Cols: []string{"org_id"}, Type: IndexType},
|
||||
{Cols: []string{"code"}, Type: IndexType},
|
||||
},
|
||||
}
|
||||
|
||||
// create table
|
||||
mg.AddMigration("create temp user table v1", NewAddTableMigration(tempUserV1))
|
||||
|
||||
addTableIndicesMigrations(mg, "v1-1", tempUserV1)
|
||||
}
|
47
pkg/services/sqlstore/temp_user.go
Normal file
47
pkg/services/sqlstore/temp_user.go
Normal file
@ -0,0 +1,47 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", CreateTempUser)
|
||||
bus.AddHandler("sql", GetTempUsersForOrg)
|
||||
}
|
||||
|
||||
func CreateTempUser(cmd *m.CreateTempUserCommand) error {
|
||||
return inTransaction2(func(sess *session) error {
|
||||
|
||||
// create user
|
||||
user := &m.TempUser{
|
||||
Email: cmd.Email,
|
||||
Name: cmd.Name,
|
||||
OrgId: cmd.OrgId,
|
||||
Code: cmd.Code,
|
||||
IsInvite: cmd.IsInvite,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
sess.UseBool("is_invite")
|
||||
|
||||
if _, err := sess.Insert(user); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.Result = user
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func GetTempUsersForOrg(query *m.GetTempUsersForOrgQuery) error {
|
||||
query.Result = make([]*m.TempUserDTO, 0)
|
||||
sess := x.Table("temp_user")
|
||||
sess.Where("org_id=?", query.OrgId)
|
||||
|
||||
err := sess.Find(&query.Result)
|
||||
return err
|
||||
}
|
36
pkg/services/sqlstore/temp_user_test.go
Normal file
36
pkg/services/sqlstore/temp_user_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func TestTempUserCommandsAndQueries(t *testing.T) {
|
||||
|
||||
Convey("Testing Temp User commands & queries", t, func() {
|
||||
InitTestDB(t)
|
||||
|
||||
Convey("Given saved api key", func() {
|
||||
cmd := m.CreateTempUserCommand{
|
||||
OrgId: 2256,
|
||||
Name: "hello",
|
||||
Email: "e@as.co",
|
||||
IsInvite: true,
|
||||
}
|
||||
err := CreateTempUser(&cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("Should be able to get temp users by org id", func() {
|
||||
query := m.GetTempUsersForOrgQuery{OrgId: 2256}
|
||||
err = GetTempUsersForOrg(&query)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user