We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    See if any of these get you anything:


    $userObject = $hook->getValue('login.user');
    
    $userName = $hook->getValue('username');
    
    $userName = $hook->getValue('login.username');

      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 41874
      • 33 Posts
      This is the result:
      $userObject = $hook->getValue('login.user');
      // Output: NULL
      $userName = $hook->getValue('username');
      // Output: String(11) "asampleuser"  
      $userName = $hook->getValue('login.username');
      // Output: NULL

      However, I can access the user with:
      $user = $hook->modx->user;
      $whitelistUserGroups = array("Team Management");
      var_dump($user->isMember($whitelistUserGroups));
      exit();
      // Output: bool(true)

      This is the wrong output, because this user is not a member of the "Team Management" group..

      Could the space in the User group be the problem?
        I love flexibility
        • 3749
        • 24,544 Posts
        I'm not sure if $hook->modx->user would be reliable.

        Try $hook->getValue('user') -- that might get you the user object.

        If not, and the username is correct, you can do this to get the $user object:

        $currentUser = $modx->getObject('modUser', array('username' => $userName));


          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 41874
          • 33 Posts
          var_dump($hook->getValue('user'));
          // Output: NULL

          The second suggestion works, I created the hook as follows:
          <?php
          // A whitelist of User Groups which may login at frontend.
          $whitelistUserGroups = array("Team Management");
          
          // Only check user at login, not logout
          if ($hook->getValue('service') === "login") {
              // Retrieve user
              $userName = $hook->getValue('username');
              $currentUser = $modx->getObject('modUser', array('username' => $userName));
              
              // Check if user has access to any available user group 
              if ($currentUser && $currentUser->isMember($whitelistUserGroups) === false) {
                  // The user has manager access, but no access to
                  // any protected resources over here, send them 
                  // to the manager
                  $modx->sendRedirect($modx->getOption("site_url") . '/manager');
              }
          }
          
          return true;


          Again, thanks a lot! This solved my initial problem.
            I love flexibility
            • 3749
            • 24,544 Posts
            I'm glad you got it sorted. Thanks for reporting back. smiley
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 9207 ☆ A M B ☆
              • 2,475 Posts
              A small improvement would be to use the MODX_MANAGER_URL constant because the MODX manager URL is configurable.
                • 41874
                • 33 Posts
                Yeah, that would even make it more awesome! I changed it right away. Thanks!
                  I love flexibility