daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.      * Check if the user has EVERY permission given in the array  , array must be given
  3.      * @return bool
  4.      */
  5.     public static function hasAllPermissions($givenPerms){
  6.         $user = Auth::user();
  7.         $user->role->perm;
  8.         $valid = true;
  9.  
  10.         if (is_array($givenPerms)) {
  11.             $user_perms = array();
  12.             foreach ($user->role->perm as $perm) {
  13.                 $user_perms[] = $perm->name;
  14.             }
  15.  
  16.             if(in_array('admin', $user_perms)){
  17.                 return true;
  18.             }
  19.  
  20.             foreach ($givenPerms as $singlePerm){
  21.                 if (!in_array($singlePerm, $user_perms)) {
  22.                     $valid = false;
  23.                 }
  24.             }
  25.  
  26.         }else{
  27.             $valid = false;
  28.             dd('No array given in hasALLPermissions');
  29.         }
  30.  
  31.        if($valid){
  32.             return true;
  33.        }
  34.        return false;
  35.     }
  36.  
  37.     /**
  38.      *  * @param array givenperms
  39.      * Check if the user has ANY of the permission given, array must be given
  40.      * if one is found we return true
  41.      * @return bool
  42.      */
  43.     public static function hasAnyPermission($givenPerms){
  44.         $user = Auth::user();
  45.         $user->role->perm;
  46.  
  47.         if (is_array($givenPerms)) {
  48.             $user_perms = array();
  49.             foreach ($user->role->perm as $perm) {
  50.                 $user_perms[] = $perm->name;
  51.             }
  52.  
  53.             if(in_array('admin', $user_perms)){
  54.                 return true;
  55.             }
  56.  
  57.             foreach ($givenPerms as $singlePerm){
  58.                 if (in_array($singlePerm, $user_perms)) {
  59.                     return true;
  60.                 }
  61.             }
  62.  
  63.         }else{
  64.             dd('No array given in hasAnyPermission');
  65.         }
  66.         return false;
  67.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top