Advertisement
swegus

Rank Permission

Jan 20th, 2020
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. public function createRank()
  2.      {
  3.          /*
  4. This function has been intentionally created to be slow, inefficient and insecure. Your task is to improve the speed of executing this function and to correct it!
  5.          We have a class called "Permissions" that contains the rights of what a user can do within a company. When creating a rank, we need to make sure that the permissions are validated correctly.
  6.          
  7.          Let's say that there are over 10000 permissions, each and every one of them has their own unique id.
  8.          We need to validate that the user input is correct. We should check the following:
  9.           - Can a user add this permission to a rank? Does every permission exist in the database?
  10.           - Is the user attempting to add the same permission multiple times to the rank?
  11.           - Does this rank already exist?
  12. */
  13.  
  14.          //A user wants to add a rank with the following permission id's
  15.          $user_input_permissions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10];
  16.          $user_input_rank_name = "Customer Support";
  17.          $canAddRank = true;
  18.  
  19.         //loop through every permission and check if it exists
  20.         foreach ($user_input_permissions as $permissionId)
  21.         {
  22.             $permission = Permission::where('id', $permissionId)->first();
  23.             if(empty($permission))
  24.             {
  25.                 $canAddRank = false;
  26.                 break;
  27.             }
  28.         }
  29.  
  30.         if(!$canAddRank)
  31.             return "Couldn't add rank. User tried to add a rank that didn't exist!";
  32.  
  33.         $rank = new CompanyRank;
  34.         $rank->title = $user_input_rank_name;
  35.         $rank->permissions = $user_input_permissions;
  36.         $rank->save();
  37.         return "Added Rank Successfully!";
  38.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement