Example #1
0
 /**
  * Returns the user data stored trough the Genetsis ID personal identifier.
  * The identifiers could be: id (ckusid), screenName, email, dni
  * Sample: array('id'=>'XXXX','screenName'=>'xxxx');
  *
  * @param array The Genetsis IDs identifier to search, 'identifier' => 'value'
  * @return array A vector of {@link User} objects with user's
  *     personal data. The array could be empty.
  * @throws /Exception
  */
 public static function getUsers($identifiers)
 {
     $druid_user = array();
     if (is_array($identifiers)) {
         try {
             if (!($druid_user_data = FileCache::get('user-' . reset($identifiers)))) {
                 Identity::getLogger()->debug('Identifier: ' . reset($identifiers) . ' is Not in Cache System');
                 $client_token = Identity::getThings()->getClientToken();
                 if (is_null($client_token)) {
                     throw new Exception('The clientToken is empty');
                 }
                 /**
                  * Parameters:
                  * oauth_token: client token
                  * s (select): dynamic user data to be returned
                  * f (from): User
                  * w (where): param with OR w.param1&w.param2...
                  */
                 $params = array();
                 $params['oauth_token'] = $client_token->getValue();
                 $params['s'] = "*";
                 $params['f'] = "User";
                 foreach ($identifiers as $key => $val) {
                     $params['w.' . $key] = $val;
                 }
                 $base = OAuthConfig::getApiUrl('api.user', 'base_url');
                 $api = OAuthConfig::getApiUrl('api.user', 'user');
                 $response = Request::execute($base . $api, $params, Request::HTTP_POST);
                 if ($response['code'] != 200 || !isset($response['result']->data) || $response['result']->count == '0') {
                     throw new Exception('The data retrieved is empty');
                 }
                 $druid_user = $response['result']->data;
                 FileCache::set('user-' . reset($identifiers), $druid_user, self::USER_TTL);
             } else {
                 Identity::getLogger()->debug('Identifier: ' . reset($identifiers) . ' is in Cache System');
                 $druid_user = json_decode(json_encode($druid_user_data));
             }
         } catch (Exception $e) {
             Identity::getLogger()->error($e->getMessage());
         }
     }
     return $druid_user;
 }
Example #2
0
 /**
  * Checks and updates the "client_token" and cache if we have a valid one
  * If we don not have a Client Token in session, we check if we have a cookie
  * If we don not have a client Token in session or in a cookie, We request a new Client Token.
  * This method set the Client Token in Things
  *
  * @return void
  * @throws \Exception
  */
 private static function checkAndUpdateClientToken()
 {
     try {
         self::$logger->debug('Checking and update client_token.');
         if (!($client_token = unserialize(FileCache::get('client_token'))) instanceof ClientToken || $client_token->getValue() == '') {
             self::$logger->debug('Get Client token');
             if (self::$gid_things->getClientToken() == null || OAuth::getStoredToken(iTokenTypes::CLIENT_TOKEN) == null) {
                 self::$logger->debug('Not has clientToken in session or cookie');
                 if (!($client_token = OAuth::getStoredToken(iTokenTypes::CLIENT_TOKEN))) {
                     self::$logger->debug('Token Cookie does not exists. Requesting a new one.');
                     $client_token = OAuth::doGetClientToken(OauthConfig::getEndpointUrl('token_endpoint'));
                 }
                 self::$gid_things->setClientToken($client_token);
             } else {
                 self::$logger->debug('Client Token from session');
             }
             FileCache::set('client_token', serialize(self::$gid_things->getClientToken()), self::$gid_things->getClientToken()->getExpiresIn());
         } else {
             self::$logger->debug('Client Token from cache');
             self::$gid_things->setClientToken($client_token);
         }
     } catch (Exception $e) {
         throw $e;
     }
 }
Example #3
0
 /**
  * Loads a XML from file.
  *
  * @param string Full path to file.
  * @return void
  * @throws \Exception If there is an error: file doesn't exists, not well-formed, ...
  */
 private static function loadXml($file)
 {
     if (($file = trim((string) $file)) == '' || !file_exists($file) || !is_file($file) || !is_readable($file)) {
         throw new Exception('The config file is not found or canĀ“t be readable' . $file);
     }
     $xmlObj = new DOMDocument();
     $xmlObj->load($file);
     try {
         foreach ($xmlObj->getElementsByTagName("oauth-config")->item(0)->attributes as $attrName => $attrNode) {
             if (!($version = $attrNode->value)) {
                 throw new Exception('Not version');
             }
         }
         if ($version !== Config::CONF_VERSION) {
             throw new Exception('Not correct version');
         }
         if (!($credentials = $xmlObj->getElementsByTagName("credentials")->item(0))) {
             throw new Exception('Not credentials');
         }
         foreach ($credentials->childNodes as $node) {
             if ($node->nodeName === 'clientid') {
                 self::$config['clientid'] = $node->nodeValue;
             } else {
                 if ($node->nodeName === 'clientsecret') {
                     self::$config['clientsecret'] = $node->nodeValue;
                 }
             }
         }
         if (!isset(self::$config['clientid']) || !isset(self::$config['clientsecret'])) {
             throw new Exception('Not defined credentials');
         }
         //data
         if (!($data = $xmlObj->getElementsByTagName("data")->item(0))) {
             throw new Exception('No data node');
         }
         foreach ($data->childNodes as $node) {
             if ($node->nodeName === 'name') {
                 self::$config['name'] = $node->nodeValue;
             } else {
                 if ($node->nodeName === 'brand') {
                     self::$config['brand'] = $node->attributes->getNamedItem('key')->nodeValue;
                 } else {
                     if ($node->nodeName === 'opi') {
                         self::$config['opi'] = $node->nodeValue;
                     }
                 }
             }
         }
         if (!isset(self::$config['name'])) {
             throw new Exception('No name defined');
         }
         self::getParserXML($xmlObj, 'redirections', 'type');
         self::getParserXML($xmlObj, 'endpoints', 'id');
         // Parse urls from apis
         $apis = $xmlObj->getElementsByTagName("apis")->item(0);
         foreach ($apis->childNodes as $singleApi) {
             if ($singleApi->nodeType == XML_ELEMENT_NODE) {
                 $apiName = trim($singleApi->getAttribute('name'));
                 self::$config['apis'][$apiName]['base_url'] = trim($singleApi->getAttribute('base-url'));
                 foreach ($singleApi->childNodes as $urlNode) {
                     if ($urlNode->nodeType == XML_ELEMENT_NODE) {
                         self::$config['apis'][$apiName][$urlNode->getAttribute('id')] = $urlNode->nodeValue;
                     }
                 }
             }
         }
         self::getParserXML($xmlObj, 'sections', 'id');
         FileCache::set('config', self::$config, 600);
     } catch (Exception $e) {
         throw new Exception('The config file is not valid - ' . $e->getMessage());
     }
 }