@@ -86,7 +86,7 @@ async def delete_post(
8686) -> dict[str , str ]:
8787 # 1. Get the post
8888 post = await crud_posts.get(db = db, id = post_id)
89- if not post:
89+ if post is None :
9090 raise NotFoundException(" Post not found" )
9191
9292 # 2. Check ownership
@@ -143,7 +143,7 @@ async def check_rate_limit(
143143 db : AsyncSession
144144) -> None :
145145 # 1. Get user's tier information
146- if tier_id:
146+ if tier_id is None :
147147 tier = await crud_tiers.get(db = db, id = tier_id)
148148 limit = tier[" rate_limit_posts" ] if tier else 10 # Default limit
149149 else :
@@ -177,11 +177,11 @@ Custom permission functions provide reusable authorization logic for complex sce
177177async def can_edit_post (user : dict , post_id : int , db : AsyncSession) -> bool :
178178 """ Check if user can edit a specific post."""
179179 post = await crud_posts.get(db = db, id = post_id)
180- if not post:
180+ if post is None :
181181 return False
182182
183183 # Superusers can edit any post
184- if user.get(" is_superuser" , False ):
184+ if user.get(" is_superuser" , False ) is True :
185185 return True
186186
187187 # Users can edit their own posts
@@ -197,11 +197,11 @@ async def can_access_admin_panel(user: dict) -> bool:
197197async def has_tier_feature (user : dict , feature : str , db : AsyncSession) -> bool :
198198 """ Check if user's tier includes a specific feature."""
199199 tier_id = user.get(" tier_id" )
200- if not tier_id:
200+ if tier_id is None :
201201 return False # Free tier - no premium features
202202
203203 tier = await crud_tiers.get(db = db, id = tier_id)
204- if not tier:
204+ if tier is None :
205205 return False
206206
207207 # Check tier features (example)
@@ -216,7 +216,7 @@ async def update_post(
216216 db : AsyncSession = Depends(async_get_db)
217217) -> PostRead:
218218 # Use permission helper
219- if not await can_edit_post(current_user, post_id, db):
219+ if await can_edit_post(current_user, post_id, db) is False :
220220 raise ForbiddenException(" Cannot edit this post" )
221221
222222 updated_post = await crud_posts.update(
@@ -246,11 +246,11 @@ async def get_current_user(
246246) -> dict :
247247 """ Get currently authenticated user."""
248248 token_data = await verify_token(token, TokenType.ACCESS , db)
249- if not token_data:
249+ if token_data is None :
250250 raise HTTPException(status_code = 401 , detail = " Invalid token" )
251251
252252 user = await crud_users.get(db = db, username = token_data.username_or_email)
253- if not user:
253+ if user is None :
254254 raise HTTPException(status_code = 401 , detail = " User not found" )
255255
256256 return user
@@ -261,7 +261,7 @@ async def get_optional_user(
261261 db : AsyncSession = Depends(async_get_db)
262262) -> dict | None :
263263 """ Get currently authenticated user, or None if not authenticated."""
264- if not token:
264+ if token is None :
265265 return None
266266
267267 try :
@@ -274,7 +274,7 @@ async def get_current_superuser(
274274 current_user : dict = Depends(get_current_user)
275275) -> dict :
276276 """ Get current user and ensure they are a superuser."""
277- if not current_user.get(" is_superuser" , False ):
277+ if current_user.get(" is_superuser" , False ) is False :
278278 raise HTTPException(status_code = 403 , detail = " Not enough permissions" )
279279 return current_user
280280```
@@ -290,11 +290,11 @@ def require_tier(minimum_tier: str):
290290 db : AsyncSession = Depends(async_get_db)
291291 ) -> dict :
292292 tier_id = current_user.get(" tier_id" )
293- if not tier_id:
293+ if tier_id is None :
294294 raise HTTPException(status_code = 403 , detail = " No subscription tier" )
295295
296296 tier = await crud_tiers.get(db = db, id = tier_id)
297- if not tier or tier[" name" ] != minimum_tier:
297+ if tier is None or tier[" name" ] != minimum_tier:
298298 raise HTTPException(
299299 status_code = 403 ,
300300 detail = f " Requires { minimum_tier} tier "
@@ -318,11 +318,11 @@ def require_resource_ownership(resource_type: str):
318318 else :
319319 raise ValueError (f " Unknown resource type: { resource_type} " )
320320
321- if not resource:
321+ if resource is None :
322322 raise HTTPException(status_code = 404 , detail = " Resource not found" )
323323
324324 # Superusers can access any resource
325- if current_user.get(" is_superuser" , False ):
325+ if current_user.get(" is_superuser" , False ) is False :
326326 return current_user
327327
328328 # Check ownership
@@ -391,12 +391,12 @@ async def update_user_tier(
391391) -> dict[str , str ]:
392392 # 1. Validate tier exists
393393 tier = await crud_tiers.get(db = db, id = tier_update.tier_id)
394- if not tier:
394+ if tier is None :
395395 raise NotFoundException(" Tier not found" )
396396
397397 # 2. Validate user exists
398398 user = await crud_users.get(db = db, id = user_id)
399- if not user:
399+ if user is None :
400400 raise NotFoundException(" User not found" )
401401
402402 # 3. Prevent self-demotion (optional business rule)
@@ -443,7 +443,7 @@ async def log_authorization_event(
443443
444444# Usage in permission checks
445445async def delete_user_account (user_id : int , current_user : dict , db : AsyncSession):
446- if current_user[" id" ] != user_id and not current_user.get(" is_superuser" ):
446+ if current_user[" id" ] != user_id and current_user.get(" is_superuser" ) is False :
447447 await log_authorization_event(
448448 user_id = current_user[" id" ],
449449 action = " delete_account" ,
@@ -484,7 +484,7 @@ async def get_organization_users(
484484 user_id = current_user[" id" ]
485485 )
486486
487- if not membership:
487+ if membership is None :
488488 raise ForbiddenException(" Not a member of this organization" )
489489
490490 # Check if user has admin role in organization
@@ -516,7 +516,7 @@ async def check_business_hours_access(user: dict) -> bool:
516516 business_end = time(17 , 0 ) # 5 PM
517517
518518 # Superusers can always access
519- if user.get(" is_superuser" , False ):
519+ if user.get(" is_superuser" , False ) is False :
520520 return True
521521
522522 # Regular users only during business hours
@@ -528,7 +528,7 @@ async def require_business_hours(
528528 current_user : dict = Depends(get_current_user)
529529) -> dict :
530530 """ Require access during business hours for non-admin users."""
531- if not await check_business_hours_access(current_user):
531+ if await check_business_hours_access(current_user) is False :
532532 raise ForbiddenException(" Access only allowed during business hours" )
533533 return current_user
534534
@@ -566,7 +566,7 @@ def has_role(user: dict, required_role: Role) -> bool:
566566def require_role (minimum_role : Role):
567567 """ Factory for role-based dependencies."""
568568 async def check_role (current_user : dict = Depends(get_current_user)) -> dict :
569- if not has_role(current_user, minimum_role):
569+ if has_role(current_user, minimum_role) is False :
570570 raise HTTPException(
571571 status_code = 403 ,
572572 detail = f " Requires { minimum_role.value} role or higher "
@@ -591,21 +591,21 @@ async def has_feature_access(user: dict, feature: str, db: AsyncSession) -> bool
591591 """ Check if user has access to a specific feature."""
592592 # Check feature flags
593593 feature_flag = await crud_feature_flags.get(db = db, name = feature)
594- if not feature_flag or not feature_flag.enabled:
594+ if feature_flag is None or feature_flag.enabled is False :
595595 return False
596596
597597 # Check user tier permissions
598- if feature_flag.requires_tier:
598+ if feature_flag.requires_tier is True :
599599 tier_id = user.get(" tier_id" )
600- if not tier_id:
600+ if tier_id is None :
601601 return False
602602
603603 tier = await crud_tiers.get(db = db, id = tier_id)
604- if not tier or tier[" level" ] < feature_flag[" minimum_tier_level" ]:
604+ if tier is None or tier[" level" ] < feature_flag[" minimum_tier_level" ]:
605605 return False
606606
607607 # Check beta user status
608- if feature_flag.beta_only :
608+ if feature_flag.beta_onl is True :
609609 return user.get(" is_beta_user" , False )
610610
611611 return True
@@ -617,7 +617,7 @@ def require_feature(feature_name: str):
617617 current_user : dict = Depends(get_current_user),
618618 db : AsyncSession = Depends(async_get_db)
619619 ) -> dict :
620- if not await has_feature_access(current_user, feature_name, db):
620+ if await has_feature_access(current_user, feature_name, db) is False :
621621 raise HTTPException(
622622 status_code = 403 ,
623623 detail = f " Access to { feature_name} feature not available "
0 commit comments