/**
  * Find the relevant MemberApiKey object for the given key
  */
 public static function findByKey($key)
 {
     $matches = MemberApiKey::get()->filter(['ApiKey' => $key]);
     switch ($matches->count()) {
         case 1:
             return $matches->first();
         case 0:
             return null;
         default:
             throw new LogicException("Multiple MemberApiKey records for '{$key}' - database corrupt!");
     }
 }
 public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
 {
     $headerName = Config::inst()->get('ApiKeyRequestFilter', 'header_name');
     if ($key = $request->getHeader($headerName)) {
         try {
             $matchingKey = MemberApiKey::findByKey($key);
         } catch (LogicException $e) {
         }
         if ($matchingKey) {
             // Log-in can't have session injected, we need to to push $session into the global state
             $controller = new Controller();
             $controller->setSession($session);
             $controller->pushCurrent();
             $matchingKey->Member()->logIn();
             // Undo our global state manipulation
             $controller->popCurrent();
             $matchingKey->markUsed();
         } else {
             throw new SS_HTTPResponse_Exception("Bad X-API-Key", 400);
         }
     }
     return true;
 }
 public function handleAction(GridField $gridField, $actionName, $arguments, $data)
 {
     if ($actionName == 'addapikey') {
         MemberApiKey::createKey($gridField->getForm()->getRecord()->ID);
     }
 }