Implementing Access Level Filters in Lightswitch
Implementing Access Level filters are one of the most common features that Cloud-based web apps need today.
A simple scenario could be a Cloud-based app that could cater to multiple companies, where each company could have many Departments & each of the departments in turn will have many users.
The Problem:
When you create a new data screen for an Employee, you will see that Lightswitch auto-magically generates an Auto Complete Box for you to select the parent properties.
In this example, when we create a new Employee Data screen, Lightswitch creates an auto-complete box for selecting Department.
To understand this better, give the app a whirl, create a few companies & some departments for all companies.
Now when you hit the "Create New Employee" button, you will notice that the Departments Auto Complete Box shows departments from all companies.
Now, If you really care for your user's data & don't want to mix up the details available under different companies, we need a way to filter the available departments to only show the ones available under the currently selected company.
The Solution:
When it comes to Lightswitch, everything is bound to be simple & so is the solution to this problem.
Here is a step-by-step solution to fix this issue:
1. Create a new query under Departments to filter them by Company.
2. Add this query to the Add New Employee page & hook the selectedCompanyId query parameter to a local Integer variable.
3. Finally, point the Department's Choice property to this query "DepartrmentsByComapny".
Give the application one more run & Voilà!! The departments are now filtered based on the company.
This filter is applied at the server side & thus no need to worry about sniffers too :)
Another way of solving this, if it works for you, is to select the Departments table & select Department_Filter under write code & apply a filter to the root of the query.
partial void Departments_Filter(ref Expression<Func<Department, bool>> filter)
{
filter = e => e.Company.Id == SelectedCompanyId;
}
Following this approach will get your Department filtered by the company throughout the application.