public ActionResult EditUser(EditUserViewModel evm) { OJewelryDB db = new OJewelryDB(); ApplicationDbContext sec = new ApplicationDbContext(); ApplicationUser user = sec.Users.Find(evm.UserId); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(sec)); if (ModelState.IsValid) { // Update the companies user can access UpdateCompaniesUsers(user, db, evm.Companies); // Update Role UpdateRoles(sec, user, evm.RoleId); db.SaveChanges(); //sec.SaveChanges(); return(RedirectToAction("UserList")); } evm.UserName = user.UserName; ViewBag.Roles = sec.Roles.ToList(); evm.Companies.Clear(); foreach (Company c in db.Companies) // should be company left outer joined to users by id, exclude Managers, admins { CompanyAuthorizedUser cau = new CompanyAuthorizedUser() { bIncluded = false, // based on users for this company CompanyId = c.Id, CompanyName = c.Name }; evm.Companies.Add(cau); } return(View(evm)); }
private void UpdateCompaniesUsers(ApplicationUser user, OJewelryDB db, List <CompanyAuthorizedUser> Companies) { /* Add companies for this user */ List <CompanyUser> addComps = new List <CompanyUser>(); List <CompanyUser> delComps = new List <CompanyUser>(); { // add bIncluded users who are not in in company foreach (CompanyAuthorizedUser cau in Companies.Where(c => c.bIncluded == true)) { CompanyUser cu = new CompanyUser() { CompanyId = cau.CompanyId, UserId = user.Id }; if (db.CompaniesUsers.Where(x => x.CompanyId == cu.CompanyId && x.UserId == cu.UserId).Count() == 0) { addComps.Add(cu); } } db.CompaniesUsers.AddRange(addComps); // remove !bIncluded users who are in company foreach (CompanyAuthorizedUser cau in Companies.Where(c => c.bIncluded == false)) { CompanyUser cu = db.CompaniesUsers.Where(x => x.CompanyId == cau.CompanyId && x.UserId == user.Id).FirstOrDefault(); if (cu != null) { delComps.Add(cu); } } db.CompaniesUsers.RemoveRange(delComps); } }
void PopulateEVMForUser(EditUserViewModel evm, ApplicationUser user) { evm.UserName = user.UserName; OJewelryDB db = new OJewelryDB(); foreach (Company c in db.Companies) // should be company left outer joined to users by id, exclude Managers, admins { CompanyAuthorizedUser cau = new CompanyAuthorizedUser() { bIncluded = false, // based on users for this company CompanyId = c.Id, CompanyName = c.Name }; evm.Companies.Add(cau); } List <CompanyUser> cus = db.CompaniesUsers.Where(x => x.UserId == user.Id).ToList(); List <int> accessibleCompanyIds = evm.Companies.Select(s1 => s1.CompanyId).ToList().Intersect(cus.Select(s2 => s2.CompanyId).ToList()).ToList(); foreach (int i in accessibleCompanyIds) { CompanyAuthorizedUser c = evm.Companies.Find(x => x.CompanyId == i); if (c != null) { c.bIncluded = true; } } }
private async Task <bool> SaveImageInStorage(OJewelryDB db, StyleViewModel svm, bool bCopy = false) { if (svm.Style.Image == null && svm.PostedImageFile == null) { Trace.TraceInformation("Image and postedfile are both blank, returnig."); return(true); } string filename; string env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"; string username = User.Identity.GetUserName(); env = (env == "Production") ? "" : env + "_"; // Set filename if (svm.PostedImageFile != null) // new image { if (bCopy) // new style { filename = env + "StyleImg_" + username + Path.GetExtension(svm.PostedImageFile.FileName); } else { filename = env + "StyleImg_" + svm.CompanyId.ToString() + "_" + svm.Style.Id.ToString() + "_" + Path.GetExtension(svm.PostedImageFile.FileName); } Trace.TraceInformation("Uploading {0} to {1}", svm.PostedImageFile.FileName, filename); svm.Style.Image = await Singletons.azureBlobStorage.Upload(svm.PostedImageFile, filename); Trace.TraceInformation("Done uploading, image=[{0}]", svm.Style.Image); } else // same image { Uri u = new Uri(svm.Style.Image); string blobFile = u.Segments.Last(); if (bCopy) // new style { // Copy old image to new filename = env + "StyleImg_" + username + Path.GetExtension(svm.Style.Image); Trace.TraceInformation("Copying {0} to {1} (bCopy=true)", blobFile, filename); svm.Style.Image = await Singletons.azureBlobStorage.Copy(blobFile, filename); Trace.TraceInformation("Done copying, image=[{0}]", svm.Style.Image); } else { filename = env + "StyleImg_" + svm.CompanyId.ToString() + "_" + svm.Style.Id.ToString() + "_" + Path.GetExtension(svm.Style.Image); if (svm.Style.Image != filename) { Trace.TraceInformation("Copying {0} to {1} (bCopy=false)", blobFile, filename); svm.Style.Image = await Singletons.azureBlobStorage.Copy(blobFile, filename); Trace.TraceInformation("Done copying, image=[{0}]", svm.Style.Image); } } //svm.Style.Image = await Singletons.azureBlobStorage.Upload(svm.Style.Image, filename); } return(true); }
public ActionResult Register() { RegisterViewModel rvm = new RegisterViewModel(); ApplicationDbContext sec = new ApplicationDbContext(); OJewelryDB db = new OJewelryDB(); foreach (Company c in db.Companies) // should be company left outer joined to users by id, exclude Managers, admins { CompanyAuthorizedUser cau = new CompanyAuthorizedUser() { bIncluded = false, // based on users for this company CompanyId = c.Id, CompanyName = c.Name }; rvm.Companies.Add(cau); } ViewBag.Roles = sec.Roles.ToList(); return(View(rvm)); }
public async Task <ActionResult> Register(RegisterViewModel model) { OJewelryDB db = new OJewelryDB(); ApplicationDbContext sec = new ApplicationDbContext(); if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.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 https://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>"); UpdateCompaniesUsers(user, db, model.Companies); UpdateRoles(sec, user, model.RoleId); db.SaveChanges(); return(RedirectToAction("UserList", "Account")); } AddErrors(result); } // If we got this far, something failed, redisplay form ViewBag.Roles = sec.Roles.ToList(); List <Company> allCompanies = db.Companies.ToList(); foreach (CompanyAuthorizedUser cau in model.Companies) // should be company left outer joined to users by id, exclude Managers, admins { cau.CompanyName = allCompanies.First(c => c.Id == cau.CompanyId).Name; } return(View(model)); }
public ActionResult Memo(MemoViewModel m) { ModelState.Clear(); OJewelryDB dc = new OJewelryDB(); // populate style data Style sdb = dc.Styles.Find(m.style.Id); m.style.Name = sdb.StyleName; //m.style.Num = sdb.StyleNum; m.style.Qty = sdb.Quantity; if (m.SendReturnMemoRadio == 1) { if (m.NewExistingPresenterRadio == 2) { //Memo a new presenter if (String.IsNullOrEmpty(m.PresenterName)) { ModelState.AddModelError("Presenter Name", "Name is required for new Presenters."); } if (String.IsNullOrEmpty(m.PresenterEmail) && String.IsNullOrEmpty(m.PresenterPhone)) { ModelState.AddModelError("Presenter Contact Info", "Phone or Email is required for new Presenters."); } Presenter p = new Presenter() { CompanyId = m.CompanyId, Name = m.PresenterName, Email = m.PresenterEmail, Phone = m.PresenterPhone, }; dc.Presenters.Add(p); m.PresenterId = p.Id; } else { // Memo an existing presenter - nothing to validate in this case as they just selected a Presenter from the list } // create new memo, reduce inventory sdb.Quantity -= m.SendQty; String note = "Sending " + m.SendQty.ToString() + " items to " + m.PresenterName + " on " + DateTime.Now.ToString(); Memo mo = new Memo() { Quantity = m.SendQty, Date = DateTime.Now, Notes = note, PresenterID = m.PresenterId, StyleID = m.style.Id, }; dc.Memos.Add(mo); if (m.SendQty > m.style.Qty) { ModelState.AddModelError("Send Quantity", "You cannot memo more items than you have in inventory."); } if (m.SendQty < 1) { ModelState.AddModelError("Send Quantity", "You can only memo a positive number of items."); } } else { //Return Items from Presenter // iterate thru the memos to take items back. Increase the inventory as appropriate. If all items are returned, delete the memo foreach (MemoModel memo in m.Memos) { if (memo.ReturnQty < 0) { ModelState.AddModelError("Return Style", "You can only return a positive number to inventory."); } if (memo.ReturnQty > 0) { if (memo.ReturnQty > memo.Quantity) { ModelState.AddModelError("Return Style", "You can't return more items than were memo'd out."); } // update db Memo mdb = dc.Memos.Find(memo.Id); if (mdb.Quantity == memo.ReturnQty) { // remove the row and remove item from collection dc.Memos.Remove(mdb); } else { // decrease the amount mdb.Quantity -= memo.ReturnQty; } sdb.Quantity += memo.ReturnQty; } // ReturnQty is 0, no action } } if (ModelState.IsValid) { // Save changes, go to clientlist dc.SaveChanges(); //return ClientList(); //return View(m); } return(Memo(m.style.Id)); }