Show More
@@ -1,151 +1,154 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2017 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | |
|
23 | 23 | from pyramid.httpexceptions import HTTPFound |
|
24 | 24 | from pyramid.view import view_config |
|
25 | 25 | |
|
26 | 26 | from rhodecode.apps._base import BaseAppView, DataGridAppView |
|
27 | 27 | from rhodecode.apps.ssh_support import SshKeyFileChangeEvent |
|
28 | 28 | from rhodecode.events import trigger |
|
29 | 29 | from rhodecode.lib import helpers as h |
|
30 | 30 | from rhodecode.lib import audit_logger |
|
31 | 31 | from rhodecode.lib.auth import LoginRequired, NotAnonymous, CSRFRequired |
|
32 | 32 | from rhodecode.model.db import IntegrityError, UserSshKeys |
|
33 | 33 | from rhodecode.model.meta import Session |
|
34 | 34 | from rhodecode.model.ssh_key import SshKeyModel |
|
35 | 35 | |
|
36 | 36 | log = logging.getLogger(__name__) |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | class MyAccountSshKeysView(BaseAppView, DataGridAppView): |
|
40 | 40 | |
|
41 | 41 | def load_default_context(self): |
|
42 | 42 | c = self._get_local_tmpl_context() |
|
43 | 43 | c.user = c.auth_user.get_instance() |
|
44 | ||
|
45 | c.ssh_enabled = self.request.registry.settings.get( | |
|
46 | 'ssh.generate_authorized_keyfile') | |
|
44 | 47 | self._register_global_c(c) |
|
45 | 48 | return c |
|
46 | 49 | |
|
47 | 50 | @LoginRequired() |
|
48 | 51 | @NotAnonymous() |
|
49 | 52 | @view_config( |
|
50 | 53 | route_name='my_account_ssh_keys', request_method='GET', |
|
51 | 54 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
52 | 55 | def my_account_ssh_keys(self): |
|
53 | 56 | _ = self.request.translate |
|
54 | 57 | |
|
55 | 58 | c = self.load_default_context() |
|
56 | 59 | c.active = 'ssh_keys' |
|
57 | 60 | c.default_key = self.request.GET.get('default_key') |
|
58 | 61 | c.user_ssh_keys = SshKeyModel().get_ssh_keys(c.user.user_id) |
|
59 | 62 | return self._get_template_context(c) |
|
60 | 63 | |
|
61 | 64 | @LoginRequired() |
|
62 | 65 | @NotAnonymous() |
|
63 | 66 | @view_config( |
|
64 | 67 | route_name='my_account_ssh_keys_generate', request_method='GET', |
|
65 | 68 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
66 | 69 | def ssh_keys_generate_keypair(self): |
|
67 | 70 | _ = self.request.translate |
|
68 | 71 | c = self.load_default_context() |
|
69 | 72 | |
|
70 | 73 | c.active = 'ssh_keys_generate' |
|
71 | 74 | comment = 'RhodeCode-SSH {}'.format(c.user.email or '') |
|
72 | 75 | c.private, c.public = SshKeyModel().generate_keypair(comment=comment) |
|
73 | 76 | c.target_form_url = h.route_path( |
|
74 | 77 | 'my_account_ssh_keys', _query=dict(default_key=c.public)) |
|
75 | 78 | return self._get_template_context(c) |
|
76 | 79 | |
|
77 | 80 | @LoginRequired() |
|
78 | 81 | @NotAnonymous() |
|
79 | 82 | @CSRFRequired() |
|
80 | 83 | @view_config( |
|
81 | 84 | route_name='my_account_ssh_keys_add', request_method='POST',) |
|
82 | 85 | def my_account_ssh_keys_add(self): |
|
83 | 86 | _ = self.request.translate |
|
84 | 87 | c = self.load_default_context() |
|
85 | 88 | |
|
86 | 89 | user_data = c.user.get_api_data() |
|
87 | 90 | key_data = self.request.POST.get('key_data') |
|
88 | 91 | description = self.request.POST.get('description') |
|
89 | 92 | |
|
90 | 93 | try: |
|
91 | 94 | if not key_data: |
|
92 | 95 | raise ValueError('Please add a valid public key') |
|
93 | 96 | |
|
94 | 97 | key = SshKeyModel().parse_key(key_data.strip()) |
|
95 | 98 | fingerprint = key.hash_md5() |
|
96 | 99 | |
|
97 | 100 | ssh_key = SshKeyModel().create( |
|
98 | 101 | c.user.user_id, fingerprint, key_data, description) |
|
99 | 102 | ssh_key_data = ssh_key.get_api_data() |
|
100 | 103 | |
|
101 | 104 | audit_logger.store_web( |
|
102 | 105 | 'user.edit.ssh_key.add', action_data={ |
|
103 | 106 | 'data': {'ssh_key': ssh_key_data, 'user': user_data}}, |
|
104 | 107 | user=self._rhodecode_user, ) |
|
105 | 108 | Session().commit() |
|
106 | 109 | |
|
107 | 110 | # Trigger an event on change of keys. |
|
108 | 111 | trigger(SshKeyFileChangeEvent(), self.request.registry) |
|
109 | 112 | |
|
110 | 113 | h.flash(_("Ssh Key successfully created"), category='success') |
|
111 | 114 | |
|
112 | 115 | except IntegrityError: |
|
113 | 116 | log.exception("Exception during ssh key saving") |
|
114 | 117 | h.flash(_('An error occurred during ssh key saving: {}').format( |
|
115 | 118 | 'Such key already exists, please use a different one'), |
|
116 | 119 | category='error') |
|
117 | 120 | except Exception as e: |
|
118 | 121 | log.exception("Exception during ssh key saving") |
|
119 | 122 | h.flash(_('An error occurred during ssh key saving: {}').format(e), |
|
120 | 123 | category='error') |
|
121 | 124 | |
|
122 | 125 | return HTTPFound(h.route_path('my_account_ssh_keys')) |
|
123 | 126 | |
|
124 | 127 | @LoginRequired() |
|
125 | 128 | @NotAnonymous() |
|
126 | 129 | @CSRFRequired() |
|
127 | 130 | @view_config( |
|
128 | 131 | route_name='my_account_ssh_keys_delete', request_method='POST') |
|
129 | 132 | def my_account_ssh_keys_delete(self): |
|
130 | 133 | _ = self.request.translate |
|
131 | 134 | c = self.load_default_context() |
|
132 | 135 | |
|
133 | 136 | user_data = c.user.get_api_data() |
|
134 | 137 | |
|
135 | 138 | del_ssh_key = self.request.POST.get('del_ssh_key') |
|
136 | 139 | |
|
137 | 140 | if del_ssh_key: |
|
138 | 141 | ssh_key = UserSshKeys.get_or_404(del_ssh_key) |
|
139 | 142 | ssh_key_data = ssh_key.get_api_data() |
|
140 | 143 | |
|
141 | 144 | SshKeyModel().delete(del_ssh_key, c.user.user_id) |
|
142 | 145 | audit_logger.store_web( |
|
143 | 146 | 'user.edit.ssh_key.delete', action_data={ |
|
144 | 147 | 'data': {'ssh_key': ssh_key_data, 'user': user_data}}, |
|
145 | 148 | user=self._rhodecode_user,) |
|
146 | 149 | Session().commit() |
|
147 | 150 | # Trigger an event on change of keys. |
|
148 | 151 | trigger(SshKeyFileChangeEvent(), self.request.registry) |
|
149 | 152 | h.flash(_("Ssh key successfully deleted"), category='success') |
|
150 | 153 | |
|
151 | 154 | return HTTPFound(h.route_path('my_account_ssh_keys')) |
@@ -1,78 +1,84 b'' | |||
|
1 | 1 | <div class="panel panel-default"> |
|
2 | 2 | <div class="panel-heading"> |
|
3 | 3 | <h3 class="panel-title">${_('SSH Keys')}</h3> |
|
4 | 4 | </div> |
|
5 | 5 | <div class="panel-body"> |
|
6 | 6 | <div class="sshkeys_wrap"> |
|
7 | 7 | <table class="rctable ssh_keys"> |
|
8 | 8 | <tr> |
|
9 | 9 | <th>${_('Fingerprint')}</th> |
|
10 | 10 | <th>${_('Description')}</th> |
|
11 | 11 | <th>${_('Created')}</th> |
|
12 | 12 | <th>${_('Action')}</th> |
|
13 | 13 | </tr> |
|
14 |
%if c. |
|
|
15 | %for ssh_key in c.user_ssh_keys: | |
|
16 |
|
|
|
17 | <td class=""> | |
|
18 |
|
|
|
19 |
< |
|
|
20 |
<td class=" |
|
|
21 | <td class="td-tags">${h.format_date(ssh_key.created_on)}</td> | |
|
14 | % if not c.ssh_enabled: | |
|
15 | <tr><td colspan="4"><div class="">${_('SSH Keys usage is currently disabled, please ask your administrator to enable them.')}</div></td></tr> | |
|
16 | % else: | |
|
17 | %if c.user_ssh_keys: | |
|
18 | %for ssh_key in c.user_ssh_keys: | |
|
19 | <tr class=""> | |
|
20 | <td class=""> | |
|
21 | <code>${ssh_key.ssh_key_fingerprint}</code> | |
|
22 | </td> | |
|
23 | <td class="td-wrap">${ssh_key.description}</td> | |
|
24 | <td class="td-tags">${h.format_date(ssh_key.created_on)}</td> | |
|
22 | 25 | |
|
23 | <td class="td-action"> | |
|
24 | ${h.secure_form(h.route_path('my_account_ssh_keys_delete'), method='POST', request=request)} | |
|
25 | ${h.hidden('del_ssh_key', ssh_key.ssh_key_id)} | |
|
26 | <button class="btn btn-link btn-danger" type="submit" | |
|
27 | onclick="return confirm('${_('Confirm to remove ssh key %s') % ssh_key.ssh_key_fingerprint}');"> | |
|
28 | ${_('Delete')} | |
|
29 | </button> | |
|
30 | ${h.end_form()} | |
|
31 | </td> | |
|
32 | </tr> | |
|
33 | %endfor | |
|
34 | %else: | |
|
35 |
<tr><td><div class=" |
|
|
36 | %endif | |
|
26 | <td class="td-action"> | |
|
27 | ${h.secure_form(h.route_path('my_account_ssh_keys_delete'), method='POST', request=request)} | |
|
28 | ${h.hidden('del_ssh_key', ssh_key.ssh_key_id)} | |
|
29 | <button class="btn btn-link btn-danger" type="submit" | |
|
30 | onclick="return confirm('${_('Confirm to remove ssh key %s') % ssh_key.ssh_key_fingerprint}');"> | |
|
31 | ${_('Delete')} | |
|
32 | </button> | |
|
33 | ${h.end_form()} | |
|
34 | </td> | |
|
35 | </tr> | |
|
36 | %endfor | |
|
37 | %else: | |
|
38 | <tr><td colspan="4"><div class="">${_('No additional ssh keys specified')}</div></td></tr> | |
|
39 | %endif | |
|
40 | % endif | |
|
37 | 41 | </table> |
|
38 | 42 | </div> |
|
39 | 43 | |
|
44 | % if c.ssh_enabled: | |
|
40 | 45 | <div class="user_ssh_keys"> |
|
41 | 46 | ${h.secure_form(h.route_path('my_account_ssh_keys_add'), method='POST', request=request)} |
|
42 | 47 | <div class="form form-vertical"> |
|
43 | 48 | <!-- fields --> |
|
44 | 49 | <div class="fields"> |
|
45 | 50 | <div class="field"> |
|
46 | 51 | <div class="label"> |
|
47 | 52 | <label for="new_email">${_('New ssh key')}:</label> |
|
48 | 53 | </div> |
|
49 | 54 | <div class="input"> |
|
50 | 55 | ${h.text('description', class_='medium', placeholder=_('Description'))} |
|
51 | 56 | <a href="${h.route_path('my_account_ssh_keys_generate')}">${_('Generate random RSA key')}</a> |
|
52 | 57 | </div> |
|
53 | 58 | </div> |
|
54 | 59 | |
|
55 | 60 | <div class="field"> |
|
56 | 61 | <div class="textarea text-area editor"> |
|
57 | 62 | ${h.textarea('key_data',c.default_key, size=30, placeholder=_("Public key, begins with 'ssh-rsa', 'ssh-dss', 'ssh-ed25519', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'"))} |
|
58 | 63 | </div> |
|
59 | 64 | </div> |
|
60 | 65 | |
|
61 | 66 | <div class="buttons"> |
|
62 | 67 | ${h.submit('save',_('Add'),class_="btn")} |
|
63 | 68 | ${h.reset('reset',_('Reset'),class_="btn")} |
|
64 | 69 | </div> |
|
65 | 70 | </div> |
|
66 | 71 | </div> |
|
67 | 72 | ${h.end_form()} |
|
68 | 73 | </div> |
|
74 | % endif | |
|
69 | 75 | </div> |
|
70 | 76 | </div> |
|
71 | 77 | |
|
72 | 78 | <script> |
|
73 | 79 | |
|
74 | 80 | $(document).ready(function(){ |
|
75 | 81 | |
|
76 | 82 | |
|
77 | 83 | }); |
|
78 | 84 | </script> |
General Comments 0
You need to be logged in to leave comments.
Login now