summaryrefslogtreecommitdiffstats
path: root/app/user.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/user.go')
-rw-r--r--app/user.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/app/user.go b/app/user.go
new file mode 100644
index 000000000..5acd9dcaa
--- /dev/null
+++ b/app/user.go
@@ -0,0 +1,60 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package app
+
+import (
+ l4g "github.com/alecthomas/log4go"
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
+)
+
+func CreateUser(user *model.User) (*model.User, *model.AppError) {
+
+ user.Roles = model.ROLE_SYSTEM_USER.Id
+
+ // Below is a special case where the first user in the entire
+ // system is granted the system_admin role
+ if result := <-Srv.Store.User().GetTotalUsersCount(); result.Err != nil {
+ return nil, result.Err
+ } else {
+ count := result.Data.(int64)
+ if count <= 0 {
+ user.Roles = model.ROLE_SYSTEM_ADMIN.Id + " " + model.ROLE_SYSTEM_USER.Id
+ }
+ }
+
+ user.MakeNonNil()
+ user.Locale = *utils.Cfg.LocalizationSettings.DefaultClientLocale
+
+ if err := utils.IsPasswordValid(user.Password); user.AuthService == "" && err != nil {
+ return nil, err
+ }
+
+ if result := <-Srv.Store.User().Save(user); result.Err != nil {
+ l4g.Error(utils.T("api.user.create_user.save.error"), result.Err)
+ return nil, result.Err
+ } else {
+ ruser := result.Data.(*model.User)
+
+ if user.EmailVerified {
+ if cresult := <-Srv.Store.User().VerifyEmail(ruser.Id); cresult.Err != nil {
+ l4g.Error(utils.T("api.user.create_user.verified.error"), cresult.Err)
+ }
+ }
+
+ pref := model.Preference{UserId: ruser.Id, Category: model.PREFERENCE_CATEGORY_TUTORIAL_STEPS, Name: ruser.Id, Value: "0"}
+ if presult := <-Srv.Store.Preference().Save(&model.Preferences{pref}); presult.Err != nil {
+ l4g.Error(utils.T("api.user.create_user.tutorial.error"), presult.Err.Message)
+ }
+
+ ruser.Sanitize(map[string]bool{})
+
+ // This message goes to everyone, so the teamId, channelId and userId are irrelevant
+ message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_NEW_USER, "", "", "", nil)
+ message.Add("user_id", ruser.Id)
+ go Publish(message)
+
+ return ruser, nil
+ }
+}