C
C
ChertyakaNUB2020-10-18 14:59:17
Laravel
ChertyakaNUB, 2020-10-18 14:59:17

Roles and permissions, how to make permissions for entries?

People, hello everyone!

It costs the laravel-permissions package. Roles are given to users (admin, manager) and some rights (edit, view, delete, create).

The package works great, you can set the display of content for certain roles and permissions in the templates.

But how to make it so that users see only those records retrieved from the database to which they have permissions?

Suppose the template displays a table of data from the database, this template with the table is visible to all users, but how to do this or what to specify in the template / service so that each user sees his records in the table, according to the rights?

For example, the admin created 6 records in the database, and created one permission ("record-one") for these types of records (3 records have a field in the database: "one", the other three: "two"), then awarded this permission to one manager so that when the template is shown to this manager, only records that have a field with a value of "one" are shown.

It turns out that you need to find out if the user has rights with the slug "record-", if he has, then take out all the available rights into an array, delete the text "record-" from each element, this will get all the values ​​\u200b\u200bof the rights to the record, and then search the database by values ​​from the array and display them in the template.
Is it right to do so?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
ChertyakaNUB, 2020-10-18
@ChertyakaNUB

In general, as he said, he did it, everything works!

...
$perms = auth()->user()->permissions;
            $records= array();
            foreach($perms as $perm){
                if (strpos($perm->slug, 'record-') !== false) {
                    $records[] = str_replace('record-','',$perm->slug);
                }
            }

            $table = Record::query();
            $table->whereIn('name_record', $records);
            return Datatables::eloquent($table)
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question