I’ve been trying to user the CheckUser snippet posted by Sottwell in response to a question by madcat999. The site needs to show the all the menu options in the drop menu and then redirect those users to a login for menu option that are restricted. Rationale being that if you can see the options you’re missing out on then you are more likely to register. The code posted previously is below:
Code:
// CheckUser snippet
// checks to see if user is a member
// of a given web user group
// redirects to given login page if not.
//
// options:
// &group - the group the user must be a member of
// &page - the page ID of the login page
if(!isset($group) || !isset($page)) return; // can’t do much without these!
$group = array($group); // isMemberOfWebGroup needs an array.
if($modx->isMemberOfWebGroup($group)) return; // if we are, quietly exit.
$url = $modx->makeURL($page); // we didn’t exit, so must not be a member of group
$modx->sendRedirect($url); // so make the url to the login page and go there.
return;
This is then called from the page being secured by:
[[CheckUser?group=`galleryuser`&page=`xx`]]
And from the login page by:
[[WebLogin? &loginhomeid=`xx`]]
One problem I have is that when the user has logged in and redirects back to the ’secured’ page, the page now displays the [[CheckUser?group................]] at the top of the page. It’s like it sees the code as something it should now display??? The other minor pain (which could be worked around with multiple login pages) is that ideally you want the login page to know where the user has come from and redirect them back to the page they tried to enter. If you had 10 ’secured’ menu items, a separate login for each would start to become a pain.......
Any help would be greatly appreciated.
-
☆ A M B ☆
- 24,524 Posts
For the second issue, make a snippet or a TV with @EVAL that gets the $_SERVER[’HTTP_REFERER’], and use it for the loginhomeid value in the weblogin snippet call.
For the first issue, the only time I’ve had that happen is if the snippet is misspelled. Try it uncached ([!...!]) that might fix it as well.
Not understanding the solution for the second issue. I don’t believe I understand enough to write a snippet or know what a TV is. If my user has selected what is page 40 on the menu then that is where I want them to go afterwards if that makes sense. Given we are calling the WebLogin from the menu page, would it make sense to add a variable to this snippet to supply the source/menu page when we call the WebLogin snippet? How would I declare the variable in the WebLogin snippet if that is a sensible approach?
On the first one I’m sure I’ve spelled correctly as it redirects to my login. I’ve tried [[ and [!. Interestingly [! is actually displaying as [[. The page is not set as cached either. Would setting as searchable cause this to happen?
-
☆ A M B ☆
- 24,524 Posts
Make a new TV. Name it referer. Make its default value
@EVAL return $_SERVER['HTTP_REFERER'];
When you make the WebLogin snippet call, set an option &loginhomeid=`[*referer*]`.
Now when the user logs in, he’ll be sent back to the page he came from.
As for your first problem, I have no idea why it would be doing that.
hmmm, I’m starting to feel decidedly dumb........I get the following error. Do I need to do something on the page calling CheckUser to populate the variable?
MODx encountered the following error while attempting to parse the requested resource:
« `[*referer*]` is not numeric and may not be passed to makeUrl() »
I suspect I need to get more heavily involved in the documentation. Can’t work out the display issue given there is other stuff on the page calling chunks etc which are working fine. Tried unchecking searchable etc etc with no joy
-
☆ A M B ☆
- 24,524 Posts
No, I was dumb here, not remembering that it needs to be a doc id, not a url!
I’m sure someone will correct me if I’m wrong, but if you just call the login using this:
[!WebLogin!]
it should by default send you back to the page the user logged in from... at least it does for me.
Not sure how that work in the context of the above. I want to display menu options through dropmenu and then have one of those associated pages check to see whether the user is logged in. If not it passes them off to a login page, once they have logged in they return to the original page they were trying to hit. When I do a standard weblogin it just goes to content saying ’Logged In’.
After reading through the thread again, here is another solution which may help. I modified the CheckUser snippet a bit and came up with this:
1. You will have to create a seperate template for the protected pages.
2. In place of the normal [*content*] tag, put the following snippet call:
[!CheckUser?group=`name of group`!]
3. here is the snippet code:
// CheckUser snippet
// checks to see if user is a member
// of a given web user group
//
// options:
// &group - the group the user must be a member of
// [!CheckUser?group=`galleryuser`!]
$output ="";
if(isset($group)){
$group = array($group); // isMemberOfWebGroup needs an array.
if($modx->isMemberOfWebGroup($group)){
$output .="[*content*]";
}else{
$output .="[[WebLogin]]";
}
}else{
$output .="";
}
return $output;
try that out and see if it will be of any use...
Yup, that works perfectly. Thanks for the help.