Django registering users against a list of existing usernames - django

I am using Django rest framework to build my application backend. I have used Django-rest-auth (https://github.com/Tivix/django-rest-auth) to enable login and registration for my app. It is already working.
However as per my new requirement, I have a list of usernames and only those usernames can register into my system. How can I achieve this?
I had few ideas (do not know if they make total sense):
1. A new model to store usernames, so that in future more usernames can be added via admin interface
2. Whenever a user makes a call from client: the entered username is checked against this new usernames table and if it exists in the table registration is allowed.
Or there can be a still easier way to do this? Any code snippets?

Related

Migrate user accounts from old LAMP website to Django

We are rebuilding our old analytics website in Django. Our old website was built with LAMP stack. We have the usernames and passwords for all our accounts in un-hashed form.
Now I want to migrate all those usernames and passwords to our Django website. All our users should be able to login to the new Django website using the same username and password.
Can someone suggest some best practices to do this? How to achieve this objective efficiently?
all our accounts in un-hashed form
Please do not do that. Django is designed for security. By default, Django uses the PBKDF2 algorithm with a SHA256 hash.
So, during data migration, you must pass your plain texted password through hash function and save the hash-ed password to database.
By applying the above approach, you do not need to force users to reset their password.
The easiest solution would be to export all the user accounts from the old platform into the Django auth_users table. With either a python script or SQL tricker-y poker-y.
Whilst this method takes a little more work from the users point of view it is the safest option (especially because you stated passwords are not encrypted).
After all usernames/ emails are in the new table I (personally) would not set a password for those migrated user accounts. Instead, make sure you have set up django password reset screens (this is built into django). You can then get the users to reset their own passwords allowing them access into the new application.

How to structure django admin for multiple users

I'm still a complete newbie on Django, so now I'm a little bit lost on what I could do to structure my server to suit my needs.
The situation is like this: my Django admin could be accessed by the admin and multiple users. Each user can add multiple item to the server, and the server will only allow them to retrieve, modify and delete item added by them and not the other users. They will also have some custom option they can pick: like receiving notifications through emails or another channels. Meanwhile, admin can see all items, and have a filter to see all items added by one user and all users's custom option.
Any help would be appreciated.
take a look here. this is where i started with custom user models. https://wsvincent.com/django-custom-user-model-tutorial/
Django has builtin user models with basic fields like username email and password and authentication. The above link will help you create custom user models and it will be a good place to start

Increasing Django login security and password strengths

When I create the Django superuser , if I try to add a weak password Django doesn't let me, but for normal users, in admin, or using register form I can add very simple password.
How can I ad the password validation from the superuser creation to all users ?
Can the number of login bad tries be limited (I prefer without third-party)
When creating users or super users alike both use the same Django configuration settings AUTH_PASSWORD_VALIDATORS and if left unmodified it'll contain a list of validators that all passwords will validate against when creating users via Django admin.
This is also the place where you strengthen your validators by adding more if you want harder or remove if you want to be more lax.
However, if you're creating users via the management commands create_user and create_superuser this list of validators will not apply. This is because Django assumes that only developers are interacting with Django at this level.
For your second ask, there is nothing built-in to Django that supports login tries and following blocking of further logins. This is something that either comes from 3rd party apps such as django-defender or from own implementation.
The broad strokes of that implementation is
Add a new tablemechanism that stores number of tries
Add a new settings in settings.py LOGIN_ATTEMPTS = 3
Override the login flow for a user in which you check this table for attempts
If failed attempt increment counter, if successful reset counter.
If user hits the limit of attempts, set users is_active to False and always return False from your login override.

Migrating cakephp application to django

I have a cakephp application that has a users database and currently has approximately 50 users.
I was wondering what would be the best way to migrate the application to django without affecting the users.
I am concerned because the passwords are all encrypted of course, and most probably the encryption will not be the same in django.
The simplest approach would be to create the users with random passwords in your new application, then when switching send them an invitation with a login link. Take a look at this app: https://github.com/fajran/django-loginurl
Then ask the users to choose a password when they login the first time.
Second way, not so nice - but if you don't want to ask for the password again and you don't have a way to decrypt the existing one:
Modify your existing application in a way that it sends the username and password (taken from the existing login form - so you have it in cleartext) to the new backend. Then pull the profile from the legacy-app to the new one and create/migrate the user-profile.

django user to be populated in LDAP

I would like when a user creates an account in Django, that the user information :
- Username
- Password
- Email
- First and Last Name
- Mobile
Gets also populated in my LDAP server. Also when the user get deactivated, this gets reflected in LDAP.
Authentication will still be done in Django.
I need the user information as i have another application which is getting the user info from LDAP. I need both to be have the same user universe.
Are there any snippet that does that already ?
I saw many code to authenticate thourgh LDAP, but what i really need is to populate the LDAP directory with my Django user on the fly
Thanks for your help
Check out this snippet, it should do exactly what you're after (a bit old though, so YMMV with newer django)