Show More
@@ -1,148 +1,148 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.controllers.admin.ldap_settings |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | ldap controller for RhodeCode |
|
7 | 7 | |
|
8 | 8 | :created_on: Nov 26, 2010 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software: you can redistribute it and/or modify |
|
14 | 14 | # it under the terms of the GNU General Public License as published by |
|
15 | 15 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | 16 | # (at your option) any later version. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | 25 | import logging |
|
26 | 26 | import formencode |
|
27 | 27 | import traceback |
|
28 | 28 | |
|
29 | 29 | from formencode import htmlfill |
|
30 | 30 | |
|
31 | 31 | from pylons import request, response, session, tmpl_context as c, url |
|
32 | 32 | from pylons.controllers.util import abort, redirect |
|
33 | 33 | from pylons.i18n.translation import _ |
|
34 | 34 | |
|
35 | 35 | from sqlalchemy.exc import DatabaseError |
|
36 | 36 | |
|
37 | 37 | from rhodecode.lib.base import BaseController, render |
|
38 | 38 | from rhodecode.lib import helpers as h |
|
39 | 39 | from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator |
|
40 | 40 | from rhodecode.lib.exceptions import LdapImportError |
|
41 | 41 | from rhodecode.model.forms import LdapSettingsForm |
|
42 | 42 | from rhodecode.model.db import RhodeCodeSetting |
|
43 | 43 | from rhodecode.model.meta import Session |
|
44 | 44 | |
|
45 | 45 | log = logging.getLogger(__name__) |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | class LdapSettingsController(BaseController): |
|
49 | 49 | |
|
50 | 50 | search_scope_choices = [('BASE', _('BASE'),), |
|
51 | 51 | ('ONELEVEL', _('ONELEVEL'),), |
|
52 | 52 | ('SUBTREE', _('SUBTREE'),), |
|
53 | 53 | ] |
|
54 | 54 | search_scope_default = 'SUBTREE' |
|
55 | 55 | |
|
56 | 56 | tls_reqcert_choices = [('NEVER', _('NEVER'),), |
|
57 | 57 | ('ALLOW', _('ALLOW'),), |
|
58 | 58 | ('TRY', _('TRY'),), |
|
59 | 59 | ('DEMAND', _('DEMAND'),), |
|
60 | 60 | ('HARD', _('HARD'),), |
|
61 | 61 | ] |
|
62 | 62 | tls_reqcert_default = 'DEMAND' |
|
63 | 63 | |
|
64 | 64 | tls_kind_choices = [('PLAIN', _('No encryption'),), |
|
65 | 65 | ('LDAPS', _('LDAPS connection'),), |
|
66 | 66 | ('START_TLS', _('START_TLS on LDAP connection'),) |
|
67 | 67 | ] |
|
68 | 68 | |
|
69 | 69 | tls_kind_default = 'PLAIN' |
|
70 | 70 | |
|
71 | 71 | @LoginRequired() |
|
72 | 72 | @HasPermissionAllDecorator('hg.admin') |
|
73 | 73 | def __before__(self): |
|
74 | 74 | c.search_scope_choices = self.search_scope_choices |
|
75 | 75 | c.tls_reqcert_choices = self.tls_reqcert_choices |
|
76 | 76 | c.tls_kind_choices = self.tls_kind_choices |
|
77 | 77 | |
|
78 | 78 | c.search_scope_cur = self.search_scope_default |
|
79 | 79 | c.tls_reqcert_cur = self.tls_reqcert_default |
|
80 | 80 | c.tls_kind_cur = self.tls_kind_default |
|
81 | 81 | |
|
82 | 82 | super(LdapSettingsController, self).__before__() |
|
83 | 83 | |
|
84 | 84 | def index(self): |
|
85 | 85 | defaults = RhodeCodeSetting.get_ldap_settings() |
|
86 | 86 | c.search_scope_cur = defaults.get('ldap_search_scope') |
|
87 | 87 | c.tls_reqcert_cur = defaults.get('ldap_tls_reqcert') |
|
88 | 88 | c.tls_kind_cur = defaults.get('ldap_tls_kind') |
|
89 | 89 | |
|
90 | 90 | return htmlfill.render( |
|
91 | 91 | render('admin/ldap/ldap.html'), |
|
92 | 92 | defaults=defaults, |
|
93 | 93 | encoding="UTF-8", |
|
94 | 94 | force_defaults=True,) |
|
95 | 95 | |
|
96 | 96 | def ldap_settings(self): |
|
97 | 97 | """POST ldap create and store ldap settings""" |
|
98 | 98 | |
|
99 | 99 | _form = LdapSettingsForm([x[0] for x in self.tls_reqcert_choices], |
|
100 | 100 | [x[0] for x in self.search_scope_choices], |
|
101 | 101 | [x[0] for x in self.tls_kind_choices])() |
|
102 | 102 | # check the ldap lib |
|
103 | 103 | ldap_active = False |
|
104 | 104 | try: |
|
105 | 105 | import ldap |
|
106 | 106 | ldap_active = True |
|
107 | 107 | except ImportError: |
|
108 | 108 | pass |
|
109 | 109 | |
|
110 | 110 | try: |
|
111 | 111 | form_result = _form.to_python(dict(request.POST)) |
|
112 | 112 | |
|
113 | 113 | try: |
|
114 | 114 | |
|
115 | 115 | for k, v in form_result.items(): |
|
116 | 116 | if k.startswith('ldap_'): |
|
117 | 117 | if k == 'ldap_active': |
|
118 | v = ldap_active | |
|
118 | v = v if ldap_active else False | |
|
119 | 119 | setting = RhodeCodeSetting.get_by_name(k) |
|
120 | 120 | setting.app_settings_value = v |
|
121 | 121 | Session().add(setting) |
|
122 | 122 | |
|
123 | 123 | Session().commit() |
|
124 | 124 | h.flash(_('LDAP settings updated successfully'), |
|
125 | 125 | category='success') |
|
126 | 126 | if not ldap_active: |
|
127 | 127 | #if ldap is missing send an info to user |
|
128 |
h.flash(_('Unable to activate ldap. The "python-ldap" |
|
|
129 | 'is missing.'), category='warning') | |
|
128 | h.flash(_('Unable to activate ldap. The "python-ldap" ' | |
|
129 | 'library is missing.'), category='warning') | |
|
130 | 130 | |
|
131 | 131 | except (DatabaseError,): |
|
132 | 132 | raise |
|
133 | 133 | |
|
134 | 134 | except formencode.Invalid, errors: |
|
135 | 135 | e = errors.error_dict or {} |
|
136 | 136 | |
|
137 | 137 | return htmlfill.render( |
|
138 | 138 | render('admin/ldap/ldap.html'), |
|
139 | 139 | defaults=errors.value, |
|
140 | 140 | errors=e, |
|
141 | 141 | prefix_error=False, |
|
142 | 142 | encoding="UTF-8") |
|
143 | 143 | except Exception: |
|
144 | 144 | log.error(traceback.format_exc()) |
|
145 | 145 | h.flash(_('Error occurred during update of ldap settings'), |
|
146 | 146 | category='error') |
|
147 | 147 | |
|
148 | 148 | return redirect(url('ldap_home')) |
General Comments 0
You need to be logged in to leave comments.
Login now