What You'll Learn in This Hour |
|
Hour 14, "Managing Site Users," covered creating users and groups and setting permissions for both. This hour discusses securing the website by forcing users to be authenticated and logged into the site to gain access to certain pages. It also discusses how to use permissions to verify that users can view and change content.
The first step in adding security to the website is creating User objects so that you know who is trying to access the website. The second step is implementing a login to force users to enter a password before they are admitted to the website.
The login process has three steps. The first is to prompt the user for his or her username and password. The next step is to authenticate the user by verifying that the username and password match a User object that exists in the database. The final step is to log the user into the website by adding the authenticated User object to the user's session. (Sessions are discussed more in Hour 16, "Managing Sessions and Cookies.")
Did you Know?
Django provides a simple way to implement logins using the django.contrib.auth.views.login view. You add the pattern (r'^accounts/login/$', login) to the URLs.py file to enable the view and then create a template named registration/login.html that creates a form. I won't cover this view in more detail here, because it uses the old forms library and likely will change. (See www.djangoproject.com for more details.)
Django provides the django.contrib.auth.authenticate() function to help you verify that usernames and passwords match User objects in the database. The authenticate() function takes the username and password as arguments and then checks them against the database and returns the matching User object if one is found. If no match is found, authenticate() returns None. For example, the following code tries to authenticate a user named Bill:
user = authenticate(username = 'bill', password = 'billPass') if user is not None: #authenticated else: #bad username or password
By the Way
You can also manually check the password using the django.contrib.auth.models.check_password() function. The check_password() function takes a string password as the first argument and the password attribute, containing the password hash, of the User object as the second. It returns True if they match and False if they don't. For example:
check_password('billPass', user.password)
After you use the authenticate() function to get a valid user, you can use the django.contrib.auth.login() functions to actually log the user into the website. The login() function accepts the HttpRequest object as the first argument and the User object as the second. You also should verify that the is_active attribute of the User object is true before calling login(). The following example shows how to log the User object into the session:
if user.is_active: login(request, user) else: #account has been disabled
Try It Yourself: Implement a User Login PageIn this section, you will create a view function and template that prompt users to enter a username and password. The view function checks the username and password against User objects in the system and logs in the user if they match. Follow these steps to create a user login view and enable it to log in users:
By the Way The admin interface uses the same session that the website does. When you log in as a different user, this affects your rights to the admin interface based on what rights the new user has. If you still want to use the admin interface, you may need to log in again as a user with rights to the admin interface. Listing 15.1. Full Contents of iFriends/templates/registration/login.html
Listing 15.2. Imports, LoginForm Definition, and the login_user() Function in the iFriends/Home/views.py File
|