OK. So here's my simplified scenario. We have a system which handles orders for a number of clients. We want staff users to be able to view all orders and we want client user to only be able to view orders which relate to them.
When attempting to view a particular record we make use of the following function in our OrderSecurity class:
Public Function CanViewOrder(order)
If currentUser.MemberOfStaff() Then
CanViewOrder = True
Else
CanViewOrder = (order.ClientId = currentUser.ClientId)
End If
End Function
At points when we want to display a list of orders to a user we can the following function defined in a OrderService class
Public Function GetOrders()
If currentUser.MemberOfStaff() Then
GetOrders = GetAllOrders()
Else
GetOrders = GetAllOrdersForClient(currentUser.ClientId)
End If
End Function
This is OK for the above but doesn't hold up well as the rules get more complicated. Say, for example, we add another user type which represents a less trusted staff member who can only view orders from a sub-set of clients. We'd then have to add logic to the CanViewOrder and GetOrders functions (and potentially in the data access classes) which in my mind violates the DRY principle.
So, my question is: Am I missing a trick here - is there some way I can combine the business logic for permission to view orders in one place which both of these functions can use?
Or am I worrying too much and should just get on and have the logic in two places?
(In this particular application I'm using ASP Classic - don't hate the player, hate the game - but I'd be interested in how you solve this problem in any language)