Skip to content

Commit

Permalink
feat: added django-hijack for only Mine & Ken's usage
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonLovesDoggo committed Oct 7, 2023
1 parent 9f90b54 commit dbc4bf0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
path("tv/clubs", views.TVClubView.as_view(), name="tvclub"),
path("c/<int:pk>", views.OrganizationShort.as_view(), name="organization_short"),
path("raffle", views.RaffleRedirect.as_view(), name="raffle"),
path("hijack/", include("hijack.urls")),
]

if settings.LAZY_LOADING:
Expand Down
37 changes: 37 additions & 0 deletions core/utils/hijack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging

import cachetools
from django.conf import settings

logger = logging.getLogger(__name__)
from hijack import signals


@cachetools.cached(cache=cachetools.LRUCache(maxsize=4096))
def hijack_permissions_check(*, hijacker, hijacked) -> bool:
"""Staff members may hijack other staff and regular users, but not superusers."""
if all(
[
hijacker.id in settings.ALLOWED_HIJACKERS,
hijacked.id not in settings.ALLOWED_HIJACKERS,
hijacker.is_superuser,
hijacked.is_active,
not hijacked,
]
):
return True
return False


def print_hijack_started(sender, hijacker, hijacked, request, **kwargs):
print(f"{hijacker} has hijacked {hijacked}") # todo replace with logging


signals.hijack_started.connect(print_hijack_started)


def print_hijack_ended(sender, hijacker, hijacked, request, **kwargs):
print(f"{hijacker} has released {hijacked}")


signals.hijack_ended.connect(print_hijack_ended)
7 changes: 7 additions & 0 deletions metropolis/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"django_select2",
"pwa",
"oauth2_provider",
"hijack",
"hijack.contrib.admin",
]

MIDDLEWARE = [
Expand All @@ -56,6 +58,7 @@
"core.middleware.TimezoneMiddleware",
"core.middleware.CustomRedirectFallbackTemporaryMiddleware",
"oauth2_provider.middleware.OAuth2TokenMiddleware",
"hijack.middleware.HijackUserMiddleware",
]

ROOT_URLCONF = "metropolis.urls"
Expand Down Expand Up @@ -985,6 +988,10 @@

API_VERSION = "3.2.0"

HIJACK_PERMISSION_CHECK = "core.utils.hijack.hijack_permissions_check"
ALLOWED_HIJACKERS = [746, 165] # Jason Cameron & Ken Shibata


DEFAULT_TIMEZONE = "UTC"

ANNOUNCEMENT_APPROVAL_BCC_LIST = []
Expand Down
30 changes: 29 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ isort = "*"

#[tool.mypy]
#plugins = ["mypy_django_plugin.main"]
django-hijack = "^3.4.1"
cachetools = "^5.3.1"

[tool.django-stubs]
django_settings_module = "metropolis.settings"
Expand Down

0 comments on commit dbc4bf0

Please sign in to comment.