server/settings/ajax/createuser.php
Bart Visscher 17e631bc5e Use OC_JSON for json responses
Create OC_JSON class, for single point of creating json responses.
No real logic change, this just cleans up the code a bit.
2011-09-25 22:19:28 +02:00

39 lines
1,012 B
PHP

<?php
// Init owncloud
require_once('../../lib/base.php');
// Check if we are a user
if( !OC_User::isLoggedIn() || !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
OC_JSON::error(array("data" => array( "message" => "Authentication error" )));
exit();
}
$groups = array();
if( isset( $_POST["groups"] )){
$groups = $_POST["groups"];
}
$username = $_POST["username"];
$password = $_POST["password"];
// Does the group exist?
if( in_array( $username, OC_User::getUsers())){
OC_JSON::error(array("data" => array( "message" => "User already exists" )));
exit();
}
// Return Success story
if( OC_User::createUser( $username, $password )){
foreach( $groups as $i ){
if(!OC_Group::groupExists($i)){
OC_Group::createGroup($i);
}
OC_Group::addToGroup( $username, $i );
}
OC_JSON::success(array("data" => array( "username" => $username, "groups" => implode( ", ", OC_Group::getUserGroups( $username )))));
}
else{
OC_JSON::error(array("data" => array( "message" => "Unable to add user" )));
}
?>