You are here

public function OAuthStoreSQL::updateConsumer in Lingotek Translation 7.3

Same name and namespace in other branches
  1. 7.7 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::updateConsumer()
  2. 7.2 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::updateConsumer()
  3. 7.4 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::updateConsumer()
  4. 7.5 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::updateConsumer()
  5. 7.6 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::updateConsumer()

* Insert/update a new consumer with this server (we will be the server) * When this is a new consumer, then also generate the consumer key and secret. * Never updates the consumer key and secret. * When the id is set, then the key and secret must correspond to the entry * being updated. * * (This is the registry at the server, registering consumers ;-) ) * *

Parameters

array consumer: * @param int user_id user registering this consumer * @param boolean user_is_admin * @return string consumer key

Overrides OAuthStoreAbstract::updateConsumer

File

lib/oauth-php/library/store/OAuthStoreSQL.php, line 915

Class

OAuthStoreSQL

Code

public function updateConsumer($consumer, $user_id, $user_is_admin = false) {
  if (!$user_is_admin) {
    foreach (array(
      'requester_name',
      'requester_email',
    ) as $f) {
      if (empty($consumer[$f])) {
        throw new OAuthException2('The field "' . $f . '" must be set and non empty');
      }
    }
  }
  if (!empty($consumer['id'])) {
    if (empty($consumer['consumer_key'])) {
      throw new OAuthException2('The field "consumer_key" must be set and non empty');
    }
    if (!$user_is_admin && empty($consumer['consumer_secret'])) {
      throw new OAuthException2('The field "consumer_secret" must be set and non empty');
    }

    // Check if the current user can update this server definition
    if (!$user_is_admin) {
      $osr_usa_id_ref = $this
        ->query_one('
									SELECT osr_usa_id_ref
									FROM oauth_server_registry
									WHERE osr_id = %d
									', $consumer['id']);
      if ($osr_usa_id_ref != $user_id) {
        throw new OAuthException2('The user "' . $user_id . '" is not allowed to update this consumer');
      }
    }
    else {

      // User is an admin, allow a key owner to be changed or key to be shared
      if (array_key_exists('user_id', $consumer)) {
        if (is_null($consumer['user_id'])) {
          $this
            ->query('
							UPDATE oauth_server_registry
							SET osr_usa_id_ref = NULL
							WHERE osr_id = %d
							', $consumer['id']);
        }
        else {
          $this
            ->query('
							UPDATE oauth_server_registry
							SET osr_usa_id_ref = %d
							WHERE osr_id = %d
							', $consumer['user_id'], $consumer['id']);
        }
      }
    }
    $this
      ->query('
				UPDATE oauth_server_registry
				SET osr_requester_name		= \'%s\',
					osr_requester_email		= \'%s\',
					osr_callback_uri		= \'%s\',
					osr_application_uri		= \'%s\',
					osr_application_title	= \'%s\',
					osr_application_descr	= \'%s\',
					osr_application_notes	= \'%s\',
					osr_application_type	= \'%s\',
					osr_application_commercial = IF(%d,1,0),
					osr_timestamp			= NOW()
				WHERE osr_id              = %d
				  AND osr_consumer_key    = \'%s\'
				  AND osr_consumer_secret = \'%s\'
				', $consumer['requester_name'], $consumer['requester_email'], isset($consumer['callback_uri']) ? $consumer['callback_uri'] : '', isset($consumer['application_uri']) ? $consumer['application_uri'] : '', isset($consumer['application_title']) ? $consumer['application_title'] : '', isset($consumer['application_descr']) ? $consumer['application_descr'] : '', isset($consumer['application_notes']) ? $consumer['application_notes'] : '', isset($consumer['application_type']) ? $consumer['application_type'] : '', isset($consumer['application_commercial']) ? $consumer['application_commercial'] : 0, $consumer['id'], $consumer['consumer_key'], $consumer['consumer_secret']);
    $consumer_key = $consumer['consumer_key'];
  }
  else {
    $consumer_key = $this
      ->generateKey(true);
    $consumer_secret = $this
      ->generateKey();

    // When the user is an admin, then the user can be forced to something else that the user
    if ($user_is_admin && array_key_exists('user_id', $consumer)) {
      if (is_null($consumer['user_id'])) {
        $owner_id = 'NULL';
      }
      else {
        $owner_id = intval($consumer['user_id']);
      }
    }
    else {

      // No admin, take the user id as the owner id.
      $owner_id = intval($user_id);
    }
    $this
      ->query('
				INSERT INTO oauth_server_registry
				SET osr_enabled				= 1,
					osr_status				= \'active\',
					osr_usa_id_ref			= \'%s\',
					osr_consumer_key		= \'%s\',
					osr_consumer_secret		= \'%s\',
					osr_requester_name		= \'%s\',
					osr_requester_email		= \'%s\',
					osr_callback_uri		= \'%s\',
					osr_application_uri		= \'%s\',
					osr_application_title	= \'%s\',
					osr_application_descr	= \'%s\',
					osr_application_notes	= \'%s\',
					osr_application_type	= \'%s\',
					osr_application_commercial = IF(%d,1,0),
					osr_timestamp			= NOW(),
					osr_issue_date			= NOW()
				', $owner_id, $consumer_key, $consumer_secret, $consumer['requester_name'], $consumer['requester_email'], isset($consumer['callback_uri']) ? $consumer['callback_uri'] : '', isset($consumer['application_uri']) ? $consumer['application_uri'] : '', isset($consumer['application_title']) ? $consumer['application_title'] : '', isset($consumer['application_descr']) ? $consumer['application_descr'] : '', isset($consumer['application_notes']) ? $consumer['application_notes'] : '', isset($consumer['application_type']) ? $consumer['application_type'] : '', isset($consumer['application_commercial']) ? $consumer['application_commercial'] : 0);
  }
  return $consumer_key;
}