Setting The Permissions For Screens Using LightSwitch Visual Studio 2012

Introduction

In this article I describe how to create permission using LightSwitch in Visual Studio 2012.

Here, we learn how to define a permission for our application. The permission can be assigned to a user or a group of users. At run time, the permission can be used to enable or disable screens when the application loads.

Permission in LightSwitch

We can define permission for:

  • Screens
  • Commands
  • Data Entries
  • Queries

Test your code by running the application as both a user who has the permission and as a user who does not have the permission.

img permi.jpg

1. Creating a permission in LightSwitch

Step 1

Open the Solution Explorer in LightSwitch Visual Studio 2012.

Image 1

solexp.jpg

Step 2

In the Solution Explorer double-click or right-click and select "Properties".

Image 2

propopen.jpg

Step 3

The Application Designer will appear.

Image 3

appdesign.jpg

Step 4

Choose the "Access control" option in the Application Designer.

Image 4

actrl.jpg

Step 5

Select the type of authentication to use and in this use form authentication.

Image 5

use.jpg

Step 6

In the "Define permissions or select permissions to use for debugging" node click on "<Add New Permission>" then type "Can_View_Employee" under "Name".

Image 6

name.jpg

Step 7

Similarily, under the "Display Name" column type "View Employee".

Image 7

Dname.jpg

Step 8

And in the same manner under the Description column type "Provide access to the employee screen".

Image 8

no permission.jpg

Step 9

Under the "Grant For Debug" column mark the checkbox as checked then the table will appear or if we mark the checkbox as unchecked then the table will not appear on the screen.

Image 9

permission.jpg

2. Writing code to set permissions for the screen

Step 1

In the Solution Explorer we will "Add Screen".

Image 1

screen.jpg

Step 2

"Add New Screen" will appear.

Image 2

visscreen.jpg

Step 3

Now we will select a screen template as "New Data Screen" and in the screen data we will select "Emp1Tables" and click "OK".

Image 3

new screen.jpg

Step 4

The Screen Designer will appear for the Screen.

Image 4

writecode.jpg

Step 5

In the "Write Code List" choose the "CreateNewEmp1Tables_CanRun" method.

Image 5

writecode.jpg

Step 6

In the CreateNewEmp1Tables_CanRun method write the following code:

using System;

using System.Linq;

using System.IO;

using System.IO.IsolatedStorage;

using System.Collections.Generic;

using Microsoft.LightSwitch;

using Microsoft.LightSwitch.Framework.Client;

using Microsoft.LightSwitch.Presentation;

using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication

{

    public partial class Application

    {

        partial void CreateNewEmpTable_CanRun(ref bool result)

        {

            if (Current.User.HasPermission(Permissions.Can_View_Employee))

            {

                result = true;

            }

            else

            {

                result = false;

            }

        }

    }

}

Output

We can grant permission by marking the checkbox as checked in the "Grant for debug" column. It allows the screen to be displayed as the current user that has been granted permission to view it.

Image 1

tabblle.jpg

If we do not mark the checkbox in the Grant for debug column then it does not allow the screen to be displayed as the current user that has not been granted permission to view it.

Image 2

notable.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all