From 46856c939433e94c83ae849ef55ade4220588a16 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 12 Feb 2016 05:49:53 +0100 Subject: [PATCH] Remove entrust package. --- app/Models/Permission.php | 21 ----------- app/Models/Role.php | 26 +++++++------- app/User.php | 75 +++++++++++++++++++++++++++++++++++---- composer.json | 1 - 4 files changed, 80 insertions(+), 43 deletions(-) delete mode 100644 app/Models/Permission.php diff --git a/app/Models/Permission.php b/app/Models/Permission.php deleted file mode 100644 index 7d0922d493..0000000000 --- a/app/Models/Permission.php +++ /dev/null @@ -1,21 +0,0 @@ -belongsToMany('FireflyIII\User'); + } + } diff --git a/app/User.php b/app/User.php index 3f0b86d787..58746d5a6b 100644 --- a/app/User.php +++ b/app/User.php @@ -3,10 +3,10 @@ declare(strict_types = 1); namespace FireflyIII; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Foundation\Auth\User as Authenticatable; -use Zizaco\Entrust\Traits\EntrustUserTrait; /** * Class User @@ -32,11 +32,10 @@ use Zizaco\Entrust\Traits\EntrustUserTrait; * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Role[] $roles * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\RuleGroup[] $ruleGroups * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\Rule[] $rules - * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\ExportJob[] $exportjobs + * @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\ExportJob[] $exportjobs */ class User extends Authenticatable { - use EntrustUserTrait; /** * The attributes that are mass assignable. @@ -44,16 +43,12 @@ class User extends Authenticatable * @var array */ protected $fillable = ['email', 'password', 'blocked', 'blocked_code']; - - /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = ['password', 'remember_token']; - - /** * The database table used by the model. * @@ -69,6 +64,26 @@ class User extends Authenticatable return $this->hasMany('FireflyIII\Models\Account'); } + /** + * Alias to eloquent many-to-many relation's attach() method. + * + * Full credit goes to: https://github.com/Zizaco/entrust + * + * @param mixed $role + */ + public function attachRole($role) + { + if (is_object($role)) { + $role = $role->getKey(); + } + + if (is_array($role)) { + $role = $role['id']; + } + + $this->roles()->attach($role); + } + /** * @return HasMany */ @@ -109,6 +124,44 @@ class User extends Authenticatable return $this->hasMany('FireflyIII\Models\ExportJob'); } + /** + * Checks if the user has a role by its name. + * + * Full credit goes to: https://github.com/Zizaco/entrust + * + * @param string|array $name Role name or array of role names. + * @param bool $requireAll All roles in the array are required. + * + * @return bool + */ + public function hasRole($name, $requireAll = false) + { + if (is_array($name)) { + foreach ($name as $roleName) { + $hasRole = $this->hasRole($roleName); + + if ($hasRole && !$requireAll) { + return true; + } elseif (!$hasRole && $requireAll) { + return false; + } + } + + // If we've made it this far and $requireAll is FALSE, then NONE of the roles were found + // If we've made it this far and $requireAll is TRUE, then ALL of the roles were found. + // Return the value of $requireAll; + return $requireAll; + } else { + foreach ($this->roles as $role) { + if ($role->name == $name) { + return true; + } + } + } + + return false; + } + /** * @return HasManyThrough */ @@ -125,6 +178,14 @@ class User extends Authenticatable return $this->hasMany('FireflyIII\Models\Preference'); } + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function roles(): BelongsToMany + { + return $this->belongsToMany('FireflyIII\Models\Role'); + } + /** * @return HasMany */ diff --git a/composer.json b/composer.json index 152fb7130e..45949b7898 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,6 @@ "doctrine/dbal": "~2.5", "league/commonmark": "~0.7", "rcrowe/twigbridge": "~0.9", - "zizaco/entrust": "dev-laravel-5", "league/csv": "^7.1", "laravelcollective/html": "^5.2" },