We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52241
    • 5 Posts
    If it is already discussed, i apoligize. Same for my poor english.

    Is there an analog for $modx->hasPermission at $user (not current one)?

    managers at my site can upload batch records, specifiying other users as authors (not resource, custom component), id like to check if user, specified in a batch has permission to post record. I think that should be quite easy, but cant even google it right... sigh

    P.S.: it should not be heavy cause there might be up to 1000+ records from one batch with lots of different users specified.

    Thanks in advance.

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

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Presuming this is based on a user's group (the group providing the needed permissions) you can check if a given user belongs to the correct group.

      $user=$modx->getObject('modUser',array('username'=>$username));
       $checkMembership = $user->isMember($usergroup);
      
        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
        • 52241
        • 5 Posts
        Not really. i was thinking of it before, but same permissions can be across several groups. hasPermission is the best way to solve this, i'm just not getting why there is no documented way to use it on $user, not just $modx instance.
        • discuss.answer
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          "You may specify either a string name of the user group, or an array of user group names."
          https://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/moduser/moduser.ismember

          No matter how you cut it, you have to check for group membership, because permissions are defined for groups, not for users.
            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
            • 52241
            • 5 Posts
            hasPermission checks a string than can be in different policy templates. so im using one string in different templates and checking it... i thought it was quite convinient untill this point. i know i can use groups, but while developing i'd prefer to keep it as flexible as possible.

            anyhow, i got the point - what i want is not possible. i have a workaround with snippets (isMemeber), as u metioned. just was worried im doing it not the right way. answer accepted.
              • 3749
              • 24,544 Posts
              Permissions are set by group, but in code, you can test for a specific permission:

              if ($modx->user->hasPermission('save_document')) {
                  /* Do something */
              }


              To answer the original question, you can use hasPermission() any time you have a modUser object. It's a method of the modUser object.

              The advantage of checking group membership is that it doesn't involve permissions at all, so it's handy when there's no permission that's only held by the group members. You can create a group and let them do something without even creating an ACL entry.

              In the case of $modx->user, I think hasPermission() would be faster because the permissions are already there as a big array in the object. I think the user's groups would have to be retrieved (I could be wrong).

                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
                • 52241
                • 5 Posts
                i'm testing right now and cant find any difference between $modx->hasPermission and $modx->user->hasPermission. it's about $modx instance again. The very point i'm using permissions is i have few policy templates and bound to groups policies.

                when i create a new group, that will require some permissions from those policies - it's easy to add new policy, bind to group and tick some checkboxes, rather than looking through pages of code and adding groups to isMember(). for now i created a snippet that returns true\false based on group and when new group added - i modify snippet, not the code. still i feel like im using a hammer in a lab.... all solutions i'm using for now are quite simple, but this one looks not that accurate (like those goto)

                i'm not very good at explanaitions, so, for example:

                policy template

                product.create.city.own
                product.create.city.any
                product.create.shop.own
                product.create.shop.any
                
                product.edit.city.own
                product.edit.city.any

                ...

                then policies

                for one group i create policy like

                product.create.city.own = 1
                product.create.city.any 
                product.create.shop.own = 1
                product.create.shop.any
                
                product.edit.city.own
                product.edit.city.any


                for another

                product.create.city.own
                product.create.city.any = 1
                product.create.shop.own
                product.create.shop.any = 1
                
                product.edit.city.own
                product.edit.city.any


                and some like

                product.create.city.own 1
                product.create.city.any
                product.create.shop.own
                product.create.shop.any = 1
                
                product.edit.city.own
                product.edit.city.any


                that fits my need - very easy to check one access when creating product if can change city or shop. so if i need another group - i create policy, bind and dont need to replace any isMemeber - i can just check if this user has permission. and while having about 20 groups for now - it's not really easy to remember the scheme, cause there are lots of permissions to check, view,edit,create,modify for 9 basic fields that can combine in different way.

                And again - it works perfectly, but while allowing user to be in multiple group and combine permissions - i'm curious why there is no easy way to check permissions for a user (not current), i have to do extra actions as grabbing groups, then grabbing policies, then grabbing values from them.

                May be im missing some basic logic of hasPermission? Im sorry if beeing too disturbing. i adore modx, thats why a bit confused here.
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Did you read the docs I linked to?

                  Step one: Get the User object for the user with the given username.
                  $user = $modx->getObject('modUser', array('username' => 'boss'));

                  Step two: See if that user is a member of an array of groups (you can define an array and use that as the second parameter to the isMember function):
                  $user->isMember(array('Staff','Investors'));


                  This will work in any code, plugin or snippet.
                    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