Check To Write Event
Prerequisite: You must be familiar with the Syntax used in Tutorials and have already created an extension.
- learning:
- Impose data integrity rules
- level:
- Intermediate
- domains:
- PHP, Constrain
- methods:
- DoCheckToWrite
- min version:
- 3.1.0
In the below example we will use a method to detect an incoherence and prevent such object to be saved in database.
-
This method in the Console or Portal, reports errors after submission.
-
And prevents creation and update of incoherent objects done by DataSynchro, REST/JSON and CSV import.
This tutorial is an extract of
the iTop 3.1.0 standard code
Theory
We will subscribe to an event and write a callback
method XxxxxXxxx()
on the object class:
-
This method is invoked just before writing to database.
-
The method add error message(s) if it encounters data incoherence.
-
Errors messages are recorded with $this->AddCheckIssue(),
-
Warnings messages are recorded with $this->AddCheckWarning(),,
-
When returning from this method, if there is at least one error the object is not written to database (creation or update)
-
Error and warning messages are
-
displayed to the user in interactive mode only: Console, Portal, CSV import
-
logged in itop/log/error.log depending on level of tracking for DataSynchro, REST/JSON, CLI
-
Example
Let's imagine that we want to check when modifying the organization of a Person, that if that Person has associated User(s), those User(s) would not be broken by this Organization modification
Subscribing to an event
- itop_design/classes
-
<class id="Person" _delta="define"> <event_listeners> <event_listener id="CheckUsersUpdate"> <event>EVENT_DB_CHECK_TO_WRITE</event> <callback>CheckUsersOnUpdate</callback> <rank>1</rank> </event_listener> </event_listeners>
Check the supported events for a given class by looking in iTop
Datamodel to the Events tab.
Checking the conditions
- Class:Person
-
public function CheckUsersOnUpdate(Combodo\iTop\Service\Events\EventData $oEventData) { // This method can block the Person modification, by adding a Check Issue $aChanges = $this->ListChanges(); // The organization's person was changed if (array_key_exists('org_id', $aChanges)) { // Current User may not be allowed to see User class, so we can't use $this->Get('user_list') $oSearch = new DBObjectSearch('User'); $oSearch->AddCondition('contactid', $this->GetKey(), '='); $oSearch->AllowAllData(); $oUserSet = new DBObjectSet($oSearch); // The person has associated Users if ($oUserSet->Count() > 0) { $oAddon = UserRights::GetModuleInstance(); // For each of the associated User while($oUser = $oUserSet->Fetch()) { // Get its Allowed organization and its Profiles $aOrgs = $oAddon->GetUserOrgs($oUser,'Organization'); // using $oUser->Get('allowed_org_list') does not cascade the organizations! $oSet = $oUser->Get('profile_list'); $aProfiles = $oSet->GetColumnAsArray('profile'); // Get the Profiles names in an array // User has Allowed organizations and is not allowed on the new Organization and has 'Portal user' Profile and is enabled if ((count($aOrgs) > 0) && !in_array($this->Get('org_id'), $aOrgs) && in_array('Portal user',$aProfiles) && ($oUser->Get('status') === 'enabled')) { // Let's block the Person modification, // replace by $this->AddCheckWarning(...) if you don't want to block the modification $this->AddCheckIssue(Dict::Format('Class:Person/Error:ChangingOrgDenied', $this->Get('org_id_friendlyname'))); } } } } }
3_1_0/customization/event_db_check_to_write.txt ยท Last
modified: 2023/08/23 16:14 by 127.0.0.1