Added ticket 165, multiple SSH keys per user.
This commit is contained in:
parent
5d24931d9b
commit
cc6f1c7cd8
@ -94,7 +94,7 @@ class IDF_Form_UserAccount extends Pluf_Form
|
|||||||
|
|
||||||
$this->fields['ssh_key'] = new Pluf_Form_Field_Varchar(
|
$this->fields['ssh_key'] = new Pluf_Form_Field_Varchar(
|
||||||
array('required' => false,
|
array('required' => false,
|
||||||
'label' => __('Your public SSH key'),
|
'label' => __('Add a public SSH key'),
|
||||||
'initial' => '',
|
'initial' => '',
|
||||||
'widget_attrs' => array('rows' => 3,
|
'widget_attrs' => array('rows' => 3,
|
||||||
'cols' => 40),
|
'cols' => 40),
|
||||||
@ -105,7 +105,6 @@ class IDF_Form_UserAccount extends Pluf_Form
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save the model in the database.
|
* 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->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);
|
$this->user->setFromFormData($this->cleaned_data);
|
||||||
// Get keys
|
// Add key as needed.
|
||||||
$keys = $this->user->get_idf_key_list();
|
|
||||||
if ($keys->count() > 0) {
|
|
||||||
$key = $keys[0];
|
|
||||||
if ('' !== $this->cleaned_data['ssh_key']) {
|
if ('' !== $this->cleaned_data['ssh_key']) {
|
||||||
$key->content = $this->cleaned_data['ssh_key'];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$key = new IDF_Key();
|
$key = new IDF_Key();
|
||||||
$key->user = $this->user;
|
$key->user = $this->user;
|
||||||
$key->content = $this->cleaned_data['ssh_key'];
|
$key->content = $this->cleaned_data['ssh_key'];
|
||||||
|
if ($commit) {
|
||||||
|
$key->create();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($commit) {
|
if ($commit) {
|
||||||
$this->user->update();
|
$this->user->update();
|
||||||
if ($key->id != '') {
|
|
||||||
$key->update();
|
|
||||||
} else {
|
|
||||||
$key->create();
|
|
||||||
}
|
|
||||||
if ($update_pass) {
|
if ($update_pass) {
|
||||||
/**
|
/**
|
||||||
* [signal]
|
* [signal]
|
||||||
@ -198,6 +189,19 @@ class IDF_Form_UserAccount extends Pluf_Form
|
|||||||
return $this->user;
|
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()
|
function clean_last_name()
|
||||||
{
|
{
|
||||||
$last_name = trim($this->cleaned_data['last_name']);
|
$last_name = trim($this->cleaned_data['last_name']);
|
||||||
|
@ -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)
|
function postSave($create=false)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -97,4 +102,30 @@ class IDF_Key extends Pluf_Model
|
|||||||
'IDF_Key', $params);
|
'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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,6 @@ class IDF_Plugin_SyncGit
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@touch(Pluf::f('idf_plugin_syncgit_sync_file'));
|
@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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,20 +124,35 @@ class IDF_Views_User
|
|||||||
$form = new IDF_Form_UserAccount($data, $params);
|
$form = new IDF_Form_UserAccount($data, $params);
|
||||||
}
|
}
|
||||||
$keys = $request->user->get_idf_key_list();
|
$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',
|
return Pluf_Shortcuts_RenderToResponse('idf/user/myaccount.html',
|
||||||
array('page_title' => __('Your Account'),
|
array('page_title' => __('Your Account'),
|
||||||
'api_key' => $api_key,
|
'api_key' => $api_key,
|
||||||
'ext_pass' => $ext_pass,
|
'ext_pass' => $ext_pass,
|
||||||
'ssh_key' => $ssh_key,
|
'keys' => $keys,
|
||||||
'form' => $form),
|
'form' => $form),
|
||||||
$request);
|
$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.
|
* Enter the key to change an email address.
|
||||||
*
|
*
|
||||||
|
@ -422,4 +422,10 @@ $ctl[] = array('regex' => '#^/preferences/email/ak/(.*)/$#',
|
|||||||
'model' => 'IDF_Views_User',
|
'model' => 'IDF_Views_User',
|
||||||
'method' => 'changeEmailDo');
|
'method' => 'changeEmailDo');
|
||||||
|
|
||||||
|
$ctl[] = array('regex' => '#^/preferences/key/(\d+)/delete/$#',
|
||||||
|
'base' => $base,
|
||||||
|
'model' => 'IDF_Views_User',
|
||||||
|
'method' => 'deleteKey');
|
||||||
|
|
||||||
|
|
||||||
return $ctl;
|
return $ctl;
|
||||||
|
@ -74,5 +74,7 @@ Pluf_Signal::connect('IDF_Key::postSave',
|
|||||||
array('IDF_Plugin_SyncGit', 'entry'));
|
array('IDF_Plugin_SyncGit', 'entry'));
|
||||||
Pluf_Signal::connect('IDF_Project::created',
|
Pluf_Signal::connect('IDF_Project::created',
|
||||||
array('IDF_Plugin_SyncGit', 'entry'));
|
array('IDF_Plugin_SyncGit', 'entry'));
|
||||||
|
Pluf_Signal::connect('IDF_Key::preDelete',
|
||||||
|
array('IDF_Plugin_SyncGit', 'entry'));
|
||||||
|
|
||||||
return $m;
|
return $m;
|
||||||
|
@ -56,7 +56,6 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<th>{$form.f.ssh_key.labelTag}:</th>
|
<th>{$form.f.ssh_key.labelTag}:</th>
|
||||||
<td>{if $form.f.ssh_key.errors}{$form.f.ssh_key.fieldErrors}{/if}
|
<td>{if $form.f.ssh_key.errors}{$form.f.ssh_key.fieldErrors}{/if}
|
||||||
{$ssh_key}<br />
|
|
||||||
{$form.f.ssh_key|unsafe}<br />
|
{$form.f.ssh_key|unsafe}<br />
|
||||||
<span class="helptext">{$form.f.ssh_key.help_text}</span>
|
<span class="helptext">{$form.f.ssh_key.help_text}</span>
|
||||||
</td>
|
</td>
|
||||||
@ -80,6 +79,16 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</form>
|
</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}
|
||||||
{block context}
|
{block context}
|
||||||
<div class="issue-submit-info">
|
<div class="issue-submit-info">
|
||||||
|
Loading…
Reference in New Issue
Block a user