public ActionResult UpdateAgreements([Bind(Include = "ProductIDs")] SupplierUpdateAgreementViewModel model) { if (model.ProductIDs.Length == 0) { return(RedirectToAction("Index", "Manage")); } SupplierWithAgreements supplier = null; try { DataObjects.User usr = _userManager.RetrieveUserByUserName(User.Identity.Name); supplier = _supplierManager.RetrieveSupplierWithAgreementsByUserId(usr.UserId); } catch (Exception ex) { if (ex.InnerException == null) { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable, ex.Message)); } else { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable, ex.Message + "\n" + ex.InnerException.Message)); } } if (null == supplier) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Unable to find supplier")); } try { foreach (int id in model.ProductIDs) { Product product = _productManager.RetrieveProductById(id); _agreementManager.CreateAgreementsForSupplier(supplier, product, null, false); } } catch (Exception ex) { if (ex.InnerException == null) { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable, ex.Message)); } else { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable, ex.Message + "\n" + ex.InnerException.Message)); } } return(RedirectToAction("Index", "Manage", new { Message = MVCPresentationLayer.Controllers.ManageController.ManageMessageId.AppliedSuccess })); }
public ActionResult Edit(string username) { if (username == null || username.Equals("")) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } DataObjects.User usr = _userManager.RetrieveUserByUserName(username.ToString()); SupplierWithAgreements supplierWithAgreements = _supplierManager.RetrieveSupplierWithAgreementsByUserId(usr.UserId); if (supplierWithAgreements == null) { return(HttpNotFound()); } supplierWithAgreements.Agreements.RemoveAll(a => a.IsApproved == false); return(View(supplierWithAgreements)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (!ModelState.IsValid) { return(View(model)); } DataObjects.User userFound = null; try { userFound = _appUserManager.AuthenticateWebUser(model.Email, model.Password); } catch (Exception ex) { Debug.WriteLine(ex.Message); } if (null != userFound) { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, change to shouldLockout: true var user = new ApplicationUser { UserName = userFound.UserName, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: 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>"); //UserManager.AddClaim(user.Id, new Claim(ClaimTypes.GivenName, userFound.FirstName)); //UserManager.AddClaim(user.Id, new Claim(ClaimTypes.Surname, userFound.LastName)); //UserManager.AddClaim(user.Id, new Claim(ClaimTypes.Email, userFound.EmailAddress)); return(RedirectToAction("Index", "Home")); } AddErrors(result); } return(View(model)); }
public ActionResult Index() { List <CompanyOrderWithLines> orders; try { DataObjects.User usr = _userManager.RetrieveUserByUserName(User.Identity.Name); Supplier supplier = _supplierManager.RetrieveSupplierByUserId(usr.UserId); //orders = _companyOrderManager.RetrieveCompanyOrdersWithLines(); orders = _companyOrderManager.RetrieveCompanyOrdersWithLinesBySupplierId(supplier.SupplierID); return(View(orders)); } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable)); } }
public ActionResult Edit([Bind(Include = "FarmTaxID,FarmName,FarmAddress,FarmCity,FarmState")] SupplierWithAgreements supplierWithAgreements) { if (ModelState.IsValid) { //db.Entry(supplierWithAgreements).State = EntityState.Modified; //db.SaveChanges(); try { DataObjects.User usr = _userManager.RetrieveUserByUserName(User.Identity.Name); if (usr == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } SupplierWithAgreements oldSupplier = _supplierManager.RetrieveSupplierWithAgreementsByUserId(usr.UserId); if (oldSupplier == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //set unchangable values supplierWithAgreements.SupplierID = oldSupplier.SupplierID; supplierWithAgreements.IsApproved = oldSupplier.IsApproved; supplierWithAgreements.UserId = oldSupplier.UserId; supplierWithAgreements.Active = oldSupplier.Active; supplierWithAgreements.Agreements = oldSupplier.Agreements; // will be modifiable in a different area supplierWithAgreements.ApprovedBy = oldSupplier.ApprovedBy; if (!_supplierManager.UpdateSupplierAccount(oldSupplier, supplierWithAgreements)) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.ServiceUnavailable)); } return(RedirectToAction("Index", "Manage", new { Message = MVCPresentationLayer.Controllers.ManageController.ManageMessageId.UpdatedSuccess })); } return(View(supplierWithAgreements)); }
/// <summary> /// Michael Takrama /// /// Created: /// 4/22/2017 /// /// Assigns Roles to Users Based On Approval Status from Snack Overflow /// </summary> /// <remarks> /// Modified by Christian Lopez /// 2017/05/05 /// /// Fix introduced bug when logging in with unregistered user /// </remarks> /// <param name="context">DbContext</param> /// <param name="model">Login View Model</param> /// <returns>Boolean indicatinng Role Assignment Success</returns> public bool HasOrAssignRoles(ApplicationDbContext context, LoginViewModel model) { DataObjects.User usr = null; User userFound = null; try { userFound = _appUserManager.AuthenticateWebUser(model.UserName, model.Password); //uses only email } catch { } try { if (_appUserManager.AuthenticateUser(model.UserName, model.Password)) { usr = _appUserManager.RetrieveUserByUserName(model.UserName); } //usr = _appUserManager.RetrieveUserByUserName(userFound.UserName); } catch (Exception ex) { Debug.WriteLine(ex.Message); } var user = context.Users.FirstOrDefault(x => x.Email == model.UserName) ?? context.Users.FirstOrDefault(x => x.UserName == model.UserName); if (user == null || usr == null) { model.Password = ""; ModelState.AddModelError("Password", "Incorrect username or password"); return(false); } //Force email if (user != null && usr.EmailAddress != null) { model.UserName = usr.EmailAddress; } //if (null == userFound) //{ // model.Password = ""; // ModelState.AddModelError("Password", "Incorrect username or password"); //} //var identityUserRoles = user.Roles; //if (identityUserRoles.Count != 0) // return true; if (null != usr) { var identityUserRoles = user.Roles; if (identityUserRoles.Count != 0) { return(true); } bool[] roles = null; try { roles = _appUserManager.GetUserRoles(usr.UserId); } catch (Exception ex) { Debug.WriteLine(ex.Message); } //var store = new UserStore<ApplicationUser>(context); //var manager = new UserManager<ApplicationUser>(store); if (user == null) { return(false); } //var identityUserRoles = user.Roles; //if (identityUserRoles.Count != 0) // return true; if (roles != null) { if (roles[0]) { UserManager.AddToRole(user.Id, "Customer"); return(true); } if (roles[1]) { UserManager.AddToRole(user.Id, "Employee"); return(true); } if (roles[2]) { UserManager.AddToRole(user.Id, "Supplier"); return(true); } } } return(false); }