createuser.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. if (!OC_User::createUser($username, $password)) {
  29. OC_JSON::error(array('data' => array( 'message' => 'User creation failed for '.$username )));
  30. exit();
  31. }
  32. foreach( $groups as $i ) {
  33. if(!OC_Group::groupExists($i)) {
  34. OC_Group::createGroup($i);
  35. }
  36. OC_Group::addToGroup( $username, $i );
  37. }
  38. OC_JSON::success(array("data" =>
  39. array(
  40. "username" => $username,
  41. "groups" => implode( ", ", OC_Group::getUserGroups( $username )))));
  42. } catch (Exception $exception) {
  43. OC_JSON::error(array("data" => array( "message" => $exception->getMessage())));
  44. }