def put_user_name( character_id: int, data: PutUserIn, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Manually updates non ESI data about a character. The required clearance level depends on the modification. """ usr: User = User.objects.get(character_id=character_id) if data.clearance_level is not None: if not 0 <= data.clearance_level <= 10: raise HTTPException( status.HTTP_422_UNPROCESSABLE_ENTITY, detail= "Field clearance_level must be between 0 and 10 (inclusive).", ) scope_name = f"sni.set_clearance_level_{data.clearance_level}" assert_has_clearance(tkn.owner, scope_name, usr) usr.clearance_level = data.clearance_level if data.authorized_to_login is not None: assert_has_clearance(tkn.owner, "sni.set_authorized_to_login") usr.authorized_to_login = data.authorized_to_login usr.save() return GetUserOut.from_record(usr)
async def get_token(tkn: Token = Depends(from_authotization_header_nondyn)): """ Returns informations about the token currently being used. Requires a clearance level of 0 or more. """ assert_has_clearance(tkn.owner, "sni.read_own_token") return GetTokenOut.from_record(tkn)
def delete_user(character_id: int, tkn: Token = Depends(from_authotization_header_nondyn)): """ Deletes a user. Requires a clearance level of 9 or more. """ assert_has_clearance(tkn.owner, "sni.delete_user") usr: User = User.objects.get(character_id=character_id) usr.delete()
def get_user_name(character_id: int, tkn: Token = Depends(from_authotization_header_nondyn)): """ Returns details about a character. Requires a clearance level of 0 or more. """ assert_has_clearance(tkn.owner, "sni.read_user") usr = User.objects.get(character_id=character_id) return GetUserOut.from_record(usr)
def get_configuration( tkn: Token = Depends(from_authotization_header_nondyn), ): """ Gets the configuration of the SNI instance. Secrets are redacted. Requires a clearance of 10. """ assert_has_clearance(tkn.owner, "sni.system.read_configuration") return CONFIGURATION
def put_coalition( coalition_id: BSONObjectId, data: PutCoalitionIn, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Updates a coalition. All fields in the request body are optional. The `add_member_alliances` and `remove_member_alliances` fields can be used together, but the `member_alliances` cannot be used in conjunction with `add_member_alliances` and `remove_member_alliances`. Similarly for `add_member_corporations`, `remove_member_corporations`, and `member_corporations`. Requires a clearance level of 6 or more. """ assert_has_clearance(tkn.owner, "sni.update_coalition") coalition: Coalition = Coalition.objects.get(pk=coalition_id) logging.debug("Updating coalition %s (%s)", coalition.coalition_name, coalition_id) if data.add_member_alliances is not None: coalition.member_alliances += [ Alliance.objects.get(alliance_id=member_id) for member_id in set(data.add_member_alliances) ] if data.add_member_corporations is not None: coalition.member_corporations += [ Corporation.objects.get(corporation_id=member_id) for member_id in set(data.add_member_corporations) ] if data.authorized_to_login is not None: assert_has_clearance(tkn.owner, "sni.set_authorized_to_login") coalition.authorized_to_login = data.authorized_to_login if data.mandatory_esi_scopes is not None: coalition.mandatory_esi_scopes = data.mandatory_esi_scopes if data.member_alliances is not None: coalition.member_alliances = [ Alliance.objects.get(alliance_id=member_id) for member_id in set(data.member_alliances) ] if data.member_corporations is not None: coalition.member_corporations = [ Corporation.objects.get(corporation_id=member_id) for member_id in set(data.member_corporations) ] if data.remove_member_alliances is not None: coalition.member_alliances = [ member for member in coalition.member_alliances if member.alliance_id not in data.remove_member_alliances ] if data.remove_member_corporations is not None: coalition.member_corporations = [ member for member in coalition.member_corporations if member.corporation_id not in data.remove_member_corporations ] if data.ticker is not None: coalition.ticker = data.ticker coalition.member_corporations = list(set(coalition.member_corporations)) coalition.member_alliances = list(set(coalition.member_alliances)) coalition.save() return GetCoalitionOut.from_record(coalition)
def get_coalition(tkn: Token = Depends(from_authotization_header_nondyn)): """ Lists all the coalition names. Requires a clearance level of 0 or more. """ assert_has_clearance(tkn.owner, "sni.read_coalition") return [ GetCoalitionShortOut.from_record(coalition) for coalition in Coalition.objects().order_by("coalition_name") ]
def delete_crash_report( crash_report_id: BSONObjectId, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Deletes a crash report. Requires a clearance level of 10. """ assert_has_clearance(tkn.owner, "sni.delete_crash_report") CrashReport.objects.get(pk=crash_report_id).delete()
def get_group(tkn: Token = Depends(from_authotization_header_nondyn), ): """ Lists all the group names. Requires a clearance level of 0 or more. """ assert_has_clearance(tkn.owner, "sni.read_group") return [ GetGroupShortOut(group_id=str(grp.pk), group_name=grp.group_name) for grp in Group.objects().order_by("group_name") ]
def get_crash_reports(tkn: Token = Depends(from_authotization_header_nondyn)): """ Get the list of the 50 most recent crash reports, sorted from most to least recent. Requires a clearance level of 10. """ assert_has_clearance(tkn.owner, "sni.read_crash_report") return [ GetCrashReportShortOut.from_record(crash) for crash in CrashReport.objects().order_by("-timestamp")[:50] ]
def get_alliance( alliance_id: int, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Gets data about an alliance """ assert_has_clearance(tkn.owner, "sni.read_alliance") alliance = Alliance.objects(alliance_id=alliance_id).get() return GetAllianceOut.from_record(alliance)
def get_group_name( group_id: BSONObjectId, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Returns details about a given group. Requires a clearance level of 0 or more. """ assert_has_clearance(tkn.owner, "sni.read_group") return GetGroupOut.from_record(Group.objects(pk=group_id).get())
def get_alliances(tkn: Token = Depends(from_authotization_header_nondyn), ): """ Gets the list of alliances registered in this instance. Requires a clearance level of 0 or more. """ assert_has_clearance(tkn.owner, "sni.read_alliance") return [ GetAllianceShortOut.from_record(alliance) for alliance in Alliance.objects().order_by("alliance_name") ]
def get_user(tkn: Token = Depends(from_authotization_header_nondyn)): """ Returns the list of all user names. Requires a clearance level of 0 or more. """ assert_has_clearance(tkn.owner, "sni.read_user") return [ GetUserShortOut.from_record(usr) for usr in User.objects( clearance_level__gte=0).order_by("character_name") ]
def delete_group( group_id: BSONObjectId, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Deletes a group. Requires a clearance level of 9 or more. """ assert_has_clearance(tkn.owner, "sni.delete_group") grp: Group = Group.objects.get(pk=group_id) logging.debug("Deleting group %s (%s)", grp.group_name, group_id) grp.delete()
def post_corporation( corporation_id: int, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Manually fetches a corporation from the ESI. Requires a clearance level of 8 or more. """ assert_has_clearance(tkn.owner, "sni.fetch_corporation") corporation = ensure_corporation(corporation_id) return GetCorporationOut.from_record(corporation)
def get_crash_report( crash_report_id: BSONObjectId, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Gets a crash report from its id. Requires a clearance level of 10. """ assert_has_clearance(tkn.owner, "sni.read_crash_report") return GetCrashReportOut.from_record( CrashReport.objects.get(pk=crash_report_id) )
def get_coalition_name( coalition_id: BSONObjectId, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Returns details about a given coalition. Requires a clearance level of 0 or more. """ assert_has_clearance(tkn.owner, "sni.read_coalition") return GetCoalitionOut.from_record( Coalition.objects(pk=coalition_id).get())
def get_corporation( corporation_id: int, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Get informations about a corporation. Note that this corporation must be registered on SNI """ assert_has_clearance(tkn.owner, "sni.read_corporation") corporation = Corporation.objects(corporation_id=corporation_id).get() return GetCorporationOut.from_record(corporation)
def get_coalition_tracking( coalition_id: BSONObjectId, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Reports which member (of a given coalition) have a valid refresh token attacked to them, and which do not. Requires a clearance level of 5 or more. """ coalition: Coalition = Coalition.objects(pk=coalition_id).get() assert_has_clearance(tkn.owner, "sni.track_coalition") return GetTrackingOut.from_user_iterator(coalition.user_iterator())
def get_corporations(tkn: Token = Depends(from_authotization_header_nondyn),): """ Gets the list of corporations registered in this instance. Requires a clearance level of 0 or more. """ assert_has_clearance(tkn.owner, "sni.read_corporation") return [ GetCorporationShortOut.from_record(corporation) for corporation in Corporation.objects( corporation_id__gte=2000000 ).order_by("corporation_name") ]
def delete_coalition( coalition_id: BSONObjectId, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Deletes a coalition. Requires a clearance level of 9 or more. """ assert_has_clearance(tkn.owner, "sni.delete_coalition") coalition: Coalition = Coalition.objects.get(pk=coalition_id) logging.debug("Deleting coalition %s (%s)", coalition.coalition_name, coalition_id) coalition.delete()
def get_alliance_tracking( alliance_id: int, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Reports which member (of a given alliance) have a valid refresh token attacked to them, and which do not. Requires a clearance level of 3 and having authority over this alliance. """ alliance: Alliance = Alliance.objects(alliance_id=alliance_id).get() assert_has_clearance(tkn.owner, "sni.track_alliance", alliance.ceo) return GetTrackingOut.from_user_iterator(alliance.user_iterator())
def post_alliance( alliance_id: int, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Manually fetches an alliance from the ESI. Requires a clearance level of 8 or more. """ assert_has_clearance(tkn.owner, "sni.fetch_alliance") alliance = ensure_alliance(alliance_id) scheduler.add_job(ensure_alliance_members, args=(alliance, )) return GetAllianceOut.from_record(alliance)
def port_auth_start(tkn: Token = Depends(from_authotization_header_nondyn)): """ Starts a new authentication challenge for the owner of the token. A random nickname is returned (see `PostAuthStartOut` for more details), and the user has 1 minute to update its teamspeak nickname to it, and then call `POST /teamspeak/auth/complete`. """ assert_has_clearance(tkn.owner, "sni.teamspeak.auth") return PostAuthStartOut( expiration_datetime=utils.now_plus(minutes=2), challenge_nickname=new_authentication_challenge(tkn.owner), user=GetUserShortOut.from_record(tkn.owner), )
def port_auth_start(tkn: Token = Depends(from_authotization_header_nondyn)): """ Starts a new authentication challenge for the owner of the token. A random code is returned (see `PostAuthStartOut` for more details), and the user has 1 minute to post it as `!auth <code>` in the dedicated authentication chanel. """ assert_has_clearance(tkn.owner, "sni.discord.auth") return PostAuthStartOut( expiration_datetime=utils.now_plus(seconds=60), code=new_authentication_challenge(tkn.owner), user=GetUserShortOut.from_record(tkn.owner), )
def get_corporation_tracking( corporation_id: int, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Reports which member (of a given corporation) have a valid refresh token attacked to them, and which do not. Requires a clearance level of 1 and having authority over this corporation. """ corporation: Corporation = Corporation.objects( corporation_id=corporation_id ).get() assert_has_clearance(tkn.owner, "sni.track_corporation", corporation.ceo) return GetTrackingOut.from_user_iterator(corporation.user_iterator())
def post_job( callable_name: str, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Submits a job to the scheduler. Requires a clearance level of 10. """ assert_has_clearance(tkn.owner, "sni.system.submit_job") try: function = object_from_name(callable_name) job = scheduler.add_job(function) return GetJobOut.from_job(job) except AttributeError: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
def post_coalitions( data: PostCoalitionIn, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Creates a coalition. Requires a clearance level of 9 or more. """ assert_has_clearance(tkn.owner, "sni.create_coalition") coa = Coalition( coalition_name=data.coalition_name, ticker=data.ticker, ).save() logging.debug("Created coalition %s (%s)", data.coalition_name, str(coa.pk)) return GetCoalitionOut.from_record(coa)
def put_alliance( alliance_id: int, data: PutAllianceIn, tkn: Token = Depends(from_authotization_header_nondyn), ): """ Modify an alliance registered on SNI. Note that it does not modify it on an ESI level. Requires a clearance level of 4 or more. """ alliance: Alliance = Alliance.objects(alliance_id=alliance_id).get() assert_has_clearance(tkn.owner, "sni.update_alliance", alliance.ceo) alliance.authorized_to_login = data.authorized_to_login if data.mandatory_esi_scopes is not None: alliance.mandatory_esi_scopes = data.mandatory_esi_scopes alliance.save() return GetAllianceOut.from_record(alliance)