Difference between revisions of "PHP Extensions"
From stoney cloud
[unchecked revision] | [unchecked revision] |
(Created page with "Several extensions to the existing PHP LDAP API have been developed to make it more suitable to our needs. == PHP Assertion Control == To be able to safely change values, th...") |
|||
Line 24: | Line 24: | ||
$control = ldap_control_assertion($link, $assertion_string); | $control = ldap_control_assertion($link, $assertion_string); | ||
− | // the following fails if the description has been changed | + | // the following fails if the description has been changed in the meantime |
ldap_modify($link, "dc=my-domain,dc=com", $entry, $control); | ldap_modify($link, "dc=my-domain,dc=com", $entry, $control); | ||
</source> | </source> | ||
+ | |||
+ | This can be used to safely increment variables, reserve IP addresses without race conditions. | ||
+ | |||
+ | Given a field in the LDAP which contains the the next free id or IP address: | ||
+ | |||
+ | * get the current value | ||
+ | * increment the value | ||
+ | * set the new value with the assert that the value must match the previously fetched one | ||
+ | * if it succeeds you can safely use the previously set value, if not: repeat the procedure | ||
[[Category:Documentation]] | [[Category:Documentation]] | ||
[[Category:PHP]] | [[Category:PHP]] | ||
[[Category:LDAP]] | [[Category:LDAP]] |
Revision as of 19:38, 27 April 2014
Several extensions to the existing PHP LDAP API have been developed to make it more suitable to our needs.
PHP Assertion Control
To be able to safely change values, the Assertion Control is needed which results in a Compare-and-Set functionality.
Example (taken from ext/ldap/tests/ldap_control_assertion_basic.phpt
):
$link = ldap_connect_and_bind($host, $port, $user, $passwd, $protocol_version); $entry = array( "objectClass" => array( "top", "dcObject", "organization"), "dc" => "stoney-cloud", "o" => "stoney-cloud", "description" => "stoney cloud root object", ); ldap_modify($link, "dc=stoney-cloud,dc=org", $entry); $assertion_string = "(description=stoney cloud root object)"; $control = ldap_control_assertion($link, $assertion_string); // the following fails if the description has been changed in the meantime ldap_modify($link, "dc=my-domain,dc=com", $entry, $control);
This can be used to safely increment variables, reserve IP addresses without race conditions.
Given a field in the LDAP which contains the the next free id or IP address:
- get the current value
- increment the value
- set the new value with the assert that the value must match the previously fetched one
- if it succeeds you can safely use the previously set value, if not: repeat the procedure