Beispiel #1
0
async def ensure_role_for_group(grp: Group):
    """
    Ensure that a Discord role exists for the given group, and returns it.
    """
    if not grp.map_to_discord:
        return
    guild = get_guild()
    current_roles = {role.name: role for role in guild.roles}
    if grp.group_name in current_roles.keys():
        role = current_roles[grp.group_name]
    else:
        role = await guild.create_role(name=grp.group_name)
        grp.discord_role_id = role.id
        grp.save()
    return role
Beispiel #2
0
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")
    ]
Beispiel #3
0
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())
Beispiel #4
0
async def update_discord_roles():
    """
    Updates discord role. A SNI group is mapped to a discord role unless that
    group's ``map_to_discord`` if ``False``
    """
    for grp in Group.objects(map_to_discord=True):
        await utils.catch_all_async(
            update_discord_role,
            f"Could not update Discord role {grp.group_name}",
            args=[grp],
        )
Beispiel #5
0
def map_teamspeak_groups():
    """
    Creates all groups on Teamspeak.
    """
    connection = new_teamspeak_connection()
    for grp in Group.objects(map_to_teamspeak=True):
        logging.debug("Mapping group %s to Teamspeak", grp.group_name)
        tsgrp = ensure_group(connection, grp.group_name)
        grp.teamspeak_sgid = tsgrp.sgid
        grp.save()
    close_teamspeak_connection(connection)
Beispiel #6
0
def update_teamspeak_groups():
    """
    Updates group memberships on Teamspeak
    """
    connection = new_teamspeak_connection()
    for grp in Group.objects(map_to_teamspeak=True, teamspeak_sgid__ne=None):
        logging.debug("Updating Teamspeak group %s", grp.group_name)
        current_cldbids: List[int] = [
            int(raw["cldbid"]) for raw in cached_teamspeak_query(
                connection,
                TS3Connection.servergroupclientlist,
                300,
                kwargs={"sgid": grp.teamspeak_sgid},
            )
        ]
        allowed_cldbids: List[int] = [
            usr.teamspeak_cldbid for usr in grp.members
            if usr.teamspeak_cldbid is not None
        ]
        for cldbid in current_cldbids:
            if cldbid in allowed_cldbids:
                continue
            try:
                connection.servergroupdelclient(
                    cldbid=cldbid,
                    sgid=grp.teamspeak_sgid,
                )
            except TS3QueryError as error:
                logging.error(
                    "Could not remove client %d from Teamspeak group %s: %s",
                    cldbid,
                    grp.group_name,
                    str(error),
                )
        for cldbid in allowed_cldbids:
            if cldbid in current_cldbids:
                continue
            try:
                connection.servergroupaddclient(
                    sgid=grp.teamspeak_sgid,
                    cldbid=cldbid,
                )
            except TS3QueryError as error:
                logging.error(
                    "Could not add client %d to Teamspeak group %s: %s",
                    cldbid,
                    grp.group_name,
                    str(error),
                )
    close_teamspeak_connection(connection)
Beispiel #7
0
def is_authorized_to_login(usr: User) -> bool:
    """
    Tells wether a user is authorized to login or not. A user is authorized to
    login if the following conditions are satisfied:

    * its ``authorized_to_login`` field is ``True`` or if the user belongs to
      **at least one** group, corporation, alliance, or coalition whose
      ``authorized_to_login`` field is ``True``;

    * the user's ``authorized_to_login`` field is not ``False``, and none of
      the groups, corporation, alliance, or coalitions the user belongs to has
      a ``authorized_to_login`` field set ot ``False``.

    In addition, the root user is always allowed to login, regardless of the
    status of the group he's part of.
    """
    if usr.character_id == 0:
        return True

    if usr.authorized_to_login is False:
        return False

    authorized_to_login: Optional[bool] = usr.authorized_to_login

    if usr.corporation is not None:
        if usr.corporation.authorized_to_login is False:
            return False
        # Note that in Python, (None or True) == True
        authorized_to_login = (authorized_to_login
                               or usr.corporation.authorized_to_login)

    if usr.alliance is not None:
        if usr.alliance.authorized_to_login is False:
            return False
        authorized_to_login = (authorized_to_login
                               or usr.alliance.authorized_to_login)

    for coa in usr.coalitions():
        if coa.authorized_to_login is False:
            return False
        authorized_to_login = authorized_to_login or coa.authorized_to_login

    for grp in Group.objects(members=usr):
        if grp.authorized_to_login is False:
            return False
        authorized_to_login = authorized_to_login or grp.authorized_to_login

    # If authorized_to_login is still None, then the result is False
    return bool(authorized_to_login)
Beispiel #8
0
def post_groups(
        data: PostGroupIn,
        tkn: Token = Depends(from_authotization_header_nondyn),
):
    """
    Creates a group. Requires a clearance level of 9 or more.
    """
    assert_has_clearance(tkn.owner, "sni.create_group")
    grp = Group(
        description=data.description,
        members=[tkn.owner],
        group_name=data.group_name,
        owner=tkn.owner,
    ).save()
    logging.debug(
        "Created group %s (%s) owned by %s",
        data.group_name,
        str(grp.pk),
        tkn.owner.character_name,
    )
    return GetGroupOut.from_record(grp)