OctoberCMS User Plugin How to Deny Reserved Names

- 1 answer

Ad

I'm using the User plugin.

Here is my previous question on how to Deny Username Change.

I have a list of Reserved Names I don't want people to use (such as admin, anonymous, guest) I need to put in an array and deny upon register.

My Custom Component's Plugin.php

public function boot() {

    \RainLab\User\Models\User::extend(function($model) {

        $model->bindEvent('model.beforeSave', function() use ($model) {

            // Reserved Names List
            // Deny Registering if Name in List

        });

    });

}

How would I do that using the Validator?

Ad

Answer

Ad

You can throw an exception to do that

public function boot() {

\RainLab\User\Models\User::extend(function($model) {

    $model->bindEvent('model.beforeSave', function() use ($model) {

        $reserved = ['admin','anonymous','guest'];

        if(in_array($model->username,$reserved)){
            throw new \October\Rain\Exception\ValidationException(['username' => \Lang::get('You can't use a reserved word as username')]);
        }

    });

});

}

Ad
source: stackoverflow.com
Ad