-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
76 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
Logic for restricting the use of Slack commands to specific parties | ||
""" | ||
|
||
from functools import wraps | ||
|
||
from config import SLACK_APP | ||
|
||
|
||
async def get_user_info(user_id: str): | ||
""" | ||
Queries Slack's API for information about a particular user. | ||
See https://api.slack.com/methods/users.info | ||
""" | ||
return await SLACK_APP.client.users_info(user=user_id) | ||
|
||
|
||
async def is_admin(user_id: str) -> bool: | ||
""" | ||
Gets info for the Slack user executing the command and checks if | ||
they're a workspace admin. | ||
""" | ||
user_info = await get_user_info(user_id) | ||
|
||
return user_info.get("user", {}).get("is_admin", False) | ||
|
||
|
||
def admin_required(command): | ||
""" | ||
Used to decorate Slack commands to ensure the executor is an admin | ||
before proceeding with allowing the command to run. | ||
""" | ||
|
||
@wraps(command) | ||
async def auth_wrapper(*args, **kwargs): | ||
if await is_admin(kwargs["command"]["user_id"]): | ||
return await command(*args, **kwargs) | ||
|
||
return await kwargs["ack"]( | ||
f"You must be a workspace admin in order to run `{kwargs['command']['command']}`" | ||
) | ||
|
||
return auth_wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
Location for configuration settings and app-wide constants. | ||
""" | ||
|
||
import os | ||
|
||
from fastapi import FastAPI | ||
from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler | ||
from slack_bolt.async_app import AsyncApp | ||
|
||
API = FastAPI() | ||
|
||
# configure Slack app | ||
SLACK_APP = AsyncApp( | ||
token=os.environ.get("BOT_TOKEN"), signing_secret=os.environ.get("SIGNING_SECRET") | ||
) | ||
SLACK_APP_HANDLER = AsyncSlackRequestHandler(SLACK_APP) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters