Skip to content

Instantly share code, notes, and snippets.

@lchoate
Created December 7, 2017 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lchoate/1475d6899facd22aa9f2c939aa4c3966 to your computer and use it in GitHub Desktop.
Save lchoate/1475d6899facd22aa9f2c939aa4c3966 to your computer and use it in GitHub Desktop.
Filter your records based on a users organization.
<?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;
}
}
@lchoate
Copy link
Author

lchoate commented Dec 7, 2017

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.

@moos3
Copy link

moos3 commented Dec 7, 2017

nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment