We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25601
    • 14 Posts
    Hi,

    I’m trying to create pages for different clients so each of them have their own page(s). I’ve read the members-only page tutorial which seemed to work out ok so do I need to create a resource group for each client’s page and their own usergroup?

    The other function I’d like is when the client logs in they are taken to their own client page - I’m sure I can figure this pout but if anyone has a quick fix that would be great.

    Thanks,

    Richard
      • 26016
      • 561 Posts
      Hi,
      If you have a typical install, take a look at Security/Web Users, and you’ll see "siteadmin". When you edit the user, you can specify their login page in the User tab.

      Also, WebLogin comes standard with MODx (at least in Evo), and you can send a parameter to that to specify a page for a group of users or specific users, if I recall. Again, a standard MODx install with "example site" included has WebLogin in a couple places, so you can see how it works.

      Good luck, Dave
        MODx and Wordpress development
        Linux, PHP 5.2, MySQL 5.0, Evo 1.05, Revo 2.08-pl, Firefox 4
        • 25601
        • 14 Posts
        Thanks although I think all that’s to do with Evolution. I ’fixed’ the permissions issue by trying a different browser and clearing the cache from everywhere. It was driving me up the wall...

        I’ve got the ’Login’ package installed which has post hooks so I hopefully can create an extended field to each user with the ID of their page and put the redirect into the post hook. A bit manual but it should do what I need.

        Thanks,
        Richard
          • 25601
          • 14 Posts
          OK - nearly there. I’ve added a numerical attribute ’Redirect’ to each client which is the ID of their page. I’ve found in the login module comes with some configuration options and the one I’m using is loginResourceId. I can put a static number in there and it redirects to that page when I login fine. I now want to put in the variable from the user and have tried [[+Redirect]] but this didn’t work. Any ideas how would I reference the user attribute from this properties list?

          Then it’s just on to the templating when the adventure will begin again...

          Thanks,

          Richard
            • 3749
            • 24,544 Posts
            If a "redirect" placeholder is set, [[~[[+redirect]]]] would provide a link to the page.

            and a snippet with this code should forward them to it:

            $pageId = $modx->getPlaceholder('redirect');
            
            $modx->sendRedirect($modx->makeUrl($pageId));


            Otherwise, you’d have to get the redirect value from the user’s profile first with something like this:

            $profile = $modx->user->getOne('Profile');
            $pageId = $profile->get('redirect');


            If it’s an extended user attribute, there’s another step involved - something like this:

            $extStuff = $profile->get('extended');
            $pageId = $extStuff['redirect']);
            
              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
              • 25601
              • 14 Posts
              Thanks - I’m much closer I think. I put the following code

              $extStuff = $profile->get(’extended’);
              $loginResourceId = ($extStuff[’Redirect’]);

              ...in part of the login snippet:

              -----------
              ...
              * to the lexicon string "login.logout"
              */
              $output = ’’;
              $modx->lexicon->load(’login:default’);

              $extStuff = $profile->get(’extended’);
              $loginResourceId = ($extStuff[’Redirect’]);



              $authenticated = $modx->user->isAuthenticated($modx->context->get(’key’));

              if (isset($_REQUEST[$actionKey]) && !empty($_REQUEST[$actionKey])) {
              if (isset($_REQUEST[’login_context’]) && !empty($_REQUEST[’login_context’])) {
              $loginContext = $_REQUEST[’login_context’];

              etc.
              -------------

              (I wasn’t sure where to put the open bracket as you missed it off in your 3rd code suggestion)

              This came up with the error:

              Fatal error: Call to a member function get() on a non-object in /home/wwwcybe/public_html/core/cache/elements/modsnippet/8.include.cache.php on line 67

              (I cleared the cache just in case)

              I then tried:

              $loginResourceId = ’23’;

              ..to test the redirect which works fine to the same page whichever user you log in as.

              I then thought of using one of the main user fields to put the number in (the fax one for example) and tried:

              $profile = $modx->user->getOne(’Profile’);
              $pageId = $profile->get(’fax’);

              I still got:

              Fatal error: Call to a member function get() on a non-object in /home/wwwcybe/public_html/core/cache/elements/modsnippet/8.include.cache.php on line 67

              Any further ideas?

              Thanks for your help so far...

              Richard
                • 3749
                • 24,544 Posts
                Most likely, that means that no user is logged in at the point where your code is running. A not-logged-in user has no profile, so $profile->get() fails.

                You could add if ($profile) before calling get() or (this is faster):

                if ($modx->user->get(’username’) == ’(anonymous)’) {

                /* user is not logged in */

                }

                Note that the parentheses in ’(anonymous)’ are actually part of the username.

                Be sure to flush permissions and test your code in another browser where you’re not logged into the Manager.
                  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
                  • 33337
                  • 3,975 Posts
                  A question, what if client edits the id/url of the page, will he/she be able to open any protected page other than defined in the snippet call?
                    Zaigham R - MODX Professional | Skype | Email | Twitter

                    Digging the interwebs for #MODX gems and bringing it to you. modx.link
                    • 3749
                    • 24,544 Posts
                    The user’s permissions and user group membership are set on login, so if the login hasn’t occurred yet, they shouldn’t be able to access any protected pages. Once logged in, they should only be able to reach pages they have permission to see.
                      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
                      • 25601
                      • 14 Posts
                      OK - I’ve tried adding the following code:

                      if ($modx->user->get(’username’) == ’(anonymous)’) {
                      /* user is not logged in */
                      }
                      else {
                      $profile = $modx->user->getOne(’Profile’);
                      $loginResourceId = $profile->get(’fax’);
                      }

                      where I’ve set the fax field to the ID of the resource I’d like the client page to relocate to and I’ve tried placing it at various points in the login template file. I was going to attach it as a file but it said the upload folder was full. If I put the code just before the final return, the user is logged in (I can return $loginResourceId correctly) but if I put it at the start the user isn’t. At what point does the login process take place in the snippet and is that before the $loginResourceId is used to set the redirect url? I tried a few places in between but without success.

                      Again - many thanks for your time. I hope I’m not being dense!

                      Richard

                      Code below....



                      <?php
                      /**
                      * Login
                      *
                      * Copyright 2010 by Jason Coward <[email protected]> and Shaun McCormick
                      * <[email protected]>
                      *
                      * Login is free software; you can redistribute it and/or modify it
                      * under the terms of the GNU General Public License as published by the Free
                      * Software Foundation; either version 2 of the License, or (at your option) any
                      * later version.
                      *
                      * Login is distributed in the hope that it will be useful, but WITHOUT ANY
                      * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
                      * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
                      *
                      * You should have received a copy of the GNU General Public License along with
                      * Login; if not, write to the Free Software Foundation, Inc., 59 Temple
                      * Place, Suite 330, Boston, MA 02111-1307 USA
                      *
                      * @package login
                      */
                      /**
                      * MODx Login Snippet
                      *
                      * This snippet handles login POSTs, sending the user back to where they came from or to a specific
                      * location if specified in the POST.
                      *
                      * @version 2.0.0
                      * @author Jason Coward <[email protected]>
                      * @author Shaun McCormick <[email protected]>
                      * @copyright Copyright &copy; 2010
                      * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License
                      * version 2 or (at your option) any later version.
                      * @package login
                      *
                      * @property textfield actionKey The REQUEST variable containing the action to take.
                      * @property textfield loginKey The actionKey for login.
                      * @property textfield logoutKey The actionKey for logout.
                      * @property list tplType The type of template to expect for the views:
                      * modChunk - name of chunk to use
                      * file - full path to file to use as tpl
                      * embedded - the tpl is embedded in the page content
                      * inline - the tpl is inline content provided directly
                      * @property textfield loginTpl The template for the login view (content based on tplType)
                      * @property textfield logoutTpl The template for the logout view (content based on tplType)
                      * @property textfield errTpl The template for any errors that occur when processing an view
                      * @property list errTplType The type of template to expect for the error messages:
                      * modChunk - name of chunk to use
                      * file - full path to file to use as tpl
                      * inline - the tpl is inline content provided directly
                      * @property integer logoutResourceId An explicit resource id to redirect users to on logout
                      * @property string loginMsg The string to use for the login action. Defaults to
                      * the lexicon string "login".
                      * @property string logoutMsg The string to use for the logout action. Defaults
                      * to the lexicon string "login.logout"
                      */
                      $output = ’’;
                      $modx->lexicon->load(’login:default’);



                      ***I tried putting code here but login hasn’t taken place obviously...

                      if ($modx->user->get(’username’) == ’(anonymous)’) {
                      /* user is not logged in */
                      }
                      else {
                      $profile = $modx->user->getOne(’Profile’);
                      $loginResourceId = $profile->get(’fax’);
                      }





                      $authenticated = $modx->user->isAuthenticated($modx->context->get(’key’));

                      if (isset($_REQUEST[$actionKey]) && !empty($_REQUEST[$actionKey])) {
                      if (isset($_REQUEST[’login_context’]) && !empty($_REQUEST[’login_context’])) {
                      $loginContext = $_REQUEST[’login_context’];
                      }
                      if (!empty($_POST) && isset($_POST[$actionKey]) && !$authenticated) {
                      if ($_POST[$actionKey] == $loginKey) {
                      /* set default POST vars if not in form */
                      if (!isset($_POST[’login_context’])) $_POST[’login_context’] = $loginContext;

                      /* send to login processor and handle response */
                      $response = $modx->executeProcessor(array(
                      ’action’ => ’login’,
                      ’location’ => ’security’
                      ));
                      if (!empty($response) && is_array($response)) {
                      if (!empty($response[’success’]) && isset($response[’object’])) {
                      if (!empty($loginResourceId) && ($url = $modx->makeUrl($loginResourceId, $loginContext, ’’, ’full’))) {
                      $modx->sendRedirect($url);
                      } elseif (isset($response[’object’][’url’])) {
                      $modx->sendRedirect($response[’object’][’url’]);
                      } else {
                      $modx->sendRedirect($modx->getOption(’site_url’));
                      }
                      } else {
                      $errorOutput = ’’;
                      if (isset($response[’errors’]) && !empty($response[’errors’])) {
                      foreach ($response[’errors’] as $error) {
                      $errorOutput .= $modx->parseChunk($errTpl, $error);
                      }
                      } elseif (isset($response[’message’]) && !empty($response[’message’])) {
                      $errorOutput = $modx->parseChunk($errTpl, array(’msg’ => $response[’message’]));
                      } else {
                      $errorOutput = $modx->parseChunk($errTpl, array(’msg’ => $modx->lexicon(’login.login_err’)));
                      }
                      $modx->setPlaceholder(’errors’, $errorOutput);
                      }
                      }
                      } else {
                      $modx->log(modX::LOG_LEVEL_ERROR,$modx->lexicon(’login.invalid_post’,array(
                      ’action’ => $_POST[$actionKey],
                      )));
                      }
                      } elseif ($_REQUEST[$actionKey] == $logoutKey && $authenticated) {
                      /* set default REQUEST vars if not provided */
                      if (empty($_REQUEST[’login_context’])) $_REQUEST[’login_context’] = $loginContext;

                      /* send to logout processor and handle response */
                      $response = $modx->executeProcessor(array(
                      ’action’ => ’logout’,
                      ’location’ => ’security’
                      ));
                      if (!empty($response) && is_array($response)) {
                      if (!empty($response[’success’]) && isset($response[’object’])) {
                      if (isset($response[’object’][’url’])) {
                      $modx->sendRedirect($response[’object’][’url’]);
                      } elseif (!empty($logoutResourceId)) {
                      $modx->sendRedirect($modx->makeUrl($logoutResourceId));
                      } else {
                      $modx->sendRedirect($_SERVER[’REQUEST_URI’]);
                      }
                      } else {
                      $errorOutput = ’’;
                      if (isset($response[’errors’]) && !empty($response[’errors’])) {
                      foreach ($response[’errors’] as $error) {
                      $errorOutput .= $modx->parseChunk($errTpl, $error);
                      }
                      } elseif (isset($response[’message’]) && !empty($response[’message’])) {
                      $errorOutput = $modx->parseChunk($errTpl, array(’msg’ => $response[’message’]));
                      } else {
                      $errorOutput = $modx->parseChunk($errTpl, array(’msg’ => $modx->lexicon(’login.logout_err’)));
                      }
                      $modx->setPlaceholder(’errors’, $errorOutput);
                      }
                      }
                      }
                      }

                      $redirectToPrior = $modx->getOption(’redirectToPrior’,$scriptProperties,false);
                      $tpl = $authenticated ? $logoutTpl : $loginTpl;
                      $actionMsg = $authenticated
                      ? (!empty($logoutMsg) ? $logoutMsg : $modx->lexicon(’login.logout’))
                      : (!empty($loginMsg) ? $loginMsg : $modx->lexicon(’login’));

                      $modx->setPlaceholder(’actionMsg’, $actionMsg);
                      $phs = $authenticated ? $scriptProperties : array_merge($scriptProperties, $_POST);
                      /* make sure to strip out logout GET parameter to prevent ghost logout */
                      if (!$redirectToPrior) {
                      $phs[’request_uri’] = str_replace(array(’?service=logout’,’&service=logout’,’&amp;service=logout’),’’,$_SERVER[’REQUEST_URI’]);
                      } else {
                      $phs[’request_uri’] = str_replace(array(’?service=logout’,’&service=logout’,’&amp;service=logout’),’’,$_SERVER[’HTTP_REFERER’]);
                      }

                      /* properly build logout url */
                      if ($authenticated) {
                      $phs[’logoutUrl’] = $phs[’request_uri’];
                      $phs[’logoutUrl’] .= strpos($phs[’logoutUrl’],’?’) ? ($modx->getOption(’xhtml_urls’,null,false) ? ’&amp;’ : ’&’) : ’?’;
                      $phs[’logoutUrl’] .= $phs[’actionKey’].’=’.$phs[’logoutKey’];
                      }

                      switch ($tplType) {
                      case ’embedded’:
                      $modx->setPlaceholders($phs);
                      break;
                      case ’modChunk’:
                      $output .= $modx->getChunk($tpl, $phs);
                      break;
                      case ’file’:
                      $output .= file_get_contents($tpl);
                      $modx->setPlaceholders($phs);
                      break;
                      case ’inline’:
                      default:
                      /* default is inline, meaning the tpl content was provided directly in the property */
                      $output .= $tpl;
                      $modx->setPlaceholders($phs);
                      break;
                      }


                      *** I also tried placing the code here (and it returns loginResourceId correctly)
                      if ($modx->user->get(’username’) == ’(anonymous)’) {
                      /* user is not logged in */
                      }
                      else {
                      $profile = $modx->user->getOne(’Profile’);
                      $loginResourceId = $profile->get(’fax’);
                      }


                      return $output;
                      ?>