locked
Using Security Roles within a LightSwitch filter query RRS feed

  • Question

  • Hi,

    I have an application that I would like to use Security Roles to filter data for users.  I've managed to get this to work for a single role, but I can't seem to make it work properly if a user has multiple roles (eg. JobTypeSydney and JobTypeMelbourne).

    Here is the code below:

            Private Sub Jobs_Filter(ByRef filter As System.Linq.Expressions.Expression(Of System.Func(Of Job, Boolean)))
    
                If Not Me.Application.User.HasPermission(Permissions.SecurityAdministration) Then
    
                    If Me.Application.User.HasPermission(Permissions.JobTypeSydney) Then
                        filter = Function(p) p.JobType.JobTypeId.Equals(2)
                    End If
    
                    If Me.Application.User.HasPermission(Permissions.JobTypeMelb) Then
                        filter = Function(p) p.JobType.JobTypeId.Equals(3)
                    End If
    
                    If Me.Application.User.HasPermission(Permissions.JobTypeInterstate) Then
                        filter = Function(p) p.JobType.JobTypeId.Equals(1)
                    End If
    
                End If
    
            End Sub

    Thanks in advance!

    David.

    Thursday, February 19, 2015 12:46 AM

Answers

  • filter = Function(p) (p.JobType.JobTypeId = 1 Or p.JobType.JobTypeId = 3)


    You should be able to add as many Or conditions to the function as you need.

    • Marked as answer by darcher Friday, February 20, 2015 12:45 AM
    Thursday, February 19, 2015 11:16 PM

All replies

  • You can use Any or All operators to apply your filter.

    string[] perms = { "Microsoft.LightSwitch.Security:SecurityAdministration", "LightSwitchApplication:TestPermsission1" };

    // if the user does not have both permissions apply filter if (!perms.All(x => this.Application.User.HasPermission(x))) { // your filter here. }

    // if the user has any of the permissions apply filter
                if (perms.Any(x => this.Application.User.HasPermission(x)))
                {
                    // your filter here.
                }





    • Edited by Hessc Thursday, February 19, 2015 5:36 AM
    Thursday, February 19, 2015 5:31 AM
  • How would this work for a user who had multiple permisions (each permission has a different filter).

    For example, if a user has  JobTypeSydney and JobTypeMelbourne, then we would filter for JobTypeId=2 or JobTypeID=3

    I wasn't sure how to combine multiple filters for multiple permissions.

    Does this make sense?

    Thanks for your help!

    Dave

    Thursday, February 19, 2015 7:00 AM
  • filter = Function(p) (p.JobType.JobTypeId = 1 Or p.JobType.JobTypeId = 3)


    You should be able to add as many Or conditions to the function as you need.

    • Marked as answer by darcher Friday, February 20, 2015 12:45 AM
    Thursday, February 19, 2015 11:16 PM
  • That's what I needed.  Thanks!
    Friday, February 20, 2015 12:45 AM