diff --git a/data/gitprep b/data/gitprep new file mode 100644 index 00000000..acb96c8d Binary files /dev/null and b/data/gitprep differ diff --git a/lib/Gitprep.pm b/lib/Gitprep.pm index 3466e485..73d6535a 100644 --- a/lib/Gitprep.pm +++ b/lib/Gitprep.pm @@ -119,6 +119,7 @@ sub startup { # Model my $models = [ {table => 'user', primary_key => 'id'}, + {table => 'ssh_public_key', primary_key => ['user_id', 'key']}, {table => 'project', primary_key => ['user_id', 'name']}, {table => 'number', primary_key => 'key'}, {table => 'collaboration', primary_key => ['user_id', 'project_name', 'collaborator_id']} @@ -185,6 +186,9 @@ sub startup { # Custom routes { + # Show ssh keys + $r->get('/:user.keys' => template '/user-keys'); + # User my $r = $r->route('/:user'); { @@ -195,7 +199,7 @@ sub startup { $r->get('/_settings' => template '/user-settings'); # SSH keys - $r->get('/_settings/ssh' => template '/user-settings/ssh'); + $r->any('/_settings/ssh' => template '/user-settings/ssh'); } # Smart HTTP diff --git a/lib/Gitprep/Manager.pm b/lib/Gitprep/Manager.pm index 30be7138..9eb76f5b 100644 --- a/lib/Gitprep/Manager.pm +++ b/lib/Gitprep/Manager.pm @@ -306,7 +306,7 @@ EOS $dbi->execute($sql); }; - # Create usert columns + # Create user columns my $user_columns = [ "admin not null default '0'", "password not null default ''", @@ -323,6 +323,35 @@ EOS $self->app->log->error($error); croak $error; } + + # Create ssh_public_key table + eval { + my $sql = <<"EOS"; +create table ssh_public_key ( + row_id integer primary key autoincrement, + user_id not null default '', + key not null default '', + unique(user_id, key) +); +EOS + $dbi->execute($sql); + }; + + # Create ssh_public_key columns + my $ssh_public_key_columns = [ + "title not null default ''", + ]; + for my $column (@$ssh_public_key_columns) { + eval { $dbi->execute("alter table ssh_public_key add column $column") }; + } + + # Check ssh_public_key table + eval { $dbi->select([qw/row_id user_id key title/], table => 'ssh_public_key') }; + if ($@) { + my $error = "Can't create ssh_public_key table properly: $@"; + $self->app->log->error($error); + croak $error; + } # Create project table eval { diff --git a/templates/include/errors.html.ep b/templates/include/errors.html.ep index c64c4e69..4e06a11e 100644 --- a/templates/include/errors.html.ep +++ b/templates/include/errors.html.ep @@ -5,7 +5,7 @@
% for my $error (@$errors) { -

<%= $error %>

+

<%= $error %>

% }
% } \ No newline at end of file diff --git a/templates/user-keys.html.ep b/templates/user-keys.html.ep new file mode 100644 index 00000000..740659f1 --- /dev/null +++ b/templates/user-keys.html.ep @@ -0,0 +1,11 @@ +<% + my $user = param('user'); + my $keys = app->dbi->model('ssh_public_key')->select(where => {user_id => $user})->all; + warn dumper $keys; + my $keys_str = ''; + for my $key (@$keys) { + $keys_str .= "$key->{key}\n"; + } + $self->render(text => $keys_str); + return; +%> diff --git a/templates/user-settings/ssh.html.ep b/templates/user-settings/ssh.html.ep index 53829264..df715b49 100644 --- a/templates/user-settings/ssh.html.ep +++ b/templates/user-settings/ssh.html.ep @@ -12,37 +12,103 @@ return; } - my $keys = [ - { - key => 'key1', - hash => '7d:d7:ec:86:f6:96:cf:8f:63:07:79:01:f4:cb:f7:78', - mtime => 'Last used on May 16, 2014' - }, - { - key => 'key1', - hash => '7d:d7:ec:86:f6:96:cf:8f:63:07:79:01:f4:cb:f7:78', - mtime => 'Last used on May 16, 2014' - }, - ]; -%> - -% layout 'common', title => 'Your Profile'; - - %= javascript begin - $(document).ready(function () { - $('#show-add-key-form').on('click', function () { - var display = $('#add-key-form').css('display'); + # Process form + my $errors; + if (lc $self->req->method eq 'post') { + # Add ssh key + if ($op eq 'add') { + + # Paramters + my $params = $api->params; + + # Rule + my $rule = [ + title => [ + ['not_blank' => 'title is empty'], + ['ascii' => 'title contains invalid character'], + ], + key => [ + ['not_blank' => 'key is empty'], + sub { + my ($original_key, $args, $vc) = @_; + + my $type; + my $original_key_edit; + if ($original_key =~ /^(ssh-rsa|ssh-dss|ecdsa-sha2-nistp25|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521) +(\S+)/) { + $type = $1; + $original_key_edit = $2; + } + + if ($type) { + if ($vc->constraints->{ascii}->($original_key_edit)) { + my $key = "$type $original_key_edit"; + + my $row = app->dbi->model('ssh_public_key')->select(id => [$user, $key])->one; + + if ($row) { + return {result => 0, message => 'Key already exists'}; + } + else { + return {result => 1, output => $key} + } + } + else { + return { + result => 0, + message => "Key contains invalid character." + } + } + } + else { + return { + result => 0, + message => "Key is invalid. It must begin with 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256'," + . "'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'. Check that you're copying the public half of the key" + }; + } + } + ] + ]; + + # Validation + my $vresult = app->vc->validate($params, $rule); + + # Register ssh key + if ($vresult->is_ok) { + my $safe_params = $vresult->data; + my $title = $safe_params->{title}; + my $key = $safe_params->{key}; + + my $p = { + user_id => $user, + title => $title, + key => $key + }; + eval { + app->dbi->model('ssh_public_key')->insert($p); + }; - if (display === 'block') { - $('#add-key-form').css('display', 'none'); + if (my $e = $@) { + app->log->error(url_for . ":$e"); + $errors = ['Internal error']; } else { - $('#add-key-form').css('display', 'block'); + flash('message' => 'Success: ssh key is added'); + $self->redirect_to('current'); + return; } - }); - }); - % end + } + else { + $errors = $vresult->messages; + } + } + } + my $keys = app->dbi->model('ssh_public_key')->select(where => {user_id => $user})->all; +%> + +% layout 'common', title => 'SSH keys'; + %= include '/include/header';
@@ -64,18 +130,12 @@
-
-
-
-
- SSH Keys -
-
-
-
- Add SSH Key -
-
+ <%= include '/include/errors', errors => $errors %> + <%= include '/include/message', message => flash('message') %> + +
+
+ SSH Keys (">see)
@@ -89,19 +149,13 @@
- <%= $key->{key} %> -
-
- <%= $key->{hash} %> -
-
- <%= $key->{mtime} %> + <%= $key->{title} %>
@@ -114,14 +168,14 @@ % }
-