# IsAdmin for MODX3

**URL:** <https://community.modx.com/t/isadmin-for-modx3/6770>\
**Category:** Support\
**Tags:** revo-3\
**Created:** [June 12, 2023, 10:06am UTC](https://community.modx.com/t/isadmin-for-modx3/6770 "2023-06-12T10:06:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 12, 2023, 10:06am UTC](https://community.modx.com/t/isadmin-for-modx3/6770/1 "2023-06-12T10:06:30Z")

</div>

Hi There,

I’ve installed my first version of MODX3 and all great (nice work!) apart from one thing - I run a snippet on all my sites called: isAdmin, which detects if a manager is logged in or not - I use this to load Google Analytics only for logged out users so I don’t skew the numbers.  
But ths snippet I’ve always used on all Modx 2 sites isn’t working on Modx 3.

This is the PHP:

```auto
<?php
if ($modx->user instanceof modUser) {
  if ($modx->user->hasSessionContext('mgr')) { 
    return true;
  }
}
return false;

```

Presumably the syntax needs updating for MODX 3 but I can’t find anything online - any suggestions?

Thanks!  
Andy

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [June 12, 2023, 10:12am UTC](https://community.modx.com/t/isadmin-for-modx3/6770/2 "2023-06-12T10:12:53Z")

</div>

> [@tm2000](#):
>
> `if ($modx->user instanceof modUser) {`

You now have to add the namespace:

```php
if ($modx->user instanceof MODX\Revolution\modUser) {

```

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 12, 2023, 10:33am UTC](https://community.modx.com/t/isadmin-for-modx3/6770/4 "2023-06-12T10:33:17Z")

</div>

Boom! brilliant - thanks so much!!

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [June 12, 2023, 11:52pm UTC](https://community.modx.com/t/isadmin-for-modx3/6770/5 "2023-06-12T23:52:06Z")

</div>

You could also just delete the test, since `$modx->user` is pretty much guaranteed to be an instance of the `modUser` or `MODX\Revolution\modUser` object.

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 13, 2023, 7:50am UTC](https://community.modx.com/t/isadmin-for-modx3/6770/6 "2023-06-13T07:50:16Z")

</div>

Oh OK - so you mean like this?

> [@tm2000](#):
>
> ```auto
> <?php
> if ($modx->user->hasSessionContext('mgr')) { 
> return true;
> }
> return false;
> 
> ```

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 13, 2023, 10:43am UTC](https://community.modx.com/t/isadmin-for-modx3/6770/7 "2023-06-13T10:43:28Z")

</div>

So i’ve run into another problem with this in modx 3.  
it seem that when i try and load the GA4 tag into the isAdmin snippet, it ignores it.

This is my code:

```auto

[[!isAdmin:notempty=`
    
<!-- Not tracking GA for Manager Users -->
`:default=`

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-54FN5RJ');</script>
<!-- End Google Tag Manager -->
`]]

```

If logged in - it shows the comment correctly of:

```auto
<!-- Not tracking GA for Manager Users -->

```

But when I check a browser or machine which isn’t logged into the manager, there is nothing, where the tag should be.

For testing I changed the Google Script to just another script tag, and it loads fine.  
So for some reason it doesn’t like the Google Analytics code, and outputs nothing.

I tried putting the javascript in a chunk, but still the same.

Has anyone had this before?

I don’t understand which it outputs nothing - not even the comments which top and tail the script.

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [June 13, 2023, 10:53am UTC](https://community.modx.com/t/isadmin-for-modx3/6770/8 "2023-06-13T10:53:56Z")

</div>

MODX 3.0.3 still has some parsing issues. (And for some reasons unknown to me, they haven’t released a new version in months.)

This pull request probably fixes the issue:

> <https://github.com/modxcms/revolution/pull/16302>
>
> \### What does it do?
> Fix where literal brackets (not intended as an opening/clo…sing tag) go in the parsed command modifiers. 
> 
> \### Why is it needed?
> 
> John MacDonald reported the parser breaking in the following scenario:
> 
> \`\`\`
> \[\[++google\_analytics:notempty=\`
> 
> \<script async src="https://www.googletagmanager.com/gtag/js?id=\[\[++google\_analytics\]\]"\>\</script\>
> \<script\>
> window.dataLayer = window.dataLayer || \[\];
> function gtag(){dataLayer.push(arguments);}
> gtag('js', new Date());
> 
> gtag('config', '\[\[++google\_analytics\]\]');
> \</script\>
> \`\]\]
> \`\`\`
> 
> Rather than outputting the script, it returned just the value of \`\[\[++google\_analytics\]\]\`. 
> 
> That test case was simplified to \`\`\`\[\[+tag:notempty=\`\[\]\`\]\]\`\`\` as the literal brackets appeared to be the problem.
> 
> Internally, the command ended up as \`notempty\[\]\` with an empty modifier. The \`notempty\[\]\` command doesn't exist so the original value is returned in its place. 
> 
> By using the \`$inModifier\` flag (which gets set in the switch case for \`\`\` \` \`\`\` when depth is 0) the brackets are parsed into the modifiers as they should. 
> 
> \### How to test
> 
> See tests for examples. Feel free to come up with more complex cases. 
> 
> \### Related issue(s)/PR(s)
> 
> Reported via Slack. 
> 
> Introduced with #16288 in 3.0.2.

And maybe apply this pull request as well:

> <https://github.com/modxcms/revolution/pull/16316>
>
> \### What does it do?
> 
> Improves the regex pattern used in modParser::collectEle…mentTags() to avoid hitting a backtrack limit under some environments (PHP 7.2, 7.3). Also adds a log message in case an error is detected. 
> 
> Credit to @JoshuaLuckers for providing the patch.
> 
> \### Why is it needed?
> 
> Make sure long tags can be parsed correctly and make debugging easier if something does fail. Please see #16263 for more details.
> 
> \### How to test
> 
> Please see #16263 for more details. It's worth noting I've so far seen 3 people refer to the fix in the issue as fixing the issue they had. As with all parser tweaks, trying it out on different real sites would be preferred to ensure this doesn't introduce any new issues. 
> 
> \[Here's a CI run of the unit test prior to applying the fix\](https://github.com/Mark-H/revolution/actions/runs/3597851277/jobs/6060109440), to show the unit test and fix work. 
> 
> Locally I've also been able to reproduce it by running the test on 7.2 before:
> 
> \`\`\` 
> ➜ 3.x git:(issue-16263) ✗ php core/vendor/bin/phpunit \_build/test/Tests/Model/modParserTest.php
> PHPUnit 8.5.31 by Sebastian Bergmann and contributors.
> 
> ..........................................................\[1\] 78526 segmentation fault /Applications/MAMP/bin/php/php7.2.34/bin/php -c core/vendor/bin/phpunit 
> \`\`\`
> 
> and after applying the fix:
> 
> \`\`\`
> ➜ 3.x git:(issue-16263) ✗ php core/vendor/bin/phpunit \_build/test/Tests/Model/modParserTest.php
> PHPUnit 8.5.31 by Sebastian Bergmann and contributors.
> 
> ........................................................... 59 / 59 (100%)
> 
> Time: 181 ms, Memory: 14.00 MB
> 
> OK (59 tests, 70 assertions)
> \`\`\`
> 
> (I'm doing \`php core/vendor/bin/phpunit\` because otherwise php can't get to mysql on my local machine.)
> 
> \### Related issue(s)/PR(s)
> 
> Fixes #16263

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [June 13, 2023, 5:25pm UTC](https://community.modx.com/t/isadmin-for-modx3/6770/9 "2023-06-13T17:25:29Z")

</div>

> [@tm2000](#):
>
> ```auto
> <?php
> if ($modx->user->hasSessionContext('mgr')) { 
> return true;
> }
> return false;
> 
> ```

Yes, that’s exactly what I meant.

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 14, 2023, 11:10am UTC](https://community.modx.com/t/isadmin-for-modx3/6770/10 "2023-06-14T11:10:18Z")

</div>

Thanks for comfirming!

---

<div class="post-metadata">

**Author:** ![system](https://community.modx.com/user_avatar/community.modx.com/system/32/2_2.png) [@system](https://community.modx.com/u/system)\
**Post date:** [June 16, 2023, 11:10am UTC](https://community.modx.com/t/isadmin-for-modx3/6770/11 "2023-06-16T11:10:30Z")

</div>

Closed on June 16, 2023, 11:10am UTC

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.
