Update to the new login API

issue5521
review20961002
This commit is contained in:
Cédric Krier
2016-05-31 12:55:54 +02:00
parent d74a359f8b
commit 189f994533
3 changed files with 22 additions and 12 deletions
+3
View File
@@ -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
*************
+12 -7
View File
@@ -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)
+7 -5
View File
@@ -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)