We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • discuss.answer
    • 43592
    • 97 Posts
    After the user-groups limitation didnt work this is the working script for this usecase, corresponding to the description field

    <?php
    $superusergroups = $modx->getOption('superusergroups', $scriptProperties,'8,9');
    $parent = $modx->getOption('parent', $scriptProperties, 47);
    
    $superusergroups = explode(',', $superusergroups);
    $groups = $modx->user->getUserGroups();
    
    $has_permission = false;
    $accepted = false; //see below
    
    foreach ($superusergroups as $group) {
        if (in_array($group, $groups)) {
            $has_permission = true; //user is in one of the superusergroups
    		 $modx->log(modX::LOG_LEVEL_ERROR, 'Is in');
        }
    	else
    	{
    		$modx->log(modX::LOG_LEVEL_ERROR, 'Is not in');
    	}
    }
    
    
    $resources = array();
    if ($parentObj = $modx->getObject('modResource', $parent)) 
    {
     $c = $modx->newQuery('modResource');
     if ($resArray = $parentObj->getMany('Children', $c)) 
     {
        foreach ($resArray as $res) 
    	{
           // if ($res instanceof modResource) 
    	//	{
                if ($res->get('description') == 'eingeschraenkt') 
    			{
                    if ($has_permission) 
    				{
                        $accepted = true;
                    }
                } else 
    			{
                    $accepted = true;
    				$modx->log(modX::LOG_LEVEL_ERROR, 'accepted');
                }
        //    }
    
            if ($accepted) 
    			{
                $resources[] = $res->get('pagetitle') . '==' .$res->get('id');
            	}
            $accepted = false; //reset accepted to false
          }
    }
    
    }
    $out = implode("||", $resources);
    
    return $out;
    
      • 43592
      • 97 Posts
      Dear Forum Members, Dear Bruno,

      I've advanced my script a little bit by granulating the user group rights in addition to the ressource description fields.
      The script works in general but I have the problem to check the definite group of the user

      if ($group[0] != "") - line 22. So the clause below doesnt filter out the entries correctly

      Any ideas how to get the definite User group name? Thanks in advance!

      <?php
      //Spezielle Gruppe Fachtrainer
      $superusergroups = $modx->getOption('superusergroups', $scriptProperties,'8','9','11');
      
      //Kursinformationen von Parent Kursinformationen
      $parent = $modx->getOption('parent', $scriptProperties, 47);
      
      $superusergroups = explode(',', $superusergroups);
      
      $groups = $modx->user->getUserGroups();
      
      $has_permission = false;
      $accepted = false; //see below
      
      //is user is in one of the superusergroups
      foreach ($superusergroups as $group) {
          if (in_array($group, $groups)) {
      
      	      $has_permission = true; //user is in one of the superusergroups
      
      
      			//NEW: check which superuser group
      			if ($group[0] != "")
      			{
      			$gruppe = "1";
      			}
      			else if ($group[1] != "")
      			{
      			$gruppe = "2";
      			}
      			else
      			{
      			$gruppe = "3";
      			}
      
      
       $modx->log(modX::LOG_LEVEL_ERROR, $gruppe);
       $modx->trigger_error($gruppe, E_USER_WARNING);
      	}
      	
      }
      
      //Überprüfen welche Gruppe
      
      
      $resources = array();
      if ($parentObj = $modx->getObject('modResource', $parent)) 
      {
       $c = $modx->newQuery('modResource');
       if ($resArray = $parentObj->getMany('Children', $c)) 
       {
          foreach ($resArray as $res) 
      	{
             // if ($res instanceof modResource) 
      	//	{
                  if ($res->get('description') == 'Impuls Lehrtrainer 2') 
      			{
                      if ($has_permission AND $gruppe > "2")
      				{
                          $accepted = true;
                      }
                  } 
      
      			else if ($res->get('description') == 'Impuls Lehrtrainer') 
      			{
                      if ($has_permission AND $gruppe > "1")
      				{
                          $accepted = true;
                      }
                  } 
      
      				else if ($res->get('description') == 'Fachtrainer') 
      			{
                      if ($has_permission AND $gruppe >= "1")
      				{
                          $accepted = true;
                      }
                  } 
      
      			else 
      			{
                      $accepted = true;
      
                  }
          //    }
      
              if ($accepted) 
      			{
                 // $resources[] = $res->get('pagetitle') . '==' .$res->get('id');
      		   $resources[] = $res->get('pagetitle') . '==' .$res->get('pagetitle');
              	}
              $accepted = false; //reset accepted to false
            }
      }
      
      }
      $out = implode("||", $resources);
      
      return $out;
      
        • 43592
        • 97 Posts
        So with help on "slack" this is the solution:

        <?php
        //Spezielle Gruppe Fachtrainer
        $superusergroups = $modx->getOption('superusergroups', $scriptProperties,'8,9,11');
        
        //Kursinformationen von Parent Kursinformationen
        $parent = $modx->getOption('parent', $scriptProperties, 47);
        
        $superusergroups = explode(',', $superusergroups);
        
        $groups = $modx->user->getUserGroups();
        
        $has_permission = false;
        $accepted = false; //see below
        
        //Ist der User überhaupt in der Superusergroup?
        foreach ($superusergroups as $group) {
            if (in_array($group, $groups)) {
        
        	      $has_permission = true; //user is in one of the superusergroups
        
                  $gruppe = $group;
        
        	}
        	
        }
        //$modx->log(modX::LOG_LEVEL_ERROR, $gruppe);
        //Überprüfen welche Gruppe
        
        
        $resources = array();
        if ($parentObj = $modx->getObject('modResource', $parent)) 
        {
         $c = $modx->newQuery('modResource');
         if ($resArray = $parentObj->getMany('Children', $c)) 
         {
            foreach ($resArray as $res) 
        	{
        
               // if ($res instanceof modResource) 
        	//	{
                    if ($res->get('description') == 'Impuls Lehrtrainer 2') 
        			{
                        if ($has_permission AND $gruppe > "10")
        				{
                            $accepted = true;
                        }
                    } 
        
        			else if ($res->get('description') == 'Impuls Lehrtrainer') 
        			{
                        if ($has_permission AND $gruppe > "8")
        				{
                            $accepted = true;
                        }
                    } 
        
        				else if ($res->get('description') == 'Fachtrainer') 
        			{
                        if ($has_permission AND $gruppe >= "8")
        				{
                            $accepted = true;
                        }
                    } 
        
        			else 
        			{
                        $accepted = true;
        
                    }
            //    }
        
                if ($accepted) 
        			{
                   // $resources[] = $res->get('pagetitle') . '==' .$res->get('id');
        		   $resources[] = $res->get('pagetitle') . '==' .$res->get('pagetitle');
                	}
                $accepted = false; //reset accepted to false
              }
        }
        
        }
        $out = implode("||", $resources);
        
        return $out;