Changeset - 4bdcc08b04c4
[Not reviewed]
beta
0 5 3
Marcin Kuzminski - 15 years ago 2010-11-26 09:32:40
marcin@python-works.com
fixes #77 moved out ldap config to it's own section
8 files changed with 208 insertions and 100 deletions:
0 comments (0 inline, 0 general)
rhodecode/config/routing.py
Show inline comments
 
@@ -85,7 +85,13 @@ def make_map(config):
 

	
 
    #ADMIN PERMISSIONS REST ROUTES
 
    map.resource('permission', 'permissions', controller='admin/permissions', path_prefix='/_admin')
 
    map.connect('permissions_ldap', '/_admin/permissions_ldap', controller='admin/permissions', action='ldap')
 

	
 

	
 
    ##ADMIN LDAP SETTINGS
 
    map.connect('ldap_settings', '/_admin/ldap', controller='admin/ldap_settings',
 
                action='ldap_settings', conditions=dict(method=["POST"]))
 
    map.connect('ldap_home', '/_admin/ldap', controller='admin/ldap_settings',)
 

	
 

	
 

	
 
    #ADMIN SETTINGS REST ROUTES
rhodecode/controllers/admin/ldap_settings.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8 -*-
 
"""
 
    package.rhodecode.controllers.admin.ldap_settings
 
    ~~~~~~~~~~~~~~
 

	
 
    ldap controller for RhodeCode
 
    :created_on: Nov 26, 2010
 
    :author: marcink
 
    :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>    
 
    :license: GPLv3, see COPYING for more details.
 
"""
 
# This program is free software; you can redistribute it and/or
 
# modify it under the terms of the GNU General Public License
 
# as published by the Free Software Foundation; version 2
 
# of the License or (at your opinion) any later version of the license.
 
# 
 
# 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 General Public License
 
# along with this program; if not, write to the Free Software
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
# MA  02110-1301, USA.
 
import logging
 
import formencode
 
import traceback
 

	
 
from formencode import htmlfill
 

	
 
from pylons import request, response, session, tmpl_context as c, url
 
from pylons.controllers.util import abort, redirect
 
from pylons.i18n.translation import _
 

	
 
from rhodecode.lib.base import BaseController, render
 
from rhodecode.lib import helpers as h
 
from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator
 
from rhodecode.lib.auth_ldap import LdapImportError
 
from rhodecode.model.settings import SettingsModel
 
from rhodecode.model.forms import LdapSettingsForm
 
from sqlalchemy.exc import DatabaseError
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 

	
 
class LdapSettingsController(BaseController):
 

	
 
    @LoginRequired()
 
    @HasPermissionAllDecorator('hg.admin')
 
    def __before__(self):
 
        c.admin_user = session.get('admin_user')
 
        c.admin_username = session.get('admin_username')
 
        super(LdapSettingsController, self).__before__()
 

	
 
    def index(self):
 
        defaults = SettingsModel().get_ldap_settings()
 

	
 
        return htmlfill.render(
 
                    render('admin/ldap/ldap.html'),
 
                    defaults=defaults,
 
                    encoding="UTF-8",
 
                    force_defaults=True,)
 

	
 
    def ldap_settings(self):
 
        """
 
        POST ldap create and store ldap settings
 
        """
 

	
 
        settings_model = SettingsModel()
 
        _form = LdapSettingsForm()()
 

	
 
        try:
 
            form_result = _form.to_python(dict(request.POST))
 
            try:
 

	
 
                for k, v in form_result.items():
 
                    if k.startswith('ldap_'):
 
                        setting = settings_model.get(k)
 
                        setting.app_settings_value = v
 
                        self.sa.add(setting)
 

	
 
                self.sa.commit()
 
                h.flash(_('Ldap settings updated successfully'),
 
                    category='success')
 
            except (DatabaseError,):
 
                raise
 
        except LdapImportError:
 
            h.flash(_('Unable to activate ldap. The "ldap-python" library '
 
                      'is missing.'), category='warning')
 

	
 
        except formencode.Invalid, errors:
 

	
 
            return htmlfill.render(
 
                render('admin/ldap/ldap.html'),
 
                defaults=errors.value,
 
                errors=errors.error_dict or {},
 
                prefix_error=False,
 
                encoding="UTF-8")
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            h.flash(_('error occured during update of ldap settings'),
 
                    category='error')
 

	
 
        return redirect(url('ldap_home'))
rhodecode/controllers/admin/permissions.py
Show inline comments
 
@@ -110,7 +110,6 @@ class PermissionsController(BaseControll
 
            c.register_choices = self.register_choices
 
            c.create_choices = self.create_choices
 
            defaults = errors.value
 
            defaults.update(SettingsModel().get_ldap_settings())
 

	
 
            return htmlfill.render(
 
                render('admin/permissions/permissions.html'),
 
@@ -151,7 +150,7 @@ class PermissionsController(BaseControll
 
            default_user = UserModel().get_by_username('default')
 
            defaults = {'_method':'put',
 
                        'anonymous':default_user.active}
 
            defaults.update(SettingsModel().get_ldap_settings())
 

	
 
            for p in default_user.user_perms:
 
                if p.permission.permission_name.startswith('repository.'):
 
                    defaults['default_perm'] = p.permission.permission_name
 
@@ -169,50 +168,3 @@ class PermissionsController(BaseControll
 
                        force_defaults=True,)
 
        else:
 
            return redirect(url('admin_home'))
 

	
 

	
 
    def ldap(self, id_user='default'):
 
        """
 
        POST ldap create and store ldap settings
 
        """
 

	
 
        settings_model = SettingsModel()
 
        _form = LdapSettingsForm()()
 

	
 
        try:
 
            form_result = _form.to_python(dict(request.POST))
 
            try:
 

	
 
                for k, v in form_result.items():
 
                    if k.startswith('ldap_'):
 
                        setting = settings_model.get(k)
 
                        setting.app_settings_value = v
 
                        self.sa.add(setting)
 

	
 
                self.sa.commit()
 
                h.flash(_('Ldap settings updated successfully'),
 
                    category='success')
 
            except:
 
                raise
 
        except LdapImportError:
 
            h.flash(_('Unable to activate ldap. The "ldap-python" library '
 
                      'is missing.'),
 
                    category='warning')
 

	
 
        except formencode.Invalid, errors:
 
            c.perms_choices = self.perms_choices
 
            c.register_choices = self.register_choices
 
            c.create_choices = self.create_choices
 

	
 
            return htmlfill.render(
 
                render('admin/permissions/permissions.html'),
 
                defaults=errors.value,
 
                errors=errors.error_dict or {},
 
                prefix_error=False,
 
                encoding="UTF-8")
 
        except Exception:
 
            log.error(traceback.format_exc())
 
            h.flash(_('error occured during update of ldap settings'),
 
                    category='error')
 

	
 
        return redirect(url('edit_permission', id=id_user))
rhodecode/public/css/style.css
Show inline comments
 
@@ -479,6 +479,13 @@ margin:0;
 
padding:12px 9px 7px 24px;
 
}
 
 
#header #header-inner #quick li ul li a.ldap,#header #header-inner #quick li ul li a.ldap:hover {
 
background:#FFF url("../images/icons/server_key.png") no-repeat 4px 9px;
 
width:167px;
 
margin:0;
 
padding:12px 9px 7px 24px;
 
}
 
 
#header #header-inner #quick li ul li a.fork,#header #header-inner #quick li ul li a.fork:hover {
 
background:#FFF url("../images/icons/arrow_divide.png") no-repeat 4px 9px;
 
width:167px;
rhodecode/templates/admin/ldap/ldap.html
Show inline comments
 
new file 100644
 
## -*- coding: utf-8 -*-
 
<%inherit file="/base/base.html"/>
 

	
 
<%def name="title()">
 
    ${_('LDAP administration')} - ${c.rhodecode_name}
 
</%def>
 

	
 
<%def name="breadcrumbs_links()">
 
    ${h.link_to(_('Admin'),h.url('admin_home'))} 
 
    &raquo;
 
    ${_('Ldap')}    
 
</%def>
 

	
 
<%def name="page_nav()">
 
    ${self.menu('admin')}
 
</%def>
 

	
 
<%def name="main()">
 
<div class="box">
 
    <!-- box / title -->
 
    <div class="title">
 
        ${self.breadcrumbs()}       
 
    </div>
 
    <h3>${_('LDAP administration')}</h3>
 
    ${h.form(url('ldap_settings'))}
 
    <div class="form">
 
        <div class="fields">
 

	
 
            <div class="field">
 
                <div class="label label-checkbox"><label for="ldap_active">${_('Enable ldap')}</label></div>
 
                <div class="checkboxes"><div class="checkbox">${h.checkbox('ldap_active',True,class_='small')}</div></div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_host">${_('Host')}</label></div>
 
                <div class="input">${h.text('ldap_host',class_='small')}</div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_port">${_('Port')}</label></div>
 
                <div class="input">${h.text('ldap_port',class_='small')}</div>
 
            </div>
 
            <div class="field">
 
                <div class="label label-checkbox"><label for="ldap_ldaps">${_('Enable LDAPS')}</label></div>
 
                <div class="checkboxes"><div class="checkbox">${h.checkbox('ldap_ldaps',True,class_='small')}</div></div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_dn_user">${_('Account')}</label></div>
 
                <div class="input">${h.text('ldap_dn_user',class_='small')}</div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_dn_pass">${_('Password')}</label></div>
 
                <div class="input">${h.password('ldap_dn_pass',class_='small')}</div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_base_dn">${_('Base DN')}</label></div>
 
                <div class="input">${h.text('ldap_base_dn',class_='small')}</div>
 
            </div>
 
            
 
            <div class="buttons">
 
            ${h.submit('save','Save',class_="ui-button ui-widget ui-state-default ui-corner-all")}
 
            </div>              
 
        </div>
 
    </div>     
 
    ${h.end_form()}    
 
</div>
 
</%def>    
 

	
 

	
 

	
 

	
 

	
 

	
 
   
 

	
rhodecode/templates/admin/permissions/permissions.html
Show inline comments
 
@@ -73,47 +73,6 @@
 
        </div>
 
    </div>  
 
    ${h.end_form()}
 
    ##LDAP
 
    <h3>${_('LDAP settings')}</h3>
 
    ${h.form(url('permissions_ldap',id_iser='default'),method='put')}
 
    <div class="form">
 
        <div class="fields">
 

	
 
            <div class="field">
 
                <div class="label label-checkbox"><label for="ldap_active">${_('Enable ldap')}</label></div>
 
	            <div class="checkboxes"><div class="checkbox">${h.checkbox('ldap_active',True,class_='small')}</div></div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_host">${_('Host')}</label></div>
 
                <div class="input">${h.text('ldap_host',class_='small')}</div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_port">${_('Port')}</label></div>
 
                <div class="input">${h.text('ldap_port',class_='small')}</div>
 
            </div>
 
            <div class="field">
 
                <div class="label label-checkbox"><label for="ldap_ldaps">${_('Enable LDAPS')}</label></div>
 
                <div class="checkboxes"><div class="checkbox">${h.checkbox('ldap_ldaps',True,class_='small')}</div></div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_dn_user">${_('Account')}</label></div>
 
                <div class="input">${h.text('ldap_dn_user',class_='small')}</div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_dn_pass">${_('Password')}</label></div>
 
                <div class="input">${h.password('ldap_dn_pass',class_='small')}</div>
 
            </div>
 
            <div class="field">
 
                <div class="label"><label for="ldap_base_dn">${_('Base DN')}</label></div>
 
                <div class="input">${h.text('ldap_base_dn',class_='small')}</div>
 
            </div>
 
            
 
            <div class="buttons">
 
            ${h.submit('save','Save',class_="ui-button ui-widget ui-state-default ui-corner-all")}
 
            </div>              
 
        </div>
 
    </div>     
 
    ${h.end_form()}
 
</div>
 
</%def>    
 

	
rhodecode/templates/base/base.html
Show inline comments
 
@@ -204,13 +204,18 @@
 
                    %if h.HasPermissionAll('hg.admin')('access admin main page'):
 
                    <li>
 
                       ${h.link_to(_('admin'),h.url('admin_home'),class_='admin')}  
 
                        <%def name="admin_menu()">
 
                        <ul>
 
                            <li>${h.link_to(_('journal'),h.url('admin_home'),class_='journal')}</li>
 
                            <li>${h.link_to(_('repositories'),h.url('repos'),class_='repos')}</li>
 
                            <li>${h.link_to(_('users'),h.url('users'),class_='users')}</li>
 
                            <li>${h.link_to(_('permissions'),h.url('edit_permission',id='default'),class_='permissions')}</li>
 
                            <li>${h.link_to(_('ldap'),h.url('ldap_home'),class_='ldap')}</li>
 
                            <li class="last">${h.link_to(_('settings'),h.url('admin_settings'),class_='settings')}</li>        
 
                        </ul>
 
                        </%def>
 
                        
 
                        ${admin_menu()}
 
                    </li>
 
                    %endif
 

	
 
@@ -274,17 +279,10 @@
 
                       <img src="/images/icons/cog_edit.png" alt="${_('Admin')}" />
 
                   </span>
 
                   <span>${_('Admin')}</span>                 
 
                   </a>    
 
				    <ul>
 
				        <li>${h.link_to(_('journal'),h.url('admin_home'),class_='journal')}</li>
 
				        <li>${h.link_to(_('repositories'),h.url('repos'),class_='repos')}</li>
 
				        <li>${h.link_to(_('users'),h.url('users'),class_='users')}</li>
 
				        <li>${h.link_to(_('permissions'),h.url('edit_permission',id='default'),class_='permissions')}</li>
 
				        <li class="last">${h.link_to(_('settings'),h.url('admin_settings'),class_='settings')}</li>        
 
				    </ul>
 
                   </a>
 
                    ${admin_menu()}
 
                </li>
 
				%endif
 
				
 
			</ul>
 
		%endif    
 
</%def>
rhodecode/tests/functional/test_admin_ldap_settings.py
Show inline comments
 
new file 100644
 
from rhodecode.tests import *
 

	
 
class TestLdapSettingsController(TestController):
 

	
 
    def test_index(self):
 
        response = self.app.get(url(controller='admin/ldap_settings', action='index'))
 
        # Test response...
0 comments (0 inline, 0 general)