Commit a14b1c8d authored by Waterfox's avatar Waterfox
Browse files

Add roles() and hasRole() methods to User model, and update test.blade.php and web.php

parent 00812023
Loading
Loading
Loading
Loading

app/Models/Role.php

0 → 100644
+16 −0
Original line number Diff line number Diff line
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    use HasFactory;

    protected $table = 'CAR_ROLE';

    protected $primaryKey = 'ROL_CODE';

}
+12 −0
Original line number Diff line number Diff line
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class RoleAttribution extends Model
{
    protected $table = 'CAR_ROLE_ATTRIBUTION';
    public $timestamps = false;
}
+11 −1
Original line number Diff line number Diff line
@@ -35,11 +35,21 @@ public function checkPassword($password)
        }
    }

    /**
     * Get the roles attributed to the user.
     */

    public function roles()
    {
        return $this->belongsToMany(Role::class, 'CAR_ROLE_ATTRIBUTION', 'US_ID', 'ROL_CODE');
        return $this->belongsToMany(Role::class, RoleAttribution::class, 'US_ID', 'ROL_CODE');
    }

    /**
     * Check if the user has a given role.
     *
     * @param string $roleCode the code of the role to check
     * @return bool true if the user has the role, false otherwise
     */
    public function hasRole($roleCode)
    {
        return $this->roles->contains('ROL_CODE', $roleCode);
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
<body>
    @if ($user->checkPassword('securepassword'))
        <p>Match</p>
        <p> {{$user->hasRoles('SEC')}} </p>
    @else
        <p>Unmatch</p>
    @endif
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@
    return view('welcome');
});

Route::get('/', function()
Route::get('/test', function()
{
    return view('test', ['user' => User::find(1)]);
});