예제 #1
0
        private async void AddNewAccount()
        {
            if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(ReEnterPassword) || string.IsNullOrEmpty(Email))
            {
                AddNewErrorMessage = "Please input the required info for your account.";
                return;
            }
            AddNewErrorMessage = "";
            if (!Password.Equals(ReEnterPassword))
            {
                AddNewErrorMessage = "The re-enter password not match.";
                return;
            }
            if (!EmailUtils.ValidateEmailFormat(Email))
            {
                AddNewErrorMessage = "Invalid email format.";
                return;
            }

            if (UserAccountManager.GetInstance().CheckUserExists(Username.ToLower()))
            {
                AddNewErrorMessage = "User already exists.";
                return;
            }

            try
            {
                Context.ProgressRingVisibility = Visibility.Visible;
                await Task.Delay(2000);

                var user = new UserAccount()
                {
                    UserName     = Username.ToLower(),
                    Password     = Utils.GenerateMD5String(Password),
                    ModifiedDate = DateTime.Now,
                    Email        = Email,
                    IsAdmin      = false
                };
                UserAccountManager.GetInstance().InsertNewUserAccount(user);
            }
            catch (Exception)
            {
                AddNewErrorMessage             = "There was an error when add new account. Please try again.";
                Context.ProgressRingVisibility = Visibility.Collapsed;
                return;
            }
            int userId = UserAccountManager.GetInstance().GetUserId(Username.ToLower(), Password);

            if (userId > 0)
            {
                var appConfig = AppConfigurationManager.GetInstance().GetAppConfiguration();
                if (appConfig == null)
                {
                    appConfig = new AppConfiguration();
                }
                appConfig.UserAccountId = userId;
                AppConfigurationManager.GetInstance().SaveAppConfiguration(appConfig);
            }

            Context.ProgressRingVisibility      = Visibility.Collapsed;
            Context.AddNewAccountViewVisibility = Visibility.Collapsed;
            ApplicationViewModel.Default.NavigateToMyWorkPage();
        }