refactor: Seperate users into multiple files

This commit is contained in:
Vikash Kothary 2022-10-14 23:51:43 +01:00
parent 82a3e729f1
commit 4c4ce64025
3 changed files with 79 additions and 67 deletions

View File

@ -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()

View File

@ -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)

View File

@ -1,45 +1,11 @@
# -*- coding: utf-8 -*-
import binascii import binascii
import hashlib import hashlib
import logging
import os import os
import sqlite3 as sqlite import sqlite3 as sqlite
from ankisyncd import logging
from ankisyncd.users.simple_manager import SimpleUserManager
logger = logging.getLogger("ankisyncd.users") 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)
class SqliteUserManager(SimpleUserManager): class SqliteUserManager(SimpleUserManager):
@ -208,33 +174,3 @@ class SqliteUserManager(SimpleUserManager):
) )
conn.commit() conn.commit()
conn.close() 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()