Esempio n. 1
0
 /**
  * Assign the role to the given authority.
  *
  * @param  \Illuminate\Database\Eloquent\Model|array|int  $authority
  * @return bool
  */
 public function to($authority)
 {
     $authorities = is_array($authority) ? $authority : [$authority];
     foreach (Helper::mapAuthorityByClass($authorities) as $class => $keys) {
         $this->assignRole($this->role(), $class, $keys);
     }
     return true;
 }
Esempio n. 2
0
 /**
  * Remove the role from the given authority.
  *
  * @param  \Illuminate\Database\Eloquent\Model|array|int  $authority
  * @return bool
  */
 public function from($authority)
 {
     if (is_null($role = $this->role())) {
         return false;
     }
     $authorities = is_array($authority) ? $authority : [$authority];
     foreach (Helper::mapAuthorityByClass($authorities) as $class => $keys) {
         $role->retractFrom($class, $keys);
     }
     return true;
 }
Esempio n. 3
0
 /**
  * Constrain the given roles query to those that were assigned to the given authorities.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $query
  * @param  string|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection  $model
  * @param  array  $keys
  * @return void
  */
 public function constrainWhereAssignedTo($query, $model, array $keys = null)
 {
     list($model, $keys) = Helper::extractModelAndKeys($model, $keys);
     $query->whereExists(function ($query) use($model, $keys) {
         $table = $model->getTable();
         $key = "{$table}.{$model->getKeyName()}";
         $pivot = Models::table('assigned_roles');
         $roles = Models::table('roles');
         $prefix = Models::prefix();
         $query->from($table)->join($pivot, $key, '=', $pivot . '.entity_id')->whereRaw("{$prefix}{$pivot}.role_id = {$prefix}{$roles}.id")->where("{$pivot}.entity_type", $model->getMorphClass())->whereIn($key, $keys);
     });
 }
Esempio n. 4
0
 /**
  * Retract the role from the given model(s).
  *
  * @param  string|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection  $model
  * @param  array|null  $keys
  * @return $this
  */
 public function retractFrom($model, array $keys = null)
 {
     list($model, $keys) = Helper::extractModelAndKeys($model, $keys);
     $this->newBaseQueryBuilder()->from(Models::table('assigned_roles'))->where('role_id', $this->getKey())->where('entity_type', $model->getMorphClass())->whereIn('entity_id', $keys)->delete();
     return $this;
 }