Example #1
0
def user_channel_post(channel_name, username):
    new_user_channel = None
    try:
        contact = request.args.get('contact')
        if contact is None or contact == '':
            return error_view(400, "invalid contact")

        channel = Channel.get(channel_name)
        user = User.get(username)

        if UserChannel.exists(user.username, channel.name):
            return error_view(500, "this channel is already linked")

        new_user_channel = UserChannel.new(user.username, channel.name, contact)
        new_user_channel.insert()

        template = CHANNEL_VERIFY_TEMPLATE
        template.set_format(token=new_user_channel.token, channel=channel.name)
        send(contact, channel, template)

        return user_ch_created_view(new_user_channel)

    except (MailSendingError, TelegramSendingError):
        if new_user_channel is not None:
            new_user_channel.delete()

        return error_view(500, "error sending token to this contact")
Example #2
0
def alert_all_process(dn_list):
    """
    Sent the DN list to all registered Users
    :param dn_list: the DN list to send
    :return:
    """

    data = []
    for dn in dn_list:
        data.append([
            dn['domain_name'],
            dn['last_ip_address'],
            dn['current_ip_address'],
        ])

    columns = ['Domain name', 'Last IP address', 'Current IP address']
    df = pandas.DataFrame(data=data, columns=columns)

    # fixme
    template = ALERT_LIST_TEMPLATE
    template.set_format(url_alerts="/alerts",
                        url_channels="/channels",
                        table=df.to_markdown(index=False))

    # fixme
    # users = User.list()
    users = [User.get("user")]
    for u in users:
        if u.role != UserRole.USER.value and u.role != UserRole.ADMIN.value:
            continue

        user_channels = UserChannel.list_from_username(u.username)
        for u_ch in user_channels:
            channel = Channel.get(u_ch.channel_name)
            send(u_ch.contact, channel, template)
Example #3
0
def user_channel_delete(channel_name, username):
    try:
        if channel_name == Channel.DEFAULT:
            return error_view(500, "cannot unlink default channel")

        user_channel = UserChannel.get(username, channel_name)
        user_channel.delete()
        return user_ch_deleted_view(user_channel)
    except ObjectNotFound:
        return error_view(404, "user channel not found")
Example #4
0
def users_channel_list():
    username = get_jwt_identity()

    out = []
    user_channels = UserChannel.list_from_username(username)
    for u_ch in user_channels:
        channel = Channel.get(u_ch.channel_name)
        out.append({
            'user_channel': u_ch,
            'channel': channel
        })

    return user_ch_list_view(out)
Example #5
0
def ch_delete(name):
    if name == Channel.DEFAULT:
        return error_view(403, f"cannot modify default channel")

    if not Channel.exists(name):
        return error_view(404, f"channel {name} not found")

    ch = Channel.get(name)
    user_channel_list = UserChannel.list_from_channel(ch.name)
    for user_ch in user_channel_list:
        user_ch.delete()

    ch.delete()

    return ch_admin_delete_view(ch)
Example #6
0
def channels_list():
    ch_list = Channel.list()

    username = get_jwt_identity()
    user_ch_list = UserChannel.list_from_username(username)

    out = []
    for ch in ch_list:
        found = False
        for u_ch in user_ch_list:
            if u_ch.channel_name == ch.name:
                found = True
                break
        if not found:
            out.append(ch)

    return ch_list_view(out)
Example #7
0
def remove_user():
    username = request.args.get('username')
    if username is None:
        return error_view(400, "missing username")

    user = User.get(username)
    if user.role != UserRole.ADMIN.value:
        user.delete()

        user_channels = UserChannel.list_from_username(user.username)
        for user_ch in user_channels:
            user_ch.delete()

        return user_deleted_view(user)

    else:
        return error_view(500, "cannot remove an admin user")
Example #8
0
def user_channel_put(channel_name, username):
    try:
        token = request.args.get('token')
        if token is None or token == '':
            return error_view(400, 'missing token')

        user_channel = UserChannel.get(username, channel_name)
        if user_channel.verified:
            return error_view(500, 'this channel has already been verified')

        if token != user_channel.token:
            return error_view(500, 'invalid token')

        user_channel.update(verified=True)
        return user_ch_updated_view(user_channel)

    except ObjectNotFound:
        return error_view(404, "user channel not found")
Example #9
0
def channel_test(channel_name):
    try:
        username = get_jwt_identity()
        if request.method == 'GET':
            user = User.get(username)
            channel = Channel.get(channel_name)
            user_channel = UserChannel.get(user.username, channel.name)

            template = TEST_TEMPLATE
            template.set_format(date=str(datetime.now().date()))
            send(user_channel.contact, channel, template)

            return user_ch_test_view(user_channel)

    except ObjectNotFound:
        return error_view(404, "object not found")

    except (MailSendingError, TelegramSendingError):
        return error_view(500, "failed to send test")
Example #10
0
def register():
    try:
        body = request.json
        if body is None:
            return error_view(400, "invalid JSON in body")

        username = body.get('username')
        password = body.get('password')
        token = request.args.get('token')

        if username is None or password is None or token is None:
            return error_view(400, "invalid parameters")

        if token == '' or username == '' or password == '':
            return error_view(400, "missing parameters")

        if User.exists(username):
            return error_view(500, f"user with username `{username}` already exists")

        user_pending = UserPending.get(token)
        created_user = User.new(username, password, user_pending.email)
        created_user.insert()
        user_pending.delete()

        default_channel = Channel.get(Channel.DEFAULT)
        user_channel = UserChannel.new(
            created_user.username,
            default_channel.name,
            created_user.email
        )
        user_channel.verified = True
        user_channel.insert()

        return user_created_view(created_user)

    except ObjectNotFound as o:
        return error_view(404, str(o))
Example #11
0
def user_ch_test_view(user_ch: UserChannel):
    return jsonify({
        "msg": f"user channel tested",
        "user_channel": user_ch.safe_json()
    })
Example #12
0
def user_ch_updated_view(user_ch: UserChannel):
    return jsonify({
        "msg": f"channel contact verified",
        "user_channel": user_ch.safe_json()
    }), 201
Example #13
0
def user_ch_created_view(user_ch: UserChannel):
    return jsonify({
        "msg": f"linked channel with user {user_ch.username}",
        "user_channel": user_ch.safe_json()
    }), 201
Example #14
0
def user_ch_get_view(user_ch: UserChannel):
    return jsonify({
        "msg": f"user channel retrieved",
        "user_channel": user_ch.safe_json(),
    })
Example #15
0
def user_channel_get(channel_name, username):
    try:
        user_channel = UserChannel.get(username, channel_name)
        return user_ch_get_view(user_channel)
    except ObjectNotFound:
        return error_view(404, "user channel not found")