public ActionResult Save(User_DTO model) { if (ModelState.IsValid) { var user = User_DTO.UpdateUser(model); return RedirectToAction("Profile", new { userName = user.UserName }); } return View(model); }
public ActionResult CreateUser(User_DTO user) { if (!ModelState.IsValid) return Json(new { result = "error", message = "The form is invalid" }); var success = User_DTO.AddUser(user); if (!success) return Json(new { result = "error", message = "There was an error creating the user." }); return RedirectToAction("Index"); }
public bool DomainAccess() { if (!string.IsNullOrEmpty(AppSettings.FullyQualifiedDomainName)) { this.FullyQualifiedDomain = AppSettings.FullyQualifiedDomainName; } if (string.IsNullOrEmpty(this.FullyQualifiedDomain)) return false; string ldap = "LDAP://"; for (int i = 0; i < FullyQualifiedDomain.Split('.').Count(); i++) { if (i == FullyQualifiedDomain.Split('.').Count() - 1) ldap = ldap + "DC=" + FullyQualifiedDomain.Split('.')[i] ; else ldap = ldap + "DC=" + FullyQualifiedDomain.Split('.')[i] + ","; } using (var entry = new DirectoryEntry(ldap, UserName, Password)) { try { object obj = entry.NativeObject; DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + UserName + ")"; search.PropertiesToLoad.Add("givenName"); search.PropertiesToLoad.Add("sn"); search.PropertiesToLoad.Add("mail"); SearchResult result = search.FindOne(); if (result == null) return false; var user = User_DTO.Get(UserName); if (user == null) { user = new User_DTO(); user.UserName = UserName; user.EmailAddress = result.Properties["mail"].Count > 0 ? result.Properties["mail"][0].ToString() : "Unknown"; user.FirstName = result.Properties["givenName"].Count > 0 ? result.Properties["givenName"][0].ToString() : "Unknown"; user.LastName = result.Properties["sn"].Count > 0 ? result.Properties["sn"][0].ToString() : "Unknown"; user.Department = result.Properties["department"].Count > 0 ? result.Properties["department"][0].ToString() : "Unknown"; user.IsAdmin = (User_DTO.Count() <= 0); user.IsActive = true; User_DTO.AddUser(user); } UserId = user.Id; IsAdminUser = user.IsAdmin; } catch (Exception ex) { return false; } } return true; }
public bool LocalAccess() { var path = string.Format("WinNT://{0},computer", Environment.MachineName); using (var entry = new DirectoryEntry(path, UserName, Password)) { try { var exists = false; foreach (DirectoryEntry childEntry in entry.Children) if (childEntry.SchemaClassName == "User") if (childEntry.Name.ToLower() == UserName.ToLower()) exists = true; if (!exists) return false; var user = User_DTO.Get(UserName); if (user == null) { user = new User_DTO(); user.UserName = UserName; user.EmailAddress = "Unknown"; user.FirstName = "Unknown"; user.LastName = "Unknown"; user.Department = "Unknown"; user.IsAdmin = (User_DTO.Count() <= 0); user.IsActive = true; User_DTO.AddUser(user); } UserId = user.Id; IsAdminUser = user.IsAdmin; } catch (Exception ex) { throw ex; } return true; } }
public static List<Exemption> GetExemptionsFor(User_DTO user) { try { var apc = user.AssignedExemptions; apc.RemoveAll(x => x.Status == ExemptionStatus.Approved); var mec = MasterExemption_DTO.GetMasterExemptionsList_For(apc); return mec.Select(x => new Exemption(x, apc.FirstOrDefault(a => a.OnlineExemptionID == x.OnlineExemptionID))).ToList(); } catch (Exception ex) { throw ex; } }
public static User_DTO UpdateUser(User_DTO user) { Mapper.CreateMap<Models.User_DTO, Models.User_DTO>() .ForMember(dest => dest.Id, opt => opt.Ignore()); using (HomesteadViewerContext context = new HomesteadViewerContext()) { try { var c = Get(user.Id); if (c == null) throw new Exception("User Could Not Be Found"); Mapper.Map<Models.User_DTO, Models.User_DTO>(user, c); context.Users.Attach(c); context.Entry(c).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return Get(user.Id); } catch (Exception ex) { return null; } } }
public static bool AddUser(User_DTO user) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { context.Users.Add(user); context.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public static bool AddUser(string userName, string phone, string email) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { User_DTO user = new User_DTO() { UserName = userName, Phone = phone, EmailAddress = email }; context.Users.Add(user); context.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public ActionResult SaveUser(User_DTO user) { if (!ModelState.IsValid) return View(); var u = User_DTO.UpdateUser(user); if (u == null) return View(); return RedirectToAction("Index"); }