Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a helper function which has to check for permission if granted then return true and the controller function executes, and if the permission not granted then redirect to dashboard.

But here if the permission not granted its, it displays the message that permission not granted and the goes forward and execute next line instead of going to dashboard.

I hope you understand my problem and will help.

thanks in advance.

What I have tried:

PHP
<pre>//Helper Function

if(!function_exists('chkPermissions')){
    function chkPermissions($action){
        $roleid = session()->get('roleid');
        $router = service('router'); 
	   $controller  = class_basename($router->controllerName());  // Controller / Class Name
        $model = new ModelPermissions();
        $data = $model->where(['roleid' => $roleid,'controller' => $controller, $action => '1'])->first();
        if($data == null) {
		// if permission not grantted then goto dashboard
        session()->setFlashdata('error', 'Permission not grantted, try again!');
        return view('dashboard/index',false);        
            }  

// if permission granted return true			
         return true;
    }
}




//Controller's Function

public function index()
    {
		chkPermissions('view');
// if permission were not granted, should not execute the below line		
        return view('home/index');
       
    }
Posted
Updated 23-Jun-23 0:37am

It's probably because you're not actually doing anything with the return value of the function you've created. The function returns either a boolean or the result of a function call to view('dashboard/index', false);, but you don't do anything with either result.

Instead you could make the function return true or false and then do something different in each instance?

if (!function_exists('chkPermissions')) {
  function chkPermissions($action) {
    $roleid = session()->get('roleid');
    $router = service('router'); 
	$controller  = class_basename($router->controllerName());  // Controller / Class Name
    $model = new ModelPermissions();
    $data = $model->where([
      'roleid' => $roleid,
      'controller' => $controller, $action => '1'])->first();
    
    if ($data == null) {
      // if permission not grantted then goto dashboard
      session()->setFlashdata('error', 'Permission not grantted, try again!');

      // Changed this here to return false when no permission set
      return false;       
    }  

    // if permission granted return true			
    return true;
  }
}




//Controller's Function

public function index()
{
  // See here we check the return value (true or false) of the function
  // call and if false then we navigate
  if (!chkPermissions('view'))
    return view('dashboard/index', false);

  return view('home/index');       
}
 
Share this answer
 
Comments
Andre Oosthuizen 23-Jun-23 6:38am    
Sorry Chris, just added the re-direct in my code, don't want to hijack your solution. +5
You should fall into the habit of doing error checking whilst coding, this will save you a tremendous amount of time on debugging -
try {
    // Code that may throw an exception
} catch (Exception $e) {
    // Handle the exception
    log_message('error', $e->getMessage()); // Log the error message
    show_error('An error occurred. Please try again later.'); // Display a user-friendly error message
}


CHris's code and explanation is spot on, if you still want to re-direct to your dashboard page, just add -
if ($data == null) {
            // if permission not granted then go to dashboard
            session()->setFlashdata('error', 'Permission not granted, try again!');
            return redirect()->to('dashboard/index');
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900