1

I am struggling to insert "permissions" to my Gantt's chart (aka DHTMLX Gantt) The problem is is that I want an if statement in my function like so:

public function getOne($id){
      if(Auth::user()->hasRole('manufacturing')) {
        $tasks = Task::where('relation_id', $id)->where('parent', 0)->orwhere('permission', 2)->get();
        $links = Link::query();
      } else {
        $tasks = Task::get();
        $links = Link::query();
      }
      return response()->json([
          "data" => $tasks,
          "links" => $links->get()
      ]);

Here I am using Laratrust as my role manager. Now the thing is that this function throws me a console error 500 and I cannot back track it in the API. If I do a simple IF like: if(1 === 1)

It works fine. So I am thinking this is a problem with API not reaching the downloaded package. My Gantt's routes are in the api.php and written like so:

use Illuminate\Http\Request;
Route::get('/data', 'GanttController@getAll');
Route::get('/data/{id}', 'GanttController@getOne');

My goal is to make that depending on what Laratrust says (which role or permission user has) I define the IF statement and change the Query. I know it's not dynamic but it has to do.

dot321
  • 45
  • 1
  • 2
  • 8
  • how do you pass the user to your api? maybe `Auth::user()` is not set – madalinivascu May 31 '19 at 06:37
  • @madalinivascu I'm not sure i need to pass it. I just define everything in Controller (function getOne()) which in theory should just send the info as JSON to the Gantt chart which just represents it in visual. – dot321 May 31 '19 at 07:52
  • 1
    if you var_dump Auth::user() and you inspect the request does the dump display any users? – madalinivascu May 31 '19 at 07:56
  • have you tried to acces the route directly what errors do you get? – madalinivascu May 31 '19 at 07:56
  • 1
    @madalinivascu not sure why i didn't do that before the error: **"Call to a member function hasRole() on null"** – dot321 May 31 '19 at 08:11
  • so you didn't include laratrust correctly in your laravel installation – madalinivascu May 31 '19 at 08:13
  • I does work everywhere else, just here it doesn't – dot321 May 31 '19 at 08:14
  • do a var_dump(Auth::user()) and paste it here – madalinivascu May 31 '19 at 08:16
  • It's a NULL. So i probably need to pass it to the function from somewhere else... – dot321 May 31 '19 at 08:36
  • you can try `$request->user()->hasRole('manufacturing');` , pass $request as a the first param of getOne – madalinivascu May 31 '19 at 08:45
  • Doesn't work as it doesn't request it from anywhere, I believe. I'm trying to pass it through other function. As it first should go to *function index2* to generate view and other details, and then go to getOne function. I can try to pass the parameter from the first function and send it over, I think. – dot321 May 31 '19 at 10:12

0 Answers0