Esempio n. 1
0
        public ActionResult ViewUserTemplate(int id)
        {
            using (var db = new ADWebDB())
            {
                ViewUserTemplateVM utVM = new ViewUserTemplateVM();

                utVM.UserTemplate = db.UserTemplate.Where(ut => ut.UserTemplateID == id).FirstOrDefault();
                var ous = db.DomainOU.Where(ou => ou.Enabled == true).ToList();

                List <SelectListItem> ouItems = new List <SelectListItem>();
                foreach (var ou in ous)
                {
                    ouItems.Add(new SelectListItem {
                        Text     = ou.Name,
                        Value    = ou.DomainOUID.ToString(),
                        Selected = utVM.UserTemplate.DomainOUID == ou.DomainOUID
                    });
                }

                List <SelectListItem> utStatus = new List <SelectListItem>();
                utStatus.Add(new SelectListItem()
                {
                    Text = "Enabled", Value = "true"
                });
                utStatus.Add(new SelectListItem()
                {
                    Text = "Disabled", Value = "false"
                });

                // I am calling the ToList method here so that we can get a list groups
                // associated with this User Template. If we don'd do this here, then
                // I cannot get access to this list from the View (I get a message that
                // the context has already been disposed of and therefore cannot access
                // this information). Calling this method here should not be that big of
                // hit performance wise as I don't expect user templates to have a lot of
                // groups associated with them.
                utVM.UserTemplate.Groups.Where(g => g.Enabled == true).ToList();

                ViewBag.OUList   = ouItems;
                ViewBag.UTStatus = utStatus;

                if (utVM.UserTemplate != null)
                {
                    return(View(utVM));
                }
                else
                {
                    TempData["invalid_user_template"] = "Invalid User template ID";
                    return(RedirectToAction("UserTemplates"));
                }
            }
        }
Esempio n. 2
0
        public ActionResult ViewUserTemplate(int id)
        {
            using (var db = new ADWebDB())
            {
                ViewUserTemplateVM utVM = new ViewUserTemplateVM();
                utVM.UserTemplate = db.UserTemplate.Where(ut => ut.UserTemplateID == id).FirstOrDefault();

                var ous = db.DomainOU.Where(ou => ou.Enabled == true).ToList();

                List <SelectListItem> ouItems = new List <SelectListItem>();
                foreach (var ou in ous)
                {
                    ouItems.Add(new SelectListItem {
                        Text     = ou.Name,
                        Value    = ou.DomainOUID.ToString(),
                        Selected = utVM.UserTemplate.DomainOUID == ou.DomainOUID
                    });
                }

                List <SelectListItem> utStatus = new List <SelectListItem>();
                utStatus.Add(new SelectListItem()
                {
                    Text = "Enabled", Value = "true"
                });
                utStatus.Add(new SelectListItem()
                {
                    Text = "Disabled", Value = "false"
                });

                ViewBag.OUList   = ouItems;
                ViewBag.UTStatus = utStatus;

                // We are only interested in seeing groups that are enabled. The users have the
                // ability to remove groups that have been added to this template, at which time
                // those groups have their Enabled property set to false.
                utVM.UserTemplate.Groups = utVM.UserTemplate.Groups.Where(g => g.Enabled).ToList();

                if (utVM.UserTemplate != null)
                {
                    return(View(utVM));
                }
                else
                {
                    TempData["invalid_user_template"] = "Invalid User template ID";
                    return(RedirectToAction("UserTemplates"));
                }
            }
        }
Esempio n. 3
0
        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 }));
            }
        }