prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Better error message when registration is disabled
  When `/deis/controller/registrationMode` is `disabled`, attempt to register returns
  
  ```
  Registration failed: {"detail":"Authentication credentials were not provided."}
  ```
  
  This message is misleading. It should explicitly say that registration is disabled.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `controller/api/permissions.py`
  Content:
  ```
  1 from rest_framework import permissions
  2 from django.conf import settings
  3 from django.contrib.auth.models import AnonymousUser
  4 
  5 from api import models
  6 
  7 
  8 def is_app_user(request, obj):
  9     if request.user.is_superuser or \
  10             isinstance(obj, models.App) and obj.owner == request.user or \
  11             hasattr(obj, 'app') and obj.app.owner == request.user:
  12         return True
  13     elif request.user.has_perm('use_app', obj) or \
  14             hasattr(obj, 'app') and request.user.has_perm('use_app', obj.app):
  15         return request.method != 'DELETE'
  16     else:
  17         return False
  18 
  19 
  20 class IsAnonymous(permissions.BasePermission):
  21     """
  22     View permission to allow anonymous users.
  23     """
  24 
  25     def has_permission(self, request, view):
  26         """
  27         Return `True` if permission is granted, `False` otherwise.
  28         """
  29         return type(request.user) is AnonymousUser
  30 
  31 
  32 class IsOwner(permissions.BasePermission):
  33     """
  34     Object-level permission to allow only owners of an object to access it.
  35     Assumes the model instance has an `owner` attribute.
  36     """
  37 
  38     def has_object_permission(self, request, view, obj):
  39         if hasattr(obj, 'owner'):
  40             return obj.owner == request.user
  41         else:
  42             return False
  43 
  44 
  45 class IsOwnerOrAdmin(permissions.BasePermission):
  46     """
  47     Object-level permission to allow only owners of an object or administrators to access it.
  48     Assumes the model instance has an `owner` attribute.
  49     """
  50     def has_object_permission(self, request, view, obj):
  51         if request.user.is_superuser:
  52             return True
  53         if hasattr(obj, 'owner'):
  54             return obj.owner == request.user
  55         else:
  56             return False
  57 
  58 
  59 class IsAppUser(permissions.BasePermission):
  60     """
  61     Object-level permission to allow owners or collaborators to access
  62     an app-related model.
  63     """
  64     def has_object_permission(self, request, view, obj):
  65         return is_app_user(request, obj)
  66 
  67 
  68 class IsAdmin(permissions.BasePermission):
  69     """
  70     View permission to allow only admins.
  71     """
  72 
  73     def has_permission(self, request, view):
  74         """
  75         Return `True` if permission is granted, `False` otherwise.
  76         """
  77         return request.user.is_superuser
  78 
  79 
  80 class IsAdminOrSafeMethod(permissions.BasePermission):
  81     """
  82     View permission to allow only admins to use unsafe methods
  83     including POST, PUT, DELETE.
  84 
  85     This allows
  86     """
  87 
  88     def has_permission(self, request, view):
  89         """
  90         Return `True` if permission is granted, `False` otherwise.
  91         """
  92         return request.method in permissions.SAFE_METHODS or request.user.is_superuser
  93 
  94 
  95 class HasRegistrationAuth(permissions.BasePermission):
  96     """
  97     Checks to see if registration is enabled
  98     """
  99     def has_permission(self, request, view):
  100         """
  101         If settings.REGISTRATION_MODE does not exist, such as during a test, return True
  102         Return `True` if permission is granted, `False` otherwise.
  103         """
  104         try:
  105             if settings.REGISTRATION_MODE == 'disabled':
  106                 return False
  107             if settings.REGISTRATION_MODE == 'enabled':
  108                 return True
  109             elif settings.REGISTRATION_MODE == 'admin_only':
  110                 return request.user.is_superuser
  111             else:
  112                 raise Exception("{} is not a valid registation mode"
  113                                 .format(settings.REGISTRATION_MODE))
  114         except AttributeError:
  115             return True
  116 
  117 
  118 class HasBuilderAuth(permissions.BasePermission):
  119     """
  120     View permission to allow builder to perform actions
  121     with a special HTTP header
  122     """
  123 
  124     def has_permission(self, request, view):
  125         """
  126         Return `True` if permission is granted, `False` otherwise.
  127         """
  128         auth_header = request.environ.get('HTTP_X_DEIS_BUILDER_AUTH')
  129         if not auth_header:
  130             return False
  131         return auth_header == settings.BUILDER_KEY
  132 
  133 
  134 class CanRegenerateToken(permissions.BasePermission):
  135     """
  136     Checks if a user can regenerate a token
  137     """
  138 
  139     def has_permission(self, request, view):
  140         """
  141         Return `True` if permission is granted, `False` otherwise.
  142         """
  143         if 'username' in request.data or 'all' in request.data:
  144             return request.user.is_superuser
  145         else:
  146             return True
  147
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/controller/api/permissions.py b/controller/api/permissions.py
  --- a/controller/api/permissions.py
  +++ b/controller/api/permissions.py
  @@ -1,3 +1,5 @@
  +
  +from rest_framework import exceptions
   from rest_framework import permissions
   from django.conf import settings
   from django.contrib.auth.models import AnonymousUser
  @@ -103,7 +105,7 @@
           """
           try:
               if settings.REGISTRATION_MODE == 'disabled':
  -                return False
  +                raise exceptions.PermissionDenied('Registration is disabled')
               if settings.REGISTRATION_MODE == 'enabled':
                   return True
               elif settings.REGISTRATION_MODE == 'admin_only':
  
verification_info: {"golden_diff": "diff --git a/controller/api/permissions.py b/controller/api/permissions.py\n--- a/controller/api/permissions.py\n+++ b/controller/api/permissions.py\n@@ -1,3 +1,5 @@\n+\n+from rest_framework import exceptions\n from rest_framework import permissions\n from django.conf import settings\n from django.contrib.auth.models import AnonymousUser\n@@ -103,7 +105,7 @@\n         \"\"\"\n         try:\n             if settings.REGISTRATION_MODE == 'disabled':\n-                return False\n+                raise exceptions.PermissionDenied('Registration is disabled')\n             if settings.REGISTRATION_MODE == 'enabled':\n                 return True\n             elif settings.REGISTRATION_MODE == 'admin_only':\n", "issue": "Better error message when registration is disabled\nWhen `/deis/controller/registrationMode` is `disabled`, attempt to register returns\n\n```\nRegistration failed: {\"detail\":\"Authentication credentials were not provided.\"}\n```\n\nThis message is misleading. It should explicitly say that registration is disabled.\n\n", "before_files": [{"content": "from rest_framework import permissions\nfrom django.conf import settings\nfrom django.contrib.auth.models import AnonymousUser\n\nfrom api import models\n\n\ndef is_app_user(request, obj):\n    if request.user.is_superuser or \\\n            isinstance(obj, models.App) and obj.owner == request.user or \\\n            hasattr(obj, 'app') and obj.app.owner == request.user:\n        return True\n    elif request.user.has_perm('use_app', obj) or \\\n            hasattr(obj, 'app') and request.user.has_perm('use_app', obj.app):\n        return request.method != 'DELETE'\n    else:\n        return False\n\n\nclass IsAnonymous(permissions.BasePermission):\n    \"\"\"\n    View permission to allow anonymous users.\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        return type(request.user) is AnonymousUser\n\n\nclass IsOwner(permissions.BasePermission):\n    \"\"\"\n    Object-level permission to allow only owners of an object to access it.\n    Assumes the model instance has an `owner` attribute.\n    \"\"\"\n\n    def has_object_permission(self, request, view, obj):\n        if hasattr(obj, 'owner'):\n            return obj.owner == request.user\n        else:\n            return False\n\n\nclass IsOwnerOrAdmin(permissions.BasePermission):\n    \"\"\"\n    Object-level permission to allow only owners of an object or administrators to access it.\n    Assumes the model instance has an `owner` attribute.\n    \"\"\"\n    def has_object_permission(self, request, view, obj):\n        if request.user.is_superuser:\n            return True\n        if hasattr(obj, 'owner'):\n            return obj.owner == request.user\n        else:\n            return False\n\n\nclass IsAppUser(permissions.BasePermission):\n    \"\"\"\n    Object-level permission to allow owners or collaborators to access\n    an app-related model.\n    \"\"\"\n    def has_object_permission(self, request, view, obj):\n        return is_app_user(request, obj)\n\n\nclass IsAdmin(permissions.BasePermission):\n    \"\"\"\n    View permission to allow only admins.\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        return request.user.is_superuser\n\n\nclass IsAdminOrSafeMethod(permissions.BasePermission):\n    \"\"\"\n    View permission to allow only admins to use unsafe methods\n    including POST, PUT, DELETE.\n\n    This allows\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        return request.method in permissions.SAFE_METHODS or request.user.is_superuser\n\n\nclass HasRegistrationAuth(permissions.BasePermission):\n    \"\"\"\n    Checks to see if registration is enabled\n    \"\"\"\n    def has_permission(self, request, view):\n        \"\"\"\n        If settings.REGISTRATION_MODE does not exist, such as during a test, return True\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        try:\n            if settings.REGISTRATION_MODE == 'disabled':\n                return False\n            if settings.REGISTRATION_MODE == 'enabled':\n                return True\n            elif settings.REGISTRATION_MODE == 'admin_only':\n                return request.user.is_superuser\n            else:\n                raise Exception(\"{} is not a valid registation mode\"\n                                .format(settings.REGISTRATION_MODE))\n        except AttributeError:\n            return True\n\n\nclass HasBuilderAuth(permissions.BasePermission):\n    \"\"\"\n    View permission to allow builder to perform actions\n    with a special HTTP header\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        auth_header = request.environ.get('HTTP_X_DEIS_BUILDER_AUTH')\n        if not auth_header:\n            return False\n        return auth_header == settings.BUILDER_KEY\n\n\nclass CanRegenerateToken(permissions.BasePermission):\n    \"\"\"\n    Checks if a user can regenerate a token\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        if 'username' in request.data or 'all' in request.data:\n            return request.user.is_superuser\n        else:\n            return True\n", "path": "controller/api/permissions.py"}], "after_files": [{"content": "\nfrom rest_framework import exceptions\nfrom rest_framework import permissions\nfrom django.conf import settings\nfrom django.contrib.auth.models import AnonymousUser\n\nfrom api import models\n\n\ndef is_app_user(request, obj):\n    if request.user.is_superuser or \\\n            isinstance(obj, models.App) and obj.owner == request.user or \\\n            hasattr(obj, 'app') and obj.app.owner == request.user:\n        return True\n    elif request.user.has_perm('use_app', obj) or \\\n            hasattr(obj, 'app') and request.user.has_perm('use_app', obj.app):\n        return request.method != 'DELETE'\n    else:\n        return False\n\n\nclass IsAnonymous(permissions.BasePermission):\n    \"\"\"\n    View permission to allow anonymous users.\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        return type(request.user) is AnonymousUser\n\n\nclass IsOwner(permissions.BasePermission):\n    \"\"\"\n    Object-level permission to allow only owners of an object to access it.\n    Assumes the model instance has an `owner` attribute.\n    \"\"\"\n\n    def has_object_permission(self, request, view, obj):\n        if hasattr(obj, 'owner'):\n            return obj.owner == request.user\n        else:\n            return False\n\n\nclass IsOwnerOrAdmin(permissions.BasePermission):\n    \"\"\"\n    Object-level permission to allow only owners of an object or administrators to access it.\n    Assumes the model instance has an `owner` attribute.\n    \"\"\"\n    def has_object_permission(self, request, view, obj):\n        if request.user.is_superuser:\n            return True\n        if hasattr(obj, 'owner'):\n            return obj.owner == request.user\n        else:\n            return False\n\n\nclass IsAppUser(permissions.BasePermission):\n    \"\"\"\n    Object-level permission to allow owners or collaborators to access\n    an app-related model.\n    \"\"\"\n    def has_object_permission(self, request, view, obj):\n        return is_app_user(request, obj)\n\n\nclass IsAdmin(permissions.BasePermission):\n    \"\"\"\n    View permission to allow only admins.\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        return request.user.is_superuser\n\n\nclass IsAdminOrSafeMethod(permissions.BasePermission):\n    \"\"\"\n    View permission to allow only admins to use unsafe methods\n    including POST, PUT, DELETE.\n\n    This allows\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        return request.method in permissions.SAFE_METHODS or request.user.is_superuser\n\n\nclass HasRegistrationAuth(permissions.BasePermission):\n    \"\"\"\n    Checks to see if registration is enabled\n    \"\"\"\n    def has_permission(self, request, view):\n        \"\"\"\n        If settings.REGISTRATION_MODE does not exist, such as during a test, return True\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        try:\n            if settings.REGISTRATION_MODE == 'disabled':\n                raise exceptions.PermissionDenied('Registration is disabled')\n            if settings.REGISTRATION_MODE == 'enabled':\n                return True\n            elif settings.REGISTRATION_MODE == 'admin_only':\n                return request.user.is_superuser\n            else:\n                raise Exception(\"{} is not a valid registation mode\"\n                                .format(settings.REGISTRATION_MODE))\n        except AttributeError:\n            return True\n\n\nclass HasBuilderAuth(permissions.BasePermission):\n    \"\"\"\n    View permission to allow builder to perform actions\n    with a special HTTP header\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        auth_header = request.environ.get('HTTP_X_DEIS_BUILDER_AUTH')\n        if not auth_header:\n            return False\n        return auth_header == settings.BUILDER_KEY\n\n\nclass CanRegenerateToken(permissions.BasePermission):\n    \"\"\"\n    Checks if a user can regenerate a token\n    \"\"\"\n\n    def has_permission(self, request, view):\n        \"\"\"\n        Return `True` if permission is granted, `False` otherwise.\n        \"\"\"\n        if 'username' in request.data or 'all' in request.data:\n            return request.user.is_superuser\n        else:\n            return True\n", "path": "controller/api/permissions.py"}]}