Rename User.php - Roles and Permissions (Laravel 5.3)

Posted 2 years ago by jasonwilson

Greetings

My apologies in advanced if this question has already been asked, as I’m still getting up to speed with learning Laravel… But I was just wondering if it is possible to rename the ‘app\User.php’ file to something else? For example, can I rename ‘app\User.php’ to ‘app\Member.php’?

The reason for my question, is that I have been following the ‘ACL in Laravel: Roles and Permissions’ lesson (https://laracasts.com/series/whats-new-in-laravel-5-1/episodes/16), but I was using a fresh installation of Laravel 5.3 instead of Laravel 5.1 (which was used in the tutorial). While I had no issue with the lesson (all worked as per the tutorial) - I thought I would try and rename ‘app\User.php’ to ‘app\Member.php’ after the lesson to see what happens…

Essentially, when I rename the file – the application stopped working and subsequently generated several errors… Consequently, I’m struggling to identify why this is so…

I’m interested to know if Laravel is designed, whereby you can rename the ‘app\User.php’ file… And if it can be renamed – what is to correct way to do so… This is more curiosity than anything else.

I have pasted my code below for your reference. Thanks in advance for your assistance…

\App\Member

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Member extends Authenticatable
{
    use Notifiable;

    protected $table = 'users';

    protected $fillable = [
        'name', 'email', 'password',
    ];

     protected $hidden = [
        'password', 'remember_token',
    ];

    public function roles() {
    return $this->belongsToMany(Role::class);
  }

  public function assignRole($role) {
    return $this->roles()->save(
      Role::whereName($role)->firstOrFail()
    );
  }

  public function hasRole($role) {
    if (is_string($role)) {
      return $this->roles->contains('name', $role);
    }
    return !! $role->intersect($this->roles)->count();
  }

}

\App\Permission

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
  /**
   * We have a Role object, and we what to know what
   * permissions are associated with this role
   * (i.e. a role relationship can Belongs-to-Many Permissions)
   */
  public function roles() {
    return $this->belongsToMany(Role::class);
  }
}

\App\Role

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
  /**
   *
   * Which Roles have the right to take (i.e. have
   * permission) this action...
   */
  public function permissions() {
    return $this->belongsToMany(Permission::class);
  }

  /**
   *
   * The Role to give permission to do
   * the task (accept Permission Object)...
   */
  public function givePermissionTo(Permission $permission) {
    return $this->permissions()->save($permission);
  }
}

\App\Providers\AuthServiceProvider

<?php

namespace App\Providers;

use App\Permission;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot() {
      $this->registerPolicies();

      /**
       * Register any application authentication / authorization services.
       *
       * @param  \Illuminate\Contracts\Auth\Access\Gate $gate
       * @return void
       */

        foreach ($this->getPermissions() as $permission) {
          Gate::define($permission->name, function ($user) use ($permission) {
            return $user->hasRole($permission->roles);
          });
        }
      }


      protected function getPermissions() {
      try {
        return Permission::with('roles')->get();

      } catch (\Exception $e) {
        return [];
      }
    }
}

\config\auth.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Member::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.