Exemple #1
0
        public ActionResult ViewUser(string user)
        {
            ADDomain domain   = new ADDomain();
            ADUser   userInfo = domain.GetUserByID(user);

            if (userInfo != null)
            {
                // This is a good candidate to use Automapper on!
                UserViewModel userInfoVM = new UserViewModel();
                userInfoVM.SamAccountName = userInfo.SamAccountName;
                userInfoVM.GivenName      = userInfo.GivenName;
                userInfoVM.MiddleName     = userInfo.MiddleName;
                userInfoVM.Surname        = userInfo.Surname;
                userInfoVM.DisplayName    = userInfo.DisplayName;
                userInfoVM.EmailAddress   = userInfo.EmailAddress;
                userInfoVM.Title          = userInfo.Title;
                userInfoVM.Department     = userInfo.Department;
                userInfoVM.PhoneNumber    = userInfo.PhoneNumber;
                userInfoVM.Company        = userInfo.Company;
                userInfoVM.Notes          = userInfo.Notes;
                userInfoVM.Enabled        = userInfo.Enabled;
                userInfoVM.ExpirationDate = userInfo.AccountExpirationDate;

                // We are not using the WhenCreated field form the DomainUser
                // table in the database because each user object in the domain
                // should have a value for this property.
                userInfoVM.WhenCreated = userInfo.WhenCreated;

                userInfoVM.WhenChanged = userInfo.WhenChanged.ToLocalTime();
                userInfoVM.LogonCount  = userInfo.LogonCount.ToString();

                using (var db = new ADWebDB())
                {
                    var userDbInfo = db.DomainUsers.Find(user);

                    if (userDbInfo != null)
                    {
                        // If this part of the code is reached, then it means that the user
                        // currently being viewed is was created outside of the application and
                        // thus has an entry in the DomainUsers table.
                        var domainUser = domain.GetUserByID(userDbInfo.CreatedBy);
                        userInfoVM.DBInfo.Createdby   = userDbInfo.CreatedBy;
                        userInfoVM.DBInfo.WhenCreated = userDbInfo.DateCreated;
                        userInfoVM.UserHistory        = userDbInfo.UpdateHistory.OrderByDescending(u => u.DateUpdated).ToList();
                    }
                    else
                    {
                        userInfoVM.DBInfo.Createdby = "Unknown";
                    }
                }

                userInfoVM.UserGroups = domain.GetUserGroupsByUserId(user);

                userInfo.Dispose();
                return(View(userInfoVM));
            }

            return(View());
        }
Exemple #2
0
        public ActionResult ViewUser(string userId)
        {
            ADDomain domain = new ADDomain();
            ADUser   user   = domain.GetUserByID(userId);

            if (user != null)
            {
                UserViewModel viewModel = new UserViewModel();
                viewModel.SamAccountName = user.SamAccountName;
                viewModel.GivenName      = user.GivenName;
                viewModel.MiddleName     = user.MiddleName;
                viewModel.Surname        = user.Surname;
                viewModel.DisplayName    = user.DisplayName;
                viewModel.EmailAddress   = user.EmailAddress;
                viewModel.Title          = user.Title;
                viewModel.Department     = user.Department;
                viewModel.PhoneNumber    = user.PhoneNumber;
                viewModel.Company        = user.Company;
                viewModel.Notes          = user.Notes;
                viewModel.Enabled        = user.Enabled;
                viewModel.ExpirationDate = user.AccountExpirationDate;

                // We are not using the WhenCreated field form the DomainUser
                // table in the database because each user object in the domain
                // should have a value for this property.
                viewModel.WhenCreated = user.WhenCreated;

                // The WhenChanged property comes straight from Active Directory
                // I may have to come back to this and use data from the database
                // to get the date when an account was last changed. The reason for
                // this is because if a user logins, this value is updated to reflect
                // this and it's not really a change in my mind. Any changes that are
                // done on a user account (thru the application) should be the real
                // indicators when an account was changed (and also indicate what type
                // of change happened).
                viewModel.WhenChanged = user.WhenChanged.ToLocalTime();
                viewModel.LogonCount  = user.LogonCount.ToString();

                using (var db = new ADWebDB())
                {
                    var userDbInfo = db.DomainUsers.Where(u => u.Username == userId).FirstOrDefault();

                    if (userDbInfo != null)
                    {
                        // If this part of the code is reached, then it means that the user
                        // currently being viewed is was created inside of the application and
                        // thus has an entry in the DomainUsers table.
                        var domainUser = domain.GetUserByID(userDbInfo.CreatedBy);
                        viewModel.DBInfo.Createdby   = userDbInfo.CreatedBy;
                        viewModel.DBInfo.WhenCreated = userDbInfo.DateCreated;
                        viewModel.UserHistory        = userDbInfo.UpdateHistory.OrderByDescending(u => u.DateUpdated).ToList();
                    }
                    else
                    {
                        viewModel.DBInfo.Createdby = "Unknown";
                    }
                }

                viewModel.UserGroups = domain.GetUserGroupsByUserId(userId);

                user.Dispose();
                return(View(viewModel));
            }

            return(View());
        }