Restricting Access to Screens

In this lesson, you will learn how to apply a permission to restrict access to a screen.

Restricting Access

In the previous lesson, you created a View Products permission. This permission is an object that you can reference in the code that you write to check permissions for a screen. If the permission is assigned to the current user, the menu item for the screen is displayed at run time.

To use a permission

  1. In Solution Explorer, double-click the Properties node.

    The Application Designer appears.

  2. In the Application Designer, click the Screen Navigation tab.

  3. Right-click the Products node and then click Edit CurrentProductList_CanRun Code.

    The Code Editor appears and displays the CurrentProductList_CanRun method.

  4. In the Code Editor, type the following code in the CurrentProductList_CanRun method:

    If Me.User.HasPermission(Permissions.Can_View_Products) Then
        result =  True
    Else
        result = False
    End If
    
    if (this.User.HasPermission(Permissions.Can_View_Products)) 
    {
        result = true;
    } 
    else 
    {
        result = false;
    }
    

    This code is evaluated every time the application starts.

Closer Look

In this lesson, you learned how to apply a permission to restrict access to a screen. Every screen has a CanRun method that can be used to check for permissions. In this case, the code runs the HasPermission method, checking to see whether the Can_View_Products permission is assigned to the current user. If it is, the method returns a result of True; if not, the method returns a result of False. When the application loads, it uses the result to determine whether the Products menu item should be shown.

This is just one example of the code that you could write for the CanRun method. You could also write code to check for multiple conditions, or check the state of other objects in making a determination.

Next Steps

In the next lesson, you will learn how to use debug permissions to test code.

Next lesson: Using Debug Permissions to Test Code

See Also

Tasks

LightSwitch Authentication and Authorization

Other Resources

Enabling Authorization and Creating Permissions

Managing Settings in LightSwitch