This article describes how to check user permissions from the HTML Client using LightSwitch in Visual Studio 2012.
The following is the procedure for checking User Permission from the HTML Client.
Step 1
Open the Solution Explorer.
Step 2
In the Solution Explorer, right-click on the server and choose "Add Table".
Step 3
The table appears.
Step 4
In the Solution Explorer, right-click or double-click on the property node and choose "open".
Step 5
The Application Designer appears.
Step 6
In the Application Designer, select "Access Control". In Access Control, under "Authentication Type", choose "Use Forms authentication", and define "New Permission".
Step 7
From the Data Designer, in the drop down list of the "Write Code" button, select the "Employee_CanInsert" method and use the following code:
partial void Employees_CanInsert(ref bool result)
{
result = this.Application.User.HasPermission(Permissions.CanAddCustomer);
}
Step 8
Here we will add a query to check the permission for the HTML Client Screen. There are two ways to add a query.
Go to the Solution Explorer and right-click on the table and choose "Add Query".
From the Menu bar of the Table Designer screen choose "Query".
Step 9
The Query designer appears.
In the Query Designer, under the Write Code drop down list, choose the Can_Execute method to check the permission.
Under it perform the following coding:
partial void CanAddEmployee_CanExecute(ref bool result)
{
result = this.Application.User.HasPermission(Permissions.CanAddCustomer);
}
Step 10
Once again go to the Solution Explorer and right-click on the HTML Client Screen and choose "Add Screen".
Step 11
From the Add New Screen dialog box select "Browse Screen" from the Screen Template and provide screen information as shown and click the "OK" button.
Step 12
The "BrowseEmployees" screen designer appears.
Step 13
Under Command Bar we will add two buttons. For that either right-click on Command Bar or click on "Add" to add a button.
The "Add Button" dialog box appears. In that select "write my own method" and provide a method and click the "OK" button. We will do that two times.
Step 14
From the menu bar of the Screen Designer choose "Add Data Item".
The Add Data Item Dialog Box appears on the screen. From the Dialog box, choose the Query option and under Name choose "CanAddCustomer" and click the "OK" button.
Step 15
Choose the control from the screen designer that you want to enable or disable.
Step 16
In the Menu Bar of the Screen Designer choose the "Write Code" option and under the write code option add some code to the created method.
/// <reference path="../GeneratedArtifacts/viewModel.js" />
myapp.BrowseEmployees.created = function (screen) {
// Write code here.
screen.getCanAddEmployee().then(function success() {
screen.findContentItem("Add Employee").isEnabled = true;
}, function error() {
screen.findContentItem("Add Employee").isEnabled = false;
});
};