Allow a single user to access another specific user's account

I would appreciate help with the concept of how to implement the following.

I have a User A who wants to allow a single additional User B to administer their account. For this example User B should only have access to their own account and User A’s account.

The idea that I have in mind is that User B logs into their own account, where there would be some link to a page called, for example, “other admin”, which displays a list of other accounts that they have permission to administer, or none as the case maybe.

So, User B clicks on the link to administer User A’s account, which outputs a list of items, for example, from A’s AccountDetails model, using a view something like this;

class ListInfo(ListView):
    model = AccountDetails
    template_name = 'account/list_template.html'

    def get_queryset(self):
        qs = AccountDetails.objects.filter(
        return qs

and to make this work, I would have to write some code to;
a. Check that User B is still authorised to access User A’s account
b. Use User A’s pk instead of User B’s for each query.

and, I would have to do this on every single view, probably via a Mixin.

Limiting permissions (add, change, delete, view) is something that I want to implement later, so please ignore it for the purposes of this question.

My question is, am I on the right track here?
Does anyone have any other ideas on a better way of implementing this, or better still the flow or code from a working example?

From the perspective of an overview, I think you’re fine here.

I don’t think you’re on the wrong track. There are multiple ways of addressing something like this, and the choice between them depends upon a number of different factors.

For example:

You don’t want to ignore this when you’re designing it. You want to ensure that whatever you design will allow for it, even if you don’t want to implement it yet.

Other factors to consider that may affect your design:

  • You mention that User A “wants to allow a single additional User B…”. This statement implies to me that User A may not also allow User C to administer their account at the same time. Am I reading that correctly?

  • If User B can manage both User A and User C, do you want this to be a kind of persistent state such that User B can switch among a number of different views - with the system tracking that User B is currently working on User A? Or does User B need to select User A for each administrative page they wish to use?

You will also want to ensure you have a solid definition of what it means for User A to be “administered” by User B. (Trivial example - you probably don’t want User B to be able to reset User A’s password.)

The rough edges are going to show up once you start examining these requirements in detail. The mechanics of the implementation will be relatively easy by comparison.

We took a slightly different approach - or maybe you would say we extended it a little. Rather than adding our Mixins to every view, we created a new set of base classes and defined our views using them.


class SecureListView(Mixin1, Mixin2, Mixin3, ListView):

Then our views are defined as:

class ActivitiesList(SecureListView):

This made it easier for us to verify that all the right mixins are used for the views.

Many thanks Ken,

You are absolutely right regarding permissions and the other factors, I just wanted to keep my question focused on the basic implementation of a second user’s access.

Good, at least I know that I am the right track.

Thanks again Ken.