Update for new Transaction
This commit is contained in:
@@ -4,7 +4,7 @@ Installing trytond_ldap_authentication
|
||||
Prerequisites
|
||||
-------------
|
||||
|
||||
* Python 2.4 or later (http://www.python.org/)
|
||||
* Python 2.5 or later (http://www.python.org/)
|
||||
* trytond (http://www.tryton.org/)
|
||||
* trytond_ldap_connection (http://www.tryton.org/)
|
||||
* python-ldap (http://python-ldap.sourceforge.net/)
|
||||
|
||||
+3
-3
@@ -19,13 +19,13 @@ class Connection(ModelSingleton, ModelSQL, ModelView):
|
||||
auth_create_user = fields.Boolean('Create User',
|
||||
help='Create user if not in database')
|
||||
|
||||
def default_auth_uid(self, cursor, user, context=None):
|
||||
def default_auth_uid(self):
|
||||
return 'uid'
|
||||
|
||||
def default_auth_scope(self, cursor, user, context=None):
|
||||
def default_auth_scope(self):
|
||||
return 'base'
|
||||
|
||||
def default_auth_create_user(self, cursor, user, context=None):
|
||||
def default_auth_create_user(self):
|
||||
return False
|
||||
|
||||
Connection()
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
#this repository contains the full copyright notices and license terms.
|
||||
from trytond.model import ModelView, ModelSQL
|
||||
from __future__ import with_statement
|
||||
import ldap
|
||||
|
||||
from trytond.model import ModelView, ModelSQL
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
class User(ModelSQL, ModelView):
|
||||
_name = 'res.user'
|
||||
@@ -14,18 +15,14 @@ class User(ModelSQL, ModelView):
|
||||
'You can not set a password to a ldap user!',
|
||||
})
|
||||
|
||||
def ldap_search_user(self, cursor, user, login, con, connection,
|
||||
attrs=None, context=None):
|
||||
def ldap_search_user(self, login, con, connection, attrs=None):
|
||||
'''
|
||||
Return the resule of a ldap search for the login
|
||||
|
||||
:param cursor: the database cursor
|
||||
:param user: the user id
|
||||
:param login: the login
|
||||
:param con: the ldap connection
|
||||
:param connection: a BrowseRecord of ldap.connection
|
||||
:param attrs: a list of attribute to return
|
||||
:param context: the context
|
||||
:return: the ldap search result
|
||||
'''
|
||||
scope = {
|
||||
@@ -45,12 +42,11 @@ class User(ModelSQL, ModelView):
|
||||
result = [x for x in result if x[0]]
|
||||
return result
|
||||
|
||||
def _check_passwd_ldap_user(self, cursor, user, logins, context=None):
|
||||
def _check_passwd_ldap_user(self, logins):
|
||||
connection_obj = self.pool.get('ldap.connection')
|
||||
connection_ids = connection_obj.search(cursor, 0, [], limit=1,
|
||||
context=context)
|
||||
connection = connection_obj.browse(cursor, 0, connection_ids[0],
|
||||
context=context)
|
||||
connection_ids = connection_obj.search([], limit=1)
|
||||
with Transaction().set_user(0):
|
||||
connection = connection_obj.browse(connection_ids[0])
|
||||
find = False
|
||||
try:
|
||||
con = ldap.initialize(connection.uri)
|
||||
@@ -61,41 +57,35 @@ class User(ModelSQL, ModelView):
|
||||
if connection.bind_dn:
|
||||
con.simple_bind_s(connection.bind_dn, connection.bind_pass)
|
||||
for login in logins:
|
||||
if self.ldap_search_user(cursor, user, login,
|
||||
con, connection, attrs=[], context=context):
|
||||
if self.ldap_search_user(login,
|
||||
con, connection, attrs=[]):
|
||||
find = True
|
||||
except Exception:
|
||||
pass
|
||||
if find:
|
||||
self.raise_user_error(cursor, 'set_passwd_ldap_user',
|
||||
context=context)
|
||||
self.raise_user_error('set_passwd_ldap_user')
|
||||
|
||||
def create(self, cursor, user, vals, context=None):
|
||||
def create(self, vals):
|
||||
if vals.get('password') and 'login' in vals:
|
||||
self._check_passwd_ldap_user(cursor, user, [vals['login']],
|
||||
context=context)
|
||||
return super(User, self).create(cursor, user, vals, context=context)
|
||||
self._check_passwd_ldap_user([vals['login']])
|
||||
return super(User, self).create(vals)
|
||||
|
||||
def write(self, cursor, user, ids, vals, context=None):
|
||||
def write(self, ids, vals):
|
||||
if vals.get('password'):
|
||||
if isinstance(ids, (int, long)):
|
||||
ids2 = [ids]
|
||||
else:
|
||||
ids2 = ids
|
||||
logins = [x.login for x in self.browse(cursor, user, ids2,
|
||||
context=context)]
|
||||
self._check_passwd_ldap_user(cursor, user, logins, context=context)
|
||||
return super(User, self).write(cursor, user, ids, vals, context=context)
|
||||
logins = [x.login for x in self.browse(ids2)]
|
||||
self._check_passwd_ldap_user(logins)
|
||||
return super(User, self).write(ids, vals)
|
||||
|
||||
def set_preferences(self, cursor, user_id, values, old_password=False,
|
||||
context=None):
|
||||
def set_preferences(self, values, old_password=False):
|
||||
connection_obj = self.pool.get('ldap.connection')
|
||||
|
||||
if 'password' in values:
|
||||
connection_ids = connection_obj.search(cursor, 0, [], limit=1,
|
||||
context=context)
|
||||
connection = connection_obj.browse(cursor, 0, connection_ids[0],
|
||||
context=context)
|
||||
connection_ids = connection_obj.search([], limit=1)
|
||||
with Transaction().set_user(0):
|
||||
connection = connection_obj.browse(connection_ids[0])
|
||||
try:
|
||||
con = ldap.initialize(connection.uri)
|
||||
if connection.active_directory:
|
||||
@@ -104,28 +94,25 @@ class User(ModelSQL, ModelView):
|
||||
con.start_tls_s()
|
||||
if connection.bind_dn:
|
||||
con.simple_bind_s(connection.bind_dn, connection.bind_pass)
|
||||
user = self.browse(cursor, user_id, user_id, context=context)
|
||||
[(dn, attrs)] = self.ldap_search_user(cursor, user_id,
|
||||
user.login, con, connection,
|
||||
attrs=[str(connection.auth_uid)], context=context)
|
||||
user = self.browse(Transaction().user)
|
||||
[(dn, attrs)] = self.ldap_search_user(user.login, con,
|
||||
connection, attrs=[str(connection.auth_uid)])
|
||||
if con.simple_bind_s(dn, old_password):
|
||||
con.passwd_s(dn, old_password, values['password'])
|
||||
values = values.copy()
|
||||
del values['password']
|
||||
else:
|
||||
self.raise_user_error(cursor, 'wrong_password',
|
||||
context=context)
|
||||
self.raise_user_error('wrong_password')
|
||||
except Exception:
|
||||
pass
|
||||
return super(User, self).set_preferences(cursor, user_id, values,
|
||||
old_password=old_password, context=context)
|
||||
return super(User, self).set_preferences(values,
|
||||
old_password=old_password)
|
||||
|
||||
def get_login(self, cursor, user, login, password, context=None):
|
||||
def get_login(self, login, password):
|
||||
connection_obj = self.pool.get('ldap.connection')
|
||||
connection_ids = connection_obj.search(cursor, 0, [], limit=1,
|
||||
context=context)
|
||||
connection = connection_obj.browse(cursor, 0, connection_ids[0],
|
||||
context=context)
|
||||
connection_ids = connection_obj.search([], limit=1)
|
||||
with Transaction().set_user(0):
|
||||
connection = connection_obj.browse(connection_ids[0])
|
||||
try:
|
||||
con = ldap.initialize(connection.uri)
|
||||
if connection.active_directory:
|
||||
@@ -134,23 +121,20 @@ class User(ModelSQL, ModelView):
|
||||
con.start_tls_s()
|
||||
if connection.bind_dn:
|
||||
con.simple_bind_s(connection.bind_dn, connection.bind_pass)
|
||||
[(dn, attrs)] = self.ldap_search_user(cursor, user, login,
|
||||
con, connection, attrs=[str(connection.auth_uid)],
|
||||
context=context)
|
||||
[(dn, attrs)] = self.ldap_search_user(login, con, connection,
|
||||
attrs=[str(connection.auth_uid)])
|
||||
if con.simple_bind_s(dn, password):
|
||||
user_id, _, _ = self._get_login(cursor, user, login,
|
||||
context=context)
|
||||
user_id, _, _ = self._get_login(login)
|
||||
if user_id:
|
||||
return user_id
|
||||
elif connection.auth_create_user:
|
||||
user_id = self.create(cursor, 0, {
|
||||
user_id = self.create({
|
||||
'name': attrs.get(str(connection.auth_uid), [login])[0],
|
||||
'login': login,
|
||||
}, context=context)
|
||||
})
|
||||
return user_id
|
||||
except Exception:
|
||||
pass
|
||||
return super(User, self).get_login(cursor, user, login, password,
|
||||
context=context)
|
||||
return super(User, self).get_login(login, password)
|
||||
|
||||
User()
|
||||
|
||||
Reference in New Issue
Block a user