Modifying relation using a custom event.
In Deepser you can modify relationships using an event, as we will see in the following example.
This can be useful for creating relationship management flows including, for example, an approval phase.
EXAMPLE: MODIFYING A RELATION USING A CUSTOM EVENT
In this example, we are going to create a flow for the management of the relation between users and the Ci class Location.
The objective of this report will be to keep records of which end users can access to the Ci class “Location”.
Suppose that a type of special service is created (Access Management), opening a ticket of this type users can ask to obtain access to a “Location”, once the request is sent, and it will be evaluated by the backend operators and if approved the relationship between the requesting user and the location will be mapped.
CONFIGURING THE RELATION
To create a new relation, we must go to the menu: System -> Relation.
Here we are going to click on the “Add Relation” button.
In the screen that will open, we will configure the “Source Model” field as “DeepCmdb – Ci”.
Now we will set the field “Destination Model” to “DeepAdmin – User”.
In the Source section, we are going to configure the field “Source Id Field” with the value “entity_id”.
In the “Source Label Fields” field and in the “Source Search Field” field, we will enter “Name”.
At this point in the “Destination” section, we will set the”Dest Id Field” field as “user_id”.
Finally, in the field “Dest Search Field” we set “username”.
As a last step, we are going to click on the “Save” or “Apply” button to save the relationship.
CUSTOM FIELD CONFIGURATION ON CIS
To create the custom field, you will need to go to System -> Custom Fields.
Here we will have to choose the model “DeepCmdb – CI”.
Now we will have to go to the item “Fields”, here we are going to click on “Add Field”.
At this point, the following screen will open:
We compile the fields as follows.
In the “Column Name” field, we are going to configure the value “cust_ci_user_relation”.
In the “Column Type” field, we are going to configure the value “Don’t Create DB Column”.
In the “Element Type” field, we are going to configure the value “Relationgrid”.
In the “Label” field, we are going to configure the value “Allowed Users”.
In the “Status” field, we are going to configure the value “Enabled”.
In the “Relation” field, we are going to select the name of the relationship created previously.
At this point, We must click on the “Save” or “Apply” button to save the field.
CUSTOM EVENT CONFIGURATION
As a prerequisite for this step there is the configuration of a type of special service with related form templates on the user portal side, you can find the documentation about it at this link: link.
To create the custom event for the population of the relation, we must go to System -> Custom Fields.
Here we will have to choose the model “DeepService – Operation”.
Now we are going to configure a new custom event by going to the “Events” Tab and then clicking on the “Add Event” button.
Note that in this example we will use the “cust_ci_id” field (check the field names to fit your configuration, in case you do not use the default fields).
At this point, the following screen will open:
Here we are going to fill in the “Name” field with the name we want to give to the event and the “Type” field with the “After Save” value.
At the end of the field “Method” we enter the following code:
$operationType = 13;
$approvedOperationStatus = 8;
/**If this is setted and the type id is Access Managemt**/
if($this && $this->getData('type_id') == $operationType ){
/**Check if the request is approved**/
if($this->getStatus() == $approvedOperationStatus){
/**Load the Relation**/
$realtion = Deep::getModel('deep_relation/relation')->load(5);
/**Get Ci id**/
$ciId = $this->getData('cust_ci_id');
/**Load the requester user**/
$requester = $this->getRequesterUser();
/**If the CIid is set and the requester is not an empty object**/
if($ciId && $requester && $requester->getId()){
/**Adding The requester user in relation with the ci**/
$realtion->addRoles($requester->getId(),[$ciId]);
}
}
}
As a last step, you will need to click on the “Save” or “Apply” button to save the event.