mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
fix: more various bug and issues
This commit is contained in:
parent
86e7ecc0fc
commit
c3a5f43f13
20 changed files with 349 additions and 251 deletions
21
src/main.py
21
src/main.py
|
|
@ -1,21 +0,0 @@
|
|||
from fastapi import APIRouter
|
||||
from src.routers import blocks, trail, users, auth, orgs, roles
|
||||
from src.routers.courses import chapters, collections, courses,activities
|
||||
|
||||
|
||||
global_router = APIRouter(prefix="/api")
|
||||
|
||||
|
||||
# API Routes
|
||||
global_router.include_router(users.router, prefix="/users", tags=["users"])
|
||||
global_router.include_router(auth.router, prefix="/auth", tags=["auth"])
|
||||
global_router.include_router(orgs.router, prefix="/orgs", tags=["orgs"])
|
||||
global_router.include_router(roles.router, prefix="/roles", tags=["roles"])
|
||||
global_router.include_router(blocks.router, prefix="/blocks", tags=["blocks"])
|
||||
global_router.include_router(courses.router, prefix="/courses", tags=["courses"])
|
||||
global_router.include_router(chapters.router, prefix="/chapters", tags=["chapters"])
|
||||
global_router.include_router(activities.router, prefix="/activities", tags=["activities"])
|
||||
global_router.include_router(collections.router, prefix="/collections", tags=["collections"])
|
||||
global_router.include_router(trail.router, prefix="/trail", tags=["trail"])
|
||||
|
||||
|
||||
25
src/router.py
Normal file
25
src/router.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from fastapi import APIRouter, Depends
|
||||
from src.routers import blocks, dev, trail, users, auth, orgs, roles
|
||||
from src.routers.courses import chapters, collections, courses, activities
|
||||
from src.services.dev.dev import isDevModeEnabled
|
||||
|
||||
|
||||
v1_router = APIRouter(prefix="/api/v1")
|
||||
|
||||
|
||||
# API Routes
|
||||
v1_router.include_router(users.router, prefix="/users", tags=["users"])
|
||||
v1_router.include_router(auth.router, prefix="/auth", tags=["auth"])
|
||||
v1_router.include_router(orgs.router, prefix="/orgs", tags=["orgs"])
|
||||
v1_router.include_router(roles.router, prefix="/roles", tags=["roles"])
|
||||
v1_router.include_router(blocks.router, prefix="/blocks", tags=["blocks"])
|
||||
v1_router.include_router(courses.router, prefix="/courses", tags=["courses"])
|
||||
v1_router.include_router(chapters.router, prefix="/chapters", tags=["chapters"])
|
||||
v1_router.include_router(activities.router, prefix="/activities", tags=["activities"])
|
||||
v1_router.include_router( collections.router, prefix="/collections", tags=["collections"])
|
||||
v1_router.include_router(trail.router, prefix="/trail", tags=["trail"])
|
||||
|
||||
# Dev Routes
|
||||
v1_router.include_router(
|
||||
dev.router, prefix="/dev", tags=["dev"], dependencies=[Depends(isDevModeEnabled)]
|
||||
)
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
from fastapi import Depends, APIRouter, HTTPException, Response, status, Request
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from config.config import get_learnhouse_config
|
||||
from src.security.auth import AuthJWT, authenticate_user
|
||||
from src.services.users.users import PublicUser
|
||||
|
||||
|
|
@ -41,7 +42,7 @@ async def login(
|
|||
refresh_token = Authorize.create_refresh_token(subject=form_data.username)
|
||||
Authorize.set_refresh_cookies(refresh_token)
|
||||
# set cookies using fastapi
|
||||
response.set_cookie(key="access_token_cookie", value=access_token, httponly=False)
|
||||
response.set_cookie(key="access_token_cookie", value=access_token, httponly=False, domain=get_learnhouse_config().hosting_config.cookie_config.domain)
|
||||
user = PublicUser(**user.dict())
|
||||
|
||||
result = {
|
||||
|
|
|
|||
18
src/routers/dev.py
Normal file
18
src/routers/dev.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
from fastapi import APIRouter, Request
|
||||
from config.config import get_learnhouse_config
|
||||
from src.services.dev.mocks.initial import create_initial_data
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/config")
|
||||
async def config():
|
||||
config = get_learnhouse_config()
|
||||
return config.dict()
|
||||
|
||||
|
||||
@router.get("/mock/initial")
|
||||
async def initial_data(request: Request):
|
||||
await create_initial_data(request)
|
||||
return {"Message": "Initial data created 🤖"}
|
||||
|
|
@ -4,6 +4,7 @@ from fastapi import Depends, HTTPException, Request, status
|
|||
from fastapi.security import OAuth2PasswordBearer
|
||||
from jose import JWTError, jwt
|
||||
from datetime import datetime, timedelta
|
||||
from src.services.dev.dev import isDevModeEnabled
|
||||
from src.services.users.schemas.users import AnonymousUser, PublicUser
|
||||
from src.services.users.users import security_get_user, security_verify_password
|
||||
from src.security.security import ALGORITHM, SECRET_KEY
|
||||
|
|
@ -14,10 +15,10 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/login")
|
|||
|
||||
#### JWT Auth ####################################################
|
||||
class Settings(BaseModel):
|
||||
authjwt_secret_key: str = "secret"
|
||||
authjwt_secret_key: str = "secret" if isDevModeEnabled() else SECRET_KEY
|
||||
authjwt_token_location = {"cookies", "headers"}
|
||||
authjwt_cookie_csrf_protect = False
|
||||
authjwt_access_token_expires = False # (pre-alpha only) # TODO: set to 1 hour
|
||||
authjwt_access_token_expires = False if isDevModeEnabled() else 3600
|
||||
authjwt_cookie_samesite = "lax"
|
||||
authjwt_cookie_secure = True
|
||||
authjwt_cookie_domain = get_learnhouse_config().hosting_config.cookie_config.domain
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from fastapi import HTTPException, status, Request
|
||||
from passlib.context import CryptContext
|
||||
from passlib.hash import pbkdf2_sha256
|
||||
from config.config import get_learnhouse_config
|
||||
from src.services.roles.schemas.roles import RoleInDB
|
||||
|
||||
from src.services.users.schemas.users import UserInDB, UserRolesInOrganization
|
||||
|
|
@ -10,7 +11,7 @@ from src.services.users.schemas.users import UserInDB, UserRolesInOrganization
|
|||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
||||
SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
|
||||
SECRET_KEY = get_learnhouse_config().security_config.auth_jwt_secret_key
|
||||
ALGORITHM = "HS256"
|
||||
|
||||
### 🔒 JWT ##############################################################
|
||||
|
|
@ -38,7 +39,7 @@ async def verify_user_rights_with_roles(
|
|||
"""
|
||||
Check if the user has the right to perform the action on the element
|
||||
"""
|
||||
roles = request.app.db["roles"]
|
||||
request.app.db["roles"]
|
||||
users = request.app.db["users"]
|
||||
|
||||
user = await users.find_one({"user_id": user_id})
|
||||
|
|
@ -75,7 +76,9 @@ async def verify_user_rights_with_roles(
|
|||
user_roles = user.roles
|
||||
|
||||
if action != "create":
|
||||
return await check_user_role_org_with_element_org(request, element_id, user_roles, action)
|
||||
return await check_user_role_org_with_element_org(
|
||||
request, element_id, user_roles, action
|
||||
)
|
||||
|
||||
# If no role is found, raise an error
|
||||
raise HTTPException(
|
||||
|
|
@ -126,7 +129,6 @@ async def check_user_role_org_with_element_org(
|
|||
|
||||
element_org = await element.find_one({element_type_id: element_id})
|
||||
|
||||
|
||||
for role in roles_list:
|
||||
# Check if The role belongs to the same organization as the element
|
||||
role_db = await roles.find_one({"role_id": role.role_id})
|
||||
|
|
|
|||
0
src/services/dev/__init__.py
Normal file
0
src/services/dev/__init__.py
Normal file
14
src/services/dev/dev.py
Normal file
14
src/services/dev/dev.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from fastapi import HTTPException
|
||||
from config.config import get_learnhouse_config
|
||||
|
||||
|
||||
def isDevModeEnabled():
|
||||
config = get_learnhouse_config()
|
||||
if config.general_config.development_mode:
|
||||
return True
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="Development mode is not enabled",
|
||||
)
|
||||
|
||||
0
src/services/dev/mocks/__init__.py
Normal file
0
src/services/dev/mocks/__init__.py
Normal file
|
|
@ -124,7 +124,7 @@ async def create_initial_data(request: Request):
|
|||
await database_orgs.delete_many({})
|
||||
|
||||
organizations = []
|
||||
for i in range(0, 5):
|
||||
for i in range(0, 2):
|
||||
company = fake.company()
|
||||
# remove whitespace and special characters and make lowercase
|
||||
slug = ''.join(e for e in company if e.isalnum()).lower()
|
||||
Loading…
Add table
Add a link
Reference in a new issue