We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53173
    • 5 Posts
    Hello.

    I would like to create a package using mycomponent. So I installed MODX and mycomponent. Basically I know how to create packages with mycomponent.

    So far the package I'm working on does only contain a modified addusers.resolver.php (no chunks, resources etc.). The code that I added to this file works fine if run as snippet. But if the code is run in the addusers.resolver.php while package installation, the installation gets stuck (no package installation success message). But the code is executed successfully because the user groups are created and the super user is added to the newly created user groups and got super user role.

    Am I doing something wrong? Is the addusers.resolver.php file not the place to create user groups? Or is it very uncommon to do that in a package installation routine?

    Here is the installation console output ('sitelogin' is the package name):
    Console running...
    Attempting to install package with signature: sitelogin-1.0
    Package found...now preparing to install.
    Grabbing package workspace...
    PHP notice: Undefined offset: 2
    Workspace environment initiated, now installing package...


    The php notice does appear on installation of every package that I created with mycomponent (may refer to a php version?).

    This is the addusers.resolver.php:
    <?php
    /**
     * Resolver for flightschool extra
     *
     * Copyright 2017 by Björn Kaiser [email protected]
     * Created on 01-20-2017
     *
     * @package flightschool
     * @subpackage build
     */
    
    /* @var $object xPDOObject */
    /* @var $modx modX */
    
    /* @var array $options */
    
    $new_user_groups = array(
        'Member Group 1',
        'Member Group 2);
    
    if ($object->xpdo) {
        $modx =& $object->xpdo;
        switch ($options[xPDOTransport::PACKAGE_ACTION]) {
            case xPDOTransport::ACTION_INSTALL:
                foreach ($new_user_groups as $new_user_group)
                {
                    // create new user group
                    $response = $modx->runProcessor('security/group/create', array('name' => $new_user_group));
                    // add super user to the new user group
                    if (!$response->isError())
                    {
                        $group = $response->getObject();
                        $modx->runProcessor('security/group/adduser', array('user_group' => $group['id'], 'member' => 1));
    
                        // change super user role to 'Super User' role
                        $user_group = $modx->getObject('modUserGroup', array('name' => $new_user_group));
                        $user_group_id = $user_group->get('id');
    
                        $user_group_member = $modx->getObject('modUserGroupMember', array('user_group' => $user_group_id, 'member' => 1, 'role' => 1));
                        $user_group_member->set('role', 2);
                        $user_group_member->save();
                    }
                }
                break;
            case xPDOTransport::ACTION_UPGRADE:
                /* [[+code]] */
                break;
    
            case xPDOTransport::ACTION_UNINSTALL:
                // remove created user group
                foreach ($new_user_groups as $new_user_group)
                {
                    $user_group = $modx->getObject('modUserGroup', array('name' => $new_user_group));
                    $response = $modx->runProcessor('security/group/remove', array('id' => $user_group->get('id')));
                }
                break;
        }
    }
    
    return true;


    Best regards,
    Björn

    This question has been answered by BobRay. See the first response.

      • 4172
      • 5,888 Posts
      you've forgotten a single quote

      $new_user_groups = array(
          'Member Group 1',
          'Member Group 2');
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 53173
        • 5 Posts
        Oh, you're right. After pasting the code in my initial posting I changed the user group names and accidentally removed the quote. In my code file all quotes are set.
        • discuss.answer
          • 3749
          • 24,544 Posts
          I've sometimes had trouble using the processors in a resolver -- possibly due to permission issues. You might try just creating the objects directly (untested and incomplete):

             /* In case group already exists */
             $user_group = $modx->getObject('modUserGroup', array('name' => $name));
             if (! user_group) {
                 $user_group = $modx->newObject('modUserGroup');
             }
             $user_group->set('name', $name);
             $user_group->set('parent', $parent);
             $user_group->save();
          }



          To add a user to the group (it's more reliable to re-get the group):

          $role = 2;
          $rank = ??;
          $userId = ??;
          
          $group = $modx->getObject('modUserGroup', array('name' => $name));
          if ($group) {
             $groupId = $group->get('id');
             /* In case the UGM already exists */
             $ugm = $modx->getObject('modUserGroupMember', array('user_group' => $groupId, 'member' => $userId));
             if (!$ugm) {
                 $ugm = $modx->newObject('modUserGroupMember');
             }
             $ugm->set('user_group', $groupId);
             $ugm->set('member', $userId);
             $ugm->set('role', $role);
             $ugm->set('rank', $rank);
             $ugm->save();
          } else {
              /* report error */
          }



          For uninstall:

          $user_group = $modx->getObject('modUserGroup', array('name' => $new_user_group));
          if ($user_group) {
             $user_group->remove();
          } else {
             /* report error */
          }


          You can also use fromArray() to set the object fields before saving rather than the individual set() calls.
            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
            • 53173
            • 5 Posts
            Thank you, BobRay.

            After working on my extra for a while ignoring the resolver issue, I came back to it. I did like you suggested (avoid processors) and now the resolver works fine.

            By the way, I like your book. It is still useful for learning the basics.

            Best regards,
            Björn
              • 3749
              • 24,544 Posts
              I'm glad you got it sorted. smiley

              Thanks for the kind words, and for buying my book.

              Bob
                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