Repository: akvo/akvo-rsr

Issue:
Inviting and accepting users of content owned organisations
The `organisation.content_owner` field is used in some parts of RSR, but not fully yet. This is the most obvious in the "invite user" functionality (User management in MyRSR) and when approving a user of a content owned organisation.
- [x] In the 'invite users' dropdown, the organisations of which the user's organisation is content owner should also be populated in the list.
- [x] Once a user requests to join an organisation, the users of the content owner (and their content owner, etc) should also receive an email. But only when they're Admin or User manager and when they're user account has 'is_support' set to True.



Discussion:
### Test plan
- [x] Check if content owned organisations are listed in 'invite users'
- [x] Check if support users of content owner are notified if user requests to join child orgnaisation

## Test plan (2)
1. Setup an organisation with can_create_projects (a paying partner).
2. Add a user to this organisation with is_support (a support user).
3. Create a new project with the paying partner and several implementing partners.
4. Check if the support user can invite users for these implementing partners.
5. Register as a new user and request to join one of the implementing partners; check if the mail is sent out to the support user.


Resolving patch:
diff --git a/akvo/rsr/views/my_rsr.py b/akvo/rsr/views/my_rsr.py
--- a/akvo/rsr/views/my_rsr.py
+++ b/akvo/rsr/views/my_rsr.py
@@ -362,13 +362,15 @@ def user_management(request):
     if not user.has_perm('rsr.user_management'):
         raise PermissionDenied
 
+    org_admin = user.approved_employments().filter(group__name='Admins').exists() or \
+        user.is_admin or user.is_superuser
+    groups = ['Users', 'User Managers', 'Project Editors', 'M&E Managers', 'Admins']
+
     if user.is_admin or user.is_superuser:
         # Superusers or RSR Admins can manage and invite someone for any organisation
         employments = Employment.objects.select_related().prefetch_related('group').order_by('-id')
         organisations = Organisation.objects.all()
-        roles = Group.objects.filter(
-            name__in=['Users', 'User Managers', 'Project Editors', 'M&E Managers', 'Admins']
-        )
+        roles = Group.objects.filter(name__in=groups)
     else:
         # Others can only manage or invite users to their own organisation, or the
         # organisations that they content own
@@ -379,7 +381,10 @@ def user_management(request):
         organisations = Organisation.objects.filter(pk__in=connected_orgs_list).\
             content_owned_organisations()
         employments = organisations.employments().exclude(user=user).order_by('-id')
-        roles = Group.objects.filter(name__in=['Users', 'Project Editors'])
+        if org_admin:
+            roles = Group.objects.filter(name__in=groups)
+        else:
+            roles = Group.objects.filter(name__in=groups[:-1])
 
     q = request.GET.get('q')
     if q:
@@ -406,9 +411,14 @@ def user_management(request):
     employments_array = []
     for employment in page:
         employment_dict = model_to_dict(employment)
-        employment_dict['other_groups'] = [
-            model_to_dict(group, fields=['id', 'name']) for group in all_groups
-        ]
+        if org_admin:
+            employment_dict['other_groups'] = [
+                model_to_dict(group, fields=['id', 'name']) for group in all_groups
+            ]
+        else:
+            employment_dict['other_groups'] = [
+                model_to_dict(group, fields=['id', 'name']) for group in all_groups[:-1]
+            ]
         if employment.country:
             employment_dict["country"] = codelist_name(Country, employment, 'country')
         if employment.group:
@@ -439,6 +449,7 @@ def user_management(request):
     context = {}
     if employments_array:
         context['employments'] = json.dumps(employments_array)
+    context['org_admin'] = org_admin
     context['organisations'] = json.dumps(organisations_list)
     context['roles'] = json.dumps(roles_list)
     context['page'] = page


Test patch: