You are here

function services_keyauth_admin_keys_save in Services 7

Same name and namespace in other branches
  1. 6.2 auth/services_keyauth/services_keyauth.admin.inc \services_keyauth_admin_keys_save()
1 call to services_keyauth_admin_keys_save()
services_keyauth_admin_keys_form_submit in auth/services_keyauth/services_keyauth.admin.inc

File

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

Code

function services_keyauth_admin_keys_save(&$key) {
  $key['kid'] = !empty($key['kid']) ? $key['kid'] : md5(uniqid(mt_rand(), TRUE));
  $is_existing = (bool) db_query_range("SELECT 1 FROM {services_keys} WHERE kid = :kid", 0, 1, array(
    ':kid' => $key['kid'],
  ))
    ->fetchField();
  if ($is_existing) {
    db_update('services_keys')
      ->fields(array(
      'title' => $key['title'],
      'domain' => $key['domain'],
    ))
      ->condition('kid', $key['kid'], '=')
      ->execute();
    db_delete('services_key_permissions')
      ->condition('kid', $key['kid'])
      ->execute();
    $return = SAVED_UPDATED;
  }
  else {
    db_insert('services_keys')
      ->fields(array(
      'kid',
      'title',
      'domain',
    ))
      ->values(array(
      'kid' => $key['kid'],
      'title' => $key['title'],
      'domain' => $key['domain'],
    ))
      ->execute();
    $return = SAVED_NEW;
  }
  foreach ($key['method_access'] as $method => $value) {
    if ($value) {
      db_insert('services_key_permissions')
        ->fields(array(
        'kid',
        'method',
      ))
        ->values(array(
        'kid' => $key['kid'],
        'method' => $value,
      ))
        ->execute();
    }
  }
  return $return;
}