Files
Sternsinger/CMBNet/cmbnet/models/accounts.py
T
2026-10-23 00:05:06 +02:00

67 lines
2.1 KiB
Python

"""
Account Model - Abstraktionsschicht für Datenbankzugriffe
"""
from cmbnet.db import get_db
class AccountModel:
@staticmethod
def get_all_by_tenant(tenant_id):
db = get_db()
return db.execute(
'SELECT * FROM accounts WHERE tenant_id = ? ORDER BY id',
(tenant_id,)
).fetchall()
@staticmethod
def get_by_id(account_id, tenant_id=None):
db = get_db()
if tenant_id:
return db.execute(
'SELECT * FROM accounts WHERE id = ? AND tenant_id = ?',
(account_id, tenant_id)
).fetchone()
return db.execute('SELECT * FROM accounts WHERE id = ?', (account_id,)).fetchone()
@staticmethod
def get_by_name_and_tenant(name, tenant_id):
db = get_db()
return db.execute(
'SELECT * FROM accounts WHERE name = ? AND tenant_id = ?',
(name, tenant_id)
).fetchone()
@staticmethod
def create(tenant_id, name, password_hash, role):
db = get_db()
cursor = db.execute(
'INSERT INTO accounts (tenant_id, name, password_hash, role) VALUES (?, ?, ?, ?)',
(tenant_id, name, password_hash, role)
)
db.commit()
return cursor.lastrowid
@staticmethod
def update_role(account_id, tenant_id, role):
db = get_db()
db.execute(
'UPDATE accounts SET role = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? AND tenant_id = ?',
(role, account_id, tenant_id)
)
db.commit()
@staticmethod
def set_password(account_id, tenant_id, password_hash):
db = get_db()
db.execute(
'UPDATE accounts SET password_hash = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? AND tenant_id = ?',
(password_hash, account_id, tenant_id)
)
db.commit()
@staticmethod
def delete(account_id, tenant_id):
db = get_db()
db.execute('DELETE FROM accounts WHERE id = ? AND tenant_id = ?', (account_id, tenant_id))
db.commit()