public function save() { if (!$this->loaded) { Z_Core::debug("Not saving unloaded key {$this->id}"); return; } if (!$this->userID) { throw new Exception("Cannot save key without userID"); } if (!$this->name) { throw new Exception("Cannot save key without name"); } if (strlen($this->name) > 255) { throw new Exception("Key name too long", Z_ERROR_KEY_NAME_TOO_LONG); } Zotero_DB::beginTransaction(); if (!$this->key) { $isNew = true; $this->key = Zotero_Keys::generate(); } else { $isNew = false; } $fields = array('key', 'userID', 'name'); $sql = "INSERT INTO `keys` (keyID, `key`, userID, name) VALUES (?, ?, ?, ?)"; $params = array($this->id); foreach ($fields as $field) { $params[] = $this->{$field}; } $sql .= " ON DUPLICATE KEY UPDATE "; $q = array(); foreach ($fields as $field) { $q[] = "`{$field}`=?"; $params[] = $this->{$field}; } $sql .= implode(", ", $q); $insertID = Zotero_DB::query($sql, $params); if (!$this->id) { if (!$insertID) { throw new Exception("Key id not available after INSERT"); } $this->id = $insertID; } if (!$insertID) { $sql = "SELECT * FROM keyPermissions WHERE keyID=?"; $oldRows = Zotero_DB::query($sql, $this->id); } $oldPermissions = []; $newPermissions = []; $librariesToAdd = []; $librariesToRemove = []; // Massage rows into permissions format if (!$isNew && isset($oldRows)) { foreach ($oldRows as $row) { $oldPermissions[$row['libraryID']][$row['permission']] = !!$row['granted']; } } // Delete existing permissions $sql = "DELETE FROM keyPermissions WHERE keyID=?"; Zotero_DB::query($sql, $this->id); if (isset($this->changed['permissions'])) { foreach ($this->changed['permissions'] as $libraryID => $p) { foreach ($p as $permission => $changed) { $enabled = $this->permissions[$libraryID][$permission]; if (!$enabled) { continue; } $sql = "INSERT INTO keyPermissions VALUES (?, ?, ?, ?)"; // TODO: support negative permissions Zotero_DB::query($sql, array($this->id, $libraryID, $permission, 1)); $newPermissions[$libraryID][$permission] = true; } } } $this->permissions = $newPermissions; // Send notifications for added and removed API key – library pairs if (!$isNew) { $librariesToAdd = $this->permissionsDiff($oldPermissions, $newPermissions, $this->userID); $librariesToRemove = $this->permissionsDiff($newPermissions, $oldPermissions, $this->userID); if ($librariesToAdd) { Zotero_Notifier::trigger('add', 'apikey-library', array_map(function ($libraryID) { return $this->key . "-" . $libraryID; }, array_unique($librariesToAdd))); } if ($librariesToRemove) { Zotero_Notifier::trigger('remove', 'apikey-library', array_map(function ($libraryID) { return $this->key . "-" . $libraryID; }, array_unique($librariesToRemove))); } } Zotero_DB::commit(); $this->load(); return $this->id; }
public function save() { if (!$this->loaded) { Z_Core::debug("Not saving unloaded key {$this->id}"); return; } if (!$this->userID) { throw new Exception("Cannot save key without userID"); } if (!$this->name) { throw new Exception("Cannot save key without name"); } if (strlen($this->name) > 255) { throw new Exception("Key name too long", Z_ERROR_KEY_NAME_TOO_LONG); } Zotero_DB::beginTransaction(); if (!$this->key) { $this->key = Zotero_Keys::generate(); } $fields = array('key', 'userID', 'name'); $sql = "INSERT INTO `keys` (keyID, `key`, userID, name) VALUES (?, ?, ?, ?)"; $params = array($this->id); foreach ($fields as $field) { $params[] = $this->{$field}; } $sql .= " ON DUPLICATE KEY UPDATE "; $q = array(); foreach ($fields as $field) { $q[] = "`{$field}`=?"; $params[] = $this->{$field}; } $sql .= implode(", ", $q); $insertID = Zotero_DB::query($sql, $params); if (!$this->id) { if (!$insertID) { throw new Exception("Key id not available after INSERT"); } $this->id = $insertID; } // Delete existing permissions $sql = "DELETE FROM keyPermissions WHERE keyID=?"; Zotero_DB::query($sql, $this->id); if (isset($this->changed['permissions'])) { foreach ($this->changed['permissions'] as $libraryID => $p) { foreach ($p as $permission => $changed) { $enabled = $this->permissions[$libraryID][$permission]; if (!$enabled) { continue; } $sql = "INSERT INTO keyPermissions VALUES (?, ?, ?, ?)"; // TODO: support negative permissions Zotero_DB::query($sql, array($this->id, $libraryID, $permission, 1)); } } } Zotero_DB::commit(); $this->load(); return $this->id; }