Search code examples
mysqldatabasepermissionsplesk

In plesk how do I give the user the privileges to create databases using php, but in his domain only


On our server we have many domains and we use plesk 12.5.30. On one domain I want to give the user the means to create his own databases in that domain only in php. This isn't the question, but I created the first database for the user and set up the username and password for that database. So the user could log into that database but he did not have permission to create new databases in php, so this is what I did.

In plesk I went to the global phpmyadmin(as the local one to that domain has not got the privileges option), and not the local one for that domain and I gave the user the privilege to create databases, but there is a problem. When he creates a database it does not show up in his domain, but it shows up in the root. You can see the database in the global phpmyadmin, but not the local one, and also plesk cannot see it. Worst than this, now the user has access to every database on the server.

My question is this. In plesk how do I give the user the privileges to create databases using php, but in his domain only.


Solution

  • In Plesk customer's user has access to API-RPC and there is possible to create DB via API-RPC call from PHP code:

    https://docs.plesk.com/en-US/12.5/api-rpc/reference/managing-databases/creating-databases.34407/#o34418

    You just need to tell to customer his webspace(subscription) id(and if need DB server id):

    <?php
    
    require_once('PleskApiClient.php'); // You can download it by link below
    
    $host = '127.0.0.1'; // It's your Plesk server IP
    $login = 'client_login'; // it's your client login name
    $password = 's$cr3t'; // Client's password
    $client = new PleskApiClient($host); // Init Plesk client
    $client->setCredentials($login, $password); 
    
    $webspaceId = 7; // webspace/subscription Id where customer wants to create database
    $dbName = 'MyNewDataBase'; // name of new database
    
    $request = <<<EOF
    <packet>
    <database>
    
    <add-db>
       <webspace-id>$webspaceId</webspace-id>
       <name>$dbName</name>
       <type>mysql</type>
    </add-db>
    
    </database>
    </packet>
    EOF;
    
    $response = $client->request($request); // Send query to Plesk host
    echo $response; // show response
    
    // parse new database id from $response to variable $dbId
    
    // assign existed DB user to new database
    $requestToAssignDefaultUser = <<<EOF
    <packet>
    <database>
       <set-default-user>
          <db-id>$dbId</db-id>
          <default-user-id>35</default-user-id>
       </set-default-user>
    </database>
    </packet>
    EOF;
    
    $response = $client->request($requestToAssignDefaultUser); // Send query to Plesk host
    echo $response; // show response
    

    You can get subscription/webspace id by another API request or from UI:

    Plesk database create from API

    You can find Plesk API PHP client and usage example here: https://github.com/plesk/api-examples/tree/master/php