security.py
72 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
r5518 | # Copyright (C) 2010-2024 RhodeCode GmbH | |||
# | ||||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program 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 Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
import logging | ||||
r5522 | import formencode | |||
r5518 | ||||
r5522 | from rhodecode import BACKENDS | |||
r5518 | from rhodecode.apps._base import BaseAppView | |||
r5522 | from rhodecode.model.meta import Session | |||
from rhodecode.model.settings import SettingsModel | ||||
from rhodecode.model.forms import WhitelistedVcsClientsForm | ||||
r5518 | from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator | |||
log = logging.getLogger(__name__) | ||||
class AdminSecurityView(BaseAppView): | ||||
def load_default_context(self): | ||||
c = self._get_local_tmpl_context() | ||||
return c | ||||
@LoginRequired() | ||||
@HasPermissionAllDecorator('hg.admin') | ||||
def security(self): | ||||
c = self.load_default_context() | ||||
c.active = 'security' | ||||
return self._get_template_context(c) | ||||
r5522 | @LoginRequired() | |||
@HasPermissionAllDecorator('hg.admin') | ||||
def vcs_whitelisted_client_versions_edit(self): | ||||
_ = self.request.translate | ||||
c = self.load_default_context() | ||||
render_ctx = {} | ||||
settings = SettingsModel() | ||||
form = WhitelistedVcsClientsForm(_, )() | ||||
if self.request.method == 'POST': | ||||
try: | ||||
result = form.to_python(self.request.POST) | ||||
for k, v in result.items(): | ||||
if v: | ||||
setting = settings.create_or_update_setting(name=f'{k}_allowed_clients', val=v) | ||||
Session().add(setting) | ||||
Session().commit() | ||||
except formencode.Invalid as errors: | ||||
render_ctx.update({ | ||||
'errors': errors.error_dict | ||||
}) | ||||
for key in BACKENDS.keys(): | ||||
verbose_name = f"initial_{key}" | ||||
if existing := settings.get_setting_by_name(name=f'{key}_allowed_clients'): | ||||
render_ctx[verbose_name] = existing.app_settings_value | ||||
else: | ||||
render_ctx[verbose_name] = '*' | ||||
return self._get_template_context(c, **render_ctx) | ||||