Commit 1181e967 by bergquist

merge create user handlers

parent da91b91b
package sqlstore package sqlstore
import ( import (
"context"
"fmt" "fmt"
"testing" "testing"
"time" "time"
...@@ -389,7 +390,7 @@ func createUser(name string, role string, isAdmin bool) m.User { ...@@ -389,7 +390,7 @@ func createUser(name string, role string, isAdmin bool) m.User {
setting.AutoAssignOrgRole = role setting.AutoAssignOrgRole = role
currentUserCmd := m.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin} currentUserCmd := m.CreateUserCommand{Login: name, Email: name + "@test.com", Name: "a " + name, IsAdmin: isAdmin}
err := CreateUser(&currentUserCmd) err := CreateUser(context.Background(), &currentUserCmd)
So(err, ShouldBeNil) So(err, ShouldBeNil)
q1 := m.GetUserOrgListQuery{UserId: currentUserCmd.Result.Id} q1 := m.GetUserOrgListQuery{UserId: currentUserCmd.Result.Id}
......
package sqlstore package sqlstore
import ( import (
"context"
"testing" "testing"
"time" "time"
...@@ -22,9 +23,9 @@ func TestAccountDataAccess(t *testing.T) { ...@@ -22,9 +23,9 @@ func TestAccountDataAccess(t *testing.T) {
ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"} ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name"} ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name"}
err := CreateUser(&ac1cmd) err := CreateUser(context.Background(), &ac1cmd)
So(err, ShouldBeNil) So(err, ShouldBeNil)
err = CreateUser(&ac2cmd) err = CreateUser(context.Background(), &ac2cmd)
So(err, ShouldBeNil) So(err, ShouldBeNil)
q1 := m.GetUserOrgListQuery{UserId: ac1cmd.Result.Id} q1 := m.GetUserOrgListQuery{UserId: ac1cmd.Result.Id}
...@@ -43,8 +44,8 @@ func TestAccountDataAccess(t *testing.T) { ...@@ -43,8 +44,8 @@ func TestAccountDataAccess(t *testing.T) {
ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"} ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true} ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
err := CreateUser(&ac1cmd) err := CreateUser(context.Background(), &ac1cmd)
err = CreateUser(&ac2cmd) err = CreateUser(context.Background(), &ac2cmd)
So(err, ShouldBeNil) So(err, ShouldBeNil)
ac1 := ac1cmd.Result ac1 := ac1cmd.Result
...@@ -182,7 +183,7 @@ func TestAccountDataAccess(t *testing.T) { ...@@ -182,7 +183,7 @@ func TestAccountDataAccess(t *testing.T) {
Convey("Given an org user with dashboard permissions", func() { Convey("Given an org user with dashboard permissions", func() {
ac3cmd := m.CreateUserCommand{Login: "ac3", Email: "ac3@test.com", Name: "ac3 name", IsAdmin: false} ac3cmd := m.CreateUserCommand{Login: "ac3", Email: "ac3@test.com", Name: "ac3 name", IsAdmin: false}
err := CreateUser(&ac3cmd) err := CreateUser(context.Background(), &ac3cmd)
So(err, ShouldBeNil) So(err, ShouldBeNil)
ac3 := ac3cmd.Result ac3 := ac3cmd.Result
......
package sqlstore package sqlstore
import ( import (
"context"
"fmt" "fmt"
"testing" "testing"
...@@ -22,7 +23,7 @@ func TestTeamCommandsAndQueries(t *testing.T) { ...@@ -22,7 +23,7 @@ func TestTeamCommandsAndQueries(t *testing.T) {
Name: fmt.Sprint("user", i), Name: fmt.Sprint("user", i),
Login: fmt.Sprint("loginuser", i), Login: fmt.Sprint("loginuser", i),
} }
err := CreateUser(userCmd) err := CreateUser(context.Background(), userCmd)
So(err, ShouldBeNil) So(err, ShouldBeNil)
userIds = append(userIds, userCmd.Result.Id) userIds = append(userIds, userCmd.Result.Id)
} }
......
...@@ -16,7 +16,7 @@ import ( ...@@ -16,7 +16,7 @@ import (
) )
func init() { func init() {
bus.AddHandler("sql", CreateUser) //bus.AddHandler("sql", CreateUser)
bus.AddHandler("sql", GetUserById) bus.AddHandler("sql", GetUserById)
bus.AddHandler("sql", UpdateUser) bus.AddHandler("sql", UpdateUser)
bus.AddHandler("sql", ChangeUserPassword) bus.AddHandler("sql", ChangeUserPassword)
...@@ -31,7 +31,7 @@ func init() { ...@@ -31,7 +31,7 @@ func init() {
bus.AddHandler("sql", DeleteUser) bus.AddHandler("sql", DeleteUser)
bus.AddHandler("sql", UpdateUserPermissions) bus.AddHandler("sql", UpdateUserPermissions)
bus.AddHandler("sql", SetUserHelpFlag) bus.AddHandler("sql", SetUserHelpFlag)
bus.AddHandlerCtx("sql", CreateUserCtx) bus.AddHandlerCtx("sql", CreateUser)
} }
func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *DBSession) (int64, error) { func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *DBSession) (int64, error) {
...@@ -81,90 +81,80 @@ func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *DBSession) (int64, error ...@@ -81,90 +81,80 @@ func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *DBSession) (int64, error
return org.Id, nil return org.Id, nil
} }
func internalCreateUser(sess *DBSession, cmd *m.CreateUserCommand) error { func CreateUser(ctx context.Context, cmd *m.CreateUserCommand) error {
orgId, err := getOrgIdForNewUser(cmd, sess) return inTransactionWithRetryCtx(ctx, func(sess *DBSession) error {
if err != nil { orgId, err := getOrgIdForNewUser(cmd, sess)
return err if err != nil {
} return err
}
if cmd.Email == "" { if cmd.Email == "" {
cmd.Email = cmd.Login cmd.Email = cmd.Login
} }
// create user // create user
user := m.User{ user := m.User{
Email: cmd.Email, Email: cmd.Email,
Name: cmd.Name, Name: cmd.Name,
Login: cmd.Login, Login: cmd.Login,
Company: cmd.Company, Company: cmd.Company,
IsAdmin: cmd.IsAdmin, IsAdmin: cmd.IsAdmin,
OrgId: orgId, OrgId: orgId,
EmailVerified: cmd.EmailVerified, EmailVerified: cmd.EmailVerified,
Created: time.Now(), Created: time.Now(),
Updated: time.Now(), Updated: time.Now(),
LastSeenAt: time.Now().AddDate(-10, 0, 0), LastSeenAt: time.Now().AddDate(-10, 0, 0),
} }
if len(cmd.Password) > 0 { if len(cmd.Password) > 0 {
user.Salt = util.GetRandomString(10) user.Salt = util.GetRandomString(10)
user.Rands = util.GetRandomString(10) user.Rands = util.GetRandomString(10)
user.Password = util.EncodePassword(cmd.Password, user.Salt) user.Password = util.EncodePassword(cmd.Password, user.Salt)
} }
sess.UseBool("is_admin") sess.UseBool("is_admin")
if _, err := sess.Insert(&user); err != nil { if _, err := sess.Insert(&user); err != nil {
return err return err
} }
sess.publishAfterCommit(&events.UserCreated{ sess.publishAfterCommit(&events.UserCreated{
Timestamp: user.Created, Timestamp: user.Created,
Id: user.Id, Id: user.Id,
Name: user.Name, Name: user.Name,
Login: user.Login, Login: user.Login,
Email: user.Email, Email: user.Email,
}) })
cmd.Result = user cmd.Result = user
// create org user link // create org user link
if !cmd.SkipOrgSetup { if !cmd.SkipOrgSetup {
orgUser := m.OrgUser{ orgUser := m.OrgUser{
OrgId: orgId, OrgId: orgId,
UserId: user.Id, UserId: user.Id,
Role: m.ROLE_ADMIN, Role: m.ROLE_ADMIN,
Created: time.Now(), Created: time.Now(),
Updated: time.Now(), Updated: time.Now(),
} }
if setting.AutoAssignOrg && !user.IsAdmin { if setting.AutoAssignOrg && !user.IsAdmin {
if len(cmd.DefaultOrgRole) > 0 { if len(cmd.DefaultOrgRole) > 0 {
orgUser.Role = m.RoleType(cmd.DefaultOrgRole) orgUser.Role = m.RoleType(cmd.DefaultOrgRole)
} else { } else {
orgUser.Role = m.RoleType(setting.AutoAssignOrgRole) orgUser.Role = m.RoleType(setting.AutoAssignOrgRole)
}
} }
}
if _, err = sess.Insert(&orgUser); err != nil { if _, err = sess.Insert(&orgUser); err != nil {
return err return err
}
} }
}
return nil return nil
}
func CreateUserCtx(ctx context.Context, cmd *m.CreateUserCommand) error {
return inTransactionWithRetryCtx(ctx, func(sess *DBSession) error {
return internalCreateUser(sess, cmd)
}, 0) }, 0)
} }
func CreateUser(cmd *m.CreateUserCommand) error {
return inTransaction(func(sess *DBSession) error {
return internalCreateUser(sess, cmd)
})
}
func GetUserById(query *m.GetUserByIdQuery) error { func GetUserById(query *m.GetUserByIdQuery) error {
user := new(m.User) user := new(m.User)
has, err := x.Id(query.Id).Get(user) has, err := x.Id(query.Id).Get(user)
......
package sqlstore package sqlstore
import ( import (
"context"
"fmt" "fmt"
"testing" "testing"
...@@ -22,7 +23,7 @@ func TestUserAuth(t *testing.T) { ...@@ -22,7 +23,7 @@ func TestUserAuth(t *testing.T) {
Name: fmt.Sprint("user", i), Name: fmt.Sprint("user", i),
Login: fmt.Sprint("loginuser", i), Login: fmt.Sprint("loginuser", i),
} }
err = CreateUser(cmd) err = CreateUser(context.Background(), cmd)
So(err, ShouldBeNil) So(err, ShouldBeNil)
users = append(users, cmd.Result) users = append(users, cmd.Result)
} }
......
package sqlstore package sqlstore
import ( import (
"context"
"fmt" "fmt"
"testing" "testing"
...@@ -24,7 +25,7 @@ func TestUserDataAccess(t *testing.T) { ...@@ -24,7 +25,7 @@ func TestUserDataAccess(t *testing.T) {
Name: fmt.Sprint("user", i), Name: fmt.Sprint("user", i),
Login: fmt.Sprint("loginuser", i), Login: fmt.Sprint("loginuser", i),
} }
err = CreateUser(cmd) err = CreateUser(context.Background(), cmd)
So(err, ShouldBeNil) So(err, ShouldBeNil)
users = append(users, cmd.Result) users = append(users, cmd.Result)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment