-
-
Notifications
You must be signed in to change notification settings - Fork 50
Question - Permissions do not seem to be taking effect #81
Comments
Remove
|
Additionally, you probably need to use |
I read StackOverflow as well and commented https://stackoverflow.com/a/49677458/1273406 |
Hi - thanks for the help! Just to clarify what I meant by the permissions in the admin section - I meant to say that I granted permissions in the built in Django Administration site to the users that I am testing with. User 1 - has been granted all the standard django permissions in the Admin site (add/change/delete) I do not think this is related as User 2 is still able to edit User 1's articles (despite not having any standard Django permissions) Have tried to fix the errors you pointed out - but am not having much luck. The changes I have made are:
|
Did you mean "User2 cannot change an article through admin site but User2 can change an article through your I'm not sure but your from django.core.exceptions import PermissionDenied
def change_article(request, *args, **kwargs):
pk = kwargs.pop('pk')
template = 'test_app/edit.html'
article = models.Article.objects.get(id=pk)
# Check permission
if not request.user.has_perm('test_app.change_article', article):
raise PermissionDenied
if request.method == 'POST':
form = forms.Article_form(request.POST, instance=article)
if form.is_valid():
article = form.save(commit=False)
article.created_by = request.user
article.title = form.cleaned_data['title']
article.content = form.cleaned_data['content']
article.save()
return HttpResponseRedirect('/test/')
else:
raise Http404
else:
form = forms.Article_form(instance=article)
return render(request, template_name=template, context={'form':form}) Anyway, you should use class-based generic view instead. |
I meant that User 2 has not been granted any permissions via the admin site and was able to edit via the change_article view. It was just to say that the Django permissions did not seem to have any effect on the behavior. The extra code that you sent through works exactly as I expect it to work! Great Can you let me know what you mean by
Is this something I can fix in the model definition so that the decorator works as expected? Yes - I am getting the feeling more and more that I have to work out how CBVs work. All the apps I want to use/ examples I find have CBVs. It is just that I a bit of a NOOB still :) |
What is your
The functional decorator exists for a historical reason and it uses If you use class-based generic view, django-permission can get an object from a |
Now I think I got why you failed to use the decorator. If you've used a correct one, you would noticed |
I think we are talking about different things regarding the Admin. I think we can drop this discussion as it does not seem to be relevant for the issue that I was trying to figure out (I just mentioned it in case it was relevant). For the decorator - I tried the following Changed the variable used in the url configuration from "pk" to "object_id" urls.py
I made sure that the permission_required was the correct one views.py Even with these changes - it does not seem to function as intended |
Sorry for the delay. I was a bit busy. Well, the Again, the functional based view has a lot of problem like this so I strongly recommend you to switch class-based generic view or check permission by your self like #81 (comment) |
Thanks very much for that - will try and figure it out (or better learn CBV!). Will accept your answer in Stackoverflow and link back the discussion to this thread. Thanks again. D |
Hello,
Have posted the same question on stackoverflow in case that is a more appropriate place to post it.
Have prepared a simple test app (almost identical to the example used in the docs) to try to figure out how it works. I have read the documentation and tried to use the example app provided on this link.
The issue is when the author of an article is not able to edit/ delete the article.
The user in question has been granted all permissions in the admin section.
Key code listed below - any help much appreciated
test_app/models.py
test_app/views.py
test_app/perms.py
The text was updated successfully, but these errors were encountered: