async def get_current_user(request: Request, response: Response, db_session: Session = Depends(get_db), Authorize: AuthJWT = Depends()): try: Authorize.jwt_required() except Exception as e: traceback.print_exc() raise token = Authorize.get_raw_jwt() # admin user admin = token.get('admin', False) if admin: user = get_admin_user_by_email(db_session, token.get('sub')) if user is None: raise CREDENTIALS_EXCEPTION return user filer = token.get('filer', False) if filer: user = get_filer_user_by_email(db_session, token.get('sub')) if user is None: raise CREDENTIALS_EXCEPTION return user raise CREDENTIALS_EXCEPTION
def authenticate_filer_user(db_session: Session, email: str, password: str): user = get_filer_user_by_email(db_session, email) if not user: return False if not verify_password(password, user.password_hash): return False return user
async def get_active_user_from_cookie(request: Request, db_session: Session = Depends(get_db), Authorize: AuthJWT = Depends()): Authorize.jwt_refresh_token_required() token = Authorize.get_raw_jwt() # admin user admin = token.get('admin', False) if admin: user = get_admin_user_by_email(db_session, token.get('sub')) if user is None or not user.active: raise CREDENTIALS_EXCEPTION return user filer = token.get('filer', False) if filer: user = get_filer_user_by_email(db_session, token.get('sub')) if user is None or not user.active: raise CREDENTIALS_EXCEPTION return user raise CREDENTIALS_EXCEPTION
async def register_new_lobbyist( db_session: Session, form: NewLobbyistRegistration, ): # do we have the lobbying entity already? res = await get_lobbying_entity_by_name(db_session, form.entity_name) if res is not None: Http400("Lobbyist name already registered.") # create the entity new_entity = await insert_lobbying_entity(db_session, form) # create general entity assoc object new_general_entity = await insert_entity(db_session, new_entity.entity_id, "lobbying") # create new human readable ID for this lobbyist new_human_readable_id = await insert_human_readable_id( db_session, object_id=str(new_entity.entity_id), object_type="lobbying_entity", hr_id_prefix="lobbyist") # check if we know the user user = get_filer_user_by_email(db_session, form.filer_email) if user is None: # we have to make the user new_user = UserBasic(email=form.filer_email, account_type="filer", city=False, active=True, first_name=form.filer_first_name, middle_name=form.filer_middle_name, last_name=form.filer_last_name) user = await insert_new_user(db_session, new_user) # now let's make a new filer new_filer = FilerBasic(user_id=user.id, first_name=form.filer_first_name, middle_name=form.filer_middle_name, last_name=form.filer_last_name) filer = await insert_new_filer_basic(db_session, new_filer) else: # get the filer filer = await get_filer_by_user_id(db_session, user.id) if filer is None: raise Http400("Filer user could not be found.") # now associate the filer with the lobbying entity lef = await assoc_filer_with_lobbying_entity(db_session, filer.filer_id, new_entity.entity_id) # email code new_entity.filer_email_code = secrets.token_hex(16) access_token_expires = datetime.timedelta( minutes=NEW_LOBBYIST_EMAIL_VERIFY_EXPIRE_MINS) data = { "sub": user.email, "filer": True, "entity_id": new_entity.entity_id, "email_code": new_entity.filer_email_code, "endpoint": FRONTEND_ROUTES["lobbyist_new_registration_verify_email"] } access_token = create_access_token( data=data, expires_delta=access_token_expires).decode('UTF-8') # send email verification email url = (APP_HOST + FRONTEND_ROUTES["lobbyist_new_registration_verify_email"] + f"?token={access_token}") await send_templated_email_disk( db_session=db_session, data={'url': url}, template_name=EMAIL_TEMPLATES[ "lobbyist_new_registration_verify_email"], message_type="lobbyist_email_confirmation", subject="[eFile San Diego] Please Confirm your Email", sender=EFILE_EMAIL, recipients=[user.email], to_id=user.id, filing_type='lobbyist', entity_id=new_entity.entity_id) # add filing type to user # save message in message db db_session.commit()
async def register_lobbyist_check_email_confirm(db_session: Session, token: str): res = check_access_token(token) if res.get('filer'): # get filer user user = get_filer_user_by_email(db_session, res['sub']) if not user: raise CREDENTIALS_EXCEPTION # get entity entity = await get_lobbying_entity_by_id(db_session, res['entity_id']) entity_contact_info = await get_lobbying_entity_current_contact_info_by_id( db_session, entity.entity_id) if not entity: raise CREDENTIALS_EXCEPTION # get filer filer = await get_filer_by_user_id(db_session, user.id) filer.active = True if not entity: raise CREDENTIALS_EXCEPTION if entity.filer_email_code != res['email_code']: raise CREDENTIALS_EXCEPTION # already confirmed if entity.filer_email_confirmed: return {"success": True, "detail": "Email already confirmed."} user.email_confirmed = True entity.filer_email_confirmed = True db_session.commit() else: raise CREDENTIALS_EXCEPTION # get the lobby types of this firm lobbyist_types = ", ".join( [LOBBYIST_TYPES[x.lobbyist_type] for x in entity.lobbyist_types]) # now notify city staff data = { "lobbyist_type": lobbyist_types, "lobbyist_name": entity_contact_info.name, "filer_first_name": user.first_name, "filer_last_name": user.last_name, "filer_email": user.email } # create new task task_meta = {'entity_id': entity.entity_id, 'filer_user_id': user.id} task_ref = str(uuid.uuid4()) task_link = (FRONTEND_ROUTES['lobbyist_new_registration_admin_review'] + f'/{entity.entity_id}?task_ref={task_ref}') task_short = f"Review new lobbyist" task_detail = f"{entity_contact_info.name}" new_task = await insert_task(db_session=db_session, task_ref=task_ref, task_type="review_new_lobbyist", admin_task=True, task_short=task_short, task_detail=task_detail, task_link=task_link, meta=task_meta) admins = get_all_active_admin_users(db_session) recipients = [x.email for x in admins] await send_templated_email_disk( db_session=db_session, data=data, template_name=EMAIL_TEMPLATES["lobbyist_new_admin_notify"], message_type="lobbyist_new_registration_verify_email", subject="[eFile San Diego] Please Review new Lobbyist", sender=EFILE_EMAIL, recipients=recipients, task_id=new_task.id, filing_type='lobbyist', entity_id=entity.entity_id) return {"success": True}