We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24393
    • 83 Posts
    rajfantastic Reply #1, 16 years ago
    start time: immediately
    job duration: one week
    payment terms: COD
    ownership: open source

    I need to create a user login plugin for Modx Revolution that authenticates front-end users against an existing .htpasswd file.
      stevesunderland.com
      GRAPHICS | INTERACTIVE | MARKETING
      xhtml + ajax + seo websites powered by modx
      • 19980 ☆ A M B ☆
      • 275 Posts
      modx.customize Reply #2, 16 years ago
      hi

      auth to front -end?
        modx and ecommerce pro
        • 24393
        • 83 Posts
        rajfantastic Reply #3, 16 years ago
        thanks for the reply...

        I have modified my original post to clarify that this is for a front-end login.
          stevesunderland.com
          GRAPHICS | INTERACTIVE | MARKETING
          xhtml + ajax + seo websites powered by modx
          • 19980 ☆ A M B ☆
          • 275 Posts
          modx.customize Reply #4, 16 years ago
          ok,

          did you check if there is the same kind of snippets as for the evolution:
          - weblogin
          - webloginPE

          ??

          Will they be able to work for you as you call it a "plugin"?
            modx and ecommerce pro
            • 24393
            • 83 Posts
            Yes, there is a snippet for Revolution called Login which supports "hooks", however I think the solution to my problem would be something similar to the [urlhttp://rtfm.modx.com/display/ADDON/modActiveDirectory]modActiveDirectory[/url] addon.
              stevesunderland.com
              GRAPHICS | INTERACTIVE | MARKETING
              xhtml + ajax + seo websites powered by modx
              • 3749
              • 24,544 Posts
              Quote from: stevesunderland at Oct 20, 2010, 02:45 PM

              Yes, there is a snippet for Revolution called Login which supports "hooks", however I think the solution to my problem would be something similar to the [urlhttp://rtfm.modx.com/display/ADDON/modActiveDirectory]modActiveDirectory[/url] addon.

              IIRC, the hooks and plugins attached to the login events can only reject a user, they can’t skip the normal steps.

              I’m pretty sure this would work: tie a plugin to OnUserNotFound. In the plugin, test the credentials, use $getObject() to get the user object (if the credentials are good), and return the $user object. Return false if the credentials aren’t good.

              Here’s the relevant code in login.php and the variables that would be available to the plugin ($user would be empty):

              $ru = $modx->invokeEvent("OnUserNotFound", array(
                      'user' => &$user,
                      'username' => $username,
                      'password' => $givenPassword,
                      'attributes' => array(
                          'rememberme' => $rememberme,
                          'lifetime' => $lifetime,
                          'loginContext' => $loginContext,
                      )
                  ));
               if (!empty($ru)) {
                      foreach ($ru as $obj) {
                          if (is_object($obj) && $obj instanceof modUser) {
                              $user = $obj;
                              break;
                          }
                      }
                  }
                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
                • 24393
                • 83 Posts
                Bob, thanks so much for your reply. My confusion at this point is whether this $user object needs to be an actual user in the database, or if I can just create a random modUser "session". Any insight you can provide is greatly appreciated.
                  stevesunderland.com
                  GRAPHICS | INTERACTIVE | MARKETING
                  xhtml + ajax + seo websites powered by modx
                  • 3749
                  • 24,544 Posts
                  Quote from: stevesunderland at Oct 20, 2010, 07:31 PM

                  Bob, thanks so much for your reply. My confusion at this point is whether this $user object needs to be an actual user in the database, or if I can just create a random modUser "session". Any insight you can provide is greatly appreciated.

                  That’s a great question. I’m not actually sure. If your plugin creates a valid user object and returns it, that might suffice, but I suspect that other snippets or parts of the Manager might look for the user in the DB (e.g., to update a user group). It would only take one line of code to add the user to the DB, so that would make sure everything was copacetic unless you have a reason not to put the user into the DB. The catch is that on future logins, the user would be found in the DB and your code wouldn’t execute (not necessarily bad).

                  Your plugin would have to create the user in the database using something like the code below.

                  <?php
                  /* validate the user here */
                  
                  if (!valid) {
                      return false;
                  }
                  
                  /* ***************** */
                  /* if we're here, the user is not in the DB, but is valid */
                  
                  $user = $modx->newObject('modUser', array('username'=>$username));
                  $user->set('password',md5($password);
                  $user->set('active','1');
                  
                  /* optional -- if you have info on the user, you can populate the user profile */
                  $profile = $modx->newObject('modUserProfile');
                  $profile->set('fullname',$fullname);
                  $profile->set('email', $userEmail);
                  $user->addOne($profile);
                  /*********************/
                  
                  /* save user to DB */
                  $user->save();
                  
                  /* could quit here */
                  
                  /* optionally add user to user group */
                  $role = $modx->getObject('modUserGroupRole', array('name'=>'PhotoEditor'));
                  $group = $modx->getObject('modUserGroup', array('name'=>'PhotoEditors'));
                  $intersect = $modx->newObject('modUserGroupMember');
                  $intersect->addOne($user);
                  $intersect->addOne($group);
                  $intersect->addOne($role);
                  
                  $modx->beginTransaction();
                  if (! $intersect->save()) {
                      $modx->rollback();
                      $output .= 'An error occurred — user, role, and group not saved';
                  } else {
                      $modx->commit();
                  }
                  
                    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
                    • 24393
                    • 83 Posts
                    Bob, thanks so much for the prompt reply and thorough example. I think this exactly the code I was looking for!
                      stevesunderland.com
                      GRAPHICS | INTERACTIVE | MARKETING
                      xhtml + ajax + seo websites powered by modx
                      • 21943
                      • 6 Posts
                      Please report this reply for removal if it’s against the practice to interrupt inquires in this board.

                      How to login the user with the API? Say, we validated them by OpenID, and now we want to either log them in, or create a new user for them with attributes from OpenID.