We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1576
    • 24 Posts
    Hello,

    I want to create pages that can be accessed (in other words, that are visible in the nav menu), but which require a specific user role to view the content contained in them.

    For example, I have a website with students and teachers. I want students, and anonymous web users, to go into the "Teacher" area of the site, but only see a login field or a "restricted access" message.

    I want Teachers to be able to go into the same area, after logging in, and see their own content.

    The problem with using role levels for this is that I also want the inverse. I want "Students" to have an area of the site that "Teachers" cannot access.

    If I give "Teacher" accounts higher roles, they inherit all the permissions of accounts with lower role numbers. Correct? This would mean that Teachers could see Student pages, but not the other way around.

    Is my question clear? I’m new to MODx and sometimes articulating the problem is difficult...

    I want Teachers to see Teacher content, and Students to see Student content, but neither to see the other’s content.

    Is this possible?

    Thank you!
      • 3749
      • 24,544 Posts
      Yes, it’s definitely possible. Note that the higher authority permissions are only inherited *within a user group*. As long as the Teachers and Students are in separate groups, they’ll never inherit each other’s permissions.

      And if there is also a TeacherResources resource group and a StudentResources resource group you can tie each user group to its own resource group and they’ll never see each others’ resources directly or in a menu (unless a resource is in both resource groups).

      Hope this makes sense.

      [update] Ok, I’m feeling guilty about not giving you more information about how to do what you want. If neither the teachers or students will be logging into the Manager, it’s actually fairly simple and you don’t care about authority numbers at all because you’re limiting access to the resources themselves.

      Create a Teachers user group and put the teachers in it.
      Create a Students user group and put the students in it.

      Create a TeacherResources resource group and put the teacher’s resources in it.
      Create a StudentResources resource group and put the student’s resources in it.

      Create a Teacher role with an authority level of 5
      Create a Student role with an authority level of 7

      Go to Security -> Access Controls - User Groups tab.
      Right-click on the Teachers user group and select "Update User Group."
      Click on the Resource Group Access tab
      Click on the "Add Resource Group" button.

      Add an ACL entry with the following fields:
      Resource Group: TeacherResources
      Minimum Role: Teacher
      Policy: Load Only
      Context: web

      Save.

      Now do exactly the same thing with the Student user group, but with the following values:

      Resource Group: StudentResources
      Minimum Role: Student
      Policy: Load Only
      Context: web

      That should do it (unless I’m forgetting something). smiley

        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
        • 1576
        • 24 Posts
        Thanks so much for your response! I’ll definitely give this a shot.
          • 1576
          • 24 Posts
          Okay, so the problem with your solution is that it removes the resources from the navigation menus.

          I want to control what users see on the Chunk level of content (HTML content inside pages). As far as I can tell, I can’t place chunks in resource groups. Please let me know if there’s a way to do this.

          So what I’m looking for is a page that both Students and Teachers can access, called "Teacher Area", but students (and public users) will see "Access restricted.", while Teachers will see their content, but not the "Access restricted" text.

          Is this possible?

          Thanks again!
            • 1576
            • 24 Posts
            Okay, the closest I’ve been able to come to the functionality I’m looking for is by:

            1. Creating user groups for each user role (Teacher/Student)
            2. Creating categories called Teacher/Student
            3. Moving Teacher/Student-specific chunks to the Teacher/Student categories
            4. Creating Teacher/Student user roles (with role levels of 5 and 6 respectively)
            5. Creating and assigning "teacher" and "student" user accounts to each user group
            6. Assigning elements within the Teacher/Student categories to the Teacher/Student user groups (under the element category access tab)
            7. Setting the access policy for both Teacher/Student element category access to "Load, List and View"
            8. Setting the minimum roles to 5 for Teachers and 6 for Students.
            9. Setting the context to web.
            10. Placing both a Teacher chunk and a Student chunk on a publicly accessible page.

            From this point I got the following results:

            1. As an anonymous web user, the public page was blank, displaying neither chunk. So far so good.
            2. I logged in as a student user, and only the Student chunk was visible. Also good.
            3. I logged out and then logged back in as a Teacher user, but now both the Teacher and Student content was visible.
            4. I flushed all permissions and logins, cleared my browser cache, and accessed the page again as an anonymous user: it was blank.
            5. I connected first as a Teacher, logged in, and viewed my Teacher content.
            6. Logging out, I logged back in as a Student, and now once again both content chunks were visible.
            7. Even worse, the content chunks were now visible to any user, logged in or not, from any browser.

            I am stumped, a little bit frustrated, and honestly surprised that such a basic type of user account functionality is so difficult to configure.

            What am I doing wrong?

            In lieu of this, is there a way to check user groups and forward users to an "Access Denied" page? Maybe using a PHP snippet?

            I really need to get this working ASAP... any help would be greatly appreciated.

            Thanks.
              • 3749
              • 24,544 Posts
              I’m sorry I misunderstood your original intention. I thought you wanted to hide whole pages from certain users, not content on the pages. The most common use of the Element Category Access ACLs is to hide elements in the Manager. I don’t know if they are intended for use in hiding individual chunks in the front end or not. They’re brand new and I don’t think any one has attempted to use them that way.

              That aside, here is a much easier (though, I think, somewhat slower) method using output modifiers. You still need the user groups (but no resource groups or categories). Be sure to delete the categories because they might interfere with this process. You can also delete the Category Access ACL entries if none of them will be using the Manager.


              [[$TeacherChunk:ismember=`Teachers`:then=`[[$TeacherChunk]]`:else=`You can't see this, you're not a teacher`]]
              
              [[$StudentChunk:ismember=`Students`:then=`[[$StudentChunk]]`:else=`You can't see this, you're not a student`]]
                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
                • 1576
                • 24 Posts
                Hi Bob,

                Thanks for your help so far. Sorry to trouble you further...

                I tried your last suggestion, but I’m getting the following error:

                Fatal error: Call to a member function isMember() on a non-object in /home/mysite/public_html/core/model/modx/filters/modoutputfilter.class.php on line 130

                This points to the following code line: $condition[]= $user->isMember($grps);

                So I tried creating a new chunk called Test.

                [[$Test:ismember=`LBParents`:then=`[[$Test]]`:else=`You can’t see this, you’re not a parent`]]

                This only displays the "else" message, regardless of your login state.

                I deleted the original Teacher and Student chunks and created new ones, but inserting even one chunk now gives me the following error messages:

                Parse error: syntax error, unexpected ’)’ in /home/leobaeck/public_html/core/model/modx/filters/modoutputfilter.class.php(151) : eval()’d code on line 1

                Parse error: syntax error, unexpected ’)’ in /home/leobaeck/public_html/core/model/modx/filters/modoutputfilter.class.php(159) : eval()’d code on line 1

                The page still loads the template, however, and the "else" message. Not the chunk content, though.

                Mind you, I’m not getting the errors in Firefox, only in Chrome. I use the manager in Chrome, so it could be a session cache problem. Still, even in Firefox, logged in as a Teacher, it doesn’t display the Teacher chunk content.

                Is there a tutorial somewhere for conditionally displaying content using PHP? That would be a godsend...
                  • 3749
                  • 24,544 Posts
                  I’m not sure why that isn’t working. The message implies that the $user object is empty. It worked for me, but I was previewing from the Manager. If I have time, I’ll try it from another browser.

                  You might try it with [[!$ChunkName ...] (I should have suggested that at first). You may be seeing a cached version of the chunk.

                  The PHP version would be much faster, but I didn’t know if you would be up for that. It would look something like this:

                  [[!ShowTeacher? &teacherChunk=`TeacherChunk` &studentChunk=`StudentChunk`]]

                  <?php
                  $output = '';
                  if ($modx->user->get('username') == '(anonymous)' {
                      return $output;   // nothing for not-logged-in users
                  } 
                  
                  if ($modx->user->isMember('Teachers') {
                     $output = $modx->getChunk($teacherChunk);
                  } elseif ($modx->user->isMember('Students') {
                      $output = $modx->getChunk($studentChunk);
                  }
                  return $output;
                  ?>


                    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
                    • 1576
                    • 24 Posts
                    I tried the code, but it just displays blank, regardless of my login.

                    Now I’m starting to worry there might be something wrong with my configuration...

                    I tried with and without access lists on the user groups, and with and without roles for the users. No luck.

                    I even tried simplifying the code down to this, and logging in as the "student" user:

                    <?php
                    $output = 'Test';
                    
                    if ($modx->user->get('username') == '(student)' {
                        return $output;
                    }
                    ?>


                    And I just get the following error:

                    Parse error: syntax error, unexpected ’{’ in /home/mysite/public_html/core/cache/elements/modsnippet/27.include.cache.php on line 9

                    Why won’t it just work?:(
                      • 1576
                      • 24 Posts
                      Okay, so I managed to get the following code to display content conditionally:

                      <?php
                      if ($modx->user->isMember("Teachers")) {
                           echo "Welcome Teacher";
                        } elseif ($modx->user->isMember("Students")) {
                          echo "Welcome Student";
                        }
                      ?>


                      I’m not sure why this works and the other stuff doesn’t, but hooray! (Now let’s hope it keeps working...)

                      Thanks again for the help, Bob!