We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    I have a site where it would be useful to have the admin/super users[s] be able to log into the web context as any user. [without having to retrieve each individual password]

    I was thinking that just appending the user name to the admin's login name + the administrator's password should provide enough info to modx to be able to tell which user to log in as... i.e

    user = administrator{anotherusername}
    pass = _admin_password_

    then detect that with a plugin before the weblogin event [basically hijack the login]

    Not sure where to start with the admin authentication though...

    any thoughts on where I should start looking to figure this out?

    -sean
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 3749
      • 24,544 Posts
      That would be a nice feature request. It would be handy to have the option in the Manager to log out the Admin and log in to the Manager as another user. Maybe it could be a right-click option on the Manage Users grid.

      I think it might be possible with a plugin tied to one or more of the login events, but it would be tricky, and the plugin might have to know the user's password. It might also create some security issues.

      I usually just create some extra users named for the user group (e.g., JoeEditor) who all have the same password.


      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 26503
        • 620 Posts
        Hi Bob;

        yea - creating extra users isn't going to do it, the task that needs to be accomplished by logging in as the user is to manage extra profile data held in a set of custom tables.

        Going to work on this tonight, maybe I can set something up that will detect an administrator in the web context and allow them to edit any users profile... sort of masquerade the user id or something... hmmm

        -sean
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 26503
          • 620 Posts
          Finally got back to this, made some progress.... It's not pretty, but it ~kinda~ works:

          <?php
          $username = $_GET['username'] ? $_GET['username'] : 'null';
          
          $lifetime = '3600';
          $rememberme = 'true';
          $loginContext = 'web';
          $addContexts = array();
          
          $user= $modx->getObjectGraph('modUser', '{"Profile":{},"UserSettings":{}}', array ('modUser.username' => $username));
          
          $user->addSessionContext($loginContext);
          
          $_SESSION['modx.' . $loginContext . '.session.cookie.lifetime']= $lifetime;
          
          $postLoginAttributes = array(
          	'user' => $user,
          	'attributes' => array(
                  'rememberme' => $rememberme,
                  'lifetime' => $lifetime,
                  'loginContext' => $loginContext,
                  'addContexts' => $addContexts,
              )
          );
          
          $modx->invokeEvent("OnWebLogin", $postLoginAttributes);
          
          $url = $modx->makeUrl(1, $loginContext, '', 'full');
          
          echo $url.'<br />';
          
          header("Location: ".$url);
          
          exit;
          
          


          called from this page:
          [[!ShowMySessionInfo]]
          
          User Internal Key: [[!+modx.user.id:userinfo=`internalKey`]]<br />
          User name: [[!+modx.user.id:userinfo=`username`]]<br />
          Full Name: [[!+modx.user.id:userinfo=`fullname`]]<br />
          Role:  [[!+modx.user.id:userinfo=`role`]]<br />
          E-mail: [[!+modx.user.id:userinfo=`email`]]<br />
          



          so right now I am just forwarding the administrator from a CMP with a username in the url... super insecure at the moment. However it does kind of work.

          - the administrator cannot click on the link & get a user's session, if you copy the url into another browser [without an active admin session] THEN it will log you in as the requested user.

          - the user seems to need to browse to another page before the session stuff shows up [hence the redirect]

          - I think I need to make sure there are no active web context sessions in the user's browser to clear that little bit up ... I think, not sure!

          - the admin needs to be able to get a session as these users, not sure what it preventing the browser/user logged in as admin from getting a user session.

          - I also need to make sure that the user/browser/agent requesting the url is authenticated as an administrator BEFORE doing anything.


          Any thoughts/advice on how to proceed from here?
            *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

            Sean Kimball CLP, CLS.
            Technical Director / Sr. Developer | BigBlock Studios
            ._______________________________________________.
            Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
            27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
            phone/fax: 905-426-5525
            • 3749
            • 24,544 Posts
            Don't forget to check that the $user object is not null before calling addSessionContext() for the user.


            As for the security, I've done something similar where I just put this at the top:

            if (! $modx->user->isMember('Administrator'))  {
                return '';
            }



            It might be more proper to forward the user with this (though you may have some reason for doing the way you do):

            $modx->sendRedirect($url);




            ------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using.
            MODX info for everyone: http://bobsguides.com/modx.html
              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