/// <summary> /// Button click hanlder, it checks provided user credentials and if are proper opens MainForm and refuses to open if not /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void LoginUser(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; this.Enabled = false; var username = tbLogin.Text; var passwd = tbPasswd.Text; //var userRepo = new UsersRepository(); //IUsersRepository userRepo = Program.GetKernel().Get<IUsersRepository>(); HttpUsersRepository userRepo = new HttpUsersRepository(); if (await userRepo.LoginUser(username, passwd)) //login success { var mainForm = Program.GetKernel().Get<MainForm>(); this.Hide(); mainForm.Show(); } else { MessageBox.Show("Provided credentials are invalid!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } this.Cursor = Cursors.Default; this.Enabled = true; }
/// <summary> /// Constructor for YearInGroup form. Assings repositories, disables buttons for non admin users. /// </summary> /// <param name="_groupId">Id of a group the year belongs to.</param> /// <param name="_yearId">Id of a selected year.</param> /// <param name="_isAdmin">Determines wether the form is displayed by an admin.</param> public YearInGroupForm(int _groupId, int _yearId, bool _isAdmin) { InitializeComponent(); groupId = _groupId; yearId = _yearId; isAdmin = _isAdmin; users = new HttpUsersRepository(); years = new HttpYearsRepository(); subjects = new HttpSubjectsRepository(); groupDetails = new HttpGroupDetailsRepository(); usersGridView.CellDoubleClick += seeUser; subjectsGridView.CellDoubleClick += editSubject; UpdateTables(); btnAddSubject.Click += AddSubject; if (!isAdmin) { btnAddSubject.Visible = false; btnDeleteYear.Visible = false; btnEditYear.Visible = false; } }
/// <summary> /// Updates binding source. /// </summary> public async void UpdateSource() { var repo = new HttpUsersRepository(); foreach (var groupDetail in currGroup.GroupDetails) { var user = await repo.GetOne(groupDetail.user_id); usersBindingSource.Add(user); } }
/// <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; }
/// <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); } }
/// <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; }
/*-----------------------------UPDATING FUNCTIONS-----------------------------*/ /// <summary> /// Updates repositories. /// </summary> private void UpdateRepositories() { years = new HttpYearsRepository(); subjects = new HttpSubjectsRepository(); grades = new HttpSubjectDetailsRepository(); users = new HttpUsersRepository(); groups = new HttpGroupsRepository(); groupDetails = new HttpGroupDetailsRepository(); }
/// <summary> /// Funciton updates currently logged user to have the same data as in databse. /// </summary> /// <returns></returns> public static async Task UpdateCurrentUser() { HttpUsersRepository repo = new HttpUsersRepository(); CurrentUser = await repo.GetOne(CurrentUser.id); }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { //var user = new ApplicationUser() { UserName = model.Username, Username = model.Username }; var user = new ApplicationUser() { UserName = model.Username, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { ////adding user to custom Users table UsersViewModel userViewModel = new UsersViewModel() { id = user.Id, username = user.UserName, email = user.Email, passwd = "tmp" }; HttpUsersRepository repo = new HttpUsersRepository(); await repo.AddOne(userViewModel); // ==== //Users newUser = new Users() //{ // id=user.Id, // username = user.UserName, // email = user.Username, // passwd = user.PasswordHash, //}; //GradingBookDbEntities db = new GradingBookDbEntities(); //db.Users.Add(newUser); //db.SaveChanges(); // await SignInAsync(user, isPersistent: false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return RedirectToAction("Index", "Home"); } else { AddErrors(result); } } // If we got this far, something failed, redisplay form return View(model); }