Page 1 of 1

UDT to get custom content based on extra content blocks

Posted: Fri Jan 16, 2009 4:59 pm
by robbedoes
Hi,

Today I was asked if it is possible to create pages only accessible for some front end users. I said yes because you can.


But I don't want to fix the values for the groups and users in the page content or in a template so this is what I did.

Maybe someone can tell me if this is smart or if there is another way to get it working.

First of all I created a page template for restricted access. Template snippet:

{content assign=pagecontent}
{content block="users" assign="theusers" wysiwyg="false" oneline="true"}
{content block="groups" assign="thegroups" wysiwyg="false" oneline="true"}

In stead of {content} I addes a call to a UDT:

{restricted_to thegroups ="$thegroups" theusers ="$theusers" thepagecontent="$pagecontent"}

Then I wrote a UDT (not complete yet):

$retstr = "";
$groups = FALSE;
$users = FALSE;
if((!isset($params['thegroups']) || empty($params['thegroups'])) && (!isset($params['theusers']) || empty($params['theusers']))){
    echo("Enter at least 1 user or group!");
}else{
   if( isset($params['thegroups']) && !empty($params['thegroups']) ) {
      $groups = explode(';',trim($params['thegroups']));
      $result = count($groups);
      if($result == 0){
          $groups = FALSE;
      }
   }

   if( isset($params['theusers']) && !empty($params['theusers']) ) {
      $users = explode(';',trim($params['theusers']));
      $result = count($users);
      if($result == 0){
          $users = FALSE;
      }
   }
}

global $gCms;
$feusers =& $gCms->modules['FrontEndUsers']['object'];
$userid = $feusers->LoggedInId();

if($userid){
$username = $feusers->LoggedInName();
echo("okay, we we got a user ".$username."");
if($users){
        if(in_array($username, $users)){
echo("hi ". $username."");
echo $params['thepagecontent'];
    }else{
    echo("hi ". $username."");
echo("Not for you");
    }
   
    }else{
    echo("Hmmm, we have got a user ".$username." but the page doesn't provide users");
    }
}else{
    echo("Members only!!!");
    echo("Login and try again");
}
echo $retstr;


Now I can create pages and select the right template to get the "fields" for users and groups. After entering the username(s) the page is automatically restricted.


I will add the grouppart later on, first I want to know if this is a workable solution...


Cheers,
-- Rob

Re: UDT to get custom content based on extra content blocks

Posted: Fri Jan 16, 2009 5:02 pm
by calguy1000
There are much easier way to do this.... no UDT required
using the extra1 property (or extra2 or extra3).... and CustomContent.

Re: UDT to get custom content based on extra content blocks

Posted: Fri Jan 16, 2009 5:09 pm
by robbedoes
calguy1000 wrote: There are much easier way to do this.... no UDT required
using the extra1 property (or extra2 or extra3).... and CustomContent.
But then you have to add that curly code to your content which will be messed up using tiny...

And the users do have to recal the fields while "users" and "groups" are self explaining.

Ahhhhhh.

{if $ccuser->loggedin() && $ccuser->memberof('members')} in a template.

$ccuser->memberof(XXX) and then pass the value of extrafield to XXX

Right?

Re: UDT to get custom content based on extra content blocks

Posted: Fri Jan 16, 2009 5:53 pm
by calguy1000
if you had for example:

group1,group2,group3  in the extra1 field of a page

in your template you could do something like:

Code: Select all

{capture assign='extra1'}{$content_obj->GetPropertyValue('extra1')}{/capture}
{if empty($extra1)}
   {* this page is public *}
{elseif $ccuser->memberof($extra1)}
   {* this page is private and the user is allowed to see the content *}
{else}
   {* this page is private, and the user is not allowed to see its content *}
{/if}
or....

Code: Select all

{capture assign='extra1'}{$content_obj->GetPropertyValue('extra1')}{/capture}
{if empty($extra1) or $ccuser->memberof($extra1)}
   {* this page is public or this logged in user is allowed to see the content *}
{else}
   {* this page is private, and the user is not allowed to see its content *}
{/if}
You don't need to test for $ccuser->loggedin()..... that's automatic with $ccuser->memberof()

the syntax may be a bit off.... but it's close.

Re: UDT to get custom content based on extra content blocks

Posted: Sat Jan 17, 2009 9:06 am
by robbedoes
Thanks for the code example. I didn't know Smarty's capture.

I still don't like the extra field because the backend user has to remember what field to use.
But I can just replace it with a custom template block of course:-)

It would be very nice if future versions of cmsms offer a kind of multy select for this kind of restriction.

Thanks again. I'm going to try it. 

Re: UDT to get custom content based on extra content blocks

Posted: Sat Jan 17, 2009 2:42 pm
by duclet
You can also use a syntax highlighter to edit your template where all those Smarty tags are in. The user can just use TinyMCE to edit the page content.

Re: UDT to get custom content based on extra content blocks

Posted: Tue Jan 20, 2009 4:23 pm
by robbedoes
duclet wrote: You can also use a syntax highlighter to edit your template where all those Smarty tags are in. The user can just use TinyMCE to edit the page content.
Yes I've added the functionality to a template and the rules (group) to an extra field. Works fine.

Re: UDT to get custom content based on extra content blocks

Posted: Sun Feb 15, 2009 4:06 pm
by zapperlot
Hmm I like Calguy's solution. But:
$content_obj->GetPropertyValue('extra1') 

this causes a PHP - Error: PHP Fatal error:  Call to a member function GetPropertyValue() on a non-object in ...

I ve replaced {content} - tag in my template with this:

{capture assign='extra1'}{$content_obj->GetPropertyValue('extra1')}{/capture}
{if empty($extra1) or $ccuser->memberof($extra1)}
  {content}
  {* this page is public or this logged in user is allowed to see the content *}
{else}
  you have no access
  {* this page is private, and the user is not allowed to see its content *}
{/if}

I think I don't have the $content_obj - object . I am using cmsms 1.41

Any hints are welcome.
Greetings from Germany

Re: UDT to get custom content based on extra content blocks

Posted: Sun Feb 15, 2009 4:36 pm
by calguy1000
Upgrade
1.4.1 is no longer supported.