Skip to content

Commit

Permalink
feature: pass context to interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Oct 5, 2023
1 parent 8a2d65c commit 19688fd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
14 changes: 11 additions & 3 deletions ckanext/let_me_in/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
from __future__ import annotations

from typing import Any

from ckan import model
from ckan.plugins.interfaces import Interface


class ILetMeIn(Interface):
"""Interface to do something on user login via OTL link"""

def manage_user(self, user: model.User) -> model.User:
def manage_user(self, user: model.User, context: dict[str, Any]) -> model.User:
"""Accept a user object that will be logged. Only Active user could be
logged in, so here we could implement user re-activation, for example.
The user is always exists, otherwise we are not getting here.
This happend before we are checking for user state and actual login
You can use context to pass some information further, for example, if you
did something to user in `manage_user`, you can pass this information
to `before_otl_login` or `after_otl_login` funcs.
Must return a user object
"""
return user

def before_otl_login(self, user: model.User) -> None:
def before_otl_login(self, user: model.User, context: dict[str, Any]) -> None:
"""Allows to do something before we are logging in a user.
Happens after all checks and `manage_user` method."""
pass

def after_otl_login(self, user: model.User) -> None:
def after_otl_login(self, user: model.User, context: dict[str, Any]) -> None:
"""Allows to do something after we logged in a user."""
pass
10 changes: 8 additions & 2 deletions ckanext/let_me_in/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ def lmi_generate_otl(
) -> types.ActionResult.AnyDict:
"""Generate a one-time login link for a specified user
:param user: username of user_id
:type user: string
:param uid: user ID
:type uid: string
:param name: username
:type name: string
:param mail: user email
:type mail: string
"""
tk.check_access("lmi_generate_otl", context, data_dict)

Expand Down
6 changes: 5 additions & 1 deletion ckanext/let_me_in/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from __future__ import annotations

from typing import Any

import ckan.model as model
import ckan.plugins as p
import ckan.plugins.toolkit as tk
Expand All @@ -16,7 +20,7 @@ class LetMeInPlugin(p.SingletonPlugin):

# ILetMeIn

def after_otl_login(self, user: model.User) -> None:
def after_otl_login(self, user: model.User, context: dict[str, Any]) -> None:
lmi_utils.update_user_last_active(user)

tk.h.flash_success("You have been logged in.")
10 changes: 7 additions & 3 deletions ckanext/let_me_in/tests/test_interface.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from __future__ import annotations

from typing import Any

import pytest

import ckan.model as model
Expand All @@ -16,15 +20,15 @@ def configure(self, _config):
self.before_otl_login_call = 0
self.after_otl_login_call = 0

def manage_user(self, user: model.User) -> model.User:
def manage_user(self, user: model.User, context: dict[str, Any]) -> model.User:
self.manage_user_call += 1

return user

def before_otl_login(self, user: model.User) -> None:
def before_otl_login(self, user: model.User, context: dict[str, Any]) -> None:
self.before_otl_login_call += 1

def after_otl_login(self, user: model.User) -> None:
def after_otl_login(self, user: model.User, context: dict[str, Any]) -> None:
self.after_otl_login_call += 1


Expand Down
7 changes: 4 additions & 3 deletions ckanext/let_me_in/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def login_with_token(token):
tk.h.flash_error(tk._("Invalid login link."))
else:
user = lmi_utils.get_user(token["user_id"])
context = {}

for plugin in p.PluginImplementations(ILetMeIn):
user = plugin.manage_user(user)
user = plugin.manage_user(user, context)

if user.state != model.State.ACTIVE:
tk.h.flash_error(tk._("User is not active. Can't login"))
Expand All @@ -44,12 +45,12 @@ def login_with_token(token):
return tk.h.redirect_to("user.login")

for plugin in p.PluginImplementations(ILetMeIn):
plugin.before_otl_login(user)
plugin.before_otl_login(user, context)

tk.login_user(user)

for plugin in p.PluginImplementations(ILetMeIn):
plugin.after_otl_login(user)
plugin.after_otl_login(user, context)

return tk.h.redirect_to("user.me")

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ckanext-let-me-in
version = 1.0.0
version = 1.0.1
description = An extension that generates a one-time login link for a user
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down

0 comments on commit 19688fd

Please sign in to comment.