Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.

Moderator: amosbatto

Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
#824228
Hello,
I need to compare the user uid send via the rest endpoint (@url GET /clients/:userUid) against logged user I tried to use the global variable
Code: Select all
$_SESSION["USER_LOGGED"]
but it is undefined in the class context.

Code: Select all
<?php
  namespace Services\Api\Estamp;
  use Luracast\Restler\RestException;
  use ProcessMaker\Services\Api;
  use \ProcessMaker\Util;
  use \Exception;


  /**
  * @protected
  */
class Client extends Api{
    /**
     * @url GET /clients/:userUid
     * 
     * @access protected
     * 
     * 
    */
    public function getClients(userUid){
//I want to get the Uid for the logged User here

}
}
is there any way to solve this?
#824240
Are you creating a custom REST endpoint in a plugin?

When you define your endpoint in your src/Services/Api/PluginName/someFile.php file, you need to add this line to the header of the endpoint's function:
@access protected

Then, in your function, you can obtain the ID of the logged-in user with this code:
$userId = $this->getUserId();


Here is an example taken from my extraRest plugin:
Code: Select all
    /**
     * Get the case counters for a user, meaning the number of To Do, Draft, 
     * Paused, Cancelled, Participated, Unassigned and Process Supervisor's Review cases. 
     * If a user's unique ID is not specified in the query string, then the 
     * case counters will be for the current logged-in user. If getting the case
     * counters for another user, then the PM_ALLCASE permission needs to be
     * in the logged user's role. 
     * 
     * @url GET /cases/counters
     * @access protected
     *
     * @param string $usr_uid Optional. Unique ID of a user. If not included 
     * then will return the case counters for the current logged-in user. {@from query}{@min 32}{@max 32}
     * 
     * @return array An array with the number of cases in the following categories: 
     * array(
     *    'to_do'      => X, //number of To Do cases
     *    'draft'      => X, //number of Draft cases
     *    'cancelled'  => X, //number of Cancelled cases
     *    'sent'       => X, //number of Participated cases
     *    'paused'     => X, //number of Paused cases
     *    'completed'  => X, //number of Completed cases
     *    'selfservice'=> X, //number of Unassigned cases (for tasks with 
     *                       //Self Service o Self Service Value Based Assignment)
     *    'to_revise'  =  X, //number of Process Supervisor > Review cases
     * ) 
     */
    public function getCaseCounters($usr_uid=null)
    {
        try {
            if (empty($usr_uid)) {
                $usr_uid = $this->getUserId();
            }
            
            if ($usr_uid != $this->getUserId() and $this->userCanAccess('PM_ALLCASES') == 0 ) {
                throw new \Exception("Logged-in user needs the PM_ALLCASES permission to access ".
                    "the case counters of other users.");
            }
            
            $aTypes = array('to_do', 'draft', 'cancelled', 'sent', 'paused', 'completed', 'selfservice', 'to_revise', 'to_reassign');            
            $oCase = new \ProcessMaker\BusinessModel\Cases();
            $aCount = $oCase->getListCounters($usr_uid, $aTypes);

            return $aCount;
        } 
        catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }
    
If you need more examples, download my plugin and look at the source code.
View Dynaform in HTML Page

You might also want to check the [url=https://wiki[…]

You are going to have to put the configuration in […]

Hola, hay alguna forma de hacer que estos log se […]

Yo trabajo con la versión PM 3.2.3 - Ente[…]