From 150e112387140433c434729b299d55dc1f8d7e4f Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Sun, 22 Jul 2018 06:46:03 +0000 Subject: [PATCH] Implement autorole for embed users --- ...ce662e9_added_autorole_columns_to_guild.py | 30 +++++++++++++++++++ webapp/titanembeds/blueprints/admin/admin.py | 7 +++++ webapp/titanembeds/blueprints/api/api.py | 8 +++-- .../titanembeds/blueprints/gateway/gateway.py | 12 ++++---- webapp/titanembeds/blueprints/user/user.py | 7 +++++ webapp/titanembeds/database/guilds.py | 2 ++ .../static/js/administrate_guild.js | 19 ++++++++++++ .../templates/administrate_guild.html.j2 | 28 +++++++++++++++++ webapp/titanembeds/utils.py | 17 ++++++++++- 9 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 webapp/alembic/versions/12267ce662e9_added_autorole_columns_to_guild.py diff --git a/webapp/alembic/versions/12267ce662e9_added_autorole_columns_to_guild.py b/webapp/alembic/versions/12267ce662e9_added_autorole_columns_to_guild.py new file mode 100644 index 0000000..4228655 --- /dev/null +++ b/webapp/alembic/versions/12267ce662e9_added_autorole_columns_to_guild.py @@ -0,0 +1,30 @@ +"""Added autorole columns to guild + +Revision ID: 12267ce662e9 +Revises: 87d043d7917e +Create Date: 2018-07-22 05:35:22.574854 + +""" + +# revision identifiers, used by Alembic. +revision = '12267ce662e9' +down_revision = '87d043d7917e' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('guilds', sa.Column('autorole_discord', sa.BigInteger(), nullable=True)) + op.add_column('guilds', sa.Column('autorole_unauth', sa.BigInteger(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('guilds', 'autorole_unauth') + op.drop_column('guilds', 'autorole_discord') + # ### end Alembic commands ### diff --git a/webapp/titanembeds/blueprints/admin/admin.py b/webapp/titanembeds/blueprints/admin/admin.py index 86ca450..fad8d66 100644 --- a/webapp/titanembeds/blueprints/admin/admin.py +++ b/webapp/titanembeds/blueprints/admin/admin.py @@ -176,6 +176,7 @@ def administrate_guild(guild_id): dbguild_dict = { "id": guild["id"], "name": guild["name"], + "roles": guild["roles"], "unauth_users": db_guild.unauth_users, "visitor_view": db_guild.visitor_view, "webhook_messages": db_guild.webhook_messages, @@ -191,6 +192,8 @@ def administrate_guild(guild_id): "banned_words_enabled": db_guild.banned_words_enabled, "banned_words_global_included": db_guild.banned_words_global_included, "banned_words": json.loads(db_guild.banned_words), + "autorole_unauth": db_guild.autorole_unauth, + "autorole_discord": db_guild.autorole_discord } return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics) @@ -209,6 +212,8 @@ def update_administrate_guild(guild_id): db_guild.max_message_length = request.form.get("max_message_length", db_guild.max_message_length) db_guild.banned_words_enabled = request.form.get("banned_words_enabled", db_guild.banned_words_enabled) in ["true", True] db_guild.banned_words_global_included = request.form.get("banned_words_global_included", db_guild.banned_words_global_included) in ["true", True] + db_guild.autorole_unauth = request.form.get("autorole_unauth", db_guild.autorole_unauth, type=int) + db_guild.autorole_discord = request.form.get("autorole_discord", db_guild.autorole_discord, type=int) invite_link = request.form.get("invite_link", db_guild.invite_link) if invite_link != None and invite_link.strip() == "": invite_link = None @@ -244,6 +249,8 @@ def update_administrate_guild(guild_id): banned_words_enabled=db_guild.banned_words_enabled, banned_words_global_included=db_guild.banned_words_global_included, banned_words=json.loads(db_guild.banned_words), + autorole_unauth=db_guild.autorole_unauth, + autorole_discord=db_guild.autorole_discord ) @admin.route("/guilds") diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index 71f505d..e1db860 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -1,6 +1,6 @@ from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, get_administrators_list, get_badges, DiscordBotsOrgTransactions from titanembeds.decorators import valid_session_required, discord_users_only, abort_if_guild_disabled -from titanembeds.utils import check_guild_existance, guild_accepts_visitors, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key, user_unauthenticated, checkUserRevoke, checkUserBanned, update_user_status, check_user_in_guild, get_guild_channels, guild_webhooks_enabled, guild_unauthcaptcha_enabled, get_member_roles, get_online_embed_user_keys, redis_store, redisqueue +from titanembeds.utils import check_guild_existance, guild_accepts_visitors, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key, user_unauthenticated, checkUserRevoke, checkUserBanned, update_user_status, check_user_in_guild, get_guild_channels, guild_webhooks_enabled, guild_unauthcaptcha_enabled, get_member_roles, get_online_embed_user_keys, redis_store, redisqueue, get_forced_role from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild import titanembeds.constants as constants from flask import Blueprint, abort, jsonify, session, request, url_for @@ -93,7 +93,8 @@ def format_everyone_mention(channel, content): return content def filter_guild_channel(guild_id, channel_id, force_everyone=False): - channels = get_guild_channels(guild_id, force_everyone) + forced_role = get_forced_role(guild_id) + channels = get_guild_channels(guild_id, force_everyone, forced_role) for chan in channels: if chan["channel"]["id"] == channel_id: return chan @@ -426,7 +427,8 @@ def get_guild_guest_icon(guild_id): def process_query_guild(guild_id, visitor=False): widget = discord_api.get_widget(guild_id) - channels = get_guild_channels(guild_id, visitor) + forced_role = get_forced_role(guild_id) + channels = get_guild_channels(guild_id, visitor, forced_role=forced_role) if widget.get("success", True): discordmembers = get_online_discord_users(guild_id, widget) else: diff --git a/webapp/titanembeds/blueprints/gateway/gateway.py b/webapp/titanembeds/blueprints/gateway/gateway.py index 9b645b1..6c5f2c2 100644 --- a/webapp/titanembeds/blueprints/gateway/gateway.py +++ b/webapp/titanembeds/blueprints/gateway/gateway.py @@ -1,4 +1,4 @@ -from titanembeds.utils import socketio, guild_accepts_visitors, get_client_ipaddr, discord_api, check_user_in_guild, get_guild_channels, update_user_status, guild_webhooks_enabled, redis_store, redisqueue +from titanembeds.utils import socketio, guild_accepts_visitors, get_client_ipaddr, discord_api, check_user_in_guild, get_guild_channels, update_user_status, guild_webhooks_enabled, redis_store, redisqueue, get_forced_role from titanembeds.database import db from flask_socketio import Namespace, emit, disconnect, join_room, leave_room import functools @@ -21,10 +21,11 @@ class Gateway(Namespace): return session["socket_guild_id"] = guild_id channels = [] + forced_role = get_forced_role(guild_id) if guild_accepts_visitors(guild_id) and not check_user_in_guild(guild_id): - channels = get_guild_channels(guild_id, force_everyone=True) + channels = get_guild_channels(guild_id, force_everyone=True, forced_role=forced_role) else: - channels = get_guild_channels(guild_id) + channels = get_guild_channels(guild_id, forced_role=forced_role) join_room("GUILD_"+guild_id) for chan in channels: if chan["read"]: @@ -96,10 +97,11 @@ class Gateway(Namespace): guild_id = data["guild_id"] visitor_mode = data["visitor_mode"] channels = None + forced_role = get_forced_role(guild_id) if visitor_mode or session.get("unauthenticated", True): - channels = get_guild_channels(guild_id, True) + channels = get_guild_channels(guild_id, True, forced_role=forced_role) else: - channels = get_guild_channels(guild_id) + channels = get_guild_channels(guild_id, forced_role=forced_role) for chan in channels: if chan["read"]: join_room("CHANNEL_"+chan["channel"]["id"]) diff --git a/webapp/titanembeds/blueprints/user/user.py b/webapp/titanembeds/blueprints/user/user.py index f138f4e..7ead457 100644 --- a/webapp/titanembeds/blueprints/user/user.py +++ b/webapp/titanembeds/blueprints/user/user.py @@ -213,6 +213,7 @@ def administrate_guild(guild_id): dbguild_dict = { "id": db_guild.guild_id, "name": guild["name"], + "roles": guild["roles"], "unauth_users": db_guild.unauth_users, "visitor_view": db_guild.visitor_view, "webhook_messages": db_guild.webhook_messages, @@ -228,6 +229,8 @@ def administrate_guild(guild_id): "banned_words_enabled": db_guild.banned_words_enabled, "banned_words_global_included": db_guild.banned_words_global_included, "banned_words": json.loads(db_guild.banned_words), + "autorole_unauth": db_guild.autorole_unauth, + "autorole_discord": db_guild.autorole_discord } return render_template("administrate_guild.html.j2", guild=dbguild_dict, members=users, permissions=permissions, cosmetics=cosmetics, disabled=(guild_id in list_disabled_guilds())) @@ -254,6 +257,8 @@ def update_administrate_guild(guild_id): db_guild.max_message_length = request.form.get("max_message_length", db_guild.max_message_length) db_guild.banned_words_enabled = request.form.get("banned_words_enabled", db_guild.banned_words_enabled) in ["true", True] db_guild.banned_words_global_included = request.form.get("banned_words_global_included", db_guild.banned_words_global_included) in ["true", True] + db_guild.autorole_unauth = request.form.get("autorole_unauth", db_guild.autorole_unauth, type=int) + db_guild.autorole_discord = request.form.get("autorole_discord", db_guild.autorole_discord, type=int) invite_link = request.form.get("invite_link", db_guild.invite_link) if invite_link != None and invite_link.strip() == "": @@ -293,6 +298,8 @@ def update_administrate_guild(guild_id): banned_words_enabled=db_guild.banned_words_enabled, banned_words_global_included=db_guild.banned_words_global_included, banned_words=json.loads(db_guild.banned_words), + autorole_unauth=db_guild.autorole_unauth, + autorole_discord=db_guild.autorole_discord ) @user.route("/add-bot/") diff --git a/webapp/titanembeds/database/guilds.py b/webapp/titanembeds/database/guilds.py index e17726d..132db23 100644 --- a/webapp/titanembeds/database/guilds.py +++ b/webapp/titanembeds/database/guilds.py @@ -17,6 +17,8 @@ class Guilds(db.Model): banned_words_enabled = db.Column(db.Boolean(), nullable=False, server_default="0") # If banned words are enforced banned_words_global_included = db.Column(db.Boolean(), nullable=False, server_default="0") # Add global banned words to the list banned_words = db.Column(db.Text(), nullable=False, server_default="[]") # JSON list of strings to block from sending + autorole_unauth = db.Column(db.BigInteger, nullable=True, server_default=None) # Automatic Role inherit for unauthenticated users + autorole_discord = db.Column(db.BigInteger, nullable=True, server_default=None) # Automatic Role inherit for discord users def __init__(self, guild_id): self.guild_id = guild_id diff --git a/webapp/titanembeds/static/js/administrate_guild.js b/webapp/titanembeds/static/js/administrate_guild.js index fba3c30..fb5c5f8 100644 --- a/webapp/titanembeds/static/js/administrate_guild.js +++ b/webapp/titanembeds/static/js/administrate_guild.js @@ -1,4 +1,5 @@ $('.chips').material_chip(); +$('select').material_select(); $('#unauth_users').change(function() { var pathname = window.location.pathname; @@ -154,6 +155,24 @@ function add_delete_banned_words(action, word) { }); } +$("#autorole_unauth").change(function () { + var pathname = window.location.pathname; + var value = $(this).val(); + var payload = {"autorole_unauth": value} + $.post(pathname, payload, function(data) { + Materialize.toast('Updated Guest AutoRole setting!', 2000) + }); +}); + +$("#autorole_discord").change(function () { + var pathname = window.location.pathname; + var value = $(this).val(); + var payload = {"autorole_discord": value} + $.post(pathname, payload, function(data) { + Materialize.toast('Updated Discord AutoRole setting!', 2000) + }); +}); + function initiate_ban(guild_id, user_id) { var reason = prompt("Please enter your reason for ban"); var payload = { diff --git a/webapp/titanembeds/templates/administrate_guild.html.j2 b/webapp/titanembeds/templates/administrate_guild.html.j2 index 7912d0b..b1628c8 100644 --- a/webapp/titanembeds/templates/administrate_guild.html.j2 +++ b/webapp/titanembeds/templates/administrate_guild.html.j2 @@ -186,6 +186,34 @@

(Hit enter after each word to add to list)

+ +

AutoRole Embed Users

+

A "hidden" role may be applied and inherited to users using the embed. Enabling this will allow the user to have different permission on the embed than in Discord. May be used so that the embed users see different channels for example.

+
+
+ + +
+
+ + +
+
+ diff --git a/webapp/titanembeds/utils.py b/webapp/titanembeds/utils.py index eaab342..520b0c4 100644 --- a/webapp/titanembeds/utils.py +++ b/webapp/titanembeds/utils.py @@ -200,13 +200,15 @@ def get_member_roles(guild_id, user_id): role_converted.append(str(role)) return role_converted -def get_guild_channels(guild_id, force_everyone=False): +def get_guild_channels(guild_id, force_everyone=False, forced_role=0): if user_unauthenticated() or force_everyone: member_roles = [guild_id] #equivilant to @everyone role else: member_roles = get_member_roles(guild_id, session['user_id']) if guild_id not in member_roles: member_roles.append(guild_id) + if forced_role: + member_roles.append(str(forced_role)) bot_member_roles = get_member_roles(guild_id, config["client-id"]) if guild_id not in bot_member_roles: bot_member_roles.append(guild_id) @@ -239,6 +241,11 @@ def get_channel_permission(channel, guild_id, guild_owner, guild_roles, member_r return result channel_perm = 0 + role_positions = {} + for role in guild_roles: + role_positions[str(role["id"])] = role["position"] + member_roles = sorted(member_roles, key=lambda x: role_positions.get(str(x), -1), reverse=True) + # @everyone for role in guild_roles: if role["id"] == guild_id: @@ -291,6 +298,14 @@ def get_channel_permission(channel, guild_id, guild_owner, guild_roles, member_r result["mention_everyone"] = False return result +def get_forced_role(guild_id): + dbguild = db.session.query(Guilds).filter(Guilds.guild_id == guild_id).first() + if not session.get("unauthenticated", True): + forced_role = dbguild.autorole_discord + else: + forced_role = dbguild.autorole_unauth + return forced_role + def bot_can_create_webhooks(guild): perm = 0 guild_roles = guild["roles"]