Skip to content

Feature access permissions#230

Merged
kbeker merged 7 commits intomasterfrom
feature-access-permissions
Jun 3, 2019
Merged

Feature access permissions#230
kbeker merged 7 commits intomasterfrom
feature-access-permissions

Conversation

@rwrzesien
Copy link
Contributor

Resolves #45
Resolves #182
Resolves #183

@rwrzesien rwrzesien force-pushed the feature-access-permissions branch 2 times, most recently from c870c4a to 872df18 Compare May 25, 2019 21:49
@rwrzesien rwrzesien changed the title [WIP] Feature access permissions Feature access permissions May 25, 2019
),
name="dispatch",
)
class ReportDetail(UserIsManagerOfCurrentReportProjectMixin, UserIsAuthorOfCurrentReportMixin, APIView):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwrzesien I've checked that this isn't working as we assupted. Can this work if this is still rest framework view? It even does not use get_queryset method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, but this view is now refactored so it should work just fine.

Copy link
Contributor

@dybi dybi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, seems like a piece of good work. Still, I would have some suggestions / questions.

),
name="dispatch",
)
class ReportList(APIView, UserIsManagerOfCurrentReportProjectMixin, UserIsAuthorOfCurrentReportMixin):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order of parent classes is different in ReportList and ReportDetail. Is it done on purpose? If yes. please explain why. If not, please keep it unified ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad and good catch, should be like in ReportDetail, but still it won't work with APIView which doesn't use get_queryset method, but it is in progress of refactoring to django view by @Szymiks so it can wait for his pull request (or got merged, as it doesn't break anything).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds fine to me ;)

check_permissions(allowed_user_types=[CustomUser.UserType.EMPLOYEE.name, CustomUser.UserType.ADMIN.name]),
name="dispatch",
)
class ReportDeleteView(UserIsAuthorOfCurrentReportMixin, DeleteView):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is because my little experience with django, but this seems a bit complex.

  1. In first decorator (login_required) we enforce user to be logged in
  2. In second one (check_permissions) we check if user is of right type to be able to have access to the resource; if not, we redirect him to login page (?)
  3. Then we modify the queryset of Employee, so only his reports are accessible.

Although the usage of mixin seems pretty clever, I am wondering if there's no easier / simpler way to achieve what we want. Don't get me wrong - I am really not sure, just asking :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing I can think of. Consider this example:

There is a view class that has get_queryset() method with some complex filtering. If you would like to do this in decorator too, you would have to call and evaluate get_queryset() method of decorated class inside decorator (which sounds ugly already) to figure out if returned object match current user/role.

If this would be added in the view code in form of if, it would require overwriting method in each view in which we would like to add it. Sounds like a lot of work.

I think the mixin approach and limiting queryset is the cleanest and most optimized way to do this. At least of all those which I have thought of, but maybe there is a better way I haven't thought of.

The part I don't like with this solution is that it returns 404 instead of 403, but I think its fair trade-off.

if not, we redirect him to login page (?)

Yes, exactly this is done in this decorator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwrzesien Ok, I'am convinced, thanks for the explanation :)

manager_project = ProjectFactory()
manager_project.managers.add(user)
# Project without current user as manager.
ProjectFactory()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly, I would suggest adding check that we have 2 objects in the DB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@rwrzesien rwrzesien force-pushed the feature-access-permissions branch 2 times, most recently from 417feb2 to fd3c9c3 Compare May 31, 2019 23:32
Copy link
Contributor

@dybi dybi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old-spice-guy-head-nod

Copy link
Contributor

@kbeker kbeker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like what you did in this PR. I tested this and this looks and works nice. Merging 👍

@kbeker kbeker force-pushed the feature-access-permissions branch from fd3c9c3 to 2fb7456 Compare June 3, 2019 13:04
@kbeker kbeker force-pushed the feature-access-permissions branch from 2fb7456 to 100363c Compare June 3, 2019 13:05
@kbeker kbeker merged commit 100363c into master Jun 3, 2019
@kbeker kbeker deleted the feature-access-permissions branch June 3, 2019 13:08
@kbeker kbeker added this to the next_release milestone Jun 7, 2019
@kbeker kbeker added the feature New feature label Jun 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

URL to editing report contain report_id URL to editing profile contain user ID Access permissions

3 participants