ponder-lab / Imperative-DL-Study-Web-App

0 stars 0 forks source link

Modify templates based on permissions assigned to roles #118

Closed mZneit closed 3 years ago

mZneit commented 3 years ago

For every permission assigned in a view, you need to modify the template to allow the user to access or edit content.

ZhongweiL commented 3 years ago

@mZneit To check user permission, I will need to use the has_permission() function imported from rolepermissions.checkers, should I import it in views.py and pass it to the templates? Or is there any way I can import them directly to the templates?

mZneit commented 3 years ago

From this page, you can use it in the template: https://django-role-permissions.readthedocs.io/en/stable/utils.html#template-tags

ZhongweiL commented 3 years ago

@mZneit user|can:"ponder.view_categorization" is always returning false, it this the correct permission name that I should be checking? I also tried user|can:"view_categorizations" but its also returning false.

mZneit commented 3 years ago

Did you define "user" or pass it in the context to the template? if you didn't then it might not work.

ZhongweiL commented 3 years ago

"user" has been used in other parts of the template, so I assume it has already been defined

mZneit commented 3 years ago

What's the template file name? and what role are you using to view the categorizations?

ZhongweiL commented 3 years ago

index.html, I'm using categorizer

mZneit commented 3 years ago

A categorizer can view the categorizations. If you login locally to the admin page, and go to the Categorizer group, you should have the permission view_categorization added to that group. Or are you removing that permission temporarily for testing?

ZhongweiL commented 3 years ago

The permission is still there and I am still able to access the categorizations page.

mZneit commented 3 years ago

ok I will look into this tonight.

mZneit commented 3 years ago

@ZhongweiL use the condition: {% if perms.ponder.view_categorization %} This worked for me and the "View your categorizations" button was hidden if a user was not granted permission to view the categorizations table.

ZhongweiL commented 3 years ago

@mZneit It worked, thanks. Do you just want to hide the button? Or the heading and the text above it as well?

mZneit commented 3 years ago

Yes the heading and the text should also be hidden. The user with the assigned role shouldn't see anything related to the restricted view.

mZneit commented 3 years ago

@ZhongweiL please see the last commit 249c857e2cd6eacab575fcd952a61c5e725cd8b0 on how to hide columns in a table based on a role. Hiding columns is done in views.py instead of tables.py.

ZhongweiL commented 3 years ago

@mZneit I get the exception 'CommitsTable' object is not callable when I try to do that to the commits table and assign it to table_class

mZneit commented 3 years ago

This is because it's a class-based view. I'll try to find a way to modify the code from tables.py in that case.

mZneit commented 3 years ago

This commit 07c587a2fe55d21442e2e3ccfdde66b9fc6440ab hides a column in a table if the user has no permission to view the column @ZhongweiL

ZhongweiL commented 3 years ago

@mZneit Is the add_form column in the Commits table the only column I should be hiding?

mZneit commented 3 years ago

Yes true.