eZ api based code sample to add a policy to a role

Author Message

Xavier Langlois

Thursday 18 June 2009 9:56:12 am

Hello nice people ;)

I'm doing an extension wich is able to create a new site by creating a new siteaccess and a new design in a new extension. Everything's fine except this: after my new site generation, when I call my new site URL, I have the "permission denied" error until I add this policy to the anonymous role:

Module: user
Fonction: login
Limitation SiteAccess(<my_new_siteaccess>)

I would like to do this with my php code.

You might think I'm lazy, 'cause everything should be somewhere near kernel/role/edit.php and policyedit.php, but time is precious and if somebody can help me before I find out by myself, that would be great!

Thank you for you're interest.
Hope to hear from you very soon.
Xavier

--
There were these two cows, chatting over the fence between their fields.
The first cow said, "I tell you, this mad-cow-disease is really pretty scary. Don't you think ?"
The other cow replies, "Hell, I ain't worried, I'm a duck !"

Carlos Revillo

Thursday 18 June 2009 2:28:02 pm

Hi. maybe you can try this

$role  = eZRole::fetchByName( 'Anonymous' );
$roleID = $role->ID;
$policy = eZPolicy::createNew( $roleID, array( 'ModuleName'=> "user",
                                               'FunctionName' => "login" ) );
$policyLimitation = eZPolicyLimitation::createNew( $policy->attribute('id'), "SiteAccess", "user", "login" );
$value = eZSys::ezcrc32( "site" ); // your siteaccess here
eZPolicyLimitationValue::createNew( $policyLimitation->attribute( 'id' ), $value );
eZUser::cleanupCache();

of course, you will need to be logged as a user who has permissions to edit roles.
hope it helps.

Xavier Langlois

Friday 19 June 2009 1:48:03 am

Hi Carlos

Thank you a lot ! that really speed my work:

my final function if somebody wants it :
it takes care of
- the case : the user login policy doesn't already exists
- the case : the user login policy already exists with others limitation so we want to add ours
- the case : the user login policy already exists but with no limitations so you don't need to add yours cause that will stop the permissions in the others siteaccess

/*
 * Add a policy : user / login / siteaccess(<your_siteaccess>) to the role you want
 * eg: to add user / login / siteaccess('fr') to the anonymous role you can do
 * addUserLoginSiteAccess('fr', 'Anonymous');
 * or
 * addUserLoginSiteAccess('fr', false, 1); //1 is the ID of the anonymous role
 * 
 */
function addUserLoginSiteAccess($siteAccessName, $roleName = false, $roleID = false)
{
	$res = $oRole = false;
	
	$siteAccessName = trim($siteAccessName);
	
	if($roleID)
	{
		$oRole = eZRole::fetch( $roleID );
	}
	else if($roleName)
	{
		$oRole = eZRole::fetchByName( $roleName );
	}
	
	if($oRole && !empty($siteAccessName))
	{
		$sSiteAccessLimitationValue = eZSys::ezcrc32( $siteAccessName );
		$rolePolicyList = $oRole->attribute( 'policies' );
		$oPolicy = $oPolicyLimitation = $hasAlready = false;
		if(!empty($rolePolicyList))
		{
			foreach($rolePolicyList as $policy)
			{
				if($policy->attribute('module_name')=='user' && $policy->attribute('function_name')=='login' )
				{
					$oPolicy = $policy;//echo '<pre>$oPolicy = '.print_r($oPolicy,true).'</pre>';
					break;
				}
			}
		}
		
		if($oPolicy)
		{
			$policyLimitationList = $oPolicy->limitationList();
			if(empty($policyLimitationList))
			{
				$hasAlready = true;
			}
			else
			{
				foreach($policyLimitationList as $limitation)
				{	
					if($limitation->attribute('identifier')=='SiteAccess')
					{
						$oPolicyLimitation = $limitation;//echo '<pre>$oPolicyLimitation = '.print_r($oPolicyLimitation,true).'</pre>';
						
						$valueList = $oPolicyLimitation->valueList();//echo '<pre>$valueList = '.print_r($valueList,true).'</pre>';
						
						foreach($valueList as $value)
						{
							if($value->attribute('value') == $sSiteAccessLimitationValue)
							{
								$hasAlready = true;
								break;
							}
						}
						
						break;
					}
				}
			}
		}
		
		if(!$hasAlready)
		{
			if(!$oPolicy) $oPolicy = eZPolicy::createNew( $oRole->ID , array( 'ModuleName'=> "user",'FunctionName' => "login" ) );
			if(!$oPolicyLimitation) $oPolicyLimitation = eZPolicyLimitation::createNew( $oPolicy->attribute('id'), "SiteAccess", "user", "login" );
			eZPolicyLimitationValue::createNew( $oPolicyLimitation->attribute( 'id' ), $sSiteAccessLimitationValue );
			eZUser::cleanupCache();
		}
		
		$res = true;
	}
	return $res;
}

Thank you again
Bye
Xavier

--
There were these two cows, chatting over the fence between their fields.
The first cow said, "I tell you, this mad-cow-disease is really pretty scary. Don't you think ?"
The other cow replies, "Hell, I ain't worried, I'm a duck !"

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 16:26:49
Script start
Timing: Jan 18 2025 16:26:49
Module start 'layout'
Timing: Jan 18 2025 16:26:49
Module start 'content'
Timing: Jan 18 2025 16:26:50
Module end 'content'
Timing: Jan 18 2025 16:26:50
Script end

Main resources:

Total runtime0.7087 sec
Peak memory usage4,096.0000 KB
Database Queries57

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0048 588.0469152.6406
Module start 'layout' 0.00480.0026 740.687539.4922
Module start 'content' 0.00740.6997 780.1797554.2891
Module end 'content' 0.70710.0016 1,334.468816.1250
Script end 0.7086  1,350.5938 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.4341160.0002
Check MTime0.00130.1790160.0001
Mysql Total
Database connection0.00100.138410.0010
Mysqli_queries0.659993.1108570.0116
Looping result0.00060.0784550.0000
Template Total0.680296.020.3401
Template load0.00230.328520.0012
Template processing0.677895.646820.3389
Template load and register function0.00010.012110.0001
states
state_id_array0.00070.100310.0007
state_identifier_array0.00110.160620.0006
Override
Cache load0.00190.2719320.0001
Sytem overhead
Fetch class attribute can translate value0.00040.060420.0002
Fetch class attribute name0.00100.147940.0003
XML
Image XML parsing0.00050.072920.0003
class_abstraction
Instantiating content class attribute0.00000.001750.0000
General
dbfile0.00090.1214160.0001
String conversion0.00000.000740.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
8content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
5content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 22
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs