Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User models not working as expected #1892

Closed
texnixe opened this issue Jul 1, 2019 · 11 comments

Comments

@texnixe
Copy link
Contributor

commented Jul 1, 2019

Describe the bug

I created a new User model like described in the docs (ok, ok, I added it there myself):

In /site/plugins/usermodels/index.php

<?php
class EditorUser extends User
{
    public function sayhello() {
        return 'Hello';
    }
}

Kirby::plugin('texnixe/usermodels', [
    'userModels' => [
        'editor' => 'EditorUser'
    ],
]);

The I created a user with the Editor role in /site/blueprints/users/editor.yml and tried to make this user say hello:

if ($user = $kirby->user('myeditoruseremail')) {
  echo $user->sayhello();
}

But the output from the above user model method is nothing. Also, the user is still a /Kirby/Cms/User object, not an EditorUser.

The same happens with FileModels.

To Reproduce
Steps to reproduce the behavior:

  1. Create a new user role, e.g. Editor in /site/blueprints/users/editor.yml
  2. Create a new user with this role.
  3. Create a plugin with the user model as above
  4. Try to echo the sayhello() method.
  5. See nothing

Expected behavior
The user model should work based on the user role (and the file models based on the file template) in the same way as Page models.

Kirby Version
3.2.0

@distantnative distantnative self-assigned this Jul 1, 2019

distantnative added a commit that referenced this issue Jul 2, 2019

@texnixe texnixe changed the title User and file methods not working as expected User and file models not working as expected Jul 7, 2019

@distantnative

This comment has been minimized.

Copy link
Contributor

commented Jul 18, 2019

@texnixe Could you please check if it works for you with this change?
8e41af4

@afbora

This comment has been minimized.

Copy link

commented Jul 24, 2019

@distantnative I've tested 8e41af4 on 3.2.2, failed. (Replaced only Users::load() method)

@texnixe

This comment has been minimized.

Copy link
Contributor Author

commented Jul 24, 2019

@afbora Please test with the dev branch there have been some other changes in the meantime, user models now work for me, file models still don't.

@afbora

This comment has been minimized.

Copy link

commented Jul 24, 2019

@texnixe as your said dev branch works properly for user models 👍

bastianallgeier added a commit that referenced this issue Jul 29, 2019

@bastianallgeier bastianallgeier changed the title User and file models not working as expected User models not working as expected Jul 29, 2019

@bastianallgeier

This comment has been minimized.

Copy link
Contributor

commented Jul 29, 2019

User models are now fixed. I create a separate issue for file models.

@afbora

This comment has been minimized.

Copy link

commented Jul 30, 2019

I have detailed tested user models with 3.2.3 RC1 and i have an issue.
I didn't feel the need to open a new issue (I can open new if necessary)

If i use kirby()->user() in plugin anywhere, user models not working:

<?php

class AdminUser extends User
{
    public function sayHello() {
        return "Hello " . $this->name();
    }
}

Kirby::plugin('my/plugin', [
    'userModels' => [
        'admin' => 'AdminUser'
    ],
]);

$user = kirby()->user(); // Use anywhere in plugin

I traced the issue and found that:

Standart plugin without using user method, working order is:

  1. AppPlugins::extendUserModels()
  2. User::model()

If kirby()->user() used in plugin, working order is:

  1. User::model()
  2. AppPlugins::extendUserModels()

https://github.com/getkirby/kirby/blob/release/3.2.3-rc.1/src/Cms/AppPlugins.php#L452-L455

https://github.com/getkirby/kirby/blob/release/3.2.3-rc.1/src/Cms/User.php#L485-L496

So when we use kirby()->user() method in plugin, userModels cant overwrite it.

I hope I can tell..

@distantnative

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2019

Could you please open a new one for this? That sounds like a separate issue. Thanks.

@lukasbestle

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2019

Actually I think this is not an issue, but a current limitation that will be fixed by getkirby/ideas#23.

@afbora

This comment has been minimized.

Copy link

commented Jul 30, 2019

@lukasbestle Not exactly. I'm using kirby()->user() method (to use user based settings/dashboard) in/for plugin, not after plugin loaded. For example currently using like that:

Kirby::plugin('custom/plugin', [
    'blueprints' => [
        'site' => kirby()->roots()->blueprints() . '/site.' . kirby()->user()->role() . '.yml'
    ]
]);
@lukasbestle

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2019

We can't fix that unfortunately. We can only load user models after all plugins have been loaded. You could potentially define the blueprint later in the process ($kirby->extend()), but that again will be possible with getkirby/ideas#23.

@afbora

This comment has been minimized.

Copy link

commented Jul 30, 2019

You know better than I do: "Kirby always has a solution" 😎

This name is "Kirby Power!" 🚀

To help developers in need:

<?php

if ($userId = kirby()->session()->get('user.id')) {
    $user = new \Kirby\Cms\User(['id' => $userId]);
    Kirby::plugin('custom/plugin', [
        'blueprints' => [
            'site' => kirby()->roots()->blueprints() . '/site.' . $user->role() . '.yml'
        ]
    ]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
5 participants
You can’t perform that action at this time.