2023-11-15 15:37:06 +00:00
|
|
|
from sqlalchemy import create_engine
|
2024-04-10 13:52:16 +00:00
|
|
|
from sqlalchemy import or_
|
2024-04-10 13:44:11 +00:00
|
|
|
from sqlalchemy import select
|
2023-11-15 15:37:06 +00:00
|
|
|
from sqlalchemy.orm import Session
|
2024-03-15 18:58:06 +00:00
|
|
|
from sqlalchemy.orm import declarative_base
|
2023-11-15 15:37:06 +00:00
|
|
|
|
2024-03-15 18:58:06 +00:00
|
|
|
from canaille.backends import BaseBackend
|
2023-11-15 15:37:06 +00:00
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
|
|
|
def db_session(db_uri=None, init=False):
|
|
|
|
engine = create_engine(db_uri, echo=False, future=True)
|
|
|
|
if init:
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
session = Session(engine)
|
|
|
|
return session
|
|
|
|
|
|
|
|
|
|
|
|
class Backend(BaseBackend):
|
|
|
|
db_session = None
|
|
|
|
|
|
|
|
@classmethod
|
2023-12-27 09:57:22 +00:00
|
|
|
def install(cls, config): # pragma: no cover
|
2023-11-15 15:37:06 +00:00
|
|
|
engine = create_engine(
|
2023-12-18 17:06:03 +00:00
|
|
|
config["CANAILLE_SQL"]["DATABASE_URI"],
|
2023-11-15 15:37:06 +00:00
|
|
|
echo=False,
|
|
|
|
future=True,
|
|
|
|
)
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
|
|
|
|
def setup(self, init=False):
|
|
|
|
if not self.db_session:
|
|
|
|
self.db_session = db_session(
|
2023-12-18 17:06:03 +00:00
|
|
|
self.config["CANAILLE_SQL"]["DATABASE_URI"],
|
2023-11-15 15:37:06 +00:00
|
|
|
init=init,
|
|
|
|
)
|
|
|
|
|
|
|
|
def teardown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validate(cls, config):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def login_placeholder(cls):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
def has_account_lockability(self):
|
|
|
|
return True
|
2024-04-07 17:56:52 +00:00
|
|
|
|
|
|
|
def get_user_from_login(self, login):
|
|
|
|
from .models import User
|
|
|
|
|
|
|
|
return User.get(user_name=login)
|
2024-04-07 18:12:13 +00:00
|
|
|
|
|
|
|
def check_user_password(self, user, password):
|
|
|
|
if password != user.password:
|
|
|
|
return (False, None)
|
|
|
|
|
|
|
|
if user.locked:
|
|
|
|
return (False, "Your account has been locked.")
|
|
|
|
|
|
|
|
return (True, None)
|
|
|
|
|
|
|
|
def set_user_password(self, user, password):
|
|
|
|
user.password = password
|
|
|
|
user.save()
|
2024-04-10 13:44:11 +00:00
|
|
|
|
|
|
|
def query(self, model, **kwargs):
|
|
|
|
filter = [
|
|
|
|
model.attribute_filter(attribute_name, expected_value)
|
|
|
|
for attribute_name, expected_value in kwargs.items()
|
|
|
|
]
|
|
|
|
return (
|
2024-04-10 13:59:25 +00:00
|
|
|
Backend.instance.db_session.execute(select(model).filter(*filter))
|
2024-04-10 13:44:11 +00:00
|
|
|
.scalars()
|
|
|
|
.all()
|
|
|
|
)
|
2024-04-10 13:52:16 +00:00
|
|
|
|
|
|
|
def fuzzy(self, model, query, attributes=None, **kwargs):
|
|
|
|
attributes = attributes or model.attributes
|
|
|
|
filter = or_(
|
|
|
|
getattr(model, attribute_name).ilike(f"%{query}%")
|
|
|
|
for attribute_name in attributes
|
|
|
|
if "str" in str(model.attributes[attribute_name])
|
|
|
|
# erk, photo is an URL string according to SCIM, but bytes here
|
|
|
|
and attribute_name != "photo"
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.db_session.execute(select(model).filter(filter)).scalars().all()
|