Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function createRank()
- {
- /*
- 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!
- 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.
- Let's say that there are over 10000 permissions, each and every one of them has their own unique id.
- We need to validate that the user input is correct. We should check the following:
- - Can a user add this permission to a rank? Does every permission exist in the database?
- - Is the user attempting to add the same permission multiple times to the rank?
- - Does this rank already exist?
- */
- //A user wants to add a rank with the following permission id's
- $user_input_permissions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10];
- $user_input_rank_name = "Customer Support";
- $canAddRank = true;
- //loop through every permission and check if it exists
- foreach ($user_input_permissions as $permissionId)
- {
- $permission = Permission::where('id', $permissionId)->first();
- if(empty($permission))
- {
- $canAddRank = false;
- break;
- }
- }
- if(!$canAddRank)
- return "Couldn't add rank. User tried to add a rank that didn't exist!";
- $rank = new CompanyRank;
- $rank->title = $user_input_rank_name;
- $rank->permissions = $user_input_permissions;
- $rank->save();
- return "Added Rank Successfully!";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement