createuser.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. OCP\JSON::callCheck();
  3. OC_JSON::checkSubAdminUser();
  4. if(OC_User::isAdminUser(OC_User::getUser())) {
  5. $groups = array();
  6. if( isset( $_POST["groups"] )) {
  7. $groups = $_POST["groups"];
  8. }
  9. }else{
  10. if(isset( $_POST["groups"] )) {
  11. $groups = array();
  12. foreach($_POST["groups"] as $group) {
  13. if(OC_SubAdmin::isGroupAccessible(OC_User::getUser(), $group)) {
  14. $groups[] = $group;
  15. }
  16. }
  17. if(count($groups) === 0) {
  18. $groups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser());
  19. }
  20. }else{
  21. $groups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser());
  22. }
  23. }
  24. $username = $_POST["username"];
  25. $password = $_POST["password"];
  26. // Return Success story
  27. try {
  28. // check whether the user's files home exists
  29. $userDirectory = OC_User::getHome($username) . '/files/';
  30. $homeExists = file_exists($userDirectory);
  31. if (!OC_User::createUser($username, $password)) {
  32. OC_JSON::error(array('data' => array( 'message' => 'User creation failed for '.$username )));
  33. exit();
  34. }
  35. foreach( $groups as $i ) {
  36. if(!OC_Group::groupExists($i)) {
  37. OC_Group::createGroup($i);
  38. }
  39. OC_Group::addToGroup( $username, $i );
  40. }
  41. OC_JSON::success(array("data" =>
  42. array(
  43. // returns whether the home already existed
  44. "homeExists" => $homeExists,
  45. "username" => $username,
  46. "groups" => OC_Group::getUserGroups( $username ))));
  47. } catch (Exception $exception) {
  48. OC_JSON::error(array("data" => array( "message" => $exception->getMessage())));
  49. }