Added the upload of the SSH key for the end user.

This commit is contained in:
Loic d'Anterroches 2009-01-14 23:05:52 +01:00
parent 7f32c6f377
commit 419601bb92
9 changed files with 257 additions and 9 deletions

View File

@ -82,6 +82,16 @@ class IDF_Form_UserAccount extends Pluf_Form
'size' => 15,
),
));
$this->fields['ssh_key'] = new Pluf_Form_Field_Varchar(
array('required' => false,
'label' => __('Your public SSH key'),
'initial' => '',
'widget_attrs' => array('rows' => 3,
'cols' => 40),
'widget' => 'Pluf_Form_Widget_TextareaInput',
'help_text' => __('Be careful to provide your public key and not your private key!')
));
}
@ -107,8 +117,22 @@ class IDF_Form_UserAccount extends Pluf_Form
$update_pass = true;
}
$this->user->setFromFormData($this->cleaned_data);
// Get keys
$keys = $this->user->get_idf_key_list();
if ($keys->count() > 0) {
$key = $keys[0];
} else {
$key = new IDF_Key();
$key->user = $this->user;
}
$key->content = $this->cleaned_data['ssh_key'];
if ($commit) {
$this->user->update();
if ($key->id != '') {
$key->update();
} else {
$key->create();
}
if ($update_pass) {
/**
* [signal]

100
src/IDF/Key.php Normal file
View File

@ -0,0 +1,100 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Storage of the SSH keys.
*
*/
class IDF_Key extends Pluf_Model
{
public $_model = __CLASS__;
function init()
{
$this->_a['table'] = 'idf_keys';
$this->_a['model'] = __CLASS__;
$this->_a['cols'] = array(
// It is mandatory to have an "id" column.
'id' =>
array(
'type' => 'Pluf_DB_Field_Sequence',
//It is automatically added.
'blank' => true,
),
'user' =>
array(
'type' => 'Pluf_DB_Field_Foreignkey',
'model' => 'Pluf_User',
'blank' => false,
'verbose' => __('user'),
),
'content' =>
array(
'type' => 'Pluf_DB_Field_Text',
'blank' => false,
'verbose' => __('ssh key'),
),
);
// WARNING: Not using getSqlTable on the Pluf_User object to
// avoid recursion.
$t_users = $this->_con->pfx.'users';
$this->_a['views'] = array(
'join_user' =>
array(
'join' => 'LEFT JOIN '.$t_users
.' ON '.$t_users.'.id='.$this->_con->qn('user'),
'select' => $this->getSelect().', '
.$t_users.'.login AS login',
'props' => array('login' => 'login'),
)
);
}
function postSave($create=false)
{
/**
* [signal]
*
* IDF_Key::postSave
*
* [sender]
*
* IDF_Key
*
* [description]
*
* This signal allows an application to perform special
* operations after the saving of a SSH Key.
*
* [parameters]
*
* array('key' => $key,
* 'created' => true/false)
*
*/
$params = array('key' => $this, 'created' => $create);
Pluf_Signal::send('IDF_Key::postSave',
'IDF_Key', $params);
}
}

View File

@ -0,0 +1,52 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Add the SSH key.
*/
function IDF_Migrations_10SshKey_up($params=null)
{
$models = array(
'IDF_Key',
);
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
foreach ($models as $model) {
$schema->model = new $model();
$schema->createTables();
}
}
function IDF_Migrations_10SshKey_down($params=null)
{
$models = array(
'IDF_Key',
);
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
foreach ($models as $model) {
$schema->model = new $model();
$schema->dropTables();
}
}

View File

@ -45,6 +45,7 @@ function IDF_Migrations_Install_setup($params=null)
'IDF_Review',
'IDF_Review_Patch',
'IDF_Review_FileComment',
'IDF_Key',
);
$db = Pluf::db();
$schema = new Pluf_DB_Schema($db);
@ -82,6 +83,7 @@ function IDF_Migrations_Install_teardown($params=null)
$perm = Pluf_Permission::getFromString('IDF.project-authorized-user');
if ($perm) $perm->delete();
$models = array(
'IDF_Key',
'IDF_Review_FileComment',
'IDF_Review_Patch',
'IDF_Review',

View File

@ -0,0 +1,60 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
/**
* Synchronize the SSH keys with InDefero.
*/
class IDF_Plugin_SyncGit_Cron
{
/**
* Template for the SSH key.
*/
public $template = 'command="%s %s",no-port-forwarding,no-X11-forwarding,'
.'no-agent-forwarding,no-pty %s';
/**
* Synchronize.
*/
public static function sync()
{
$template = Pluf::factory(__CLASS__)->template;
$keys = Pluf::factory('IDF_Key')->getList(array('view'=>'join_user'));
$cmd = Pluf::f('idf_plugin_syncgit_path_gitserve', '/bin/false');
$authorized_keys = Pluf::f('idf_plugin_syncgit_path_authorized_keys', false);
if (false == $authorized_keys) {
throw new Pluf_Exception_SettingError('Setting git_path_authorized_keys not set.');
}
if (!is_writable($authorized_keys)) {
throw new Exception('Cannot create file: '.$authorized_keys);
}
$out = '';
foreach ($keys as $key) {
if (strlen($key->content) > 40 // minimal check
and preg_match('/^[a-zA-Z][a-zA-Z0-9_.-]*(@[a-zA-Z][a-zA-Z0-9.-]*)?$/', $key->login)) {
$content = str_replace("\n", '', $key->content);
$out .= sprintf($template, $cmd, $key->login, $content)."\n";
}
}
file_put_contents($authorized_keys, $out, LOCK_EX);
}
}

View File

@ -35,11 +35,6 @@ class IDF_Plugin_SyncGit_Serve
public $commands_readonly = array('git-upload-pack', 'git upload-pack');
public $commands_write = array('git-receive-pack', 'git receive-pack');
/**
* Check that the command is authorized.
*/
/**
* Serve a git request.
*
@ -120,7 +115,7 @@ class IDF_Plugin_SyncGit_Serve
exit(1);
}
$cmd = $env['SSH_ORIGINAL_COMMAND'];
chdir(Pluf::f('git_home_dir', '/home/git'));
chdir(Pluf::f('idf_plugin_syncgit_git_home_dir', '/home/git'));
$serve = new IDF_Plugin_SyncGit_Serve();
try {
$new_cmd = $serve->serve($username, $cmd);
@ -171,11 +166,11 @@ class IDF_Plugin_SyncGit_Serve
$request->user = $user;
if (true === IDF_Precondition::accessTabGeneric($request, 'source_access_rights')) {
if ($mode == 'readonly') {
return array(Pluf::f('git_base_repositories', '/home/git/repositories'),
return array(Pluf::f('idf_plugin_syncgit_base_repositories', '/home/git/repositories'),
$project->shortname);
}
if (true === IDF_Precondition::projectMemberOrOwner($request)) {
return array(Pluf::f('git_base_repositories', '/home/git/repositories'),
return array(Pluf::f('idf_plugin_syncgit_base_repositories', '/home/git/repositories'),
$project->shortname);
}
}

View File

@ -124,10 +124,17 @@ class IDF_Views_User
unset($data['password']);
$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">'.__('Troncated 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,
'form' => $form),
$request);
}

View File

@ -37,7 +37,7 @@ $m['IDF_Review'] = array('relate_to' => array('IDF_Project', 'Pluf_User', 'IDF_T
'relate_to_many' => array('IDF_Tag', 'Pluf_User'));
$m['IDF_Review_Patch'] = array('relate_to' => array('IDF_Review', 'Pluf_User'));
$m['IDF_Review_FileComment'] = array('relate_to' => array('IDF_Review_Patch', 'Pluf_User'));
$m['IDF_Key'] = array('relate_to' => array('Pluf_User'));
Pluf_Signal::connect('Pluf_Template_Compiler::construct_template_tags_modifiers',
array('IDF_Middleware', 'updateTemplateTagsModifiers'));

View File

@ -46,6 +46,14 @@
{$form.f.password2|unsafe}
</td>
</tr>
<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>
</tr>
<tr class="pass-info" id="extra-password">
<th>{trans 'Extra password'}:</th>
<td><span class="mono">{$ext_pass}</span><br />