/** * Creates the assigned key to the resource * * @param ResourceConfigForm $form * * @return bool */ public static function beforeAdd(ResourceConfigForm $form) { $configDir = Icinga::app()->getConfigDir(); $user = $form->getElement('user')->getValue(); $filePath = $configDir . '/ssh/' . $user; if (!file_exists($filePath)) { $file = File::create($filePath, 0600); } else { $form->error(sprintf($form->translate('The private key for the user "%s" is already exists.'), $user)); return false; } $file->fwrite($form->getElement('private_key')->getValue()); $form->getElement('private_key')->setValue($configDir . '/ssh/' . $user); return true; }
/** * Write the preferences * * @throws NotWritableError In case the INI file cannot be written */ public function write() { if ($this->writer === null) { if (!file_exists($this->preferencesFile)) { if (!is_writable($this->getStoreConfig()->location)) { throw new NotWritableError(sprintf('Path to the preferences INI files %s is not writable', $this->getStoreConfig()->location)); } File::create($this->preferencesFile); } if (!is_writable($this->preferencesFile)) { throw new NotWritableError('Preferences INI file ' . $this->preferencesFile . ' for user ' . $this->getUser()->getUsername() . ' is not writable'); } $this->writer = new PreservingIniWriter(array('config' => new Zend_Config($this->preferences), 'filename' => $this->preferencesFile)); } $this->writer->write(); }
/** * Save configuration to the given INI file * * @param string|null $filePath The path to the INI file or null in case this config's path should be used * @param int $fileMode The file mode to store the file with * * @throws LogicException In case this config has no path and none is passed in either * @throws NotWritableError In case the INI file cannot be written * * @todo create basepath and throw NotWritableError in case its not possible */ public function saveIni($filePath = null, $fileMode = 0660) { if ($filePath === null && $this->configFile) { $filePath = $this->configFile; } elseif ($filePath === null) { throw new LogicException('You need to pass $filePath or set a path using Config::setConfigFile()'); } if (!file_exists($filePath)) { File::create($filePath, $fileMode); } $this->getIniWriter($filePath, $fileMode)->write(); }