diff --git a/src/ankisyncd/users/__init__.py b/src/ankisyncd/users/__init__.py new file mode 100644 index 0000000..9708b25 --- /dev/null +++ b/src/ankisyncd/users/__init__.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- + +import importlib +import inspect + +from ankisyncd import logging +from ankisyncd.users.simple_manager import SimpleUserManager +from ankisyncd.users.sqlite_manager import SqliteUserManager + +logger = logging.get_logger(__name__) + + +def get_user_manager(config): + if "auth_db_path" in config and config["auth_db_path"]: + logger.info("Found auth_db_path in config, using SqliteUserManager for auth") + return SqliteUserManager(config["auth_db_path"], config["data_root"]) + elif "user_manager" in config and config["user_manager"]: # load from config + logger.info( + "Found user_manager in config, using {} for auth".format( + config["user_manager"] + ) + ) + + module_name, class_name = config["user_manager"].rsplit(".", 1) + module = importlib.import_module(module_name.strip()) + class_ = getattr(module, class_name.strip()) + + if not SimpleUserManager in inspect.getmro(class_): + raise TypeError( + """"user_manager" found in the conf file but it doesn''t + inherit from SimpleUserManager""" + ) + return class_(config) + else: + logger.warning( + "neither auth_db_path nor user_manager set, ankisyncd will accept any password" + ) + return SimpleUserManager() diff --git a/src/ankisyncd/users/simple_manager.py b/src/ankisyncd/users/simple_manager.py new file mode 100644 index 0000000..c62e35b --- /dev/null +++ b/src/ankisyncd/users/simple_manager.py @@ -0,0 +1,38 @@ +import os +from ankisyncd import logging + +logger = logging.get_logger(__name__) + + +class SimpleUserManager: + """A simple user manager that always allows any user.""" + + def __init__(self, collection_path=""): + self.collection_path = collection_path + + def authenticate(self, username, password): + """ + Returns True if this username is allowed to connect with this password. + False otherwise. Override this to change how users are authenticated. + """ + + return True + + def userdir(self, username): + """ + Returns the directory name for the given user. By default, this is just + the username. Override this to adjust the mapping between users and + their directory. + """ + + return username + + def _create_user_dir(self, username): + user_dir_path = os.path.join(self.collection_path, username) + if not os.path.isdir(user_dir_path): + logger.info( + "Creating collection directory for user '{}' at {}".format( + username, user_dir_path + ) + ) + os.makedirs(user_dir_path) diff --git a/src/ankisyncd/users.py b/src/ankisyncd/users/sqlite_manager.py similarity index 70% rename from src/ankisyncd/users.py rename to src/ankisyncd/users/sqlite_manager.py index ab1fb62..bf5fd9b 100644 --- a/src/ankisyncd/users.py +++ b/src/ankisyncd/users/sqlite_manager.py @@ -1,45 +1,11 @@ -# -*- coding: utf-8 -*- import binascii import hashlib -import logging import os import sqlite3 as sqlite +from ankisyncd import logging +from ankisyncd.users.simple_manager import SimpleUserManager -logger = logging.getLogger("ankisyncd.users") - - -class SimpleUserManager: - """A simple user manager that always allows any user.""" - - def __init__(self, collection_path=""): - self.collection_path = collection_path - - def authenticate(self, username, password): - """ - Returns True if this username is allowed to connect with this password. - False otherwise. Override this to change how users are authenticated. - """ - - return True - - def userdir(self, username): - """ - Returns the directory name for the given user. By default, this is just - the username. Override this to adjust the mapping between users and - their directory. - """ - - return username - - def _create_user_dir(self, username): - user_dir_path = os.path.join(self.collection_path, username) - if not os.path.isdir(user_dir_path): - logger.info( - "Creating collection directory for user '{}' at {}".format( - username, user_dir_path - ) - ) - os.makedirs(user_dir_path) +logger = logging.get_logger(__name__) class SqliteUserManager(SimpleUserManager): @@ -208,33 +174,3 @@ class SqliteUserManager(SimpleUserManager): ) conn.commit() conn.close() - - -def get_user_manager(config): - if "auth_db_path" in config and config["auth_db_path"]: - logger.info("Found auth_db_path in config, using SqliteUserManager for auth") - return SqliteUserManager(config["auth_db_path"], config["data_root"]) - elif "user_manager" in config and config["user_manager"]: # load from config - logger.info( - "Found user_manager in config, using {} for auth".format( - config["user_manager"] - ) - ) - import importlib - import inspect - - module_name, class_name = config["user_manager"].rsplit(".", 1) - module = importlib.import_module(module_name.strip()) - class_ = getattr(module, class_name.strip()) - - if not SimpleUserManager in inspect.getmro(class_): - raise TypeError( - """"user_manager" found in the conf file but it doesn''t - inherit from SimpleUserManager""" - ) - return class_(config) - else: - logger.warning( - "neither auth_db_path nor user_manager set, ankisyncd will accept any password" - ) - return SimpleUserManager()