Example #1
0
        public ActionResult CreateUserTemplate(CreateUserTemplateVM id)
        {
            if (ModelState.IsValid)
            {
                using (var db = new ADWebDB())
                {
                    id.UserTemplate.Enabled = true;
                    db.UserTemplate.Add(id.UserTemplate);
                    db.SaveChanges();

                    // We now have to iterate thru the list of groups that
                    // this user template has been configured to have so that
                    // we can add this information to the database. But we have
                    // to also be careful to make sure that only valid groups are
                    // added to the database because we are allowing users to enter
                    // the name of the group(s) they are looking for, there is a
                    // possibility that a group may not exist in the domain.
                    ADDomain domain = new ADDomain();
                    ADGroup  group;

                    // There is a possibility that the users will not add any groups
                    // when they first create the user template. We have to check for
                    // this now so that we can avoid a nasty error message!
                    if (id.Groups.Count > 0)
                    {
                        foreach (var grp in id.Groups)
                        {
                            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_created"] = "The user template '" + id.UserTemplate.Name + "' has been created successfully!";

                    return(RedirectToAction("UserTemplates"));
                }
            }
            else
            {
                return(View());
            }
        }
Example #2
0
        public ActionResult CreateUserTemplate()
        {
            using (var db = new ADWebDB())
            {
                CreateUserTemplateVM userTemplateVM = new CreateUserTemplateVM();
                userTemplateVM.OrganizationalUnits          = db.DomainOU.Where(o => o.Enabled == true).ToList();
                userTemplateVM.UserTemplate.ExpirationRange = UserExpirationRange.Days;

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

                ViewBag.OUList = ouItems;

                return(View(userTemplateVM));
            }
        }