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

    I have written a snippet that modifies the user group, its a simple mysql request.
    The problem is that the new group setting doesn’t affect the user until cookies have been cleared.

    So let’s remove cookies, right after the request :
    $user->removeSessionCookie('web'); 
    

    but this returns a fatal error. Fatal error: Call to a member function removeSessionCookie() on a non-object
    How do I load something into $user ? thanks to the support team

      • 22303 MODX Staff
      • 10,725 Posts
      First of all, it would be $modx->user->removeSessionCookie(), but you do not need to end the session or remove the cookie (and that function does nothing currently). You simply need to refresh the appropriate user session data.

      If all you want to do is reload user access permissions for web, you can:
      <?php
      $modx->user->loadAttributes(array(), 'web', true);
      ?>


      However, I think the API functions modUser::joinGroup() and modUser::leaveGroup() should call this automatically, as well as remove any other stale session data related to user access permissions (Resource Groups, User Groups, etc.). I’ll add a ticket to make this happen. In the meantime, if you want to refresh this other data manually, you can unset any $_SESSION data with the key modx.user.{id}. (where {id} is the $modx->user->id), e.g.
      <?php
      $key = "modx.user.{$modx->user->id}";
      $keyLength = strlen($key);
      foreach ($_SESSION as $sKey => $sVal) {
          if (substr($sKey, 0, $keyLength) === $key) unset($_SESSION[$sKey]);
      }
      ?>


      I assume you are using modUser::joinGroup() and/or modUser::leaveGroup() API functions to modify their memberships?
        • 10152
        • 156 Posts
        Thanks for theses explanations.
        I did a classic php request grin:
        	$memberId = $modx->getLoginUserID('web');  
        	$sql = "UPDATE modx_member_groups SET `user_group`='".$userGroups[$group]."' WHERE `member`='" .$memberId."' AND `user_group` ='".$memberGroupId."' ";
        	$db = mysql_connect($database_server, $database_user, $database_password);
        	mysql_select_db(trim($dbase,'`'),$db); 
        	mysql_query($sql) or die('Erreur SQL !'.$sql.''.mysql_error()); 
        	mysql_close(); 
        

        but I will change that when familiarized with this kind of API.
          • 22303 MODX Staff
          • 10,725 Posts
          (shudder)

          Honestly, you should never use mysql functions in a Revolution site. This creates a second connection to the database and increases overhead of a request by more than double. Database connection is generally the most expensive operation in any database-driven web site. MODx already creates a connection to the database using PDO, so there is zero need to ever do this.
            • 3749
            • 24,544 Posts
            LOL . . .

            Trust me, once you get used to the MODx/xPDO way of doing things, old-fashioned PHP DB access code looks like an open wound to you.

            Even more so to the author of xPDO. wink

            You might find some help here: http://bobsguides.com/revolution-objects.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