This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

DMeganoski
DMeganoski

I know you can extend a model with events. I.E.

User::extend(function($model) { $model->bindEvent('user.register', function() use ($model) { // Code to register $model->email to mailing list }); });

However I don't think events can help me with what I'm trying to accomplish. What I would like to do is add a "use UserFilterTrait" to the class.

Essentially what I am trying to do is prevent certain users from seeing certain others. I have used traits like this before on Eloquent models and it works great.

Would there be an easy way to implement this? Or at least a workaround that someone can think of which would allow me to filter the users shown?

KurtJensen
KurtJensen

You could create your own model that extends the user model. Then add the trait there. This way you can override any methods you need to in your new model.

Example:

<?php namespace KurtJensen\Staff\Models;

use Model;
use RainLab\User\Models\User;

/**
 * worker Model
 */
class Worker extends User {
    use KurtJensen\Staff\Traits\MyTrait;
    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Relations
     */
    public $hasOne = [
        'seniority' => ['KurtJensen\Staff\Models\Seniority',
            'key' => 'user_id',
            'otherKey' => 'id'],
    ];

    public function getRankOptions() {
        $ranks = array_map('trim', explode("\n", Settings::get('ranks', 'none-listed')));
        return array_combine($ranks, $ranks);
    }   

 // Etc...
}

Last updated

DMeganoski
DMeganoski

Thanks for the reply. I had considered this, but I'm not sure that alone will achieve what I'm trying to accomplish.

I could then use this extended class in my plugin, but it would not change the reference to the original class in October's backend, unless there is something implemented to override classes that I am unaware of.

It would not affect the Administrators list, which is where my concern is.

As of right now, if the 'super' admin creates another admin, the new admin can edit the super. And I don't even want the super showing up in the list. Likewise for other groups I will create.

There are some other issues concerning what groups admins should be able to add users to, but that I could probably also sort out if I could extend the User model.

So is there any way to tell October to use my User model instead? Simply using model events ('model.beforeFetch') doesn't seem to be a viable option.

KurtJensen
KurtJensen

Just creat new controller that uses your own class. Then grant access to this new controller instead of the regular user controller.

DMeganoski
DMeganoski

Hmm not a bad suggestion. I have done similar things in the past for laravel packages, perhaps you are onto something. I'll look into it, thanks. :)

arif04cuet34308
arif04cuet34308

so far i know, trait can't be added by extending model, you can implement model behaviors instead.

BackendUserModel::extend(function ($model) {
            $model->implement[] = 'Np.Structure.Behaviors.UserFilterBehaviors';
    });

Details https://octobercms.com/docs/database/behaviors


edit: I found a better solution here: https://talk.octobercms.com/t/extend-user-model-adding-a-trait/639/7

Last updated

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.