public void GetUsers( )
        {
            var userData = UserProcessor.SelectUsers( );

            Users = new List <User>( );
            foreach (var row in userData)
            {
                var user = new User
                {
                    UserId       = row.UserId,
                    Username     = row.Username,
                    FirstName    = row.FirstName,
                    LastName     = row.LastName,
                    EmailAddress = row.Email,
                    PhoneNumber  = row.PhoneNo,
                    Password     = row.Password
                };
                var userRoleModel = UserRoleProcessor.LoadRolesForUser(row.UserId);
                foreach (var ur in userRoleModel)
                {
                    var roleModel = RoleProcessor.SelectRole(ur.RoleId);
                    var role      = new Role
                    {
                        RoleId = roleModel.RoleId,
                        Name   = roleModel.Name
                    };
                    var userRole = new UserRole
                    {
                        UserRoleId = ur.UserRoleId,
                        UserId     = ur.UserId,
                        RoleId     = ur.RoleId,
                        User       = user,
                        Role       = role
                    };
                    user.UserRoles.Add(userRole);
                }
                Users.Add(user);
            }
        }
        public ActionResult Save(UserRole _UserRole)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.User))
            {
                return(RedirectToPermissionDenied());
            }

            // Null safe check to prevent crashes.
            if (_UserRole.User == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            try
            {
                if (_UserRole.RoleId <= 0)
                {
                    throw new DataException("No Role selected.");
                }

                var rowsFound = UserRoleProcessor.SelectUserRoleForUserIdAndRoleId(_UserRole.User.UserId, _UserRole.RoleId);
                if (rowsFound > 0)
                {
                    throw new DataException($"User is already assigned this Role.");
                }

                var user = db.GetUser(_UserRole.User.UserId);

                if (user.UserRoles.Any(ur => ur.Role.Name == "Student"))
                {
                    throw new DataException("A User with a Role of Student cannot be assigned additional roles.");
                }

                var role = RoleProcessor.SelectRole(_UserRole.RoleId);

                if (user.UserRoles.Count( ) > 0 && user.UserRoles.Any(ur => ur.Role.Name != "Student") && role.Name == "Student")
                {
                    throw new DataException("A User with an Employee role type cannot be a Student.");
                }


                // Attempt to insert new UserRole to database using data from parameter
                var data = UserRoleProcessor.InsertUserRole(_UserRole.User.UserId, _UserRole.RoleId);
                // Checks if Insert operation was successful. If not throws an error.
                if (data == null)
                {
                    throw new DataException("Role added was invalid.");
                }
            }
            catch (Exception _Ex)
            {
                // Error handling
                ModelState.AddModelError("", $"Unable to save changes due to Error: { _Ex.Message }");
                db.GetUser(_UserRole.User.UserId);
                PopulateRoleDropDownList( );

                return(View(db));
            }

            // Redirects to page where data is reloaded.
            return(Redirect(Request.UrlReferrer.ToString( )));
        }