routes.py
154 lines
| 5.0 KiB
| text/x-python
|
PythonLexer
r5088 | # Copyright (C) 2012-2023 RhodeCode GmbH | |||
r1 | # | |||
# 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 | ||||
r3233 | import collections | |||
r1 | ||||
from pyramid.exceptions import ConfigurationError | ||||
from rhodecode.lib.utils2 import safe_str | ||||
from rhodecode.model.settings import SettingsModel | ||||
r51 | from rhodecode.translation import _ | |||
r1 | ||||
log = logging.getLogger(__name__) | ||||
class AuthnResourceBase(object): | ||||
__name__ = None | ||||
__parent__ = None | ||||
def get_root(self): | ||||
current = self | ||||
while current.__parent__ is not None: | ||||
current = current.__parent__ | ||||
return current | ||||
class AuthnPluginResourceBase(AuthnResourceBase): | ||||
def __init__(self, plugin): | ||||
self.plugin = plugin | ||||
Martin Bornhold
|
r1086 | self.__name__ = plugin.get_url_slug() | ||
r1 | self.display_name = plugin.get_display_name() | |||
class AuthnRootResource(AuthnResourceBase): | ||||
""" | ||||
This is the root traversal resource object for the authentication settings. | ||||
""" | ||||
r5505 | is_root = True | |||
r1 | ||||
def __init__(self): | ||||
r3233 | self._store = collections.OrderedDict() | |||
r1 | self._resource_name_map = {} | |||
r3234 | self.display_name = _('Authentication Plugins') | |||
r1 | ||||
def __getitem__(self, key): | ||||
""" | ||||
Customized get item function to return only items (plugins) that are | ||||
activated. | ||||
""" | ||||
if self._is_item_active(key): | ||||
return self._store[key] | ||||
else: | ||||
raise KeyError('Authentication plugin "{}" is not active.'.format( | ||||
key)) | ||||
def __iter__(self): | ||||
for key in self._store.keys(): | ||||
if self._is_item_active(key): | ||||
yield self._store[key] | ||||
def _is_item_active(self, key): | ||||
activated_plugins = SettingsModel().get_auth_plugins() | ||||
plugin_id = self.get_plugin_id(key) | ||||
return plugin_id in activated_plugins | ||||
def get_plugin_id(self, resource_name): | ||||
""" | ||||
Return the plugin id for the given traversal resource name. | ||||
""" | ||||
# TODO: Store this info in the resource element. | ||||
return self._resource_name_map[resource_name] | ||||
r3257 | def get_sorted_list(self, sort_key=None): | |||
r1 | """ | |||
Returns a sorted list of sub resources for displaying purposes. | ||||
""" | ||||
r3257 | def default_sort_key(resource): | |||
r1 | return str.lower(safe_str(resource.display_name)) | |||
active = [item for item in self] | ||||
r3257 | return sorted(active, key=sort_key or default_sort_key) | |||
r1 | ||||
r3233 | def get_nav_list(self, sort=True): | |||
r1 | """ | |||
Returns a sorted list of resources for displaying the navigation. | ||||
""" | ||||
r3233 | if sort: | |||
nav_list = self.get_sorted_list() | ||||
else: | ||||
nav_list = [item for item in self] | ||||
nav_list.insert(0, self) | ||||
return nav_list | ||||
r1 | ||||
def add_authn_resource(self, config, plugin_id, resource): | ||||
""" | ||||
Register a traversal resource as a sub element to the authentication | ||||
settings. This method is registered as a directive on the pyramid | ||||
configurator object and called by plugins. | ||||
""" | ||||
def _ensure_unique_name(name, limit=100): | ||||
counter = 1 | ||||
current = name | ||||
while current in self._store.keys(): | ||||
r5094 | current = f'{name}{counter}' | |||
r1 | counter += 1 | |||
if counter > limit: | ||||
raise ConfigurationError( | ||||
'Cannot build unique name for traversal resource "%s" ' | ||||
'registered by plugin "%s"', name, plugin_id) | ||||
return current | ||||
# Allow plugin resources with identical names by rename duplicates. | ||||
unique_name = _ensure_unique_name(resource.__name__) | ||||
if unique_name != resource.__name__: | ||||
r4999 | log.warning('Name collision for traversal resource "%s" registered ' | |||
'by authentication plugin "%s"', resource.__name__, | ||||
plugin_id) | ||||
r1 | resource.__name__ = unique_name | |||
log.debug('Register traversal resource "%s" for plugin "%s"', | ||||
unique_name, plugin_id) | ||||
self._resource_name_map[unique_name] = plugin_id | ||||
resource.__parent__ = self | ||||
self._store[unique_name] = resource | ||||
root = AuthnRootResource() | ||||
def root_factory(request=None): | ||||
""" | ||||
Returns the root traversal resource instance used for the authentication | ||||
settings route. | ||||
""" | ||||
return root | ||||