Filter data by user
Level: Intermediate
Keywords: browse filtering, data filtering, my-tasks
The result: possibility to create customized browses only with data for logged in user, integrate filtering of data in Action / Edit forms
How to filter data by currently logged in user?​
This is a very frequently used functionality almost on every type of project. Following chapters summarize the implementation.
Define user feature type​
First of all, you need to define datamodel (model.xml), where you will store your users/employees/workers etc. Afterwards, in each feature type (e.g. Task), which should contain information about its linked user, you will use featureRef attribute referencing this user.
<ber:container id="ct_user" name="User container" dbName="CT_USER">
<ber:groupContainer refId="gc_workrep"/>
</ber:container>
<ber:featureType id="ft_user" name="User" parentId="ft_5000002" abstract="false">
<ber:description>user</ber:description>
<ber:groupsArray>
<ber:group id="users"/>
</ber:groupsArray>
<ber:container refId="ct_user"/>
<ber:featureAttributeArray>
<ber:attribute id="at_user_id" name="id" dbName="ID" nillable="true">
<ber:dataType>
<ber:string maxLength="10"/>
</ber:dataType>
</ber:attribute>
<ber:attribute id="at_user_name" name="Name" dbName="NAME" nillable="true">
<ber:dataType>
<ber:string maxLength="30"/>
</ber:dataType>
</ber:attribute>
<ber:attribute id="at_user_valid" name="Valid" dbName="AN_VALID" nillable="true">
<ber:dataType>
<ber:decimal precision="1"/>
</ber:dataType>
</ber:attribute>
</ber:featureAttributeArray>
<ber:formArray>
<ber:form id="fmd_user" name="User">
<ber:fieldGroup>
<ber:caption label="" collapsible="expanded"/>
<ber:field refId="at_user_name" readOnly="false" length="200"/>
<ber:field refId="at_user_id" label="Link to SEC_USER" readOnly="false" length="200"/>
<ber:field refId="at_user_valid" readOnly="false" length="200"/>
</ber:fieldGroup>
</ber:form>
<ber:assignedForms>
<ber:assignedForm usage="default" refId="fmd_user"/>
</ber:assignedForms>
</ber:formArray>
</ber:featureType>
Notice the at_user_id attribute, which stores the reference to SEC_USER table, which stores your application users. It is important to link your users/employees/workers with an account for your application.
<ber:featureRefAttribute id="at_task_fr_assignee" name="Assignee" dbName="FR_ASSIGNEE">
<ber:dataType>
<ber:featureRef>
<ber:attribute refId="at_user_name"/>
<ber:featureRefRelationAssoc refId="as_task_assignee"/>
</ber:featureRef>
</ber:dataType>
</ber:featureRefAttribute>
Extend information provided from user-profile​
Now you need to extend the information provided from the user-profile, so it would contain information about your user featureType. This is accomplished by defining searchQuery and entityType in ..gateway\applications\applicationName\metadata\dynamic-client\common\appModules\user-profile.jsonuser-profile.json.
If you are using samo-base package in your project, this user-profile.json file is loaded from there. In this case you will have to create new user-profile.json which will override it.
{
"type" : "component:entity-modules/components/samo-entity-profile-app-module",
"security": {
"loggedIn": true
},
"entityType" : "ft_user",
"profileProperty" : "entityProfile",
"requiredToLoadPage" : true,
"searchQuery":{
"type" : "terms",
"field" : "at_user_id",
"values": ["{user:#id}"]
}
}
Use your user reference for data filtering​
Now you are able to use your created user reference for filtering for example in SAMO Browse, entity count in dashboard widgets, or in Action / Edit forms.
Following browse will show only tasks, which are assigned to currently logged in user. Similar query configuration is also used for filtering entity count in dashboard wisgets.
{
"title" : "My tasks",
"module" :{
"type": "component:entity-modules/browse/samo-browse",
"entitiesGroup": "tasks",
"wideDetail": true,
"security": {
"loggedIn": true
},
"map": {
"extends" : "default"
},
"query": {
"must": { "type": "terms", "field": "at_task_fr_assignee.id", "values": ["{user:#entityProfile.id}"] }
}
}
}
In the following detail used to create new Work Log, the user will be automatically prefilled according to the currently logged in user. Also user will be able to only chose his/her assigned tasks to reference them in new Work Log.
{
"default": {
"sections": [{
"module": {
"type": "component:entity-modules/form/samo-entity-properties-form",
"propertyGroupId": "fmd_workLog_create",
"overrideProperties": {
"at_workLog_fr_user": {
"type" : "entity-modules:entity-codelist",
"searchDefaultValue": {
"type": "matchPhrase",
"field": "at_user_id",
"query": "{user:#id}"
},
"disabled" : true
},
"at_workLog_fr_task" : {
"query": {
"must" : [{
"type" : "terms",
"field" : "at_task_fr_assignee.id",
"values" : ["{user:#entityProfile.id}"]
}]
}
}
}
}
}]
}
}