MCPcopy
hub / github.com/learnhouse/learnhouse / check_course_access

Function check_course_access

apps/api/src/services/admin/admin.py:195–228  ·  view source on GitHub ↗

Check if a user can access a specific course within the token's org.

(
    token_user: APITokenUser,
    course_uuid: str,
    user_id: int,
    db_session: AsyncSession,
)

Source from the content-addressed store, hash-verified

193
194
195async def check_course_access(
196 token_user: APITokenUser,
197 course_uuid: str,
198 user_id: int,
199 db_session: AsyncSession,
200) -> dict:
201 """Check if a user can access a specific course within the token's org."""
202
203 await _get_user_in_org(user_id, token_user.org_id, db_session)
204
205 course = (await db_session.execute(
206 select(Course).where(
207 Course.course_uuid == course_uuid,
208 Course.org_id == token_user.org_id,
209 )
210 )).scalars().first()
211 if not course:
212 raise HTTPException(status_code=404, detail="Course not found")
213
214 # Check if user has a TrailRun (enrollment) for this course
215 enrollment = (await db_session.execute(
216 select(TrailRun).where(
217 TrailRun.course_id == course.id,
218 TrailRun.user_id == user_id,
219 TrailRun.org_id == token_user.org_id,
220 )
221 )).scalars().first()
222
223 return {
224 "has_access": course.public or enrollment is not None,
225 "is_enrolled": enrollment is not None,
226 "is_public": course.public,
227 "is_published": course.published,
228 }
229
230
231# -- Enrollment endpoints -----------------------------------------------------

Callers 5

test_enrolled_userMethod · 0.90
test_course_not_foundMethod · 0.90

Calls 4

_get_user_in_orgFunction · 0.85
firstMethod · 0.45
scalarsMethod · 0.45
executeMethod · 0.45

Tested by 4

test_enrolled_userMethod · 0.72
test_course_not_foundMethod · 0.72