Added ticket 165, multiple SSH keys per user.

This commit is contained in:
Loic d'Anterroches 2010-02-22 22:27:31 +01:00
parent 5d24931d9b
commit cc6f1c7cd8
7 changed files with 90 additions and 23 deletions

View File

@ -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];
// Add key as needed.
if ('' !== $this->cleaned_data['ssh_key']) {
$key->content = $this->cleaned_data['ssh_key'];
}
} else {
$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']);

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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('<span class="mono">'.Pluf_esc(substr($keys[0]->content, 0, 30)).'...</span><br /><span class="helptext">'.__('Truncated for security reasons.').'</span>');
} 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.
*

View File

@ -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;

View File

@ -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;

View File

@ -56,7 +56,6 @@
<tr>
<th>{$form.f.ssh_key.labelTag}:</th>
<td>{if $form.f.ssh_key.errors}{$form.f.ssh_key.fieldErrors}{/if}
{$ssh_key}<br />
{$form.f.ssh_key|unsafe}<br />
<span class="helptext">{$form.f.ssh_key.help_text}</span>
</td>
@ -80,6 +79,16 @@
</tr>
</table>
</form>
{if count($keys)}
<table summary=" " class="recent-issues">
<tr><th colspan="2">{trans 'Your Current SSH Keys'}</th></tr>
{foreach $keys as $key}<tr><td>
<span class="mono">{$key.showCompact()}</span></td><td> <form class="star" method="post" action="{url 'IDF_Views_User::deleteKey', array($key.id)}"><input type="image" src="{media '/idf/img/trash.png'}" name="submit" value="{trans 'Delete this key'}" /></form>
</td>
</tr>{/foreach}
</table>
{/if}
{/block}
{block context}
<div class="issue-submit-info">