Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. /*********************************************
  2. * Import Users by JSON file
  3. *
  4. *********************************************/
  5. function import($file){
  6. $string = file_get_contents($file); // Read JSON file
  7. $users = json_decode($string, true); // Decode JSON to $users array
  8.  
  9. // Iterate to each user
  10. foreach ($users as $user) {
  11. $sanitizer = wire('sanitizer'); // initialize $sanitizer for input sanitizing
  12.  
  13. /*****************************************************
  14. * Make shure there is no user with the same username
  15. * you can skip, update or add the user
  16. *****************************************************/
  17. $counter = '';
  18.  
  19. if(wire('users')->get("name={$user['Name']}")){
  20. // Skip double users by uncommand the following line
  21. // continue;
  22.  
  23. // Add the User by adding a number to his name
  24. $counter = wire('users')->count("firstname={$user['Name']}") + 1; // count all users with the same name.
  25.  
  26. // Update the user.
  27. $u = wire('users')->get("name={$user['Name']});
  28. }
  29.  
  30. if(empty($u)) $u = new User(); // Create new User
  31. $u->of(false); // Outputformatting of for adding new values to the user
  32.  
  33. /***************************************************
  34. * Now you can add the Information from the JSON file
  35. * to the new User. Don't forgett to sanitize the inputs.
  36. ****************************************************/
  37. $u->name = $sanitizer->name($user['Name']). $counter; // name as username + counter if more user has the same name.
  38. $u->email = $sanitizer->email($user['email']);
  39. $u->pass = $sanitizer->text($user['password']);
  40. $u->addRole('User'); // Add Userrole to the User
  41.  
  42. // Save the User
  43. $u->save();
  44. $u->of(true);
  45.  
  46. // Feedback for each added user.
  47. echo "User: {$u->name} added. <br />";
  48. }
  49.  
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement