Skip to content

Instantly share code, notes, and snippets.

@timcole
Created August 29, 2021 13:16
Show Gist options
  • Save timcole/4f0eaac1dd352e47a713af27a762e11c to your computer and use it in GitHub Desktop.
Save timcole/4f0eaac1dd352e47a713af27a762e11c to your computer and use it in GitHub Desktop.
Example for calculating hiven permissions
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Hiven-Perms.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "1Rt55EfpxybV"
},
"source": [
"Permissions in Hiven are handled using bitwise operations.\n",
"\n",
"The total permissions integer can be determined by OR'ing together each individual value, and flags can be checked using AND operations."
]
},
{
"cell_type": "code",
"metadata": {
"id": "-TtYfxvnu8x_"
},
"source": [
"import enum\n",
"\n",
"class Permissions(enum.Enum):\n",
" SEND_MESSAGES = 1 << 0\n",
" READ_MESSAGES = 1 << 1\n",
" ADMINISTRATOR = 1 << 2\n",
" MODERATE_ROOM = 1 << 3\n",
" EVICT_MEMBERS = 1 << 4\n",
" KICK_MEMBERS = 1 << 5\n",
" ATTACH_MEDIA = 1 << 6\n",
" MANAGE_ROLES = 1 << 7\n",
" MANAGE_BILLING = 1 << 8\n",
" MANAGE_BOTS = 1 << 9\n",
" MANAGE_INTEGRATIONS = 1 << 10\n",
" MANAGE_ROOMS = 1 << 11\n",
" START_PORTAL = 1 << 12\n",
" STREAM_TO_PORTAL = 1 << 13\n",
" TAKEOVER_PORTAL = 1 << 14\n",
" POST_TO_FEED = 1 << 15\n",
" MANAGE_USER_OVERRIDES = 1 << 16\n",
" CREATE_INVITES = 1 << 17\n",
" MANAGE_INVITES = 1 << 18\n",
" HERE_MENTIONS = 1 << 19"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "tB9y8BsZ3vxb"
},
"source": [
"These are just some example values pulled from swarm websockets."
]
},
{
"cell_type": "code",
"metadata": {
"id": "yu8CFhQfxLCm"
},
"source": [
"# d.default_permissions from HOUSE_JOIN\n",
"houses_default_permissions = 64\n",
"# HOUSE_MEMBER_ENTER or HOUSE_MEMBER_UPDATE\n",
"members_roles = [\"283943632086497556\"]\n",
"# house roles and room come from HOUSE_JOIN\n",
"house_roles = [\n",
" {\n",
" \"position\": 0,\n",
" \"name\": \"Launch Director\",\n",
" \"level\": 0,\n",
" \"id\": \"273379689253302315\",\n",
" \"deny\": 0,\n",
" \"color\": \"#ff4d4d\",\n",
" \"allow\": 247\n",
" },\n",
" {\n",
" \"position\": 0,\n",
" \"name\": \"test role\",\n",
" \"level\": 0,\n",
" \"id\": \"283943632086497556\",\n",
" \"house_id\": \"273341283148230910\",\n",
" \"deny\": 32,\n",
" \"color\": \"#9900ef\",\n",
" \"allow\": 0\n",
" }\n",
"]\n",
"house_room = {\n",
" \"type\": 0,\n",
" \"recipients\": None, \n",
" \"position\": 0,\n",
" \"permission_overrides\": {\n",
" \"283943632086497556\": {\n",
" \"type\": 0,\n",
" \"id\": \"283943632086497556\",\n",
" \"deny\": 1,\n",
" \"allow\": 0\n",
" }\n",
" },\n",
" \"owner_id\": None,\n",
" \"name\": \"Welcome\",\n",
" \"last_message_id\": \"274070937597374736\",\n",
" \"id\": \"273372612762005519\",\n",
" \"house_id\": \"273372608798388238\",\n",
" \"emoji\": {\n",
" \"type\": 0,\n",
" \"data\": \"wave\"\n",
" },\n",
" \"description\": None,\n",
" \"default_permission_override\": {\n",
" \"deny\": 135233,\n",
" \"allow_\": 2\n",
" }\n",
"}"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "-6pLcs03CnW6"
},
"source": [
"Firstly lets calculate some base permissions for the entire house"
]
},
{
"cell_type": "code",
"metadata": {
"id": "snAq-APM81Tb"
},
"source": [
"# level always starts at 0\n",
"level = 0\n",
"permissions = houses_default_permissions\n",
"for role in members_roles:\n",
" role = next(r for r in house_roles if r['id'] == role)\n",
"\n",
" permissions |= role['allow']\n",
" permissions &= ~role['deny']\n",
"\n",
" if role['level'] > level:\n",
" level = role['level']\n",
"\n",
" if permissions & Permissions.ADMINISTRATOR.value == Permissions.ADMINISTRATOR.value:\n",
" print('is admin of house and _should_ have all permissions')"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Fts0Ohkj_NMM"
},
"source": [
"Now lets calculate room overrides"
]
},
{
"cell_type": "code",
"metadata": {
"id": "96VIgvpZ_Mof"
},
"source": [
"# Handling default room overrides first\n",
"permissions |= house_room['default_permission_override']['allow_']\n",
"permissions &= ~house_room['default_permission_override']['deny']\n",
"\n",
"# Handling role based room overrides\n",
"for role in members_roles:\n",
" if house_room['permission_overrides'] is None:\n",
" break\n",
" \n",
" permissions |= house_room['permission_overrides'][role]['allow']\n",
" permissions &= ~house_room['permission_overrides'][role]['deny']"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "3SS0kC2tCTff"
},
"source": [
"Can this member send messages in the channel?"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WtI_4V1qBTDT",
"outputId": "4842c7ba-bb8c-46d2-c5c4-32397772e801"
},
"source": [
"if permissions & Permissions.SEND_MESSAGES.value == Permissions.SEND_MESSAGES.value:\n",
" print('can send messages in', house_room['name'])\n",
"else:\n",
" print('can NOT send messages in', house_room['name'])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"can NOT send messages in Welcome\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5fwWB5zdCW__"
},
"source": [
"How about reading messages?"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-l7nZLxfCNfC",
"outputId": "c00886e6-c091-4b81-e379-193f6b2e5efb"
},
"source": [
"if permissions & Permissions.READ_MESSAGES.value == Permissions.READ_MESSAGES.value:\n",
" print('can read messages in', house_room['name'])\n",
"else:\n",
" print('can NOT read messages in', house_room['name'])"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"can read messages in Welcome\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment