From 189f9945331e6501e863b14ebecee8f5737af08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 31 May 2016 12:55:54 +0200 Subject: [PATCH] Update to the new login API issue5521 review20961002 --- doc/index.rst | 3 +++ res.py | 19 ++++++++++++------- tests/test_ldap_authentication.py | 12 +++++++----- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index f5c48a74be..c5abfeca0c 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -6,6 +6,9 @@ The LDAP authentication module allows to authenticate users via a LDAP server. The configuration of the LDAP connection is set in the `ldap_authentication` section. +To be activated, the `ldap` method must be added to the `authentications` +methods list of the `session` section of the configuration. + Configuration ************* diff --git a/res.py b/res.py index 21dff2c9cc..0d20b8be25 100644 --- a/res.py +++ b/res.py @@ -5,8 +5,9 @@ import urlparse import ldap from trytond.transaction import Transaction -from trytond.pool import Pool, PoolMeta +from trytond.pool import PoolMeta from trytond.config import config, parse_uri +from trytond.exceptions import LoginException __all__ = ['User'] __metaclass__ = PoolMeta @@ -140,8 +141,12 @@ class User: super(User, cls).write(*args) @classmethod - def set_preferences(cls, values, old_password=False): + def set_preferences(cls, values, parameters): if 'password' in values: + if 'password' not in parameters: + msg = cls.fields_get(['password'])['password']['string'] + raise LoginException('password', msg, type='password') + old_password = parameters['password'] try: con = ldap_connection() if con: @@ -164,9 +169,11 @@ class User: super(User, cls).set_preferences(values, old_password=old_password) @classmethod - def get_login(cls, login, password): - pool = Pool() - LoginAttempt = pool.get('res.user.login.attempt') + def _login_ldap(cls, login, parameters): + if 'password' not in parameters: + msg = cls.fields_get(['password'])['password']['string'] + raise LoginException('password', msg, type='password') + password = parameters['password'] try: con = ldap_connection() if con: @@ -180,7 +187,6 @@ class User: login = attrs.get(uid, [login])[0] user_id, _ = cls._get_login(login) if user_id: - LoginAttempt.remove(login) return user_id elif config.getboolean(section, 'create_user'): user, = cls.create([{ @@ -190,4 +196,3 @@ class User: return user.id except ldap.LDAPError: logger.error('LDAPError when login', exc_info=True) - return super(User, cls).get_login(login, password) diff --git a/tests/test_ldap_authentication.py b/tests/test_ldap_authentication.py index 740b7edeea..9ab2a7bda8 100644 --- a/tests/test_ldap_authentication.py +++ b/tests/test_ldap_authentication.py @@ -21,11 +21,12 @@ class LDAPAuthenticationTestCase(ModuleTestCase): def setUp(self): super(LDAPAuthenticationTestCase, self).setUp() + methods = config.get('session', 'authentications') + config.set('session', 'authentications', 'ldap') + self.addCleanup(config.set, 'session', 'authentications', methods) config.add_section(section) config.set(section, 'uri', 'ldap://localhost/dc=tryton,dc=org') - - def tearDown(self): - config.remove_section(section) + self.addCleanup(config.remove_section, section) @with_transaction() def test_user_get_login(self): @@ -42,11 +43,12 @@ class LDAPAuthenticationTestCase(ModuleTestCase): ldap_search_user.return_value = [('dn', {'uid': [find]})] else: ldap_search_user.return_value = None - return User.get_login(login, password) + return User.get_login(login, { + 'password': password, + }) # Test existing user user, = User.search([('login', '=', 'admin')]) - self.assertEqual(get_login('admin', 'admin', None), user.id) self.assertEqual(get_login('admin', 'admin', 'admin'), user.id) self.assertEqual(get_login('AdMiN', 'admin', 'admin'), user.id)