Created
August 20, 2017 11:45
-
-
Save roquie/4b78e4bf11488bb09ec01701bb11bd51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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