A
A
Alexey Sklyarov2021-01-20 21:37:55
Laravel
Alexey Sklyarov, 2021-01-20 21:37:55

How can I implement user settings that depend on his role?

The essence of the task: I'm trying to implement user settings, depending on his role (administrator, editor, user). At the moment, these settings are exclusively related to notifications. For example: An

administrator can have the following settings:

Administrator
+----+---------------------+----------------------------------+
| id | name                | description                      |
+----+---------------------+----------------------------------+
| 1  | notify_new_comments | Уведомлять о новых комментариях  |
+----+---------------------+----------------------------------+
| 2  | notify_new_users    | Уведомлять о новых пользователях |
+----+---------------------+----------------------------------+

Editor:
+----+--------------------+---------------------------------+
| id | name               | description                     |
+----+--------------------+---------------------------------+
| 1  | notify_new_posts   | Уведомлять о новых записях      |
+----+--------------------+---------------------------------+
| 2  | notify_new_comment | Уведомлять о новых комментариях |
+----+--------------------+---------------------------------+

User:
+----+----------------------------+--------------------------------------+
| id | name                       | description                          |
+----+----------------------------+--------------------------------------+
| 1  | notify_new_replies         | Уведомлять об ответах на комментарии |
+----+----------------------------+--------------------------------------+
| 2  | notify_new_comment_in_post | Уведомлять об ответах в моих постах  |
+----+----------------------------+--------------------------------------+


Examples of settings attached in the form of a table for greater clarity. I plan to store these settings in the database json field, there is already a method that allows you to take any setting and perform any actions with it.

But I can't decide how to properly store such presets?
Option 1: just store in the config, under each role, when creating a new user, get them like this: . That is, when creating a new user, I take the settings corresponding to his role from the config and save it in the database. But in this case, it is not clear what to do if you need to add a new setting, say admins, you will have to write a script that will add this setting to the profile for all admins. Option 2:config('user_presettings.admin);

store in the database along with the role_id, and when registering, take all the settings for the specified category, and save them to the user in the database. In this case, it will be possible to create similar settings from the admin panel.

Which option do you think is more correct, perhaps there are better implementation examples?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jazzus, 2021-01-21
@jazzus

I take from the config / default model attributes and so on. But I have programming projects) Ie. there is nothing to do in the admin. If it is important to do everything in the project from the admin panel, the choice is obvious. Those. the answer is to focus on the needs of the project

D
Denis, 2021-01-21
@Gromfer

xs, I would probably make a separate table and load everything depending on the role during authorization. And you can put in the cache

D
Dmitry, 2021-02-12
@dlnsk

Laravel. Hierarchical RBAC for the smallest ones
I've never had a reason to edit a role in the admin panel... Let a stupid user edit a role? I do not think that in projects of the scale of "Laravel" it is worth bothering with this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question