From 519165c0c5d26d4c9d1cb205012e29dc7472c529 Mon Sep 17 00:00:00 2001 From: Patrick Uiterwijk Date: Oct 07 2018 11:32:55 +0000 Subject: Do not mark generate_acls and remove_acls as abstract methods They are only required in non-dynamic ACL implementations. Signed-off-by: Patrick Uiterwijk --- diff --git a/pagure/lib/git_auth.py b/pagure/lib/git_auth.py index b2532e9..1a40d41 100644 --- a/pagure/lib/git_auth.py +++ b/pagure/lib/git_auth.py @@ -9,7 +9,6 @@ """ from __future__ import print_function, unicode_literals -import abc import json import logging import os @@ -19,7 +18,6 @@ import tempfile from io import open import werkzeug -from six import with_metaclass from six.moves import dbm_gnu import pagure.exceptions @@ -80,16 +78,41 @@ def get_git_auth_helper(backend=None): return GIT_AUTH_BACKEND_INSTANCE -class GitAuthHelper(with_metaclass(abc.ABCMeta, object)): +class GitAuthHelper(object): """ The class to inherit from when creating your own git authentication helper. """ is_dynamic = False + def __init__(self): + """ This method verifies that the correct functions are implemented. + + When a subclass gets initiated, we verify that the correct functions + are implemented for it (generate/remote_acls for non-dynamic, check_acl + for dynamic subclassess), and raise a NotImplementedError otherwise. + """ + if self.is_dynamic: + if self.check_acl == GitAuthHelper.check_acl: + raise NotImplementedError( + "Dynamic Git auth backend %s does not implement check_acl" + % self.__class__.__name__ + ) + else: + if self.generate_acls == GitAuthHelper.generate_acls: + raise NotImplementedError( + "Static Git auth backend %s does not implement " + "generate_acls" + % self.__class__.__name__ + ) + if self.remove_acls == GitAuthHelper.remove_acls: + raise NotImplementedError( + "Static Git auth backend %s does not implement remove_acls" + % self.__class__.__name__ + ) + @classmethod - @abc.abstractmethod - def generate_acls(self, project, group=None): + def generate_acls(cls, project, group=None): """ This is the method that is called by pagure to generate the configuration file. @@ -111,11 +134,13 @@ class GitAuthHelper(with_metaclass(abc.ABCMeta, object)): feel free to let us know.) """ - pass + if not cls.is_dynamic: + raise NotImplementedError( + "Non-dynamic ACL plugins must implement generate_acls" + ) @classmethod - @abc.abstractmethod - def remove_acls(self, session, project): + def remove_acls(cls, session, project): """ This is the method that is called by pagure to remove a project from the configuration file. @@ -127,11 +152,12 @@ class GitAuthHelper(with_metaclass(abc.ABCMeta, object)): :type project: pagure.lib.model.Project """ - pass + if not cls.is_dynamic: + raise NotImplementedError( + "Non-dynamic ACL plugins must implement remove_acls" + ) @classmethod - # This method can't be marked as abstract, since it's new and that would - # break backwards compatibility def check_acl(cls, session, project, username, refname, **info): """ This method is used in Dynamic Git Auth helpers to check acls. @@ -171,9 +197,10 @@ class GitAuthHelper(with_metaclass(abc.ABCMeta, object)): single ref from being updated, only return False if is_update is True. """ - raise NotImplementedError( - "check_acl on static Git Auth Backend called" - ) + if cls.is_dynamic: + raise NotImplementedError( + "check_acl on static Git Auth Backend called" + ) def _read_file(filename):