feat: init easy backend install from cli

This commit is contained in:
swve 2024-04-19 19:00:49 +02:00
parent 5c7c405e41
commit a6742d17c1
9 changed files with 134 additions and 55 deletions

View file

@ -3,7 +3,6 @@ from src.db.install import InstallRead
from src.core.events.database import get_db_session
from src.db.organizations import OrganizationCreate
from src.db.users import UserCreate
from src.services.install.install import (
create_install_instance,
get_latest_install_instance,
@ -43,7 +42,7 @@ async def api_get_latest_install_instance(
async def api_install_def_elements(
db_session=Depends(get_db_session),
):
elements = await install_default_elements(db_session)
elements = install_default_elements(db_session)
return elements
@ -53,7 +52,7 @@ async def api_install_org(
org: OrganizationCreate,
db_session=Depends(get_db_session),
):
organization = await install_create_organization(org, db_session)
organization = install_create_organization(org, db_session)
return organization
@ -64,7 +63,7 @@ async def api_install_user(
org_slug: str,
db_session=Depends(get_db_session),
):
user = await install_create_organization_user(data, org_slug, db_session)
user = install_create_organization_user(data, org_slug, db_session)
return user

View file

@ -59,7 +59,7 @@ async def authenticate_user(
user = await security_get_user(request, db_session, email)
if not user:
return False
if not await security_verify_password(password, user.password):
if not security_verify_password(password, user.password):
return False
return user

View file

@ -17,11 +17,11 @@ ALGORITHM = "HS256"
### 🔒 Passwords Hashing ##############################################################
async def security_hash_password(password: str):
def security_hash_password(password: str):
return pbkdf2_sha256.hash(password)
async def security_verify_password(plain_password: str, hashed_password: str):
def security_verify_password(plain_password: str, hashed_password: str):
return pbkdf2_sha256.verify(plain_password, hashed_password)

View file

@ -95,7 +95,7 @@ async def update_install_instance(
# Install Default roles
async def install_default_elements(db_session: Session):
def install_default_elements(db_session: Session):
"""
"""
# remove all default roles
@ -300,7 +300,7 @@ async def install_default_elements(db_session: Session):
# Organization creation
async def install_create_organization(
def install_create_organization(
org_object: OrganizationCreate, db_session: Session
):
org = Organization.model_validate(org_object)
@ -364,14 +364,14 @@ async def install_create_organization(
return org
async def install_create_organization_user(
def install_create_organization_user(
user_object: UserCreate, org_slug: str, db_session: Session
):
user = User.model_validate(user_object)
# Complete the user object
user.user_uuid = f"user_{uuid4()}"
user.password = await security_hash_password(user_object.password)
user.password = security_hash_password(user_object.password)
user.email_verified = False
user.creation_date = str(datetime.now())
user.update_date = str(datetime.now())

View file

@ -190,7 +190,7 @@ async def change_password_with_reset_code(
)
# Change password
user.password = await security_hash_password(new_password)
user.password = security_hash_password(new_password)
db_session.add(user)
db_session.commit()

View file

@ -44,7 +44,7 @@ async def create_user(
# Complete the user object
user.user_uuid = f"user_{uuid4()}"
user.password = await security_hash_password(user_object.password)
user.password = security_hash_password(user_object.password)
user.email_verified = False
user.creation_date = str(datetime.now())
user.update_date = str(datetime.now())
@ -164,7 +164,7 @@ async def create_user_without_org(
# Complete the user object
user.user_uuid = f"user_{uuid4()}"
user.password = await security_hash_password(user_object.password)
user.password = security_hash_password(user_object.password)
user.email_verified = False
user.creation_date = str(datetime.now())
user.update_date = str(datetime.now())
@ -340,13 +340,13 @@ async def update_user_password(
# RBAC check
await rbac_check(request, current_user, "update", user.user_uuid, db_session)
if not await security_verify_password(form.old_password, user.password):
if not security_verify_password(form.old_password, user.password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Wrong password"
)
# Update user
user.password = await security_hash_password(form.new_password)
user.password = security_hash_password(form.new_password)
user.update_date = str(datetime.now())
# Update user in database