Esempio n. 1
0
        public override string[] GetRolesForUser(string username)
        {
            ADDomain domain = new ADDomain();

            return(domain.GetCurrentUserGroups(username).ToArray());
        }
Esempio n. 2
0
        public ActionResult AddUserToGroups(string SamAccountName, List <string> Groups)
        {
            ADDomain domain = new ADDomain();

            // The current implementation of this feature will allow users to type in
            // a few characters of a group name and if any matches are found then those
            // matches are displayed to the user. There is a chance that the user may just
            // type in the name of the group he/she wants to add and not use any of the
            // returned results. For this reason, I am doing an extra check on the domain
            // to make sure that whatever has been entered are valid groups. If a group
            // name has been entered that is not valid (i.e. it doesn't exist) then that
            // group will not be added to the following list. After generating this list,
            // we are doing an extra check to see if it's empty (which can theoretically
            // happen) and if so then we just re-direct the user back to the ViewUser page
            // and send a long a message of the issue why no group(s) were added to the user.
            List <string> validatedGroups = domain.ValidateGroups(Groups);

            if (validatedGroups.Count == 0)
            {
                TempData["invalid_groups"] = @"Invalid Group Names. The group(s) you tried to add are not valid group name. 
                                               Please check the name of the group and try again.";
                return(RedirectToAction("ViewUser", new { user = SamAccountName }));
            }

            // There is the posibility that a group that the user already belongs
            // to is part of the groups list being passed to this method. I have to
            // get a list of the current groups that this user belongs to and before
            // adding any of the groups that have been passed to this method, I must
            // make sure that it doesn't already exist. If it does, then the group
            // trying to be added will just be discarded.
            List <string> currentGroups = domain.GetCurrentUserGroups(SamAccountName);

            // This will hold the list of groups that will be added to the
            // user account.
            List <string> newGroupsToAdd = new List <string>();

            foreach (var group in validatedGroups)
            {
                if (!currentGroups.Contains(group))
                {
                    newGroupsToAdd.Add(group);
                }
            }

            // If we are adding a group (or list of groups) that the user already
            // belongs to then none of these group should be added.
            if (newGroupsToAdd.Count == 0)
            {
                TempData["no_groups_added"] = "No Groups have been added to this user as the user already is part of the groups submitted.";
                return(RedirectToAction("ViewUser", new { user = SamAccountName }));
            }

            // At this time we have filtered out the groups so that only
            // new groups are added to this user
            domain.AddUserToGroups(SamAccountName, newGroupsToAdd);

            // Now we have to log this action so that it shows up on the
            // change history for this user
            using (var db = new ADWebDB())
            {
                ADUser loggedInUser = domain.GetUserByID(User.Identity.Name);

                // The following code generates the update details for this action
                StringBuilder updateNotes = new StringBuilder();
                updateNotes.Append("<p>The following groups have been added to this user:</p>");
                updateNotes.Append("<ul class=\"update-details\">");

                foreach (var group in newGroupsToAdd)
                {
                    updateNotes.Append("<li>" + group + "</li>");
                }

                updateNotes.Append("</ul>");

                UserUpdateHistory newGroupHistory = new UserUpdateHistory();
                newGroupHistory.UpdatedBy   = loggedInUser.GivenName + " " + loggedInUser.Surname;
                newGroupHistory.DateUpdated = DateTime.Now;
                newGroupHistory.UpdateType  = UserUpdateType.AddedToGroup;
                newGroupHistory.Notes       = updateNotes.ToString();
                newGroupHistory.Username    = SamAccountName;

                // Before adding this update history entry into the database
                // we have to check for the possibility of the user having no
                // entry in the DomainUsers table.
                DomainUser user = db.DomainUsers.Find(SamAccountName);

                if (user != null)
                {
                    // The user has an existing entry in the DomainUser
                    // table
                    db.UserUpdateHistory.Add(newGroupHistory);
                    db.SaveChanges();
                }
                else
                {
                    DomainUser newDomainUser = new DomainUser();
                    newDomainUser.DateCreated = DateTime.Now;
                    newDomainUser.CreatedBy   = loggedInUser.GivenName + " " + loggedInUser.Surname;
                    newDomainUser.Username    = SamAccountName;

                    db.DomainUsers.Add(newDomainUser);
                    db.SaveChanges();

                    // Entry that identifies this as a user who we just inserted
                    // an entry to the DomainUsers table for.
                    UserUpdateHistory newUserHistory = new UserUpdateHistory();
                    newUserHistory.UpdatedBy   = "System Generated";
                    newUserHistory.Username    = SamAccountName;
                    newUserHistory.UpdateType  = UserUpdateType.CreatedDBEntry;
                    newUserHistory.DateUpdated = DateTime.Now;
                    newUserHistory.Notes       = "<ul class=\"update-details\"><li>New User Added to table by the system.</li></ul>";

                    db.UserUpdateHistory.Add(newUserHistory);
                    db.UserUpdateHistory.Add(newGroupHistory);
                    db.SaveChanges();
                }

                TempData["groups_added_successfully"] = "Groups have been added successfully to this user!";
                return(RedirectToAction("ViewUser", new { user = SamAccountName }));
            }
        }