Seedstars / django-react-redux-base

Seedstars Labs Base Django React Redux Project
MIT License
2.66k stars 341 forks source link

Logged in as superuser: Accessing admin/ gives error "User object has no attribute has_module_perms". Accessing api/v1/ api/v1/accounts/ gives "Not Found". Also, second browser tab shows "Login". #86

Closed StefanScott closed 7 years ago

StefanScott commented 7 years ago

I installed Seedstars django-react-redux-base on Windows 7 64bit with Python 3.6 and it runs fine and looks great.

I'm impressed that there were no errors during the install - which is rare for a system built on top of this many JavaScript and Python components.

Now I'm able to login and see the "protected content".


However there may be something strange happening with "authorization".

I get errors when I try to access the following URLs:

AttributeError at /admin/

'User' object has no attribute 'has_module_perms'

Django React Redux Demo    Home  Login

NOT FOUND

I'm confused because these routes apparently do exist in the file:

src\djangoreactredux\urls.py

which says:

urlpatterns = [
    url(r'^api/v1/accounts/', include('accounts.urls', namespace='accounts')),
    url(r'^api/v1/getdata/', include('base.urls', namespace='base')),
    url(r'^admin/', admin.site.urls),

    # catch all others because of how history is handled by react router - cache this page because it will never change
    url(r'', cache_page(settings.PAGE_CACHE_SECONDS)(base_views.IndexView.as_view()), name='index'),
]

I checked the previous similar Issue here:

Accessing the built-in Django admin area?

https://github.com/Seedstars/django-react-redux-base/issues/62

They resolved this by making sure to put the "/" after "admin" - but this didn't work for me.


To explore the database, I installed DB Browser for SQLite and verified that user a@a.com (plus another "superuser" I created using python manage.py createsuperuser in my virtual environment) both have a value of 1 (true) for is_staff and is_superuser.

But I still get the errors when I go to the above URLs.


Finally, I did a different test:

I logged in as a@a.com - then I opened another window (browser tab), in the same browser.

In the new window, it didn't say "Logout" - it said "Login", as if I hadn't logged in yet.

In the old window (browser tab), I could access the "Protected Content".

But in the new window / tab (in the same browser), I couldn't access the "Protected Content".

This happened in both Chrome and Firefox.

(By the way, if anyone cares, I remembered I had IE9 on my machine. For all URLs, IE9 showed a blank page. I never use IE, and I suppose IE9 doesn't support React.)

Anyways, isn't the browser supposed to "remember" that I'm logged in, no matter how many windows or tabs I open?


Seedstars django-react-redux-base seems like a great foundation for building web apps, so I'm eager to start using it.

Thanks for any help!

annavice commented 7 years ago

Great project! Would like to log in as well...

Added a user with manage.py createuser which updated the db, but still no luck authenticating.


> python --version
Python 3.6.2

———————

AttributeError: 'User' object has no attribute 'has_module_perms'


builtins.AttributeError AttributeError: 'User' object has no attribute 'has_module_perms'

Traceback (most recent call last) File ".../lib/python3.6/site-packages/django/contrib/staticfiles/handlers.py", line 63, in call return self.application(environ, start_response)

File ".../lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 157, in call response = self.get_response(request)

File ".../lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in get_response response = self._middleware_chain(request)

File ".../lib/python3.6/site-packages/django/core/handlers/exception.py", line 43, in inner response = response_for_exception(request, exc)

File ".../lib/python3.6/site-packages/django/core/handlers/exception.py", line 93, in response_for_exception response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

File ".../lib/python3.6/site-packages/django/core/handlers/exception.py", line 139, in handle_uncaught_exception return debug.technical_500_response(request, *exc_info)

File ".../lib/python3.6/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response six.reraise(exc_type, exc_value, tb)

File ".../lib/python3.6/site-packages/six.py", line 686, in reraise raise value

File ".../lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request)

File ".../lib/python3.6/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response response = self._get_response(request)

File ".../lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request)

File ".../lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)

File ".../lib/python3.6/site-packages/django/contrib/admin/sites.py", line 242, in wrapper return self.admin_view(view, cacheable)(*args, **kwargs)

File ".../lib/python3.6/site-packages/django/utils/decorators.py", line 149, in _wrapped_view response = view_func(request, *args, **kwargs)

File ".../lib/python3.6/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func response = view_func(request, *args, **kwargs)

File ".../lib/python3.6/site-packages/django/contrib/admin/sites.py", line 224, in inner return view(request, *args, **kwargs)

File ".../lib/python3.6/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func response = view_func(request, *args, **kwargs)

File ".../lib/python3.6/site-packages/django/contrib/admin/sites.py", line 482, in index app_list = self.get_app_list(request)

File ".../lib/python3.6/site-packages/django/contrib/admin/sites.py", line 465, in get_app_list app_dict = self._build_app_dict(request)

File ".../lib/python3.6/site-packages/django/contrib/admin/sites.py", line 413, in _build_app_dict has_module_perms = model_admin.has_module_permission(request)

File ".../lib/python3.6/site-packages/django/contrib/admin/options.py", line 482, in has_module_permission return request.user.has_module_perms(self.opts.app_label)

File ".../lib/python3.6/site-packages/django/utils/functional.py", line 239, in inner return func(self._wrapped, *args)

AttributeError: 'User' object has no attribute 'has_module_perms'

———————

> pip list

apipkg (1.4)
asn1crypto (0.22.0)
astroid (1.5.3)
bandit (1.4.0)
block-disposable-email (1.0.1)
cffi (1.10.0)
contextlib2 (0.5.5)
coverage (4.4.1)
cryptography (2.0.3)
Django (1.11.5)
django-disposable-email-checker (1.2.1)
django-extensions (1.9.1)
django-rest-knox (3.0.0)
django-rest-logger (1.0.2)
djangorestframework (3.6.4)
dnspython (1.15.0)
docopt (0.6.2)
dodgy (0.1.9)
email-validator (1.0.3)
execnet (1.4.1)
factory-boy (2.9.2)
Faker (0.8.3)
gitdb2 (2.0.2)
GitPython (2.1.5)
idna (2.6)
isort (4.2.15)
jedi (0.10.2)
lazy-object-proxy (1.3.1)
mccabe (0.6.1)
mock (2.0.0)
pbr (3.1.1)
pep8-naming (0.4.1)
pip (9.0.1)
prompt-toolkit (1.0.15)
prospector (0.12.7)
psycopg2 (2.7.3.1)
ptpython (0.41)
py (1.4.34)
pycodestyle (2.3.1)
pycparser (2.18)
pydocstyle (2.0.0)
pyflakes (1.6.0)
Pygments (2.2.0)
pylint (1.7.2)
pylint-celery (0.3)
pylint-common (0.2.5)
pylint-django (0.7.2)
pylint-flask (0.5)
pylint-plugin-utils (0.2.6)
pyOpenSSL (17.2.0)
pytest (3.2.2)
pytest-cov (2.5.1)
pytest-django (3.1.2)
pytest-forked (0.2)
pytest-pythonpath (0.7.1)
pytest-xdist (1.20.0)
python-dateutil (2.6.1)
pytz (2017.2)
PyYAML (3.12)
raven (6.1.0)
requirements-detector (0.5.2)
setoptconf (0.2.0)
setuptools (36.4.0)
six (1.10.0)
smmap2 (2.0.3)
snowballstemmer (1.2.1)
stevedore (1.26.0)
wcwidth (0.1.7)
Werkzeug (0.12.2)
wheel (0.30.0)
whitenoise (3.3.0)
wrapt (1.10.11)
kjschill commented 7 years ago

I was having the same issues. I had to add the PermissionsMixin to the User. The AbstractBaseUser does implement has_module_perms

See Django Documentation on PermissionsMixin here: https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#custom-users-and-permissions

Here is how I updated the code in src\accounts\models.py:

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
...

class User(AbstractBaseUser, PermissionsMixin):
...
m2jobe commented 7 years ago

You can add the following methods to your User model:

class User(AbstractBaseUser): ..... def has_perm(self, perm, obj=None): return self.is_superuser

def has_module_perms(self, app_label): return self.is_superuser

lfrodrigues commented 7 years ago

Thanks everyone for the comments, you are right that's how to add Django Admin but it should be enabled in this project. I'm closing this and removing the lines in urls.py.