Show More
@@ -1,48 +1,54 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 collections |
|
22 | 22 | import logging |
|
23 | 23 | |
|
24 | from pylons import tmpl_context as c | |
|
24 | ||
|
25 | 25 | from pyramid.view import view_config |
|
26 | 26 | |
|
27 | 27 | from rhodecode.apps._base import BaseAppView |
|
28 | 28 | from rhodecode.apps.admin.navigation import navigation_list |
|
29 | 29 | from rhodecode.lib.auth import (LoginRequired, HasPermissionAllDecorator) |
|
30 | 30 | from rhodecode.lib.utils import read_opensource_licenses |
|
31 | 31 | |
|
32 | 32 | log = logging.getLogger(__name__) |
|
33 | 33 | |
|
34 | 34 | |
|
35 | 35 | class OpenSourceLicensesAdminSettingsView(BaseAppView): |
|
36 | 36 | |
|
37 | def load_default_context(self): | |
|
38 | c = self._get_local_tmpl_context() | |
|
39 | self._register_global_c(c) | |
|
40 | return c | |
|
41 | ||
|
37 | 42 | @LoginRequired() |
|
38 | 43 | @HasPermissionAllDecorator('hg.admin') |
|
39 | 44 | @view_config( |
|
40 | 45 | route_name='admin_settings_open_source', request_method='GET', |
|
41 | 46 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
42 | 47 | def open_source_licenses(self): |
|
48 | c = self.load_default_context() | |
|
43 | 49 | c.active = 'open_source' |
|
44 | 50 | c.navlist = navigation_list(self.request) |
|
45 | 51 | c.opensource_licenses = collections.OrderedDict( |
|
46 | 52 | sorted(read_opensource_licenses().items(), key=lambda t: t[0])) |
|
47 | 53 | |
|
48 | return {} | |
|
54 | return self._get_template_context(c) |
@@ -1,96 +1,102 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 | from pylons import tmpl_context as c | |
|
24 | 23 | from pyramid.view import view_config |
|
25 | 24 | from pyramid.httpexceptions import HTTPFound |
|
26 | 25 | |
|
27 | 26 | from rhodecode.apps._base import BaseAppView |
|
28 | 27 | from rhodecode.apps.admin.navigation import navigation_list |
|
29 | 28 | from rhodecode.lib.auth import ( |
|
30 | 29 | LoginRequired, HasPermissionAllDecorator, CSRFRequired) |
|
31 | 30 | from rhodecode.lib.utils2 import safe_int |
|
32 | 31 | from rhodecode.lib import system_info |
|
33 | 32 | from rhodecode.lib import user_sessions |
|
34 | 33 | |
|
35 | 34 | |
|
36 | 35 | log = logging.getLogger(__name__) |
|
37 | 36 | |
|
38 | 37 | |
|
39 | 38 | class AdminSessionSettingsView(BaseAppView): |
|
39 | def load_default_context(self): | |
|
40 | c = self._get_local_tmpl_context() | |
|
41 | ||
|
42 | self._register_global_c(c) | |
|
43 | return c | |
|
40 | 44 | |
|
41 | 45 | @LoginRequired() |
|
42 | 46 | @HasPermissionAllDecorator('hg.admin') |
|
43 | 47 | @view_config( |
|
44 | 48 | route_name='admin_settings_sessions', request_method='GET', |
|
45 | 49 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
46 | 50 | def settings_sessions(self): |
|
51 | c = self.load_default_context() | |
|
52 | ||
|
47 | 53 | c.active = 'sessions' |
|
48 | 54 | c.navlist = navigation_list(self.request) |
|
49 | 55 | |
|
50 | 56 | c.cleanup_older_days = 60 |
|
51 | 57 | older_than_seconds = 60 * 60 * 24 * c.cleanup_older_days |
|
52 | 58 | |
|
53 | 59 | config = system_info.rhodecode_config().get_value()['value']['config'] |
|
54 | 60 | c.session_model = user_sessions.get_session_handler( |
|
55 | 61 | config.get('beaker.session.type', 'memory'))(config) |
|
56 | 62 | |
|
57 | 63 | c.session_conf = c.session_model.config |
|
58 | 64 | c.session_count = c.session_model.get_count() |
|
59 | 65 | c.session_expired_count = c.session_model.get_expired_count( |
|
60 | 66 | older_than_seconds) |
|
61 | 67 | |
|
62 | return {} | |
|
68 | return self._get_template_context(c) | |
|
63 | 69 | |
|
64 | 70 | @LoginRequired() |
|
65 | 71 | @CSRFRequired() |
|
66 | 72 | @HasPermissionAllDecorator('hg.admin') |
|
67 | 73 | @view_config( |
|
68 | 74 | route_name='admin_settings_sessions_cleanup', request_method='POST') |
|
69 | 75 | def settings_sessions_cleanup(self): |
|
70 | 76 | _ = self.request.translate |
|
71 | 77 | expire_days = safe_int(self.request.params.get('expire_days')) |
|
72 | 78 | |
|
73 | 79 | if expire_days is None: |
|
74 | 80 | expire_days = 60 |
|
75 | 81 | |
|
76 | 82 | older_than_seconds = 60 * 60 * 24 * expire_days |
|
77 | 83 | |
|
78 | 84 | config = system_info.rhodecode_config().get_value()['value']['config'] |
|
79 | 85 | session_model = user_sessions.get_session_handler( |
|
80 | 86 | config.get('beaker.session.type', 'memory'))(config) |
|
81 | 87 | |
|
82 | 88 | try: |
|
83 | 89 | session_model.clean_sessions( |
|
84 | 90 | older_than_seconds=older_than_seconds) |
|
85 | 91 | self.request.session.flash( |
|
86 | 92 | _('Cleaned up old sessions'), queue='success') |
|
87 | 93 | except user_sessions.CleanupCommand as msg: |
|
88 | 94 | self.request.session.flash(msg.message, queue='warning') |
|
89 | 95 | except Exception as e: |
|
90 | 96 | log.exception('Failed session cleanup') |
|
91 | 97 | self.request.session.flash( |
|
92 | 98 | _('Failed to cleanup up old sessions'), queue='error') |
|
93 | 99 | |
|
94 | 100 | redirect_to = self.request.resource_path( |
|
95 | 101 | self.context, route_name='admin_settings_sessions') |
|
96 | 102 | return HTTPFound(redirect_to) |
@@ -1,204 +1,209 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 | import urllib2 |
|
23 | 23 | import packaging.version |
|
24 | 24 | |
|
25 | from pylons import tmpl_context as c | |
|
26 | 25 | from pyramid.view import view_config |
|
27 | 26 | |
|
28 | 27 | import rhodecode |
|
29 | 28 | from rhodecode.apps._base import BaseAppView |
|
30 | 29 | from rhodecode.apps.admin.navigation import navigation_list |
|
31 | 30 | from rhodecode.lib import helpers as h |
|
32 | 31 | from rhodecode.lib.auth import (LoginRequired, HasPermissionAllDecorator) |
|
33 | 32 | from rhodecode.lib.utils2 import str2bool |
|
34 | 33 | from rhodecode.lib import system_info |
|
35 | 34 | from rhodecode.lib.ext_json import json |
|
36 | 35 | from rhodecode.model.settings import SettingsModel |
|
37 | 36 | |
|
38 | 37 | log = logging.getLogger(__name__) |
|
39 | 38 | |
|
40 | 39 | |
|
41 | 40 | class AdminSystemInfoSettingsView(BaseAppView): |
|
41 | def load_default_context(self): | |
|
42 | c = self._get_local_tmpl_context() | |
|
43 | self._register_global_c(c) | |
|
44 | return c | |
|
42 | 45 | |
|
43 | 46 | @staticmethod |
|
44 | 47 | def get_update_data(update_url): |
|
45 | 48 | """Return the JSON update data.""" |
|
46 | 49 | ver = rhodecode.__version__ |
|
47 | 50 | log.debug('Checking for upgrade on `%s` server', update_url) |
|
48 | 51 | opener = urllib2.build_opener() |
|
49 | 52 | opener.addheaders = [('User-agent', 'RhodeCode-SCM/%s' % ver)] |
|
50 | 53 | response = opener.open(update_url) |
|
51 | 54 | response_data = response.read() |
|
52 | 55 | data = json.loads(response_data) |
|
53 | 56 | |
|
54 | 57 | return data |
|
55 | 58 | |
|
56 | 59 | def get_update_url(self): |
|
57 | 60 | settings = SettingsModel().get_all_settings() |
|
58 | 61 | return settings.get('rhodecode_update_url') |
|
59 | 62 | |
|
60 | 63 | @LoginRequired() |
|
61 | 64 | @HasPermissionAllDecorator('hg.admin') |
|
62 | 65 | @view_config( |
|
63 | 66 | route_name='admin_settings_system', request_method='GET', |
|
64 | 67 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
65 | 68 | def settings_system_info(self): |
|
66 | 69 | _ = self.request.translate |
|
70 | c = self.load_default_context() | |
|
67 | 71 | |
|
68 | 72 | c.active = 'system' |
|
69 | 73 | c.navlist = navigation_list(self.request) |
|
70 | 74 | |
|
71 | 75 | # TODO(marcink), figure out how to allow only selected users to do this |
|
72 | 76 | c.allowed_to_snapshot = self._rhodecode_user.admin |
|
73 | 77 | |
|
74 | 78 | snapshot = str2bool(self.request.params.get('snapshot')) |
|
75 | 79 | |
|
76 | 80 | c.rhodecode_update_url = self.get_update_url() |
|
77 | 81 | server_info = system_info.get_system_info(self.request.environ) |
|
78 | 82 | |
|
79 | 83 | for key, val in server_info.items(): |
|
80 | 84 | setattr(c, key, val) |
|
81 | 85 | |
|
82 | 86 | def val(name, subkey='human_value'): |
|
83 | 87 | return server_info[name][subkey] |
|
84 | 88 | |
|
85 | 89 | def state(name): |
|
86 | 90 | return server_info[name]['state'] |
|
87 | 91 | |
|
88 | 92 | def val2(name): |
|
89 | 93 | val = server_info[name]['human_value'] |
|
90 | 94 | state = server_info[name]['state'] |
|
91 | 95 | return val, state |
|
92 | 96 | |
|
93 | 97 | update_info_msg = _('Note: please make sure this server can ' |
|
94 | 98 | 'access `${url}` for the update link to work', |
|
95 | 99 | mapping=dict(url=c.rhodecode_update_url)) |
|
96 | 100 | c.data_items = [ |
|
97 | 101 | # update info |
|
98 | 102 | (_('Update info'), h.literal( |
|
99 | 103 | '<span class="link" id="check_for_update" >%s.</span>' % ( |
|
100 | 104 | _('Check for updates')) + |
|
101 | 105 | '<br/> <span >%s.</span>' % (update_info_msg) |
|
102 | 106 | ), ''), |
|
103 | 107 | |
|
104 | 108 | # RhodeCode specific |
|
105 | 109 | (_('RhodeCode Version'), val('rhodecode_app')['text'], state('rhodecode_app')), |
|
106 | 110 | (_('RhodeCode Server IP'), val('server')['server_ip'], state('server')), |
|
107 | 111 | (_('RhodeCode Server ID'), val('server')['server_id'], state('server')), |
|
108 | 112 | (_('RhodeCode Configuration'), val('rhodecode_config')['path'], state('rhodecode_config')), |
|
109 | 113 | (_('RhodeCode Certificate'), val('rhodecode_config')['cert_path'], state('rhodecode_config')), |
|
110 | 114 | (_('Workers'), val('rhodecode_config')['config']['server:main'].get('workers', '?'), state('rhodecode_config')), |
|
111 | 115 | (_('Worker Type'), val('rhodecode_config')['config']['server:main'].get('worker_class', 'sync'), state('rhodecode_config')), |
|
112 | 116 | ('', '', ''), # spacer |
|
113 | 117 | |
|
114 | 118 | # Database |
|
115 | 119 | (_('Database'), val('database')['url'], state('database')), |
|
116 | 120 | (_('Database version'), val('database')['version'], state('database')), |
|
117 | 121 | ('', '', ''), # spacer |
|
118 | 122 | |
|
119 | 123 | # Platform/Python |
|
120 | 124 | (_('Platform'), val('platform')['name'], state('platform')), |
|
121 | 125 | (_('Platform UUID'), val('platform')['uuid'], state('platform')), |
|
122 | 126 | (_('Python version'), val('python')['version'], state('python')), |
|
123 | 127 | (_('Python path'), val('python')['executable'], state('python')), |
|
124 | 128 | ('', '', ''), # spacer |
|
125 | 129 | |
|
126 | 130 | # Systems stats |
|
127 | 131 | (_('CPU'), val('cpu')['text'], state('cpu')), |
|
128 | 132 | (_('Load'), val('load')['text'], state('load')), |
|
129 | 133 | (_('Memory'), val('memory')['text'], state('memory')), |
|
130 | 134 | (_('Uptime'), val('uptime')['text'], state('uptime')), |
|
131 | 135 | ('', '', ''), # spacer |
|
132 | 136 | |
|
133 | 137 | # Repo storage |
|
134 | 138 | (_('Storage location'), val('storage')['path'], state('storage')), |
|
135 | 139 | (_('Storage info'), val('storage')['text'], state('storage')), |
|
136 | 140 | (_('Storage inodes'), val('storage_inodes')['text'], state('storage_inodes')), |
|
137 | 141 | |
|
138 | 142 | (_('Gist storage location'), val('storage_gist')['path'], state('storage_gist')), |
|
139 | 143 | (_('Gist storage info'), val('storage_gist')['text'], state('storage_gist')), |
|
140 | 144 | |
|
141 | 145 | (_('Archive cache storage location'), val('storage_archive')['path'], state('storage_archive')), |
|
142 | 146 | (_('Archive cache info'), val('storage_archive')['text'], state('storage_archive')), |
|
143 | 147 | |
|
144 | 148 | (_('Temp storage location'), val('storage_temp')['path'], state('storage_temp')), |
|
145 | 149 | (_('Temp storage info'), val('storage_temp')['text'], state('storage_temp')), |
|
146 | 150 | |
|
147 | 151 | (_('Search info'), val('search')['text'], state('search')), |
|
148 | 152 | (_('Search location'), val('search')['location'], state('search')), |
|
149 | 153 | ('', '', ''), # spacer |
|
150 | 154 | |
|
151 | 155 | # VCS specific |
|
152 | 156 | (_('VCS Backends'), val('vcs_backends'), state('vcs_backends')), |
|
153 | 157 | (_('VCS Server'), val('vcs_server')['text'], state('vcs_server')), |
|
154 | 158 | (_('GIT'), val('git'), state('git')), |
|
155 | 159 | (_('HG'), val('hg'), state('hg')), |
|
156 | 160 | (_('SVN'), val('svn'), state('svn')), |
|
157 | 161 | |
|
158 | 162 | ] |
|
159 | 163 | |
|
160 | 164 | if snapshot: |
|
161 | 165 | if c.allowed_to_snapshot: |
|
162 | 166 | c.data_items.pop(0) # remove server info |
|
163 | 167 | self.request.override_renderer = 'admin/settings/settings_system_snapshot.mako' |
|
164 | 168 | else: |
|
165 | 169 | self.request.session.flash( |
|
166 | 170 | 'You are not allowed to do this', queue='warning') |
|
167 | return {} | |
|
171 | return self._get_template_context(c) | |
|
168 | 172 | |
|
169 | 173 | @LoginRequired() |
|
170 | 174 | @HasPermissionAllDecorator('hg.admin') |
|
171 | 175 | @view_config( |
|
172 | 176 | route_name='admin_settings_system_update', request_method='GET', |
|
173 | 177 | renderer='rhodecode:templates/admin/settings/settings_system_update.mako') |
|
174 | 178 | def settings_system_info_check_update(self): |
|
175 | 179 | _ = self.request.translate |
|
180 | c = self.load_default_context() | |
|
176 | 181 | |
|
177 | 182 | update_url = self.get_update_url() |
|
178 | 183 | |
|
179 | 184 | _err = lambda s: '<div style="color:#ff8888; padding:4px 0px">{}</div>'.format(s) |
|
180 | 185 | try: |
|
181 | 186 | data = self.get_update_data(update_url) |
|
182 | 187 | except urllib2.URLError as e: |
|
183 | 188 | log.exception("Exception contacting upgrade server") |
|
184 | 189 | self.request.override_renderer = 'string' |
|
185 | 190 | return _err('Failed to contact upgrade server: %r' % e) |
|
186 | 191 | except ValueError as e: |
|
187 | 192 | log.exception("Bad data sent from update server") |
|
188 | 193 | self.request.override_renderer = 'string' |
|
189 | 194 | return _err('Bad data sent from update server') |
|
190 | 195 | |
|
191 | 196 | latest = data['versions'][0] |
|
192 | 197 | |
|
193 | 198 | c.update_url = update_url |
|
194 | 199 | c.latest_data = latest |
|
195 | 200 | c.latest_ver = latest['version'] |
|
196 | 201 | c.cur_ver = rhodecode.__version__ |
|
197 | 202 | c.should_upgrade = False |
|
198 | 203 | |
|
199 | 204 | if (packaging.version.Version(c.latest_ver) > |
|
200 | 205 | packaging.version.Version(c.cur_ver)): |
|
201 | 206 | c.should_upgrade = True |
|
202 | 207 | c.important_notices = latest['general'] |
|
203 | 208 | |
|
204 | return {} | |
|
209 | return self._get_template_context(c) |
@@ -1,178 +1,175 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-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 | """ |
|
22 | 22 | Channel Stream controller for rhodecode |
|
23 | 23 | |
|
24 | 24 | :created_on: Oct 10, 2015 |
|
25 | 25 | :author: marcinl |
|
26 | 26 | :copyright: (c) 2013-2015 RhodeCode GmbH. |
|
27 | 27 | :license: Commercial License, see LICENSE for more details. |
|
28 | 28 | """ |
|
29 | 29 | |
|
30 | 30 | import logging |
|
31 | 31 | import uuid |
|
32 | 32 | |
|
33 | from pylons import tmpl_context as c | |
|
34 | from pyramid.settings import asbool | |
|
35 | 33 | from pyramid.view import view_config |
|
36 | 34 | from webob.exc import HTTPBadRequest, HTTPForbidden, HTTPBadGateway |
|
37 | 35 | |
|
38 | 36 | from rhodecode.lib.channelstream import ( |
|
39 | 37 | channelstream_request, |
|
40 | 38 | ChannelstreamConnectionException, |
|
41 | 39 | ChannelstreamPermissionException, |
|
42 | 40 | check_channel_permissions, |
|
43 | 41 | get_connection_validators, |
|
44 | 42 | get_user_data, |
|
45 | 43 | parse_channels_info, |
|
46 | 44 | update_history_from_logs, |
|
47 | 45 | STATE_PUBLIC_KEYS) |
|
48 | 46 | from rhodecode.lib.auth import NotAnonymous |
|
49 | from rhodecode.lib.utils2 import str2bool | |
|
50 | 47 | |
|
51 | 48 | log = logging.getLogger(__name__) |
|
52 | 49 | |
|
53 | 50 | |
|
54 | 51 | class ChannelstreamView(object): |
|
55 | 52 | def __init__(self, context, request): |
|
56 | 53 | self.context = context |
|
57 | 54 | self.request = request |
|
58 | 55 | |
|
59 | 56 | # Some of the decorators rely on this attribute to be present |
|
60 | 57 | # on the class of the decorated method. |
|
61 | 58 | self._rhodecode_user = request.user |
|
62 | 59 | registry = request.registry |
|
63 | 60 | self.channelstream_config = registry.rhodecode_plugins['channelstream'] |
|
64 | 61 | if not self.channelstream_config.get('enabled'): |
|
65 | 62 | log.error('Channelstream plugin is disabled') |
|
66 | 63 | raise HTTPBadRequest() |
|
67 | 64 | |
|
68 | 65 | @NotAnonymous() |
|
69 | 66 | @view_config(route_name='channelstream_connect', renderer='json') |
|
70 | 67 | def connect(self): |
|
71 | 68 | """ handle authorization of users trying to connect """ |
|
72 | 69 | try: |
|
73 | 70 | json_body = self.request.json_body |
|
74 | 71 | except Exception: |
|
75 | 72 | log.exception('Failed to decode json from request') |
|
76 | 73 | raise HTTPBadRequest() |
|
77 | 74 | try: |
|
78 | 75 | channels = check_channel_permissions( |
|
79 | 76 | json_body.get('channels'), |
|
80 | 77 | get_connection_validators(self.request.registry)) |
|
81 | 78 | except ChannelstreamPermissionException: |
|
82 | 79 | log.error('Incorrect permissions for requested channels') |
|
83 | 80 | raise HTTPForbidden() |
|
84 | 81 | |
|
85 |
user = |
|
|
82 | user = self._rhodecode_user | |
|
86 | 83 | if user.user_id: |
|
87 | 84 | user_data = get_user_data(user.user_id) |
|
88 | 85 | else: |
|
89 | 86 | user_data = { |
|
90 | 87 | 'id': None, |
|
91 | 88 | 'username': None, |
|
92 | 89 | 'first_name': None, |
|
93 | 90 | 'last_name': None, |
|
94 | 91 | 'icon_link': None, |
|
95 | 92 | 'display_name': None, |
|
96 | 93 | 'display_link': None, |
|
97 | 94 | } |
|
98 |
user_data['permissions'] = |
|
|
95 | user_data['permissions'] = self._rhodecode_user.permissions | |
|
99 | 96 | payload = { |
|
100 | 97 | 'username': user.username, |
|
101 | 98 | 'user_state': user_data, |
|
102 | 99 | 'conn_id': str(uuid.uuid4()), |
|
103 | 100 | 'channels': channels, |
|
104 | 101 | 'channel_configs': {}, |
|
105 | 102 | 'state_public_keys': STATE_PUBLIC_KEYS, |
|
106 | 103 | 'info': { |
|
107 | 104 | 'exclude_channels': ['broadcast'] |
|
108 | 105 | } |
|
109 | 106 | } |
|
110 | 107 | filtered_channels = [channel for channel in channels |
|
111 | 108 | if channel != 'broadcast'] |
|
112 | 109 | for channel in filtered_channels: |
|
113 | 110 | payload['channel_configs'][channel] = { |
|
114 | 111 | 'notify_presence': True, |
|
115 | 112 | 'history_size': 100, |
|
116 | 113 | 'store_history': True, |
|
117 | 114 | 'broadcast_presence_with_user_lists': True |
|
118 | 115 | } |
|
119 | 116 | # connect user to server |
|
120 | 117 | try: |
|
121 | 118 | connect_result = channelstream_request(self.channelstream_config, |
|
122 | 119 | payload, '/connect') |
|
123 | 120 | except ChannelstreamConnectionException: |
|
124 | 121 | log.exception('Channelstream service is down') |
|
125 | 122 | return HTTPBadGateway() |
|
126 | 123 | |
|
127 | 124 | connect_result['channels'] = channels |
|
128 | 125 | connect_result['channels_info'] = parse_channels_info( |
|
129 | 126 | connect_result['channels_info'], |
|
130 | 127 | include_channel_info=filtered_channels) |
|
131 | 128 | update_history_from_logs(self.channelstream_config, |
|
132 | 129 | filtered_channels, connect_result) |
|
133 | 130 | return connect_result |
|
134 | 131 | |
|
135 | 132 | @NotAnonymous() |
|
136 | 133 | @view_config(route_name='channelstream_subscribe', renderer='json') |
|
137 | 134 | def subscribe(self): |
|
138 | 135 | """ can be used to subscribe specific connection to other channels """ |
|
139 | 136 | try: |
|
140 | 137 | json_body = self.request.json_body |
|
141 | 138 | except Exception: |
|
142 | 139 | log.exception('Failed to decode json from request') |
|
143 | 140 | raise HTTPBadRequest() |
|
144 | 141 | try: |
|
145 | 142 | channels = check_channel_permissions( |
|
146 | 143 | json_body.get('channels'), |
|
147 | 144 | get_connection_validators(self.request.registry)) |
|
148 | 145 | except ChannelstreamPermissionException: |
|
149 | 146 | log.error('Incorrect permissions for requested channels') |
|
150 | 147 | raise HTTPForbidden() |
|
151 | 148 | payload = {'conn_id': json_body.get('conn_id', ''), |
|
152 | 149 | 'channels': channels, |
|
153 | 150 | 'channel_configs': {}, |
|
154 | 151 | 'info': { |
|
155 | 152 | 'exclude_channels': ['broadcast']} |
|
156 | 153 | } |
|
157 | 154 | filtered_channels = [chan for chan in channels if chan != 'broadcast'] |
|
158 | 155 | for channel in filtered_channels: |
|
159 | 156 | payload['channel_configs'][channel] = { |
|
160 | 157 | 'notify_presence': True, |
|
161 | 158 | 'history_size': 100, |
|
162 | 159 | 'store_history': True, |
|
163 | 160 | 'broadcast_presence_with_user_lists': True |
|
164 | 161 | } |
|
165 | 162 | try: |
|
166 | 163 | connect_result = channelstream_request( |
|
167 | 164 | self.channelstream_config, payload, '/subscribe') |
|
168 | 165 | except ChannelstreamConnectionException: |
|
169 | 166 | log.exception('Channelstream service is down') |
|
170 | 167 | return HTTPBadGateway() |
|
171 | 168 | # include_channel_info will limit history only to new channel |
|
172 | 169 | # to not overwrite histories on other channels in client |
|
173 | 170 | connect_result['channels_info'] = parse_channels_info( |
|
174 | 171 | connect_result['channels_info'], |
|
175 | 172 | include_channel_info=filtered_channels) |
|
176 | 173 | update_history_from_logs(self.channelstream_config, |
|
177 | 174 | filtered_channels, connect_result) |
|
178 | 175 | return connect_result |
General Comments 0
You need to be logged in to leave comments.
Login now