title: Django hook to deny user access to admin based on site
body: <p>We have a Django project with multiple sites (using a derivation of the Sites framework) where each site is a subdomain of our domain.  I need to be able to deny access to the admin site for some staff users (instance of a custom user model with a foreign key to site) based on whether the user has a foreign key to the Site or not.</p>
  
  <p>The custom user model looks like basically like this:</p>
  
  <pre><code>class SiteTenantUser(AbstractUser):
      site = models.ForeignKey(Site, null=True)
  </code></pre>
  
  <p>Your first inclination may be to suggest that we prevent authentication with the <code>authenticate</code> method of our custom <code>SiteAuthBackend</code> authentication back end in order to only let users log into their own site. That works to prevent authentication, but the trick is that we have superusers who do have cross-site access and therefore we want to use a wilcard session cookie (.ourdomain.com) to let the superusers go from site to site without having to log in every time. By doing that, however, we will now need something besides just <code>authenticate</code> to prevent cross-site access by users without that privilege. Otherwise they can authenticate against their own site and then switch to another site's admin url and have a legit session and be in the door. </p>
  
  <p>In sum, although our custom auth backend prevents non-superuser staff users (who are limited to a single site) from logging in to another site's admin, it doesn't prevent them from logging into their own admin and then switching to another site's admin. Is there a straightforward hook to prevent this, similar to how Django will reject a user without <code>is_staff</code> from any admin page regardless of whether they are authenticated? </p>
  
  <p>PS We have a global <code>get_current_site</code> method that uses a cached value from middleware and works from anywhere and doesn't require a request.</p>