Beispiel #1
0
        /// <summary>
        /// Validates input data to registration
        /// </summary>
        /// <param name="sender">Object that created the event </param>
        /// <param name="e">Event arguments</param>
        private async void ValidateData(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.Enabled = false;

            IStringValidator val =  Program.GetKernel().Get<IStringValidator>();
            
            var username = tbUsername.Text;
            var password = tbPasswd.Text;
            var confirmPasswd = tbPasswdConfirm.Text;

            try
            {
                var validatedUsername = val.ValidateUsername(username);
                
                var validatedPasswd = val.ValidatePassword(password);

                val.ValidatePasswordConfirmation(password, confirmPasswd);

                //put proper user to database
                //var uRepo = Program.GetKernel().Get<IUsersRepository>();
                var uRepo = new HttpUsersRepository();

                if(await uRepo.UserExists(validatedUsername))
                    throw new Exception("Such a user already exists!");

                await uRepo.AddOne(new UsersViewModel()
                {
                    passwd = validatedPasswd,
                    username = validatedUsername,
                    email = "desktopApp" //temporary mail (cant be basically null)
                });
                MessageBox.Show("User was created properly!", 
                    "Added user", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Information);
                this.Close();
                
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, 
                    "Error", 
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
            this.Cursor = Cursors.Default;
            this.Enabled = true;
        }
Beispiel #2
0
        /// <summary>
        /// Adds user to binding source and also validates the added user.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void AddUserClick(object sender, EventArgs e)
        {
            HttpUsersRepository repo = new HttpUsersRepository();
            IStringValidator validator = new Validator();
            string username = tbUser.Text;
            if (!validator.IsNotEmpty(username))
            {
                MessageBox.Show("Provide username!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //if (username == Globals.CurrentUser.username)
            //{
            //    MessageBox.Show("You can not add yourself to group! (you are already in it)");
            //    return;
            //}
            if (usersBindingSource.Cast<UsersViewModel>().Any(user => user.username == username))
            {
                MessageBox.Show("You have already added such a user!");
                return;
            }

            if (await repo.UserExists(username))
            {
                var user = await repo.GetUser(username);
                usersBindingSource.Add(user);
            }
            else
            {
                MessageBox.Show("There is no such a user!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Validates given data and modifies user's credentials
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void SaveChangesClick(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.Enabled = false;

            var validator = Program.GetKernel().Get<IStringValidator>();
            //var currUser = Globals.CurrentUser;

            var name = tbName.Text;
            var surname = tbSurname.Text;
            var email = tbEmail.Text;
            var username = tbUsername.Text;
            var passwd = tbPasswd.Text;
            var confPasswd = tbConfPasswd.Text;


            //var repo = new UsersRepository();
            //IUsersRepository repo = Program.GetKernel().Get<IUsersRepository>();
            HttpUsersRepository repo = new HttpUsersRepository();
           
            try
            {
                username = validator.ValidateUsername(username);
                if (username != currUser.username) //only edit if any changes were provided
                {
                    if (await repo.UserExists(username))
                    {
                        MessageBox.Show("Such a user already exists!");
                        return;
                    }
                    currUser.username = username;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
            currUser.name = name;
            currUser.surname = surname;
            if(email != "") //if was not provided do not let to
            {
                if (validator.isValidMail(email))
                {
                    currUser.email = email;
                }
                else
                {
                    MessageBox.Show("Not valid email was provided!");
                    return;
                }
            }

            try
            {
                if (passwd != "xxx") //it means that nth was changed
                {   
                    passwd = validator.ValidatePassword(passwd);
                    confPasswd = validator.ValidatePassword(confPasswd);

                    if (validator.ValidatePasswordConfirmation(passwd, confPasswd))
                    {
                        currUser.passwd = passwd;
                    }

                }
                await repo.EditOne(currUser);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.Cursor = Cursors.Default;
            this.Enabled = true;
        }