We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36502 ☆ A M B ☆
    • 491 Posts
    I'm developing an extra with revo 2.2.14

    Let's take the tutorial about doodles, I added in the database a field "context".
    Let's take a user that has access only to context "it", how can I fill that field with the current context value "it"? Is it possible?

    I tried adding:

    $scriptProperties['context'] = $modx->context->key;


    in the processors/mgr/doodles/create.class.php but it gives me always the context "web" as result...

    Thanks
      TilliLab | MODX Ambassador
      website
      • 4172
      • 5,888 Posts
      If you are talking about a processor in a CMP (custom manager page), the current context will allways be 'mgr', if you would not switch the context somehow programmatically.

      But I wouldn't know of a reason, why you should want that.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 3749
        • 24,544 Posts
        I think it would involve examining the Context Access ACL entries and their user intersects, which would be fairly difficult (for me anyway).

        I would be tempted to cheat and create a User Setting called allowed_context or allowed_contexts, listing the Context(s) the user has access to (even easier if there's only one). If there's only one, the code would look like this:

        $ctx = $modx->getOption('allowed_context', null, null);
        
        if ($ctx && ($ctx == 'it')) {
           /* User has access */
        }

          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
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You can get the context of a resource being edited with $resource->get('context_key')
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 36502 ☆ A M B ☆
            • 491 Posts
            3 answers from 3 candidates to modmore prize smiley

            @Bruno: I'd need this information to filter the results of cmp and show the correct information only to the correct users. Users belong to different group so I thought about using context to filter.

            @Susan: thanks but I need this information in a cmp, so I don't think I can use the $resource

            @Bob: adding the system setting could be a good solution!

            Now the last step...how can I pass this value in the extjs grid? the code you wrote can be used in the saving connector, right?
            Do you think it's best to add the value in the extjs grid or after in the save connector?

            Petitions.window.CreatePetition = function(config) {
                config = config || {};
                Ext.applyIf(config,{
                    title: _('petitions.petition_create')
                    ,url: Petitions.config.connectorUrl
                    ,baseParams: {
                        action: 'mgr/petition/create'
                    }
                    ,fields: [{
                        xtype: 'hidden'
                        ,name: 'context'
                        ,value: HOW CAN I ADD THE VALUE HERE
                    },


            thanks!
              TilliLab | MODX Ambassador
              website
              • 4172
              • 5,888 Posts
              I would set this value in your create/update - processor.
              This should be easier and the user couldn't manipulate the hidden-field, you know?
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 3749
                • 24,544 Posts
                I agree with Bruno17 if it would work there. The code will get the value wherever you run it (as long as $modx is initialized), but if you need it in your JS and the setting will have only one value, you can just do this:

                ,value: [[++allowed_context]]


                  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
                  • 36502 ☆ A M B ☆
                  • 491 Posts
                  Thanks Bob, but I'm afraid I miss some step somewhere, if I add the code you suggested I notice in the console the error:

                  Uncaught ReferenceError: allowed_context is not defined

                  Do I have to declare it somewhere?
                    TilliLab | MODX Ambassador
                    website
                    • 3749
                    • 24,544 Posts
                    create a User Setting called allowed_context
                    wink

                    Note that it's a User Setting, not a System Setting, and it has to be set for each user (on the Settings tab when you edit the user).

                    You should probably also create a System Setting of the same name with 'web' as the context. It will be used (and needed) if the User Setting is not set.
                      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
                      • 39519 ☆ A M B ☆
                      • 79 Posts
                      <?php
                      /**
                       * Created by JetBrains PhpStorm.
                       * User: matdave
                       * Date: 9/5/13
                       * Time: 11:36 AM
                       */
                      
                      //$modx->initialize('mgr');
                      
                          $list = array();
                          $userWUG_arr = $modx->user->getUserGroupNames();
                          $userid = $modx->user->get('id');
                          $ug = $modx->newQuery('modUserGroup');
                          $ug->where(array(
                              'name:IN' => $userWUG_arr,
                          ));
                          $gc_groups = $modx->getIterator('modUserGroup', $ug);
                          if(count($gc_groups)){
                              foreach($gc_groups AS $mxg){
                      
                                      $webContextAccess = $modx->newQuery('modAccessContext');
                                      $webContextAccess->where(array(
                                          'principal' => $mxg->get('id'),
                                          'AND:target:!=' => 'mgr',
                                      ));
                                      $gc_cntx = $modx->getIterator('modAccessContext', $webContextAccess);
                      
                                      if(count($gc_cntx)){
                                          foreach($gc_cntx AS $acl){
                                              if(!in_array($acl->get('target'), $list))
                                                  $list[] = array('key'=>$acl->get('target'));
                                          }
                                      }
                      
                              }
                          }
                          if($modx->user->isMember('Administrator')) { $list[] = array('key'=>''); }
                      
                      
                          if(!count($list)){
                              $contextKeys = array();
                              $query = $modx->newQuery('modContext', array('key:NOT IN' => array('web', 'mgr')));
                              $query->select($modx->getSelectColumns('modContext', 'modContext', '', array('key')));
                              if ($query->prepare() && $query->stmt->execute()) {
                                  $contextKeys = $query->stmt->fetchAll(PDO::FETCH_COLUMN);
                              }
                              foreach ($contextKeys as $ctx) {
                                  $list[] = array('key'=>$ctx);
                              }
                          }
                      return $this->outputArray($list,sizeof($list));
                      


                      I use this to check what key's a user has access to (leaving out web and mgr). I also modify it based on the result to limit the output of whatever data I'm trying to retrieve.
                        Mat Dave Jones