public ActionResult UpdateOU(DomainOU id) { if (ModelState.IsValid) { using (var db = new ADWebDB()) { db.Entry(id).State = EntityState.Modified; db.SaveChanges(); TempData["ou_updated"] = "The OU " + id.Name + " has been successfully updated!"; return(RedirectToAction("OU")); } } else { ModelState.AddModelError("", "Unable to update the Organizational Unit."); } return(View()); }
public ActionResult UpdateUserTemplate(ViewUserTemplateVM id) { if (ModelState.IsValid) { using (var db = new ADWebDB()) { db.UserTemplate.Attach(id.UserTemplate); db.Entry(id.UserTemplate).Property(ut => ut.Name).IsModified = true; db.Entry(id.UserTemplate).Property(ut => ut.Enabled).IsModified = true; db.Entry(id.UserTemplate).Property(ut => ut.DomainOUID).IsModified = true; db.Entry(id.UserTemplate).Property(ut => ut.PasswordNeverExpires).IsModified = true; db.Entry(id.UserTemplate).Property(ut => ut.ChangePasswordAtNextLogon).IsModified = true; db.Entry(id.UserTemplate).Property(ut => ut.UserCannotChangePassword).IsModified = true; db.Entry(id.UserTemplate).Property(ut => ut.AccountExpires).IsModified = true; db.Entry(id.UserTemplate).Property(ut => ut.ExpirationRange).IsModified = true; db.Entry(id.UserTemplate).Property(ut => ut.ExpirationValue).IsModified = true; db.Entry(id.UserTemplate).Property(ut => ut.Notes).IsModified = true; db.SaveChanges(); // We need to check to see if a new group (or groups) have been // added to this user template. If so then we'll add the group! if (id.Groups.Count > 0) { ADDomain domain = new ADDomain(); ADGroup group; // We also have to check that the group(s) being added to this // user template don't alreay exist. If it does, then it will // not be added. For us to do this check, we have to get the list // of groups first. Also, please note that we have to check that we // only get active groups! var existingGroups = db.UserTemplateGroup .Where(u => u.UserTemplateID == id.UserTemplate.UserTemplateID && u.Enabled == true) .Select(u => u.Name).ToList(); foreach (var grp in id.Groups) { // This is where we check if this user template already has // the group that is being added. If it does, then we simply // continue out of this iteration of the foreach loop and go on // to the next group being added. if (existingGroups.Contains(grp)) { continue; } group = domain.GetGroupBasicInfo(grp); // We have to check if this group is in the domain, if it // is then we would have retrieved a name for the group. // If it's not a valid name, then the group name will be // blank and thus this is a group that doesn't exit in // the domain. if (!string.IsNullOrWhiteSpace(group.GroupName)) { db.UserTemplateGroup.Add(new UserTemplateGroup() { Enabled = true, Name = group.GroupName, DistinguishedName = group.DN, UserTemplateID = id.UserTemplate.UserTemplateID }); db.SaveChanges(); } } } TempData["user_template_updated"] = "The user template '" + id.UserTemplate.Name + "' has been successfully updated!"; return(RedirectToAction("UserTemplates")); } } else { TempData["error_updating_user_template"] = "Error updating Template"; return(RedirectToAction("ViewUserTemplate", new { id = id.UserTemplate.UserTemplateID })); } }