private void UpdateUserSchool(dbTIREntities context, tblUserExt newUser) { try { if (!String.IsNullOrEmpty(newUser.SelectedSchools)) { // 2. Delete all schools for this user context.tblUserSchools.Delete(x => x.UserId == newUser.UserId && x.SchoolYearId == newUser.SchoolYearId); // 3. Add schools string[] schoolIds = newUser.SelectedSchools.Split(','); foreach (var schoolId in schoolIds) { context.tblUserSchools.Add(new tblUserSchool() { SchoolId = Convert.ToInt32(schoolId), UserId = newUser.UserId, CreatedDatetime = DateTime.Now, ChangedDatetime = DateTime.Now, SchoolYearId = newUser.SchoolYearId }); context.SaveChanges(); } } } catch (Exception ex) { throw new Exception(String.Format("Error in UpdateUserSchool(). Error: {0}", ex.ToString())); } }
// This function is used while Create User(post). public List <CheckBoxes> GetSelectedSchoolCheckBoxes(tblUserExt tblUserExtended) { var selectedSchools = tblUserExtended.SelectedSchools.Split(',').Select(int.Parse).ToArray(); var schools = GetUserSchoolWithCheckBoxes(tblUserExtended, selectedSchools); return(schools); }
// GET: /User/Create public ActionResult Create() { try { db = new dbTIREntities(); siteUser = ((SiteUser)Session["SiteUser"]); modelService = new ModelServices(); schoolService = new SchoolService(siteUser, db); userService = new UserService(siteUser, db); int userAssignedDistrict = siteUser.Districts[0].Id; int schoolYearId = modelService.SchoolYearId(); tblUserExt userExtended = new tblUserExt(); userExtended.SchoolYearId = schoolYearId; userExtended.Schools = userService.GetSchoolWithCheckBoxes(userExtended); ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc"); FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, schoolYearId); return(View(userExtended)); } catch (Exception ex) { Logging log = new Logging(); log.LogException(ex); return(View("GeneralError")); } }
//This function is used while Edit User. public List <CheckBoxes> GetUserSchoolWithCheckBoxes(tblUserExt tblUserExtended) { var userSchools = _db.tblUserSchools.Where(x => x.UserId == tblUserExtended.UserId && x.SchoolYearId == tblUserExtended.SchoolYearId) .Select(x => x.SchoolId).ToArray(); var schools = GetUserSchoolWithCheckBoxes(tblUserExtended, userSchools); return(schools); }
public ActionResult Edit(tblUserExt tblUserExtended) { try { db = new dbTIREntities(); modelService = new ModelServices(); siteUser = ((SiteUser)Session["SiteUser"]); userService = new UserService(siteUser, db); schoolService = new SchoolService(siteUser, db); int userAssignedDistrict = siteUser.Districts[0].Id; if (ModelState.IsValid) { if (tblUserExtended.SelectedSchools != null && tblUserExtended.SelectedSchools.Count() > 0) { bool isEmailAddressExist = db.tblUsers.Where(x => x.UserEmail == tblUserExtended.UserEmail && x.UserId != tblUserExtended.UserId).Count() > 0 ? true : false; bool isStateIdExist = db.tblUsers.Where(x => x.StateId == tblUserExtended.StateId && x.UserId != tblUserExtended.UserId).Count() > 0 ? true : false; if ((!isEmailAddressExist) && (!isStateIdExist)) { userService.UpdateUser(tblUserExtended); HelperService.UpdateSiteUserProfile(siteUser, db); return(RedirectToAction("Index")); } else { if (isEmailAddressExist) { ModelState.AddModelError("UserEmail", "Duplicate email - please choose a unique email."); } if (isStateIdExist) { ModelState.AddModelError("StateId", "Duplicate state id - please choose a unique state."); } } } else { ViewBag.SchoolMessage = "Required"; } } tblUserExtended.Schools = userService.GetSelectedSchoolCheckBoxes(tblUserExtended); FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, tblUserExtended.SchoolYearId); FillUserExtendedCommanData(modelService, tblUserExtended); return(View(tblUserExtended)); } catch (Exception ex) { Logging log = new Logging(); log.LogException(ex); return(View("GeneralError")); } }
public IQueryable <UserSchool> GetSchoolsInfo(tblUserExt tblUserExtended) { int userAssignedDistrict = _siteUser.Districts[0].Id; var query = (from school in _db.tblSchools join user in _db.tblUserSchools on school.SchoolId equals user.SchoolId into t from ust in t.Where(ut => ut.UserId == _siteUser.EdsUserId).DefaultIfEmpty() where school.DistrictId == userAssignedDistrict select new UserSchool() { Id = school.SchoolId, Name = school.SchoolDesc, SchoolYearId = ust.SchoolYearId, UserId = ust.UserId }); return(query); }
// Get data while create (get) public List <CheckBoxes> GetSchoolWithCheckBoxes(tblUserExt tblUserExtended) { var query = GetSchoolsInfo(tblUserExtended); var schools = from q in query select new CheckBoxes() { Id = q.Id, Text = q.Name, UserId = (int?)q.UserId, SchoolYearId = q.SchoolYearId, IsLocked = q.UserId == null || tblUserExtended.SchoolYearId != q.SchoolYearId }; var result = schools.OrderBy(x => x.Text).ThenBy(x => x.IsLocked).ToList(); result = RemoveDuplicateSchool(result); return(result); }
private void FillUserExtendedCommanData(ModelServices modelService, tblUserExt tbluserExtended) { ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc", tbluserExtended.RoleId); ViewBag.SchoolYear = schoolService.GetSchoolYearDownData((int)tbluserExtended.SchoolYearId); int currentSchoolYear = Convert.ToInt32(schoolService.GetCurrentSchoolYear()); ViewBag.CurrentSchoolYearId = modelService.GetSchoolYearId(currentSchoolYear); var classData = modelService.GetClassesByTeacher((int)tbluserExtended.SchoolYearId, new[] { (int)tbluserExtended.UserId }); tbluserExtended.SchoolClasses = new List <SchoolClass>(); foreach (var classItem in classData) { SchoolClass classItems = new SchoolClass() { ClassDesc = classItem.Name, SchoolClassId = classItem.Id }; tbluserExtended.SchoolClasses.Add(classItems); } }
public bool IsUserHasPermissionForSchool(tblUserExt tblUserExtended) { bool isUserHasPermissionForSchool = true; int userAssignedDistrict = _siteUser.Districts[0].Id; var edsUserSchools = (from school in _db.tblSchools join userSchool in _db.tblUserSchools on school.SchoolId equals userSchool.SchoolId into t from ust in t.Where(ut => ut.UserId == _siteUser.EdsUserId).DefaultIfEmpty() where school.DistrictId == userAssignedDistrict && ust.SchoolYearId == tblUserExtended.SchoolYearId select school.SchoolId).ToList(); var userSchools = _db.tblUserSchools.Where(x => x.UserId == tblUserExtended.UserId && x.SchoolYearId == tblUserExtended.SchoolYearId).Select(x => x.SchoolId).ToList(); var commonElements = edsUserSchools.Intersect(userSchools).ToList(); if (commonElements.Count == 0) { isUserHasPermissionForSchool = false; } return(isUserHasPermissionForSchool); }
private void UpdateUserSchool(dbTIREntities context, tblUserExt newUser) { try { if (!String.IsNullOrEmpty(newUser.SelectedSchools)) { // 2. Delete all schools for this user context.tblUserSchools.Delete(x => x.UserId == newUser.UserId && x.SchoolYearId == newUser.SchoolYearId); // 3. Add schools string[] schoolIds = newUser.SelectedSchools.Split(','); foreach (var schoolId in schoolIds) { context.tblUserSchools.Add(new tblUserSchool() { SchoolId = Convert.ToInt32(schoolId), UserId = newUser.UserId, CreatedDatetime = DateTime.Now, ChangedDatetime = DateTime.Now, SchoolYearId = newUser.SchoolYearId }); context.SaveChanges(); } } } catch (Exception ex) { throw new Exception(String.Format("Error in UpdateUserSchool(). Error: {0}", ex.ToString())); } }
public ActionResult Edit(int?id, int schoolYearId) { try { db = new dbTIREntities(); modelService = new ModelServices(); siteUser = ((SiteUser)Session["SiteUser"]); schoolService = new SchoolService(siteUser, db); userService = new UserService(siteUser, db); //tblUserExt tbluserExtended = null; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tblUser tbluser = db.tblUsers.Find(id); if (tbluser == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var context = new Models.ApplicationDbContext(); var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); string aspNetUserName = "******"; if (!String.IsNullOrEmpty(tbluser.AspNetUserId)) { ApplicationUser aspNetUser = userManager.FindById(tbluser.AspNetUserId); if (aspNetUser != null) { aspNetUserName = aspNetUser.UserName; } } //Get RoleId from tblUserDistrict instead of tblUser int roleId = userService.GetRoleId(id, schoolYearId); tblUserExt tbluserExtended = new tblUserExt() { UserId = tbluser.UserId, UserName = aspNetUserName, FirstName = tbluser.FirstName, LastName = tbluser.LastName, UserEmail = tbluser.UserEmail, StateId = tbluser.StateId, Schools = tbluser.Schools, SchoolYearId = schoolYearId, RoleId = roleId }; //Check that edited user's school must be from EDSUser schools or edsUser must have permissions to view user school bool isUserHasPermissionForSchool = userService.IsUserHasPermissionForSchool(tbluserExtended); if (!isUserHasPermissionForSchool) { return(RedirectToAction("Index")); } //Get User schools tbluserExtended.Schools = userService.GetUserSchoolWithCheckBoxes(tbluserExtended); var dropDownEmpty = Enumerable.Repeat(new SelectListItem { Value = "", Text = "" }, count: 1); FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, schoolYearId); FillUserExtendedCommanData(modelService, tbluserExtended); return(View(tbluserExtended)); } catch (Exception ex) { Logging log = new Logging(); log.LogException(ex); return(View("GeneralError")); } }
public async Task <ActionResult> Create(tblUserExt tblUserExtended) { try { db = new dbTIREntities(); modelService = new ModelServices(); siteUser = ((SiteUser)Session["SiteUser"]); userService = new UserService(siteUser, db); schoolService = new SchoolService(siteUser, db); int userAssignedDistrict = siteUser.Districts[0].Id; string currentSchoolYear = schoolService.GetCurrentSchoolYear(); if (ModelState.IsValid) { if (tblUserExtended.SelectedSchools != null && tblUserExtended.SelectedSchools.Count() > 0) { var context = new Models.ApplicationDbContext(); var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); // 1. Create ASPNET user string userName = tblUserExtended.UserName; string password = tblUserExtended.Password; var isPasswordValid = password != null && password.Length >= 6 ? true : false; var isUserNameExist = userManager.FindByName(userName); bool isEmailAddressExist = db.tblUsers.Where(x => x.UserEmail == tblUserExtended.UserEmail).Count() > 0 ? true : false; bool isStateIdExist = db.tblUsers.Where(x => x.StateId == tblUserExtended.StateId).Count() > 0 ? true : false; if ((isUserNameExist == null) && (!isEmailAddressExist) && (!isStateIdExist) && (isPasswordValid)) { var user = new ApplicationUser() { UserName = userName }; var result = await userManager.CreateAsync(user, password); if (result.Succeeded) { // 2. Create EDS user ApplicationUser newAspNetUser = userManager.FindByName(userName); if (newAspNetUser != null) { userService.CreateEdsUser(newAspNetUser.Id, tblUserExtended); } } else { throw new Exception(String.Format("ERROR: {0}", result.Errors)); } return(RedirectToAction("Index")); } else { if (isUserNameExist != null) { ModelState.AddModelError("UserName", "Duplicate name - please choose a unique name."); } if (isEmailAddressExist) { ModelState.AddModelError("UserEmail", "Duplicate email - please choose a unique email."); } if (isStateIdExist) { ModelState.AddModelError("StateId", "Duplicate state id - please choose a unique state."); } if (!isPasswordValid) { ModelState.AddModelError("Password", "Please enter password at least 6 characters."); } } } else { ViewBag.SchoolMessage = "Required"; } } tblUserExtended.Schools = userService.GetSelectedSchoolCheckBoxes(tblUserExtended); ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc", tblUserExtended.RoleId); FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, tblUserExtended.SchoolYearId); return(View(tblUserExtended)); } catch (Exception ex) { Logging log = new Logging(); log.LogException(ex); return(View("GeneralError")); } }
public bool IsUserHasPermissionForSchool(tblUserExt tblUserExtended) { bool isUserHasPermissionForSchool = true; int userAssignedDistrict = _siteUser.Districts[0].Id; var edsUserSchools = (from school in _db.tblSchools join userSchool in _db.tblUserSchools on school.SchoolId equals userSchool.SchoolId into t from ust in t.Where(ut => ut.UserId == _siteUser.EdsUserId).DefaultIfEmpty() where school.DistrictId == userAssignedDistrict && ust.SchoolYearId == tblUserExtended.SchoolYearId select school.SchoolId).ToList(); var userSchools = _db.tblUserSchools.Where(x => x.UserId == tblUserExtended.UserId && x.SchoolYearId == tblUserExtended.SchoolYearId).Select(x=>x.SchoolId).ToList(); var commonElements = edsUserSchools.Intersect(userSchools).ToList(); if (commonElements.Count == 0) { isUserHasPermissionForSchool = false; } return isUserHasPermissionForSchool; }
public void UpdateUser(tblUserExt tbluserExtended) { using (var context = new dbTIREntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { var updateUser = context.tblUsers.Find(tbluserExtended.UserId); if (updateUser != null) { // 1. Update user updateUser.ChangeDatetime = DateTime.Now; updateUser.FirstName = tbluserExtended.FirstName; updateUser.LastName = tbluserExtended.LastName; updateUser.UserEmail = tbluserExtended.UserEmail; updateUser.StateId = tbluserExtended.StateId; updateUser.LocalId = tbluserExtended.LocalId; updateUser.RoleId = tbluserExtended.RoleId; context.SaveChanges(); // 2. Update User district info var updatedUserDistrict = context.tblUserDistricts.Where(x => x.UserId == tbluserExtended.UserId && x.SchoolYearId == tbluserExtended.SchoolYearId).FirstOrDefault(); if (updatedUserDistrict != null) { updatedUserDistrict.RoleId = (int)tbluserExtended.RoleId; context.tblUserDistricts.Add(updatedUserDistrict); context.Entry(updatedUserDistrict).State = EntityState.Modified; } else { // 3. Add district info int userAssignedDistrict = _siteUser.Districts[0].Id; context.tblUserDistricts.Add(new tblUserDistrict() { DistrictId = userAssignedDistrict, UserId = tbluserExtended.UserId, CreatedDatetime = DateTime.Now, ChangedDatetime = null, SchoolYearId = tbluserExtended.SchoolYearId, RoleId = (int)tbluserExtended.RoleId }); } context.SaveChanges(); // 4. Update User schools info UpdateUserSchool(context, tbluserExtended); } // 3. Commit changes dbContextTransaction.Commit(); } catch (Exception ex) { // 4. Rollback changes dbContextTransaction.Rollback(); } } } }
public IQueryable<UserSchool> GetSchoolsInfo(tblUserExt tblUserExtended) { int userAssignedDistrict = _siteUser.Districts[0].Id; var query = (from school in _db.tblSchools join user in _db.tblUserSchools on school.SchoolId equals user.SchoolId into t from ust in t.Where(ut => ut.UserId == _siteUser.EdsUserId).DefaultIfEmpty() where school.DistrictId == userAssignedDistrict select new UserSchool() { Id = school.SchoolId, Name = school.SchoolDesc, SchoolYearId = ust.SchoolYearId, UserId = ust.UserId }); return query; }
// Get data while create (get) public List<CheckBoxes> GetSchoolWithCheckBoxes(tblUserExt tblUserExtended) { var query = GetSchoolsInfo(tblUserExtended); var schools = from q in query select new CheckBoxes() { Id = q.Id, Text = q.Name, UserId = (int?)q.UserId, SchoolYearId = q.SchoolYearId, IsLocked = q.UserId == null || tblUserExtended.SchoolYearId != q.SchoolYearId }; var result = schools.OrderBy(x => x.Text).ThenBy(x => x.IsLocked).ToList(); result = RemoveDuplicateSchool(result); return result; }
//This function is used while Edit User. public List<CheckBoxes> GetUserSchoolWithCheckBoxes(tblUserExt tblUserExtended) { var userSchools = _db.tblUserSchools.Where(x => x.UserId == tblUserExtended.UserId && x.SchoolYearId == tblUserExtended.SchoolYearId) .Select(x => x.SchoolId).ToArray(); var schools = GetUserSchoolWithCheckBoxes(tblUserExtended, userSchools); return schools; }
public void CreateEdsUser(string aspnetUserId, tblUserExt newUser) { try { // 1. Add user to tblUser tblUser newTblUser = new tblUser() { CreateDatetime = DateTime.Now, AspNetUserId = aspnetUserId, FirstName = newUser.FirstName, LastName = newUser.LastName, UserEmail = newUser.UserEmail, StateId = newUser.StateId, LocalId = newUser.LocalId, RoleId = newUser.RoleId }; _db.tblUsers.Add(newTblUser); _db.SaveChanges(); // Get new user int edsUserId = _db.tblUsers .Where(x => x.AspNetUserId == aspnetUserId) .Select(x => x.UserId) .SingleOrDefault(); int districtId = _siteUser.Districts[0].Id; newUser.UserId = edsUserId; using (var context = new dbTIREntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { // 2. Add user to tblUserDistrict tblUserDistrict newTblUserDistrict = new tblUserDistrict() { UserId = edsUserId, DistrictId = districtId, CreatedDatetime = DateTime.Now, ChangedDatetime = DateTime.Now, SchoolYearId = newUser.SchoolYearId, RoleId = (int)newUser.RoleId }; context.tblUserDistricts.Add(newTblUserDistrict); // 3. Add schools for this user UpdateUserSchool(context, newUser); // 4. Commit changes dbContextTransaction.Commit(); } catch (Exception ex) { // 4. Rollback changes dbContextTransaction.Rollback(); throw ex; } } } } catch (Exception ex) { DeleteAspNetUser(aspnetUserId); throw new Exception(String.Format("ERROR: {0}", ex)); } }
public void UpdateUser(tblUserExt tbluserExtended) { using (var context = new dbTIREntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { var updateUser = context.tblUsers.Find(tbluserExtended.UserId); if (updateUser != null) { // 1. Update user updateUser.ChangeDatetime = DateTime.Now; updateUser.FirstName = tbluserExtended.FirstName; updateUser.LastName = tbluserExtended.LastName; updateUser.UserEmail = tbluserExtended.UserEmail; updateUser.StateId = tbluserExtended.StateId; updateUser.LocalId = tbluserExtended.LocalId; updateUser.RoleId = tbluserExtended.RoleId; context.SaveChanges(); // 2. Update User district info var updatedUserDistrict = context.tblUserDistricts.Where(x => x.UserId == tbluserExtended.UserId && x.SchoolYearId == tbluserExtended.SchoolYearId).FirstOrDefault(); if (updatedUserDistrict != null) { updatedUserDistrict.RoleId = (int)tbluserExtended.RoleId; context.tblUserDistricts.Add(updatedUserDistrict); context.Entry(updatedUserDistrict).State = EntityState.Modified; } else { // 3. Add district info int userAssignedDistrict = _siteUser.Districts[0].Id; context.tblUserDistricts.Add(new tblUserDistrict() { DistrictId = userAssignedDistrict, UserId = tbluserExtended.UserId, CreatedDatetime = DateTime.Now, ChangedDatetime = null, SchoolYearId = tbluserExtended.SchoolYearId, RoleId = (int)tbluserExtended.RoleId }); } context.SaveChanges(); // 4. Update User schools info UpdateUserSchool(context, tbluserExtended); } // 3. Commit changes dbContextTransaction.Commit(); } catch (Exception ex) { // 4. Rollback changes dbContextTransaction.Rollback(); } } } }
// This function is used while Create User(post). public List<CheckBoxes> GetSelectedSchoolCheckBoxes(tblUserExt tblUserExtended) { var selectedSchools = tblUserExtended.SelectedSchools.Split(',').Select(int.Parse).ToArray(); var schools = GetUserSchoolWithCheckBoxes(tblUserExtended, selectedSchools); return schools; }
public void CreateEdsUser(string aspnetUserId, tblUserExt newUser) { try { // 1. Add user to tblUser tblUser newTblUser = new tblUser() { CreateDatetime = DateTime.Now, AspNetUserId = aspnetUserId, FirstName = newUser.FirstName, LastName = newUser.LastName, UserEmail = newUser.UserEmail, StateId = newUser.StateId, LocalId = newUser.LocalId, RoleId = newUser.RoleId }; _db.tblUsers.Add(newTblUser); _db.SaveChanges(); // Get new user int edsUserId = _db.tblUsers .Where(x => x.AspNetUserId == aspnetUserId) .Select(x => x.UserId) .SingleOrDefault(); int districtId = _siteUser.Districts[0].Id; newUser.UserId = edsUserId; using (var context = new dbTIREntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { // 2. Add user to tblUserDistrict tblUserDistrict newTblUserDistrict = new tblUserDistrict() { UserId = edsUserId, DistrictId = districtId, CreatedDatetime = DateTime.Now, ChangedDatetime = DateTime.Now, SchoolYearId = newUser.SchoolYearId, RoleId = (int)newUser.RoleId }; context.tblUserDistricts.Add(newTblUserDistrict); // 3. Add schools for this user UpdateUserSchool(context, newUser); // 4. Commit changes dbContextTransaction.Commit(); } catch (Exception ex) { // 4. Rollback changes dbContextTransaction.Rollback(); throw ex; } } } } catch (Exception ex) { DeleteAspNetUser(aspnetUserId); throw new Exception(String.Format("ERROR: {0}", ex)); } }