Skip to content

Commit

Permalink
Merge pull request #74 from eea/main
Browse files Browse the repository at this point in the history
Search in fullname, email and fix login on Volto frontend when already logged-in in Plone Classic
  • Loading branch information
jensens authored Nov 8, 2024
2 parents 5f796ea + 3354fd8 commit 4caae86
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.2.1 (unreleased)

- Search users by fullname and email. @alecghica
- Fix login on Volto frontend when already logged-in in Plone Classic. @avoinea
- Add the possibility to override the ZopeRequestAdapter.


Expand Down
18 changes: 16 additions & 2 deletions src/pas/plugins/authomatic/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,24 @@ def enumerateUsers(
if not userid:
logger.warn("None userid found. This should not happen!")
continue
if not userid.startswith(search_id):
continue

# search for a match in fullname, email and userid
identity = self._useridentities_by_userid[userid]
search_term = search_id.lower()
identity_userid = identity.userid
identity_fullname = identity.propertysheet.getProperty("fullname", "").lower()
identity_email = identity.propertysheet.getProperty("email", "").lower()
if (
not search_term in identity_userid
and not search_term in identity_fullname
and not search_term in identity_email
):
continue

# if not userid.startswith(search_id):
# continue
# identity = self._useridentities_by_userid[userid]
# identity_userid = identity.userid
ret.append(
{
"id": identity_userid,
Expand Down
23 changes: 15 additions & 8 deletions src/pas/plugins/authomatic/services/authomatic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from authomatic import Authomatic
from pas.plugins.authomatic.integration import RestAPIAdapter
from pas.plugins.authomatic.utils import authomatic_cfg
Expand All @@ -14,6 +15,7 @@
from zope.publisher.interfaces import IPublishTraverse

import transaction
logger = logging.getLogger("pas.plugins.authomatic")


@implementer(IPublishTraverse)
Expand Down Expand Up @@ -161,15 +163,14 @@ def _get_jwt_plugin(self):
break
return plugin

def _add_identity(self, result):
def _add_identity(self, result, userid=None):
"""Add an identity to an existing user.
:param result: Authomatic login result.
"""
aclu = self._get_acl_users()
aclu.authomatic.remember_identity(
result,
)
result, userid)

def _remember_identity(self, result):
"""Store identity information.
Expand Down Expand Up @@ -237,17 +238,23 @@ def reply(self) -> dict:
}
elif result:
alsoProvides(self.request, IDisableCSRFProtection)
action = ''
if api.user.is_anonymous():
self._remember_identity(result)
action = "login"
else:
# Authenticated user, add an identity to it
self._add_identity(result)
action = "add_identity"
try:
userid = api.user.get_current().getId()
self._add_identity(result, userid)
action = "add_identity"
except ValueError as err:
logger.exception(err)

user = api.user.get_current()
# Make sure we are not setting cookies here
# as it will break the authentication mechanism with JWT tokens
self.request.response.cookies = {}
token = self.get_token(user)
self._annotate_transaction(action, user=user)
return {"token": token}
if action:
self._annotate_transaction(action, user=user)
return {"token": self.get_token(user)}

0 comments on commit 4caae86

Please sign in to comment.