decorators.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from typing import Optional
  2. from fastapi import FastAPI, Header, HTTPException, Depends, Request
  3. from sqlalchemy.orm import Session
  4. from app import crud, get_db, get_page, schemas
  5. from configs.globals import g
  6. async def verify_user(request: Request, user_token: str = Header(), db: Session = Depends(get_db)):
  7. try:
  8. user = crud.verify_user_token(db, user_token)
  9. g.user_id = user.id
  10. g.user_name = user.name
  11. except Exception as e:
  12. raise HTTPException(status_code=401, detail=str(e))
  13. async def verify_super_admin(request: Request, user_token: str = Header(), db: Session = Depends(get_db)):
  14. try:
  15. user = crud.verify_user_token(db, user_token)
  16. g.user_id = user.id
  17. g.user_name = user.name
  18. except Exception as e:
  19. raise HTTPException(status_code=401, detail=str(e))
  20. if user.id != 1:
  21. raise HTTPException(status_code=402, detail='该用户不拥有此权限')
  22. async def verify_super_and_project(request: Request, user_token: str = Header(), item_token: str = Header(), db: Session = Depends(get_db)):
  23. try:
  24. user = crud.verify_user_token(db, user_token)
  25. g.user_id = user.id
  26. g.user_name = user.name
  27. except Exception as e:
  28. raise HTTPException(status_code=401, detail=str(e))
  29. if user.id != 1:
  30. raise HTTPException(status_code=402, detail='该用户不拥有此权限')
  31. try:
  32. project = crud.verify_item_token(db, item_token)
  33. g.project_id = project.id
  34. except Exception as e:
  35. raise HTTPException(status_code=403, detail=str(e))
  36. async def verify_all(request: Request, user_token: str = Header(), item_token: str = Header(), db: Session = Depends(get_db)):
  37. try:
  38. user = crud.verify_user_token(db, user_token)
  39. g.user_id = user.id
  40. g.user_name = user.name
  41. except Exception as e:
  42. raise HTTPException(status_code=401, detail=str(e))
  43. try:
  44. project = crud.verify_item_token(db, item_token)
  45. g.project_id = project.id
  46. except Exception as e:
  47. raise HTTPException(status_code=403, detail=str(e))
  48. relations = crud.get_relations_by_user(db,user.id)
  49. project_role = {r.project_id:r.role_id for r in relations}
  50. if not project.id in project_role.keys():
  51. raise HTTPException(status_code=402, detail='该用户不属于此项目')
  52. role = crud.get_role_info(db,project_role[project.id])
  53. async def verify_special(request: Request, user_token: str = Header(), item_token: str = Header(), db: Session = Depends(get_db)):
  54. try:
  55. user = crud.verify_user_token(db, user_token)
  56. g.user_id = user.id
  57. g.user_name = user.name
  58. except Exception as e:
  59. raise HTTPException(status_code=401, detail=str(e))
  60. try:
  61. project = crud.verify_item_token(db, item_token)
  62. g.project_id = project.id
  63. except Exception as e:
  64. raise HTTPException(status_code=403, detail=str(e))
  65. relations = crud.get_relations_by_user(db,user.id)
  66. project_role = {r.project_id:r.role_id for r in relations}
  67. if not project.id in project_role.keys():
  68. raise HTTPException(status_code=402, detail='该用户不属于此项目')
  69. role = crud.get_role_info(db,project_role[project.id])
  70. if not role.code in ['726a51e45b4d11edbb4809c4df301a','9ff183445b4d11ed87db29f50d093a','eef984e65b4d11ed8cc491f9c82401']:
  71. raise HTTPException(status_code=402, detail='该用户没有此接口的权限')
  72. async def verify_admin(request: Request, user_token: str = Header(), item_token: str = Header(), db: Session = Depends(get_db)):
  73. try:
  74. user = crud.verify_user_token(db, user_token)
  75. g.user_id = user.id
  76. g.user_name = user.name
  77. except Exception as e:
  78. raise HTTPException(status_code=401, detail=str(e))
  79. try:
  80. project = crud.verify_item_token(db, item_token)
  81. g.project_id = project.id
  82. except Exception as e:
  83. raise HTTPException(status_code=403, detail=str(e))
  84. relations = crud.get_relations_by_user(db,user.id)
  85. project_role = {r.project_id:r.role_id for r in relations}
  86. if not project.id in project_role.keys():
  87. raise HTTPException(status_code=402, detail='该用户不属于此项目')
  88. role = crud.get_role_info(db,project_role[project.id])
  89. if not role.code in ['726a51e45b4d11edbb4809c4df301a','9ff183445b4d11ed87db29f50d093a','026bd8bc5b4e11ed857e6b5ec5c8d6']:
  90. raise HTTPException(status_code=402, detail='该用户没有此接口的权限')