Bypassing tenancy for system users

We have set up tenancy between two objects, in this case between “products” and “manufacturer”. Each admin user that is assigned a manufacturer must have access to his products only when accessing the data manager list view. Works like a charm.

However, we’d like the system users to be able to see all products. I had hoped that using the getAdditionalQueryStringForBuildAjaxListingLink() customization to add bypassTenants=manufacturer to the query string for the ajax call would do the trick, but, apparently it does not.

It seems that DataManger.getObjectRecordsForAjaxDataTables() does not expect bypassTenants as an argument and thus doesn’t pass it on to the internal method that is called subsequently.

So how can we bypass the tenancy in this case, please?

Thank you!

1 Like

There are two parts to the solution here - you’ve implemented the first.

getAdditionalQueryStringForBuildAjaxListingLink() generates the Ajax link used to populate the datatable. This might be something related to the context in which the datatable exists. For example, we list event attendees in the context of a specific event, so we pass through the eventId in the Ajax query string.

You then need to do something with this extra information when it is received, which you would do using the preFetchRecordsForGridListing() customization:
https://docs.preside.org/devguides/datamanager/customization/preFetchRecordsForGridListing.html

This receives as its args the selectData args which will be used to perform the query, which you can then modify. In our example, we append a filter to args.extraFilters to filter the results by the received rc.eventId. In your case, you would set args.bypassTenants = "manufacturer".

In addition, as your logic only depends on who the logged-in admin user is, you shouldn’t actually need to modify the Ajax link - simply detect a sysadmin user in the preFetchRecordsForGridListing() and add the tenancy bypass if required. (Indeed, depending on the sensitivity of the data, you’d want to avoid putting it just in the link, as this could be modified to return all the data. At the very least, you’d want to check the permissions again in preFetchRecordsForGridListing()).

2 Likes

Thanks @seb! Don’t know why prefetchRecordsForGridListing() never occurred to me. Just putting

if ( loginService.isSystemUser() ) {
    args.bypassTenants=["lieferant"]
}

works perfectly.

Thanks again!

1 Like

Additional question on tenancy and editing records: Following the steps above a tenant get a list of his records, while a sysadmin gets a list with all records.

If I now want to edit a record as a sysadmin the edit record form does not have the field for the tenant. So I can’t fix/change a record and change the tenant.

Is there a way to make the field available in the edit form? Like a preEditRecord interceptor?

hint: The object currently does not have an edit form definition. I use the preside generated form definition.