Changeset - fd3f8035d98a
[Not reviewed]
0 1 0
Lance Edgar (lance) - 4 months ago 2024-07-04 21:25:28
lance@edbob.org
fix: add `get_role()` method for auth handler

for common lookup logic; this is used when auto-creating users so
config can say which role to assign them to
1 file changed with 33 insertions and 4 deletions:
0 comments (0 inline, 0 general)
rattail/auth.py
Show inline comments
 
@@ -2,7 +2,7 @@
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2023 Lance Edgar
 
#  Copyright © 2010-2024 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -360,6 +360,36 @@ class AuthHandler(GenericHandler, MergeMixin):
 
            session.add(user)
 
        return user
 

	
 
    def get_role(self, session, key, **kwargs):
 
        """
 
        Locate and return a Role for the given key, if possible.
 

	
 
        :param session: App database session.
 

	
 
        :param key: Value to use when searching for the role.  Can
 
           be a UUID or name of a role.
 

	
 
        :returns: The :class:`~rattail.db.model.Role` instance if
 
           found; or ``None``.
 
        """
 
        model = self.model
 

	
 
        # Role.uuid match?
 
        role = session.get(model.Role, key)
 
        if role:
 
            return role
 

	
 
        # Role.name match?
 
        try:
 
            return session.query(model.Role).filter_by(name=key).one()
 
        except orm.exc.NoResultFound:
 
            pass
 

	
 
        # try settings, if value then recurse.
 
        key = self.app.get_setting(session, f'rattail.role.{key}')
 
        if key:
 
            return self.get_role(session, key)
 

	
 
    def get_email_address(self, user, **kwargs):
 
        """
 
        Get the "best" email address we have on file for the given user.
 
@@ -452,8 +482,7 @@ class AuthHandler(GenericHandler, MergeMixin):
 
        }
 

	
 
    def get_merge_resulting_data(self, removing, keeping, **kwargs):
 
        result = super(AuthHandler, self).get_merge_resulting_data(
 
            removing, keeping, **kwargs)
 
        result = super().get_merge_resulting_data(removing, keeping, **kwargs)
 

	
 
        # nb. must "manually" coalesce the role count
 
        result['role_count'] = len(set(removing['_roles'] + keeping['_roles']))
 
@@ -472,7 +501,7 @@ class AuthHandler(GenericHandler, MergeMixin):
 
            return "Cannot (yet) remove a user who is assigned to roles"
 

	
 
    def merge_update_keeping_object(self, removing, keeping):
 
        super(AuthHandler, self).merge_update_keeping_object(removing, keeping)
 
        super().merge_update_keeping_object(removing, keeping)
 
        session = self.app.get_session(keeping)
 
        model = self.model
 

	
0 comments (0 inline, 0 general)