This is going to seem like a lot here, but you don't need to read all of it, I'm just breaking it all into sections for simplicity's sake / to be more clear. This probably isn't a terribly hard problem, but I'm having trouble with it, so there will definately be a star for anyone that can figure this out for me!
DESCRIPTION OF SITUATION / WHAT I NEED TO DO
I have information spread across the 5 tables above. We have several companies that can purchase the use of web applications from us for their customers. Each client company has separate applications they can use (based on what they pay to use). So client One could have 5 applications for their users, client Two could have 7 applications to use, and so on. Now, each of the Client's customers are authorized to use some, but rarely all of the applications. So, Client One could have 10 customers with their 5 applications, and may have 2 customers to each application, or any mixture (i.e. all Ten customers use one application, while only One customer uses another, and Five use another.)
I need to be able to list all applications for the specific client logged in, and the users authorized to use each application for that client. (So if someone from client One is logged in, I need to be able to list all 5 applications for it, and then all 10 users, each under every application they are authorized for. So it may look something like this:
Application One
________________
- User 1
- User 2
- User 3
- User 4
- User 5
Application Two
________________
- User 1
- User 2
- User 6
- User 7
- User 8
- User 9
- User 10
Application Three
________________
- User 1
- User 3
- User 7
- User 10
etc.
DESCRIPTION OF DATA / SETUP
I'm dealing with 5 tables:
1. CP_CLIENT - client company's profile information
2. NAVIGATION - all applications
3. SECURITY_APPLICATION - applications associated with each specific client
4. WEB_SECURITY - lists which client is registered for which application
5. WEB_USER - lists the registered customers/users...all of them.
CP_CLIENT is related to NAVIGATION on CP_CLIENT.site_id = NAVIGATION.site_id
NAVIGATION is related to SECURITY_APPLICATION on NAVIGATION.id = SECURITY_APPLICATION.application_id
SECURITY_APPLICATION is related to WEB_SECURITY on SECURITY_APPLICATION.application_id = WEB_SECURITY.application_id
WEB_SECURITY is related to CP_CLIENT on WEB_SECURITY.client_id = CP_CLIENT.client_id
WEB_SECURITY is related to WEB_USER on WEB_SECURITY.user_id = WEB_USER.user_id
So as you can see, four of the tables have a circle reference from CP_CLIENT to NAVIGATION to SECURITY_APPLICATION to WEB_SECURITY and back to CP_CLIENT again, and the fifth is branched off of WEB_SECURITY.
*******
When logged in, the client id will be known (i.e. client_id = 2). That finds the correct profile in the CP_CLIENT table. Then it needs to pull all records that are on the navigation table that match the site_id of the client in CP_CLIENT table. It needs to further narrow that list down by selecting only applications out of that list that are listed in the SECURITY_APPLICATION table...whatever applications are listed in the SECURITY_APPLICATION table that match records on the NAVIGATION table where the NAVIGATION table site_id field matches the currently logged in client's site_id.
Now that the applications are found, I need to be able to pull which users are authorized for those applications; but, I need to still list the applications, even if no user's are authorized for them (i.e. returns application title, and null value for the user), and only pull those users that are also associated with the currently logged in client.
Then, lastly, I just need to pull the user_name from the WEB_USER table to get the actual user name, rather than just their ID.
*******
What I have so far
Here is the code I've come up with so far; however, it doesn't work exactly right, although it is close. What happens is it will list all users and applications appropriately, including null values for users when none are authorized for it, only, it'll list all users and applications for any client that is logged in that has the same site_id as another.
So for instance, if Client One and Client Two both have a site_id of 3, all users and applications that are associated with a client of site_id 3 will always be listed, and I can't seem to narrow it down to only list those clients that are associated with the currently logged in client.
Here is the code:
Code:
CREATE PROCEDURE sql_getSecurityApplicationAssignments
@strClientID varchar(6)
AS
SET NOCOUNT ON
SELECT DISTINCT NAVIGATION.id, NAVIGATION.title, WEB_SECURITY.user_id, WEB_USER.user_name, CP_CLIENT.site_id
FROM WEB_USER INNER JOIN
WEB_SECURITY ON WEB_USER.user_id = WEB_SECURITY.user_id RIGHT OUTER JOIN
CP_CLIENT INNER JOIN
NAVIGATION ON CP_CLIENT.site_id = NAVIGATION.site_id INNER JOIN
SECURITY_APPLICATION ON NAVIGATION.id = SECURITY_APPLICATION.application_id ON WEB_SECURITY.application_id = NAVIGATION.id
WHERE (CP_CLIENT.client_id = @strClientID)
ORDER BY NAVIGATION.title, WEB_USER.user_name
Once again, I am sorry this is a bit long, but I wanted to make sure I described it very clearly.
-Ovatvvon :-Q