From cc6f1c7cd86266c79a46cfd2e87fa293026dfb6a Mon Sep 17 00:00:00 2001 From: Loic d'Anterroches Date: Mon, 22 Feb 2010 22:27:31 +0100 Subject: [PATCH] Added ticket 165, multiple SSH keys per user. --- src/IDF/Form/UserAccount.php | 34 +++++++++++++---------- src/IDF/Key.php | 31 +++++++++++++++++++++ src/IDF/Plugin/SyncGit.php | 2 +- src/IDF/Views/User.php | 27 ++++++++++++++---- src/IDF/conf/urls.php | 6 ++++ src/IDF/relations.php | 2 ++ src/IDF/templates/idf/user/myaccount.html | 11 +++++++- 7 files changed, 90 insertions(+), 23 deletions(-) diff --git a/src/IDF/Form/UserAccount.php b/src/IDF/Form/UserAccount.php index 576b03a..a08bafb 100644 --- a/src/IDF/Form/UserAccount.php +++ b/src/IDF/Form/UserAccount.php @@ -94,7 +94,7 @@ class IDF_Form_UserAccount extends Pluf_Form $this->fields['ssh_key'] = new Pluf_Form_Field_Varchar( array('required' => false, - 'label' => __('Your public SSH key'), + 'label' => __('Add a public SSH key'), 'initial' => '', 'widget_attrs' => array('rows' => 3, 'cols' => 40), @@ -105,7 +105,6 @@ class IDF_Form_UserAccount extends Pluf_Form } - /** * Save the model in the database. * @@ -151,25 +150,17 @@ class IDF_Form_UserAccount extends Pluf_Form $this->user->setMessage(sprintf(__('A validation email has been sent to "%s" to validate the email address change.'), Pluf_esc($new_email))); } $this->user->setFromFormData($this->cleaned_data); - // Get keys - $keys = $this->user->get_idf_key_list(); - if ($keys->count() > 0) { - $key = $keys[0]; - if ('' !== $this->cleaned_data['ssh_key']) { - $key->content = $this->cleaned_data['ssh_key']; - } - } else { + // Add key as needed. + if ('' !== $this->cleaned_data['ssh_key']) { $key = new IDF_Key(); $key->user = $this->user; $key->content = $this->cleaned_data['ssh_key']; + if ($commit) { + $key->create(); + } } if ($commit) { $this->user->update(); - if ($key->id != '') { - $key->update(); - } else { - $key->create(); - } if ($update_pass) { /** * [signal] @@ -198,6 +189,19 @@ class IDF_Form_UserAccount extends Pluf_Form return $this->user; } + function clean_ssh_key() + { + $key = trim($this->cleaned_data['ssh_key']); + if (strlen($key) == 0) { + return ''; + } + $key = str_replace(array("\n", "\r"), '', $key); + if (!preg_match('#^ssh\-[a-z]{3}\s(\S+)\s\S+$#', $key, $matches)) { + throw new Pluf_Form_Invalid(__('The format of the key is not valid. It must start with ssh-dss or ssh-rsa, a long string on a single line and at the end a comment.')); + } + return $key; + } + function clean_last_name() { $last_name = trim($this->cleaned_data['last_name']); diff --git a/src/IDF/Key.php b/src/IDF/Key.php index d480321..5cc80c3 100644 --- a/src/IDF/Key.php +++ b/src/IDF/Key.php @@ -70,6 +70,11 @@ class IDF_Key extends Pluf_Model ); } + function showCompact() + { + return Pluf_Template::markSafe(Pluf_esc(substr($this->content, 0, 25)).' [...] '.Pluf_esc(substr($this->content, -55))); + } + function postSave($create=false) { /** @@ -97,4 +102,30 @@ class IDF_Key extends Pluf_Model 'IDF_Key', $params); } + function preDelete() + { + /** + * [signal] + * + * IDF_Key::preDelete + * + * [sender] + * + * IDF_Key + * + * [description] + * + * This signal allows an application to perform special + * operations before a key is deleted. + * + * [parameters] + * + * array('key' => $key) + * + */ + $params = array('key' => $this); + Pluf_Signal::send('IDF_Key::preDelete', + 'IDF_Key', $params); + } + } diff --git a/src/IDF/Plugin/SyncGit.php b/src/IDF/Plugin/SyncGit.php index 8bdc1bb..9e69143 100644 --- a/src/IDF/Plugin/SyncGit.php +++ b/src/IDF/Plugin/SyncGit.php @@ -47,6 +47,6 @@ class IDF_Plugin_SyncGit return; } @touch(Pluf::f('idf_plugin_syncgit_sync_file')); - @chmod(Pluf::f('idf_plugin_syncgit_sync_file'), 0666); + @chmod(Pluf::f('idf_plugin_syncgit_sync_file'), 0777); } } diff --git a/src/IDF/Views/User.php b/src/IDF/Views/User.php index f65851b..d5cae9a 100644 --- a/src/IDF/Views/User.php +++ b/src/IDF/Views/User.php @@ -124,20 +124,35 @@ class IDF_Views_User $form = new IDF_Form_UserAccount($data, $params); } $keys = $request->user->get_idf_key_list(); - if ($keys->count() > 0 and strlen($keys[0]->content) > 30) { - $ssh_key = Pluf_Template::markSafe(''.Pluf_esc(substr($keys[0]->content, 0, 30)).'...
'.__('Truncated for security reasons.').''); - } else { - $ssh_key = __('You have not upload your public SSH key yet.'); - } return Pluf_Shortcuts_RenderToResponse('idf/user/myaccount.html', array('page_title' => __('Your Account'), 'api_key' => $api_key, 'ext_pass' => $ext_pass, - 'ssh_key' => $ssh_key, + 'keys' => $keys, 'form' => $form), $request); } + /** + * Delete a SSH key. + * + * This is redirecting to the preferences + */ + public $deleteKey_precond = array('Pluf_Precondition::loginRequired'); + public function deleteKey($request, $match) + { + $url = Pluf_HTTP_URL_urlForView('IDF_Views_User::myAccount'); + if ($request->method == 'POST') { + $key = Pluf_Shortcuts_GetObjectOr404('IDF_Key', $match[1]); + if ($key->user != $request->user->id) { + return new Pluf_HTTP_Response_Forbidden($request); + } + $key->delete(); + $request->user->setMessage(__('The SSH key has been deleted.')); + } + return new Pluf_HTTP_Response_Redirect($url); + } + /** * Enter the key to change an email address. * diff --git a/src/IDF/conf/urls.php b/src/IDF/conf/urls.php index 495db53..105eb1b 100644 --- a/src/IDF/conf/urls.php +++ b/src/IDF/conf/urls.php @@ -422,4 +422,10 @@ $ctl[] = array('regex' => '#^/preferences/email/ak/(.*)/$#', 'model' => 'IDF_Views_User', 'method' => 'changeEmailDo'); +$ctl[] = array('regex' => '#^/preferences/key/(\d+)/delete/$#', + 'base' => $base, + 'model' => 'IDF_Views_User', + 'method' => 'deleteKey'); + + return $ctl; diff --git a/src/IDF/relations.php b/src/IDF/relations.php index a3c933f..1d7b979 100644 --- a/src/IDF/relations.php +++ b/src/IDF/relations.php @@ -74,5 +74,7 @@ Pluf_Signal::connect('IDF_Key::postSave', array('IDF_Plugin_SyncGit', 'entry')); Pluf_Signal::connect('IDF_Project::created', array('IDF_Plugin_SyncGit', 'entry')); +Pluf_Signal::connect('IDF_Key::preDelete', + array('IDF_Plugin_SyncGit', 'entry')); return $m; diff --git a/src/IDF/templates/idf/user/myaccount.html b/src/IDF/templates/idf/user/myaccount.html index 06ab12f..4c1e328 100644 --- a/src/IDF/templates/idf/user/myaccount.html +++ b/src/IDF/templates/idf/user/myaccount.html @@ -56,7 +56,6 @@ {$form.f.ssh_key.labelTag}: {if $form.f.ssh_key.errors}{$form.f.ssh_key.fieldErrors}{/if} -{$ssh_key}
{$form.f.ssh_key|unsafe}
{$form.f.ssh_key.help_text} @@ -80,6 +79,16 @@ + +{if count($keys)} + + +{foreach $keys as $key} +{/foreach} +
{trans 'Your Current SSH Keys'}
+{$key.showCompact()}
+
+{/if} {/block} {block context}