Skip to content

Commit ff5bbc9

Browse files
committed
remove dependance between ldapmanager and config
1 parent a276bfb commit ff5bbc9

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

supysonic/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DefaultConfig:
5454
"base_dn": None,
5555
"user_filter": "(&(objectClass=inetOrgPerson))",
5656
"admin_filter": None,
57-
"bind_user": None,
57+
"bind_dn": None,
5858
"bind_password": None,
5959
"username_attr": "uid",
6060
"email_attr": "mail",

supysonic/managers/ldap.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,68 @@
11
import logging
22

3-
try:
4-
import ldap3
5-
except ModuleNotFoundError:
6-
ldap3 = None
73

8-
from ..config import get_current_config
4+
import ldap3
5+
96

107
logger = logging.getLogger(__name__)
118

129

1310
class LdapManager:
14-
@staticmethod
15-
def try_auth(user, password):
16-
config = get_current_config().LDAP
17-
entrie = LdapManager.search_user(user, config["admin_filter"])
18-
if entrie:
19-
logger.debug("{0} is admin".format(user))
20-
admin = True
21-
else:
22-
entrie = LdapManager.search_user(user, config["user_filter"])
11+
12+
def __init__(self, ldap_server, base_dn, user_filter, admin_filter, bind_dn, bind_password, username_attr, email_attr):
13+
self.ldap_server=ldap_server
14+
self.base_dn=base_dn
15+
self.user_filter=user_filter
16+
self.admin_filter=admin_filter
17+
self.bind_dn=bind_dn
18+
self.bind_password=bind_password
19+
self.username_attr=username_attr
20+
self.email_attr=email_attr
21+
if not self.ldap_server:
22+
raise ValueError("No LDAP configured")
23+
self.server = ldap3.Server(self.ldap_server, get_info="ALL")
24+
25+
def try_auth(self,user, password):
26+
admin= False
27+
if self.admin_filter:
28+
entrie = self.search_user(user, self.admin_filter)
2329
if entrie:
24-
admin = False
25-
else:
30+
logger.debug("{0} is admin".format(user))
31+
admin = True
32+
if not admin:
33+
entrie = self.search_user(user, self.user_filter)
34+
if not entrie:
2635
return False
27-
server = ldap3.Server(config["ldap_server"], get_info="ALL")
2836
try:
2937
with ldap3.Connection(
30-
server, entrie.entry_dn, password, read_only=True
38+
self.server, entrie.entry_dn, password, read_only=True
3139
) as conn:
3240
return {
33-
"uid": entrie[config["username_attr"]],
34-
"mail": entrie[config["email_attr"]],
41+
"uid": entrie[self.username_attr],
42+
"mail": entrie[self.email_attr],
3543
"admin": admin,
3644
}
3745
except ldap3.core.exceptions.LDAPBindError:
3846
logger.warning("wrong password for user {0}".format(user))
3947
return False
4048

41-
@staticmethod
42-
def search_user(user, filter):
43-
if not ldap3:
44-
logger.warning("module 'ldap2' is not installed")
45-
return False
46-
config = get_current_config().LDAP
47-
if not config["ldap_server"]:
48-
logger.info("No LDAP configured")
49-
return False
50-
server = ldap3.Server(config["ldap_server"], get_info="ALL")
49+
def search_user(self,user, filter):
50+
5151
try:
5252
with ldap3.Connection(
53-
server, config["bind_dn"], config["bind_password"], read_only=True
53+
self.server, self.bind_dn, self.bind_password, read_only=True
5454
) as conn:
5555
conn.search(
56-
config["base_dn"],
56+
self.base_dn,
5757
filter,
58-
attributes=[config["email_attr"], config["username_attr"]],
58+
attributes=[self.email_attr, self.username_attr],
5959
)
6060
entries = conn.entries
6161
except ldap3.core.exceptions.LDAPBindError:
62-
logger.warning("wrong can't bind LDAP with {-1}".format(config["bind_dn"]))
62+
logger.warning(
63+
"wrong can't bind LDAP with {0}".format(self.bind_dn))
6364

6465
for entrie in entries:
65-
if entrie[config["username_attr"]] == user:
66+
if entrie[self.username_attr] == user:
6667
return entrie
6768
return False

supysonic/managers/user.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
import uuid
1313

1414
from ..db import User
15-
from .ldap import LdapManager
15+
from ..config import get_current_config
1616

17+
try:
18+
from .ldap import LdapManager
19+
ldap=LdapManager(**get_current_config().LDAP)
20+
except:
21+
ldap=None
1722

1823
class UserManager:
1924
@staticmethod
@@ -47,7 +52,10 @@ def delete_by_name(name):
4752

4853
@staticmethod
4954
def try_auth(name, password):
50-
ldap_user = LdapManager.try_auth(name, password)
55+
if ldap:
56+
ldap_user = ldap.try_auth(name, password)
57+
else:
58+
ldap_user= False
5159
user = User.get_or_none(name=name)
5260
if ldap_user:
5361
if user is None:

0 commit comments

Comments
 (0)