##// END OF EJS Templates
authn: Fix error in call to logger.
johbo -
r20:bc0e5600 default
parent child Browse files
Show More
@@ -1,151 +1,151 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2012-2016 RhodeCode GmbH
3 # Copyright (C) 2012-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import logging
21 import logging
22
22
23 from pyramid.exceptions import ConfigurationError
23 from pyramid.exceptions import ConfigurationError
24 from pyramid.i18n import TranslationStringFactory
24 from pyramid.i18n import TranslationStringFactory
25
25
26 from rhodecode.lib.utils2 import safe_str
26 from rhodecode.lib.utils2 import safe_str
27 from rhodecode.model.settings import SettingsModel
27 from rhodecode.model.settings import SettingsModel
28
28
29 _ = TranslationStringFactory('rhodecode-enterprise')
29 _ = TranslationStringFactory('rhodecode-enterprise')
30
30
31 log = logging.getLogger(__name__)
31 log = logging.getLogger(__name__)
32
32
33
33
34 class AuthnResourceBase(object):
34 class AuthnResourceBase(object):
35 __name__ = None
35 __name__ = None
36 __parent__ = None
36 __parent__ = None
37
37
38 def get_root(self):
38 def get_root(self):
39 current = self
39 current = self
40 while current.__parent__ is not None:
40 while current.__parent__ is not None:
41 current = current.__parent__
41 current = current.__parent__
42 return current
42 return current
43
43
44
44
45 class AuthnPluginResourceBase(AuthnResourceBase):
45 class AuthnPluginResourceBase(AuthnResourceBase):
46
46
47 def __init__(self, plugin):
47 def __init__(self, plugin):
48 self.plugin = plugin
48 self.plugin = plugin
49 self.__name__ = plugin.name
49 self.__name__ = plugin.name
50 self.display_name = plugin.get_display_name()
50 self.display_name = plugin.get_display_name()
51
51
52
52
53 class AuthnRootResource(AuthnResourceBase):
53 class AuthnRootResource(AuthnResourceBase):
54 """
54 """
55 This is the root traversal resource object for the authentication settings.
55 This is the root traversal resource object for the authentication settings.
56 """
56 """
57
57
58 def __init__(self):
58 def __init__(self):
59 self._store = {}
59 self._store = {}
60 self._resource_name_map = {}
60 self._resource_name_map = {}
61 self.display_name = _('Global')
61 self.display_name = _('Global')
62
62
63 def __getitem__(self, key):
63 def __getitem__(self, key):
64 """
64 """
65 Customized get item function to return only items (plugins) that are
65 Customized get item function to return only items (plugins) that are
66 activated.
66 activated.
67 """
67 """
68 if self._is_item_active(key):
68 if self._is_item_active(key):
69 return self._store[key]
69 return self._store[key]
70 else:
70 else:
71 raise KeyError('Authentication plugin "{}" is not active.'.format(
71 raise KeyError('Authentication plugin "{}" is not active.'.format(
72 key))
72 key))
73
73
74 def __iter__(self):
74 def __iter__(self):
75 for key in self._store.keys():
75 for key in self._store.keys():
76 if self._is_item_active(key):
76 if self._is_item_active(key):
77 yield self._store[key]
77 yield self._store[key]
78
78
79 def _is_item_active(self, key):
79 def _is_item_active(self, key):
80 activated_plugins = SettingsModel().get_auth_plugins()
80 activated_plugins = SettingsModel().get_auth_plugins()
81 plugin_id = self.get_plugin_id(key)
81 plugin_id = self.get_plugin_id(key)
82 return plugin_id in activated_plugins
82 return plugin_id in activated_plugins
83
83
84 def get_plugin_id(self, resource_name):
84 def get_plugin_id(self, resource_name):
85 """
85 """
86 Return the plugin id for the given traversal resource name.
86 Return the plugin id for the given traversal resource name.
87 """
87 """
88 # TODO: Store this info in the resource element.
88 # TODO: Store this info in the resource element.
89 return self._resource_name_map[resource_name]
89 return self._resource_name_map[resource_name]
90
90
91 def get_sorted_list(self):
91 def get_sorted_list(self):
92 """
92 """
93 Returns a sorted list of sub resources for displaying purposes.
93 Returns a sorted list of sub resources for displaying purposes.
94 """
94 """
95 def sort_key(resource):
95 def sort_key(resource):
96 return str.lower(safe_str(resource.display_name))
96 return str.lower(safe_str(resource.display_name))
97
97
98 active = [item for item in self]
98 active = [item for item in self]
99 return sorted(active, key=sort_key)
99 return sorted(active, key=sort_key)
100
100
101 def get_nav_list(self):
101 def get_nav_list(self):
102 """
102 """
103 Returns a sorted list of resources for displaying the navigation.
103 Returns a sorted list of resources for displaying the navigation.
104 """
104 """
105 list = self.get_sorted_list()
105 list = self.get_sorted_list()
106 list.insert(0, self)
106 list.insert(0, self)
107 return list
107 return list
108
108
109 def add_authn_resource(self, config, plugin_id, resource):
109 def add_authn_resource(self, config, plugin_id, resource):
110 """
110 """
111 Register a traversal resource as a sub element to the authentication
111 Register a traversal resource as a sub element to the authentication
112 settings. This method is registered as a directive on the pyramid
112 settings. This method is registered as a directive on the pyramid
113 configurator object and called by plugins.
113 configurator object and called by plugins.
114 """
114 """
115
115
116 def _ensure_unique_name(name, limit=100):
116 def _ensure_unique_name(name, limit=100):
117 counter = 1
117 counter = 1
118 current = name
118 current = name
119 while current in self._store.keys():
119 while current in self._store.keys():
120 current = '{}{}'.format(name, counter)
120 current = '{}{}'.format(name, counter)
121 counter += 1
121 counter += 1
122 if counter > limit:
122 if counter > limit:
123 raise ConfigurationError(
123 raise ConfigurationError(
124 'Cannot build unique name for traversal resource "%s" '
124 'Cannot build unique name for traversal resource "%s" '
125 'registered by plugin "%s"', name, plugin_id)
125 'registered by plugin "%s"', name, plugin_id)
126 return current
126 return current
127
127
128 # Allow plugin resources with identical names by rename duplicates.
128 # Allow plugin resources with identical names by rename duplicates.
129 unique_name = _ensure_unique_name(resource.__name__)
129 unique_name = _ensure_unique_name(resource.__name__)
130 if unique_name != resource.__name__:
130 if unique_name != resource.__name__:
131 log.warn('Name collision for traversal resource "%s" registered',
131 log.warn('Name collision for traversal resource "%s" registered '
132 'by authentication plugin "%s"', resource.__name__,
132 'by authentication plugin "%s"', resource.__name__,
133 plugin_id)
133 plugin_id)
134 resource.__name__ = unique_name
134 resource.__name__ = unique_name
135
135
136 log.debug('Register traversal resource "%s" for plugin "%s"',
136 log.debug('Register traversal resource "%s" for plugin "%s"',
137 unique_name, plugin_id)
137 unique_name, plugin_id)
138 self._resource_name_map[unique_name] = plugin_id
138 self._resource_name_map[unique_name] = plugin_id
139 resource.__parent__ = self
139 resource.__parent__ = self
140 self._store[unique_name] = resource
140 self._store[unique_name] = resource
141
141
142
142
143 root = AuthnRootResource()
143 root = AuthnRootResource()
144
144
145
145
146 def root_factory(request=None):
146 def root_factory(request=None):
147 """
147 """
148 Returns the root traversal resource instance used for the authentication
148 Returns the root traversal resource instance used for the authentication
149 settings route.
149 settings route.
150 """
150 """
151 return root
151 return root
General Comments 0
You need to be logged in to leave comments. Login now