You are here

function services_keyauth_admin_keys_save in Services 6.2

Same name and namespace in other branches
  1. 7 auth/services_keyauth/services_keyauth.admin.inc \services_keyauth_admin_keys_save()

Save an API key.

Parameters

$key: Associative array containing API key information.

Return value

A constant, either SAVED_NEW or SAVED_UDPATED.

1 call to services_keyauth_admin_keys_save()
services_keyauth_admin_keys_form_submit in auth/services_keyauth/services_keyauth.admin.inc
Submit callback for services_keyauth_admin_keys_form().

File

auth/services_keyauth/services_keyauth.admin.inc, line 144
Generate security keys.

Code

function services_keyauth_admin_keys_save(&$key) {
  $is_existing = FALSE;
  $key['kid'] = !empty($key['kid']) ? $key['kid'] : md5(uniqid(mt_rand(), TRUE));
  $is_existing = db_result(db_query("SELECT count(*) FROM {services_keys}\n    WHERE kid = '%s'", $key['kid']));
  if ($is_existing) {
    db_query("UPDATE {services_keys} SET title = '%s', domain = '%s'\n      WHERE kid = '%s'", $key['title'], $key['domain'], $key['kid']);
    db_query("DELETE FROM {services_key_permissions} WHERE kid = '%s'", $key['kid']);
    $return = SAVED_UPDATED;
  }
  else {
    db_query("INSERT INTO {services_keys} (kid, title, domain)\n      VALUES ('%s', '%s', '%s')", $key['kid'], $key['title'], $key['domain']);
    $return = SAVED_NEW;
  }
  foreach ($key['method_access'] as $method => $value) {
    if ($value) {
      db_query("INSERT INTO {services_key_permissions} (kid, method) VALUES ('%s', '%s')", $key['kid'], $value);
    }
  }
  return $return;
}