Пример #1
0
        public static int           newUser(this TM_UserData userData, string username, string password, string email, string firstname, string lastname, string note, string title, string company, string country, string state, List <UserTag> userTags, int groupId)
        {
            if (userData.isNull())
            {
                return(-1);
            }

            var userId = Math.Abs(Guid.NewGuid().hash());

            "Creating new user: {0} with id {1}".debug(username, userId);

            if (groupId < 1)                                    //set default user type
            {
                groupId = 2;                                    //by default new users are of type 2 (i.e. Reader)
            }
            else
            {
                UserRole.ManageUsers.demand();  // only users with UserRole.ManageUsers should be able to create non-default users
            }
            var tmUser = new TMUser {
                UserID    = userId,
                UserName  = username,
                FirstName = firstname,
                LastName  = lastname,
                Company   = (company),
                Country   = country,
                State     = state,
                GroupID   = groupId,
                Title     = title,
                EMail     = email ?? "",
                UserTags  = userTags
            };

            var tmConfig = TMConfig.Current;

            tmUser.AccountStatus.UserEnabled    = tmConfig.newAccountsEnabled();
            tmUser.AccountStatus.ExpirationDate = tmConfig.currentExpirationDate();

            tmUser.SecretData.PasswordHash = tmUser.createPasswordHash(password);
            userData.TMUsers.Add(tmUser);

            if (TMConfig.Current.windowsAuthentication_Enabled().isFalse())
            {
                SendEmails.SendNewUserEmails("New user created: {0}".format(tmUser.UserName), tmUser);
            }

            tmUser.logUserActivity("New User", "");     // this will trigger tmUser.event_User_Updated();

            //tmUser.event_User_Updated(); //tmUser.saveTmUser();
            //userData.triggerGitCommit();
            return(userId);
        }