func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *session) (int64, error) { var org m.Org if setting.AutoAssignOrg { // right now auto assign to org with id 1 has, err := sess.Where("id=?", 1).Get(&org) if err != nil { return 0, err } if has { return org.Id, nil } else { org.Name = "Main Org." org.Id = 1 } } else { org.Name = util.StringsFallback2(cmd.Email, cmd.Login) } org.Created = time.Now() org.Updated = time.Now() if _, err := sess.Insert(&org); err != nil { return 0, err } sess.publishAfterCommit(&events.OrgCreated{ Timestamp: org.Created, Id: org.Id, Name: org.Name, }) return org.Id, nil }
func AddOrgInvite(c *middleware.Context, inviteDto dtos.AddInviteForm) Response { logger := log.New("main") logger.Info(" AddOrgInvite1", c.OrgId) if !inviteDto.Role.IsValid() { return ApiError(400, "Invalid role specified", nil) } // first try get existing user userQuery := m.GetUserByLoginQuery{LoginOrEmail: inviteDto.LoginOrEmail} if err := bus.Dispatch(&userQuery); err != nil { if err != m.ErrUserNotFound { return ApiError(500, "Failed to query db for existing user check", err) } } else { return inviteExistingUserToOrg(c, userQuery.Result, &inviteDto) } logger1 := log.New("main") logger1.Info(" AddOrgInvite2", c.OrgId) cmd := m.CreateTempUserCommand{} cmd.OrgId = c.OrgId cmd.Email = inviteDto.LoginOrEmail cmd.Name = inviteDto.Name cmd.Status = m.TmpUserInvitePending cmd.InvitedByUserId = c.UserId cmd.Code = util.GetRandomString(30) cmd.Role = inviteDto.Role cmd.RemoteAddr = c.Req.RemoteAddr if err := bus.Dispatch(&cmd); err != nil { return ApiError(500, "Failed to save invite to database", err) } // send invite email if !inviteDto.SkipEmails && util.IsEmail(inviteDto.LoginOrEmail) { emailCmd := m.SendEmailCommand{ To: []string{inviteDto.LoginOrEmail}, Template: "new_user_invite.html", Data: map[string]interface{}{ "Name": util.StringsFallback2(cmd.Name, cmd.Email), "OrgName": c.OrgName, "Email": c.Email, "LinkUrl": setting.ToAbsUrl("invite/" + cmd.Code), "InvitedBy": util.StringsFallback3(c.Name, c.Email, c.Login), }, } if err := bus.Dispatch(&emailCmd); err != nil { return ApiError(500, "Failed to send email invite", err) } return ApiSuccess(fmt.Sprintf("Sent invite to %s", inviteDto.LoginOrEmail)) } return ApiSuccess(fmt.Sprintf("Created invite for %s", inviteDto.LoginOrEmail)) }