From 75694712bb8c904650066175084bce2d1ef90e18 Mon Sep 17 00:00:00 2001 From: Nevin Morgan Date: Fri, 20 Sep 2024 13:29:56 -0400 Subject: [PATCH] add role and permission models to application --- app/Models/Permission.php | 31 +++++++++++++++++++++++++++++++ app/Models/Role.php | 39 +++++++++++++++++++++++++++++++++++++++ app/Models/User.php | 25 +++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 app/Models/Permission.php create mode 100644 app/Models/Role.php diff --git a/app/Models/Permission.php b/app/Models/Permission.php new file mode 100644 index 0000000..3fd3ac7 --- /dev/null +++ b/app/Models/Permission.php @@ -0,0 +1,31 @@ +belongsToMany(Role::class, 'privilege_role_link', 'pid', 'rid'); + } +} diff --git a/app/Models/Role.php b/app/Models/Role.php new file mode 100644 index 0000000..8b23fc5 --- /dev/null +++ b/app/Models/Role.php @@ -0,0 +1,39 @@ +belongsToMany(User::class, 'appuser_role_link', 'rid', 'uid'); + } + + /** + * The permissions that belong to the role. + */ + public function permissions(): BelongsToMany + { + return $this->belongsToMany(Permission::class, 'privilege_role_link', 'rid', 'pid'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index c895c58..76ff7e2 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\DB; @@ -54,6 +55,30 @@ class User extends Authenticatable protected $table = 'appuser'; + /** + * The roles that belong to the user. + */ + public function roles(): BelongsToMany + { + return $this->belongsToMany(Role::class, 'appuser_role_link', 'uid', 'rid'); + } + + /** + * The permissions that belong to the user. + */ + public function getPermissions() + { + return Permission::whereIn('id', function ($query) { + $query->select('pid') + ->from('privilege_role_link') + ->whereIn('rid', function ($query) { + $query->select('rid') + ->from('appuser_role_link') + ->where('uid', $this->id); + }); + })->get(); + } + public function isVocabularyEditor() { $userId = $this->id;