Skip to content

Instantly share code, notes, and snippets.

@roquie
Created August 20, 2017 11:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save roquie/4b78e4bf11488bb09ec01701bb11bd51 to your computer and use it in GitHub Desktop.
<?php
namespace App\Criteria;
use App\User;
use Auth;
/**
* Глобальный скоуп сужающий
* выборку до данных пользователя (where user_id = ${CURRENT_USER_ID}).
*
* @package namespace App\Criteria;
*/
class PermissionsCriteria implements CriteriaInterface
{
/**
* Apply criteria in query repository
*
* @param $model
* @param RepositoryInterface $repository
*
* @return mixed
*/
public function apply($model, RepositoryInterface $repository)
{
$column = $model instanceof User ? 'id' : 'user_id';
if (null === ($user = Auth::user())) {
return $model;
}
if ($user->inRole('admin') || $user->inRole('user')) {
return $model;
}
if ($user->inRole('agent')) {
$query = "{$column} in (select id from users where agent_id = ? union select ? order by 1)";
return $model->whereRaw($query, [Auth::id(), Auth::id()]);
}
// остальные роли
return $model->where($column, Auth::id());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment