![]() |
Hey everyone,
I have a small website running with Codeigniter 4 and Shield installed, users have a login system. So one of them shouldn't be able to actually write/change data in the database. My idea was to make a beforeupdate, beforeinsert and beforedelete callback to check for the user id, so this function looks like this PHP Code: protected function checkWritePermission(array $data) CodeIgniter\Database\Exceptions\DatabaseException You must use the "set" method to update an entry. Is there any different way to achieve this?
Hello,
Please note that Shield is not only for authentication but also provides authorization capabilities. You can leverage Shield's authorization system to create the necessary restrictions for users, such as preventing the user with ID 173 from modifying or writing data to the database. Before making any further changes, I recommend reading the following link to understand how to utilize Shield's authorization features: https://shield.codeigniter.com/quick_sta...orization/
I use the groups for admin and users, but I don't see how this one can protect all database changes?
Background is, we need a read-only admin, that can see everything but not change anything.
Step 1: Add a New Permission
Update your permissions configuration file to include the new permission PHP Code: /** Step 2: Assign the New Permission to a User To assign the new permission to a user, use the following code: PHP Code: // Retrieve the user with ID 173 Step 3: Restrict Access in the Controller In your controller, check if the user has the admin.readonly permission before performing the edit operation. If they have the permission, redirect them back with an error message: PHP Code: public function update(Request $request, $id)
my original plan was not to touch the controllers as it's pretty dangerous to oversee one piece, so I thought model would be the better place. But I did ask for help and I appreaciate your help. I've adapted the code and use it similar. Thank you.
If you're determined to use the model for any reason, I suggest instead of using the beforeupdate, beforeinsert and beforedelete, you Creating Custom Rules using:
PHP Code: auth()->user()->can('admin.readonly') Then apply this rule in your model. This way, you can handle the errors more effectively and provide a clearer message to the user. Controller: PHP Code: |
Welcome Guest, Not a member yet? Register Sign In |