You are here

function shurly_generate_new_api_key in ShURLy 8

Same name and namespace in other branches
  1. 6 shurly_service/shurly_api_keys.inc \shurly_generate_new_api_key()
  2. 7 shurly_service/shurly_api_keys.inc \shurly_generate_new_api_key()

Generate a new api key for this user and put it into the database.

Return value

the new API key or FALSE on database insert error

1 call to shurly_generate_new_api_key()
shurly_api_key_form_submit in shurly_service/shurly_service.module
Submit handler for shurly_api_key_form(). Regenerate a user API key.

File

shurly_service/shurly_api_keys.inc, line 14

Code

function shurly_generate_new_api_key($uid) {

  // Be sure that the new key is unique.
  do {
    $key = md5(uniqid(rand(), TRUE)) . '_' . API_VERSION;
  } while (shurly_get_uid($key));
  \Drupal::database()
    ->query('DELETE FROM {shurly_keys} WHERE uid = :uid', [
    'uid' => $uid,
  ]);
  $result = \Drupal::database()
    ->query("INSERT INTO {shurly_keys} (uid, apikey) VALUES (:uid, :key)", [
    'uid' => $uid,
    'key' => $key,
  ]);
  return $result ? $key : FALSE;
}