Add explict admin user creation in database creation (REST API)
@@ -43,11 +43,7 @@ import com.orientechnologies.orient.server.network.protocol.http.OHttpUtils;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommandAuthenticatedServerAbstract;
import java.io.IOException;
import java.io.StringWriter;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
public class OServerCommandPostDatabase extends OServerCommandAuthenticatedServerAbstract {
private static final String[] NAMES = {"POST|database/*"};
@@ -66,6 +62,21 @@ public class OServerCommandPostDatabase extends OServerCommandAuthenticatedServe
final String storageMode = urlParts[2];
String url = getStoragePath(databaseName, storageMode);
final String type = urlParts.length > 3 ? urlParts[3] : "document";
+
+ boolean createAdmin = false;
+ String adminPwd = null;
+ if (iRequest.getContent() != null && !iRequest.getContent().isEmpty()) {
+ // CONTENT REPLACES TEXT
+ if (iRequest.getContent().startsWith("{")) {
+ // JSON PAYLOAD
+ final ODocument doc = new ODocument().fromJSON(iRequest.getContent());
+ if (doc.hasProperty("adminPassword")) {
+ createAdmin = true;
+ adminPwd = doc.getProperty("adminPassword");
+ }
+ }
+ }
+
if (url != null) {
if (server.existsDatabase(databaseName)) {
sendJsonError(
@@ -78,8 +89,19 @@ public class OServerCommandPostDatabase extends OServerCommandAuthenticatedServe
} else {
server.createDatabase(
databaseName, ODatabaseType.valueOf(storageMode.toUpperCase(Locale.ENGLISH)), null);
+
try (ODatabaseDocumentInternal database =
server.openDatabase(databaseName, serverUser, serverPassword, null)) {
+
+ if (createAdmin) {
+ try {
+ database.command("CREATE USER admin IDENTIFIED BY ? ROLE admin", adminPwd);
+ } catch (Exception e) {
+ OLogManager.instance()
+ .warn(this, "Could not create admin user for database " + databaseName, e);
+ }
+ }
+
sendDatabaseInfo(iRequest, iResponse, database);
}
}
