public ActionResult AddUser() { var ctx1 = PTFContext.Current; var ctx2 = PTFReportsContext.Current; var model = Session.Get <UserModel>(Strings.CachedMODELUserDetails, new UserModel()); TryUpdateModel(model); switch (model.CurrentStep) { case null: case "": case "FormStart": ViewData["CountryList"] = new SelectList(ctx1.ISO_ptf .OrderBy(iso => iso.iso_country), "iso_number", "iso_country", null); break; case "CountryList": model.CountryID = Request.Parse <int>("CountryList", model.CountryID); ViewData["CompanyList"] = new SelectList(ctx1.HeadOffices .Where(ho => ho.ho_iso_id == model.CountryID) .OrderBy(ho => ho.ho_name), "ho_id", "ho_name", null); break; case "CompanyList": model.CompanyID = Request.Parse <int>("CompanyList", model.CompanyID); ViewData["BranchList"] = new SelectList(ctx1.Branches .Where(br => br.br_iso_id == model.CountryID && br.br_ho_id == model.CompanyID && (br.br_active == "Y" || !br.br_date_left.HasValue)) .OrderBy(br => br.br_name), "br_id", "FullName", null); break; case "BranchList": model.BranchID = Request.Parse <int>("BranchList", model.BranchID); ViewData["UserTypeList"] = new SelectList(UserType.Normal.ToDict <UserType>(), "Key", "Value", model.UserTypeID); ViewData["LanguageList"] = new SelectList(eLanguage.English.ToDict <eLanguage>(), "Key", "Value", model.LanguageID); break; case "UserTypeList": model.UserTypeID = Request.Parse <int>("UserTypeList", model.UserTypeID); model.LanguageID = Request.Parse <int>("LanguageList", model.LanguageID); model.Language = model.LanguageID.GetEnumName <eLanguage>(); model.DefaultPass = string.IsNullOrEmpty(model.DefaultPass) ? new Random().GenerateString(DEFPASSLEN) : model.DefaultPass; model.CurrentStep = "FormSubmit"; break; case "FormSubmit": { if (IsValidPostBack("Name")) { return(RunSafe(() => { if (model.UserID == 0) { //Add var user = (UserDetail)model; user.Ud_firstLogin = true; ctx2.AddToUserDetails(user); //TODO: Remove var root = ctx2.Folders.FirstOrDefault(f => !f.ParentID.HasValue); if (root != null) { user.Folders.Add(root); } } else { //Update var pass = model.DefaultPass.Encript(); var user = ctx2.UserDetails.First(u => u.Ud_id == model.UserID); user.Ud_loginName = model.Login; user.Ud_firstName = model.Name; user.Ud_lastName = model.Surname; user.Ud_email = model.Email; user.Ud_userType = model.UserTypeID; user.Ud_password = pass; user.Ud_salt = model.Salt ?? ""; user.Ud_iso_id = model.CountryID; user.Ud_ho_id = model.CompanyID; user.Ud_br_id = model.BranchID; user.Ud_languageId = model.LanguageID; } ctx2.SaveChanges(); Session.Remove(Strings.CachedMODELUserDetails); })); } } break; } model.Country = ISO_ptf.GetNameByID(model.CountryID); model.Company = HeadOffice.GetNameByID(model.CountryID, model.CompanyID); model.Branch = Branch.GetNameByID(model.CountryID, model.CompanyID, model.BranchID); model.UserType = ((UserType)model.UserTypeID).ToString(); return(View(model)); }
private void ShowLongParameterForm(ReportParametersModel model, Report report, ParamObj data) { var user = Session[Strings.USER].Cast <UserDetail>(); var repository = Session.Get <TagRepository>(Strings.TagRepository, new TagRepository()); var ctx1 = PTFContext.Current; var ctx2 = PTFReportsContext.Current; if (model.CurrentStep == "IsoID") { model.CurrentStep = "CompanyID"; } else if (model.CurrentStep == "CompanyID") { model.CurrentStep = "RetailerID"; } else if (model.CurrentStep == "RetailerID") { model.CurrentStep = "FormSubmit"; } else if (model.CurrentStep == "FormSubmit") { model.CurrentStep = null; } switch (model.CurrentStep) { case "": case null: { goto case "IsoID"; } case "IsoID": { model.CurrentStep = "IsoID"; model.SkipCountry = true; model.CountryID = user.Ud_iso_id; goto case "CompanyID"; //always get user country var list = new List <ISO_ptf>(); list.Add(ISO_ptf.GetUserDefault(user)); ViewData[ParamMapping.IsoID.ToString()] = new SelectList(list.OrderBy(c => c.iso_country), "iso_number", "iso_country", null); } break; case "CompanyID": { model.CurrentStep = "CompanyID"; model.CompanyID = user.Ud_ho_id; model.Country = ISO_ptf.GetNameByID(model.CountryID); var list = new List <HeadOffice>(); //user permissions var permissions = ctx2.Permissions.Where(p => p.UserID == user.Ud_id && p.IsoID == model.CountryID); foreach (var gp in permissions.GroupBy(c => c.HoID)) { var p = gp.First(); list.Add(ctx1.HeadOffices.FirstOrDefault( ho => ho.ho_iso_id == p.IsoID && ho.ho_id == p.HoID)); } model.SkipCompany = list.Count < 2; //get user's company if (list.Count == 0) { model.CompanyID = user.Ud_ho_id; goto case "RetailerID"; } ViewData[ParamMapping.CompanyID.ToString()] = new SelectList(list.OrderBy(ho => ho.ho_name), "ho_id", "ho_name", null); } break; case "RetailerID": { model.CurrentStep = "RetailerID"; model.CompanyID = data.CompanyID.Parse(user.Ud_ho_id); model.Company = HeadOffice.GetNameByID(user.Ud_iso_id, model.CompanyID); // retailers var list = new List <Branch>(); //user permissions var permissions = ctx2.Permissions.Where(p => p.UserID == user.Ud_id && p.IsoID == user.Ud_iso_id && p.HoID == model.CompanyID); repository.Clear(); foreach (Permission p in permissions) { var branch = ctx1.Branches.FirstOrDefault( br => br.br_iso_id == p.IsoID && br.br_ho_id == p.HoID && br.br_id == p.RetailerID); list.Add(branch); repository.SetTag(branch); } //get user's retailer if (list.Count == 0) { var branch = Branch.GetUserDefault(user); list.Add(branch); repository.SetTag(branch); } ViewData[ParamMapping.RetailerID.ToString()] = new MultiSelectList(list.OrderBy(br => br.FullName2), "br_id", "FullName2", null); } break; } }