If your extension needs to retrieve information from the user that is currently logged in to Plesk, use the pm_Session class.

Examples

Restrict user access to the extension

For restricting user access to the extension, first retrieve the current user (a pm_Client object), using the method

The following code restricts access for all users except administrators:

if (!pm_Session::getClient()->isAdmin()) {
    throw new pm_Exception('Permission denied');
}

Retrieve customer’s identifier when logged as a customer

If an administrator or a reseller is logged in the Customer Panel as one of their customers, you can retrieve the customer’s identifier, using the methods:

Use the following code:

if (pm_Session::isImpersonated()) {
    $clientId = pm_Session::getImpersonatedClientId();
    var_dump($clientId);
}

Retrieve the domain information

Additionally, if the session is opened in the Customer Panel, you can retrieve information about the domain that is currently being operated on using the method

Use the following code:

$domain = pm_Session::getCurrentDomain();
var_dump($domain->getName());

$domain in this example is an instance of the pm_Domain class.

If Plesk is unable to retrieve the domain, a pm_Exception will be thrown.

Retrieve the current main domain

You can also retrieve information about all domains that are currently being operated on using the method

This method is best used if the session is opened in Power User view. By default, it returns the main domains and their additional domains and subdomains. If the $mainDomainsOnly argument is set to “true”, only the main domains are returned. In most cases the method will return an array containing a single main domain (analogous to pm_Session::getCurrentDomain()) and its additional domains and subdomains.

However, in Power User view, when “All subscriptions” is selected (transparent webspaces mode), all current user domains would be returned together with their additional domains and subdomains.

In Service Provider view, the method will return domains based on the last subscription context, or all user domains if there is no last subscription context.

Use the following code:

$currentDomains = pm_Session::getCurrentDomains();
var_dump($domain->getName());