codeigniter4/settings

Bug: Context Update Spillover

jozefrebjak opened this issue · 3 comments

Hello, I'm trying to implement new settings library into my project. Everything is going fine, but I would like to have an option for the user to change his theme of app based on the logged in user ID. I am using myth-auth so is no problem to get actually logged in user. I tried to use user_id() as a $context so if is logged in user with ID for example 1 than he can go to settings and change via the form theme of the app.

Here is the form-select:

<div class="mb-3">
    <label class="form-label" for="theme"><?= lang('Settings.theme') ?></label>
    <div class="col">
        <select name="theme" class="form-select <?php if (session('errors.theme')) : ?>is-invalid<?php endif ?>">
            <?php
            foreach ([1 => lang('Settings.themes.light'), 2 => lang('Settings.themes.dark')] as $k => $v) {
                if ($k === set_value('theme', (int) setting()->get('App.theme', user_id()))){
                    echo '<option value="'.$k.'" selected>'.$v.'</option>';
                } else {
                    echo '<option value="'.$k.'">'.$v.'</option>';
                }
            }
            ?>
        </select>
    </div>
</div>

This show to user by default theme-light what is ok. Now he want to change theme to theme-dark so he will choose theme-dark and submit the form. Form is going to the controller method:

    public function attemptUpdate()
    {
        // Validate basics first since some password rules rely on these fields
        $rules = [
            'theme' => 'required',
        ];

        if (! $this->validate($rules)) {
            return redirect()
                ->back()
                ->with('errors', $this->validator->getErrors());
        }

        // Update settings of the user logged in
        setting()->set('App.theme', $this->request->getPost('theme'), user_id());

        // Success!
        return redirect()
            ->route('settings')
            ->with('message', lang('Settings.updateSuccess'));
    }

and create settings row in the table settings like:

1	Config\App	theme	2	string	1	2021-11-20 08:07:47	2021-11-20 08:07:47

as far so good. When another user log in and try to change his settings of theme another row will be created what is ok:

2	Config\App	theme	2	string	5	2021-11-20 08:07:47	2021-11-20 08:07:47

but for example he want to change his settings back to ligh-theme and all rows in the table are changed like:

1	Config\App	theme	1	string	5	2021-11-20 08:07:26	2021-11-20 08:09:32
2	Config\App	theme	1	string	5	2021-11-20 08:07:47	2021-11-20 08:09:32

and setttings of the user with ID 1 are lost.

@MGatner hello I tried to use your new library preferences but it's the same. What I see your library is adding user: before user_id() in context column, but it's not usable for more users or I'm doing something wrong.

@jozefrebjak Sounds like a bug with the DatabaseHandler updating without the context - probably my fault! I'm working with both these libraries today, I will spend some time on a fix.

I do recommend my Preferences library - it will handle the context wrapping for you, and I have a new feature coming in RC.2 that will use Session to give semi-persistent settings to users who aren't logged in as well.

@MGatner Great. Thank you. I'm using a lot of your libraries. I'm wait for the fix.🙂