We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 577
    • 132 Posts
    I have had API code that I am using directly and outside of modx itself to handle some migration scripts. Last checked it worked in 2.0.8 now on 2.1 rc4 I am getting error.

    "Principal 0 does not have permission to load object of class modContext with primary key: toyotaplace"

    The code is this and modx is initialized in mgr.

    $resource = $modx->newObject('modResource');
        $resource->fromArray(array(
        'type'        => 'document',
        'contentType' => 'text/html',
        'pagetitle'   => '_config',
        'template'    => 0,
        'isfolder'    => 1,
        'context_key' => 'toyotaplace',
        'hidemenu'    => 1,
        'alias'       => 'config'
        ), '', true);
    
    $resource->save();
    
      "One of these days I will get around to my own website... Its only been about 12 years... maybe tomorrow smiley"
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      I’m not sure if things have changed, but ImportX (tho the processor is accessed from ExtJS through a connector) has had no issues from 2.0.8 to 2.1-rc3 (not tested rc4 yet).

      Perhaps you can show the init code you’ve used?
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 577
        • 132 Posts
        this would in effect be doing the same things as a connector just not rolled as one. This is a migration script that once we have things finalized and migrated will never be used again so just using it as its own outside file. Its quite large so I can show the whole thing but basically it creates a bunch of resources, container, users, user group, context, etc etc. while at the same time reading data out of our system and inserting where needed into modx.

        This may also become a "Create Site" Add-on down the road to allow for all the standard setup and default resource needs to be setup in the click of a button based on a schema.

        So here it is in a nut shell

        define('MODX_API_MODE', true);
        $modx = new modX();
        $modx->initialize('mgr');
        $modx->setDebug(true);
        $modx->setLogLevel(modX::LOG_LEVEL_INFO);
        


        Then the code processes all the following and saves with no issue

        $modx->newObject('modContext');
        $modx->newObject('modAccessContext');
        $modx->newObject('modUserGroup');
        $modx->newObject('modUserGroupMember');
        $modx->newObject('modContextSetting'); // Creates and sets context settings (base_url, etc etc)
        
        Then fails on 
        $modx->newObject('modResource');
        


        So I have created a new context; a new user for that context; user group to put the user in; added settings to the context; but then fails to be able to add resources because it says I do not have permission.

        This all worked flawlessly before and nothing has been touched other then the 2.1 upgrade.


          "One of these days I will get around to my own website... Its only been about 12 years... maybe tomorrow smiley"
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          When debugging stuff, seeing it all in context really helps... We don’t need all your resource data or anything, but what are you including, what methods are you calling to get the modx object running, etc etc.

          If you would rather not publicize the complete code, I wouldn’t mind having a brief look if you send it to the email in my sig, link to a gist or anything. But I can’t guess what’s happening without any details.
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • 3749
            • 24,544 Posts
            It sounds like a bug to me, especially since it was working before. Could you report it here: http://bugs.modx.com/?
              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
              • 22303 MODX Staff
              • 10,725 Posts
              Actually, we need a lot more information to determine if this is a bug or not. There is a bug with Element Category access in RC4, for instance, that has already been fixed. There are also recent changes that require users to have permission to "load" the Context when it is being initialized, which was failing in some cases. Let’s explore the details a little more before we jump into a bug report...
                • 18373 ☆ A M B ☆
                • 3,141 Posts
                I had a look & discussion on this code, and the best I could come up with is that the newly created context (which is linked to a usergroup, user & access policy) does not allow a new resource being created. My guess is the permissions weren’t flushed yet or something similar.

                I think this was also cross-posted in the partner forum..
                  Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                  Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                  • 577
                  • 132 Posts
                  -----------------------------------------------
                  This is what I had put in the partner side, sorry to cross post, was trying to draw attention. Also not clear when to post there and when to post here. I will remove the other post.
                  ----------------------------------------------
                  Again as in my original statement all is well with the object and I can create context, user for context, ug’s for users. All is well until I need to create resources for said context.

                  My thoughts as to why this is happening lies somewhere in the new caching of 2.1 because all was well in 2.0.8.

                  So my thought it that now in 2.1 I cannot create a context and then immediately save a resource to it without first doing X. My thoughts on X are

                  - flush permissions ?
                  - reload cache ?
                  - not create resource with cacheFlag true ?
                  - re initialize mgr?

                  Anyone have any insight.

                  Thanks
                  Adam
                    "One of these days I will get around to my own website... Its only been about 12 years... maybe tomorrow smiley"
                    • 577
                    • 132 Posts
                    I will see if I can put together a smaller script that reproduces the issue that will be easier to pass around and see if that helps communicate. Thanks everyone for their support.
                      "One of these days I will get around to my own website... Its only been about 12 years... maybe tomorrow smiley"
                      • 577
                      • 132 Posts
                      Ok did some more testing and breaking down of the code. And I now have a test case. My apologies , I should have done something like this in the first place and will be better at posting my issue in the future. I don’t imagine that I have found a bug and rather things have changed in 2.1 either in an enhancement or bug fix to something I was unknowingly exploiting. So I am not taking the position "It was working before" , but simply as a point of reference this worked before and should still work again 2.0.8 but dont have a copy to test with again myself.

                      So as it stands
                      
                      ## THIS WORKS ##############
                          
                          $modx = new modX();
                          $modx->initialize('mgr');
                      
                          $modx->setDebug(true);
                          $modx->setLogLevel(modX::LOG_LEVEL_INFO);
                          echo '<pre>'; $modx->setLogTarget('ECHO');
                          
                          $context_key = 'MigrationTest1';
                      
                          /*
                           * Set Context
                           */
                      
                          $context = $modx->newObject('modContext');
                          $context->fromArray(array(
                          'key'         => $context_key,
                          'description' => ''
                          ), '', true);
                      
                          $context->save();
                      
                          /*
                           * Resource "_config"
                           * Build _config container
                           */
                      
                          $resource = $modx->newObject('modResource');
                          $resource->fromArray(array(
                          'type'        => 'document',
                          'contentType' => 'text/html',
                          'pagetitle'   => '_config',
                          'template'    => 0,
                          'isfolder'    => 1,
                          'context_key' => $context_key,
                          'hidemenu'    => 1,
                          'alias'       => 'config'
                          ), '', true);
                      
                          $resource->save();
                      
                      


                      BUT

                      ## THIS *DOES NOT* WORK ##############
                       
                          $modx = new modX(); 
                          $modx->initialize('mgr');
                      
                          $modx->setDebug(true);
                          $modx->setLogLevel(modX::LOG_LEVEL_INFO);  
                          echo '<pre>'; $modx->setLogTarget('ECHO');
                          
                          $context_key = 'MigrationTest1';
                      
                          /*
                           * Set Context
                           */
                      
                          $context = $modx->newObject('modContext');
                          $context->fromArray(array(
                          'key'         => $context_key,
                          'description' => ''
                          ), '', true);
                      
                          $context->save();
                      
                          // Administrator "Resource Policy"
                              $access = $modx->newObject('modAccessContext');
                              $access->fromArray(array(
                              'target'          => $context_key,
                              'principal_class' => 'modUserGroup',
                              'principal'       => 1,
                              'authority'       => 9999,
                              'policy'          => 1
                              ), '', true);
                      
                              $access->save();
                      
                          /*
                           * Resource "_config"
                           * Build _config container
                           */
                      
                          $resource = $modx->newObject('modResource');
                          $resource->fromArray(array(
                          'type'        => 'document',
                          'contentType' => 'text/html',
                          'pagetitle'   => '_config',
                          'template'    => 0,
                          'isfolder'    => 1,
                          'context_key' => $context_key,
                          'hidemenu'    => 1,
                          'alias'       => 'config'
                          ), '', true);
                      
                          $resource->save();
                      
                      



                        "One of these days I will get around to my own website... Its only been about 12 years... maybe tomorrow smiley"