Member-Only Pages

Can anyone point me in the right direction? I’m creating a project for work. There’ll be a webpage were people will register their interest on a form. The details added to that form will be sent to a database. I want to create another webpage that will display all the entries from the database BUT I only want a certain amount of people to have access to it. So for example if someone clicked on the URL, it would ask for login details.

Anyone do anything like this before?

You probably want to have a look at the Login Extra to achieve what you want.

The Personalize extra will allow you to provide the logged in user the data, while delivering others to a login page

See the “Easy Way” section here. You can also use the ACL method describe earlier in that article.

I’ve decided Bob that I’m going to use your ‘The Easy Way’. It seems ‘Easy’ enough :smile: I just have a couple of questions if that’s okay.

I’m creating 2 pages that I don’t want the public to see. One of the pages will be seen by the User Group: Editors and the second page will be seen by the User Groups Editors and Media.

My question is this. Do I need to create 2 different login pages? 1 for each of the different pages as they’ll have different User Groups?

ALSO*** How do I create the login page? Do you have a tutorial on that?

I don’t think you’d need two login pages.

For the snippet, see the fist code snippet in the “Putting it all Together” section.

On the first page you describe, the tag would look like this:

[[!SnippetName? &userGroups=`SomeGroup`]]

on the other page, it would look like this:

[[!SnippetName? &userGroups=`SomeGroup,SomeOtherGroup`]]

As for the login page, just install the Login extra and put a snippet tag on your login page. The minimal version looks like this (though you’ll probably want to set some properties):

[[!Login]] 

There’s a description of the available snippet properties here.

Put the properties directly in the snippet tag. That way they’ll survive upgrades to the Login package.

Thanks a million again Bob. I really apreciate your input. I’ve a question (again). I’m having a few issues with the ‘Putting it all Together’.

I put this code in a Snippet I called PrivatePage_Editor:

<?php
/* PrivatePage snippet */
/* Redirect users who shouldn't see this page */

/** @param $redirectTo string -- (optional) ID of page to redirect to;
 *  *      default: unauthorized page
 *  *  @param $userGroups string -- (optional) Comma-separated list of
 *  *      User Groups that are authorized;
 *  *      if not set, users who are not logged in are redirected
 *  * */

 $output =  '';

function forward($id, &$modx)
{
    
    if (empty($id)) {
                $modx->sendUnauthorizedPage();
    }
    $url =  $modx->makeUrl((int)  $id,  "",  "",  "full");
    if (empty($url)) {
                die('MakeUrl failed. ID: ' .  $id);
    }
    $modx->sendRedirect($url);
}

$redirectTo =  $modx->getOption('redirectTo',  $scriptProperties,  '');
$userGroups =  $modx->getOption('userGroups',  $scriptProperties,  '');

if ((!empty($userGroups)) &&  strpos($userGroups,  ',') !== false) {
        $userGroups =  explode(',',  $userGroups);
}

if (empty($userGroups)) {
        /* Redirect anyone who is not logged in */
    $key =  $modx->context->get('key');
    if (!$modx->user->hasSessionContext($key)) {
                forward($redirectTo,  $modx);
    }
} else {
        /* redirect if not a member of specified group(s) */
    if (!$modx->user->isMember($userGroups)) {
                forward($redirectTo,  $modx);
    }
}

return $output;

And the code in my Template I had : [[!PrivatePage_Editor? &userGroups=Administrator, Editor &redirectTo=247]]

But no joy. It keeps refreshing the Login page.

I then tried the simple Snippet:

/* PrivatePage snippet */
/* Redirect anyone who is not a member of a particular user group */
 
if (! $modx->user->isMember('Administrator')) {
$modx->sendUnauthorizedPage();
}
return '';

And in my Template: [[!PrivatePage_Editor]] and that worked.

Am I doing something wrong with the Putting it all together code?

It’s kind of difficult to read the code without the formatting. Could you put three back-ticks on their own line above and below each section of code?

Sorry Bob…maybe I didn’t explain myself properly. The code that is under ‘Putting It All Together’, I just copy and paste it in without any changes. That’s the code above. The Putting It All Together didn’t work for me so I just used the second snippet on that page. It works perfectly for me. So thank you.

When I try the ‘Putting It All Together’ snippet, the login just keeps refreshing on the same page.

Refreshing when you submit it, or refreshing repeatedly on its own?

When I submit it. The smaller snippet is working perfectly fine

Yes, but I’m asking about exactly what happens when you try to use the “Putting it all together” snippet. Do you get an infinite loop refreshing the page over and over, or does it just reload once.

My apologies…it just reloads once.

This isn’t my place, but that sounds like a misconfigured login…make sure the login code you are using there works in a basic template

Pls check the error log for any errors

It could also be an error in the code from the article. I’ll try to find time to look into it.

Hey Bob,

I got that working great. Thanks. It was an error on my part. I was missing a second ] in the snippet call. A rookie error :frowning:

Can I ask your expert opinion on this. I’ve created 3 pages;

  1. Login
  2. Editor
  3. Directory

I’ve 2 user groups created also;

  1. Editor
  2. Media

User group Editor will see the pages Edit and Directory. User group Media will only see Directory. My question is this, when a user Media logs into directory.html and then logs out, how do I get the URL to stay at …/directory.html?service=logout and not redirect to the login.html URL.

I tried using this: <.a href="[[~[[*id]]? &service=logout]]" title=“Logout”>Logout </a.>

It keeps the URL but doesn’t actually log me out.

That’s a good question. Do you have a Login snippet tag in the template of the Directory page (or in its page content)?

No. I have the login snippet in the login page and the private page snippet in the directory page. I do have the logout code that I mentioned above.

If I could get a logout code thats working on the Directory and Editor pages that logouts out but keeps, shows the login template but keeps the directory or editor url, that would be great.

Since users who are not logged in won’t see the page, you could put a tag for Login on the page or template, and style it with CSS to just show a Logout button. That should keep them on the same page. I could be wrong.

I have created a snippet that reads the user group and will direct the user to the appropriate page. When they both logout, it send them both to the login page. I’m happy with that. Thanks a million Bob for all your help to date.

1 Like