Skip to content
Advertisement

Roles and permissions in php

I am making a menu, but I want to limit it to only some users with a specific permission can see it.

the query:

SELECT idpermission,userid FROM user_permissions WHERE userid = “U001”

Result: User U001 has 3 permissions stored.

JavaScript

I have 3 tables (users,permissions and user_permissions), in user_permissions I store each permission of each user

JavaScript

when i call the query

JavaScript

but the result that appears to me is 3:

user menu user menu user menu

You should only show me one, making only a comparison if you have such permission shows but shows nothing.

What I want to reach is that the user can have many permissions for different items on the menu, such as view, edit, create, delete, each one is a permission, and each item (user menu, books menu) is another permission.

Advertisement

Answer

Use a pivot technique to gather all permissions for a single user and form a single row to fetch with pdo.

This way all expected columns are declared and you can use simple truthy/falsey conditional checks in your php for any of the permission settings.

So long as the userid exists in the table, you will have a fully populated row to access. This will be clean, direct, efficient, and easy to maintain.

Schema (MySQL v5.7)

JavaScript

Query #1

JavaScript

Result set:

JavaScript

View on DB Fiddle

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement