FaStie9000
FaStie9000

Reputation: 68

search with different privileges php

There is a catalog of items. Each item is located under categories. For each of the category we can have users with different privileges(admin, expert, user). When we add an item, we select the visibility: (1) - visible for all (2) - visible for only admins and experts of the category this item was posted (3) - visible for all members of the category this item was posted (0) - hidden for all (only the creator can sees).

Let say i have 500 items with the word "Keyboard" in title. Most of them are in different categories. Only 1 item of theses 500 has privacy - 2(visible for experts and admins for the category) and located under category "Logitech". When i as an admin of "Logitech" category type "keyboard" in the search, i should see that the search found 500 items, and show me 10 of them. If just an ordinary user(not admin or expert), types "keyboard" in the search he will see, that the search found 499 items, and show him 10 of that 499 items.

Is to grab 20 items instead of 10 and to check the privacy of each of the item, and then show only 10 items in search results the only possible solution? But what if all of this 20 will be "hidden for the user"?

Thanks in advance for your responses.

Database sketch:

1)items[id, title, category_id, visibility]

2) categories:[id, title]

3) category_user[id, user_id, category_id, role(expert, admin, user)]

4) users[id, name]

Upvotes: 0

Views: 70

Answers (1)

Dennis Hackethal
Dennis Hackethal

Reputation: 14305

Something like this might do the trick:

SELECT i.*,c.*,cu.*u.*
FROM items i,categories c, category_user cu, user u
WHERE i.title LIKE '%$search%'
AND i.category_id=c.id
AND cu.category_id=c.id
AND u.id=cu.user_id
AND cu.role='expert'
AND i.visibility=1

Change cu.role and i.visbility according to userdata.

Upvotes: 1

Related Questions