コード例 #1
0
ファイル: AdminController.cs プロジェクト: huuthangcnc/ADWeb
        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());
            }
        }
コード例 #2
0
ファイル: AdminController.cs プロジェクト: huuthangcnc/ADWeb
        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 }));
            }
        }