feat(invite): worked on pending invitations list, revoke invite now works, #2353
This commit is contained in:
@@ -13,10 +13,11 @@ func addTempUserMigrations(mg *Migrator) {
|
||||
{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: "status", Type: DB_Varchar, Length: 20},
|
||||
{Name: "invited_by_user_id", Type: DB_BigInt, Nullable: true},
|
||||
{Name: "email_sent", Type: DB_Bool},
|
||||
{Name: "email_sent_on", Type: DB_DateTime, Nullable: true},
|
||||
{Name: "remote_addr", Type: DB_Varchar, Nullable: true},
|
||||
{Name: "created", Type: DB_DateTime},
|
||||
{Name: "updated", Type: DB_DateTime},
|
||||
},
|
||||
@@ -24,11 +25,14 @@ func addTempUserMigrations(mg *Migrator) {
|
||||
{Cols: []string{"email"}, Type: IndexType},
|
||||
{Cols: []string{"org_id"}, Type: IndexType},
|
||||
{Cols: []string{"code"}, Type: IndexType},
|
||||
{Cols: []string{"status"}, Type: IndexType},
|
||||
},
|
||||
}
|
||||
|
||||
// create table
|
||||
mg.AddMigration("create temp user table v1-3", NewAddTableMigration(tempUserV1))
|
||||
// addDropAllIndicesMigrations(mg, "v7", tempUserV1)
|
||||
// mg.AddMigration("Drop old table tempUser v7", NewDropTableMigration("temp_user"))
|
||||
|
||||
addTableIndicesMigrations(mg, "v1-3", tempUserV1)
|
||||
// create table
|
||||
mg.AddMigration("create temp user table v1-7", NewAddTableMigration(tempUserV1))
|
||||
addTableIndicesMigrations(mg, "v1-7", tempUserV1)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package sqlstore
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
@@ -10,6 +11,15 @@ import (
|
||||
func init() {
|
||||
bus.AddHandler("sql", CreateTempUser)
|
||||
bus.AddHandler("sql", GetTempUsersForOrg)
|
||||
bus.AddHandler("sql", UpdateTempUserStatus)
|
||||
}
|
||||
|
||||
func UpdateTempUserStatus(cmd *m.UpdateTempUserStatusCommand) error {
|
||||
return inTransaction(func(sess *xorm.Session) error {
|
||||
var rawSql = "UPDATE temp_user SET status=? WHERE id=? and org_id=?"
|
||||
_, err := sess.Exec(rawSql, string(cmd.Status), cmd.Id, cmd.OrgId)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func CreateTempUser(cmd *m.CreateTempUserCommand) error {
|
||||
@@ -22,14 +32,13 @@ func CreateTempUser(cmd *m.CreateTempUserCommand) error {
|
||||
OrgId: cmd.OrgId,
|
||||
Code: cmd.Code,
|
||||
Role: cmd.Role,
|
||||
IsInvite: cmd.IsInvite,
|
||||
Status: cmd.Status,
|
||||
RemoteAddr: cmd.RemoteAddr,
|
||||
InvitedByUserId: cmd.InvitedByUserId,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
sess.UseBool("is_invite")
|
||||
|
||||
if _, err := sess.Insert(user); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -51,10 +60,10 @@ func GetTempUsersForOrg(query *m.GetTempUsersForOrgQuery) error {
|
||||
u.login as invited_by
|
||||
FROM ` + dialect.Quote("temp_user") + ` as tu
|
||||
LEFT OUTER JOIN ` + dialect.Quote("user") + ` as u on u.id = tu.invited_by_user_id
|
||||
WHERE tu.org_id=? ORDER BY tu.created desc`
|
||||
WHERE tu.org_id=? AND tu.status =? ORDER BY tu.created desc`
|
||||
|
||||
query.Result = make([]*m.TempUserDTO, 0)
|
||||
sess := x.Sql(rawSql, query.OrgId)
|
||||
sess := x.Sql(rawSql, query.OrgId, string(query.Status))
|
||||
err := sess.Find(&query.Result)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -15,22 +15,28 @@ func TestTempUserCommandsAndQueries(t *testing.T) {
|
||||
|
||||
Convey("Given saved api key", func() {
|
||||
cmd := m.CreateTempUserCommand{
|
||||
OrgId: 2256,
|
||||
Name: "hello",
|
||||
Email: "e@as.co",
|
||||
IsInvite: true,
|
||||
OrgId: 2256,
|
||||
Name: "hello",
|
||||
Email: "e@as.co",
|
||||
Status: m.TmpUserInvitePending,
|
||||
}
|
||||
err := CreateTempUser(&cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("Should be able to get temp users by org id", func() {
|
||||
query := m.GetTempUsersForOrgQuery{OrgId: 2256}
|
||||
query := m.GetTempUsersForOrgQuery{OrgId: 2256, Status: m.TmpUserInvitePending}
|
||||
err = GetTempUsersForOrg(&query)
|
||||
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
})
|
||||
|
||||
Convey("Should be able update status", func() {
|
||||
cmd2 := m.UpdateTempUserStatusCommand{OrgId: 2256, Status: m.TmpUserRevoked, Id: cmd.Result.Id}
|
||||
err := UpdateTempUserStatus(&cmd2)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user