The process of adding code to verify permissions is similar to that of verifying authentication. Just like authentication, you can verify permissions in both your view functions and template files. The following sections describe this process.
You can use the User.has_perm() function of the User object in the HttpRequest of a view function to determine if the user has specific permissions. Then you can modify the view's behavior, depending on what permissions the user has.
The has_perm() function accepts a string representation of the permission in the format application.permission identifier. If the User object has the permission specified, the has_perm() function returns True. For example, the following line of code returns True if the user object has the can_modify permission for an application called Report:
user.has_perm.('Report.can_modify')
The permission identifier comes from the permission definition. In the case of custom permissions, it is the first string specified in the definition. In the case of the default permissions that are automatically created for the admin interface, the value is add_ with the object name appended. For example, if you defined an object named Person in your model, the identifiers for the add, delete, and change permissions would be add_Person, delete_Person, and change_Person, respectively.
By the Way
You can also determine if the User object has any permissions in an application by using the User.has_module_perms() function. The has_module_perms() function accepts an application name as an argument and returns True if the User object has any permissions set in the module. The following is an example of using the has_module_perms() function:
if user.has_module_perms('Report'):
The following code snippet shows an example of using the has_perm() function in a view to render two completely different templates, depending on what permission the user has:
def blog_view(request): if request.users.has_perm('Blog.blog_edit'): return render_to_response('blog_form.html', {}) else: return render_to_response('blog_form.html', {})
You can also verify user permissions in a template by passing in a RequestContext and then accessing the perms template variable. The perms template variable is an instance of the django.core.context_processors.PermWrapper object, which wraps the permissions together in a template-friendly form.
The permissions can be accessed using the perms variable in two different levels—the module level and the permission level. The module level is equivalent to using the User.has_module_perms() function. For example, to access the permissions for an application named Blog at the module level, you would use the following template code:
{{ perms.Blog }}
The permission level is equivalent to using the User.has_perms() function. For example, to access the can_edit permissions for an application named Blog at the permission level, you would use the following template code:
{{ perms.Blog.can_edit }}
Using the perms variable, you can determine in your template what permissions the user has and render the template based on those permissions. For example, the following code snippet determines if the user has the can_edit permission for the Report application. It displays editable data if the user has permission and view-only data if he doesn't:
{% if perms.Report.can_edit %} {{ reportUpdateForm }} {% else %} {{ viewOnlyData }} {% endif %}
Try It Yourself: Use User Permissions to Limit Access in Views and TemplatesIn this section, you will use the can_blog permission that you created in Hour 14 to limit access to the add_blog() view. You will first modify the person_details.html template to determine if the user has the can_blog permission and see whether the request user matches the userID of the Person object that is being displayed. If those two things don't match, omit the link to the add_blog() view. That will stop people who don't have permission from seeing the link. Next, you will modify the add_blog() function directly to verify that the user has the can_blog permission. If he doesn't, he is redirected to the home page. This will stop users who try to directly access the add_blog() view. You will also modify the add_blog() function to use the User object from the HttpRequest to find the Person object that the Blog will be added to. By the Way We are using a permission created in the Blog class to show that permissions are tied to the module and not the object. The People application has two classes—Blog and Person. However, to access the can_blog permission, you use the People module. Be careful how you name your permissions if the same model has multiple classes so that two classes don't end up with the same permission name. Follow these steps to implement the can_blog permission to limit access to the add_blog() view:
Listing 15.7. Table Column Entry That Displays the Blog List in the iFriends/templates/People/person_details.html File
Listing 15.8. Beginning of the add_blog() View Function in the iFriends/People/views.py File
|