Created
December 7, 2017 00:24
-
-
Save lchoate/1475d6899facd22aa9f2c939aa4c3966 to your computer and use it in GitHub Desktop.
Filter your records based on a users organization.
This file contains 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 | |
/** | |
* File Name: CourseOrgCriteria.php / lms-laravel | |
* By: Lucas Choate (twitter: @lchoate) | |
* Date: 10/18/16 | |
* Time: 1:41 PM | |
*/ | |
namespace Modules\Course\Criteria; | |
use Prettus\Repository\Contracts\CriteriaInterface; | |
use Prettus\Repository\Contracts\RepositoryInterface; | |
class CourseOrgCriteria implements CriteriaInterface | |
{ | |
/** | |
* Apply criteria in query repository | |
* | |
* @param $model | |
* @param RepositoryInterface $repository | |
* | |
* @return mixed | |
*/ | |
public function apply($model, RepositoryInterface $repository) | |
{ | |
$orgs = auth()->user()->organization_id; | |
if(auth()->user()->hasRole(['admin','api-user','district-admin','school-admin'])){ | |
return $model; | |
} elseif(auth()->user()->hasRole(['student','teacher','tutor'])) { | |
$procChildOrgs = \DB::select('call GetChildOrgs(?)',[$orgs]); | |
$childOrgs = array_column($procChildOrgs,'id'); | |
array_push($childOrgs,auth()->user()->organization_id); | |
$orgs = $childOrgs; | |
} | |
$model = $model->WhereIn('organization_id',$orgs); | |
return $model; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My orgs are setup like:
ID | Orgname | ParentID | ...
Certain users are allowed to see the children of their organization, but never up the chain. Student would not normally be able to see down the chain, but this is code from my dev box. I was testing something.