diff --git a/janus/__init__.py b/janus/__init__.py index 26df8dc..20fe805 100644 --- a/janus/__init__.py +++ b/janus/__init__.py @@ -1,3 +1,3 @@ -__version__ = VERSION = (1, 0, 0) \ No newline at end of file +__version__ = VERSION = (1, 0, 1) \ No newline at end of file diff --git a/janus/admin.py b/janus/admin.py index 90614bb..3f8686c 100644 --- a/janus/admin.py +++ b/janus/admin.py @@ -1,5 +1,9 @@ +import json + from django.contrib import admin from django.contrib.auth import get_user_model +from django.template import Template +from fakeinline.datastructures import FakeFormSet, FakeInline, FakeForm from oauth2_provider.admin import Application, ApplicationAdmin from janus.models import Profile, ApplicationGroup, ProfilePermission, GroupPermission, ProfileGroup, \ @@ -19,10 +23,56 @@ class ProfileInline(admin.StackedInline): verbose_name_plural = 'Profile' +###### extent admin for easy user debug + +class FakeFormNew(FakeForm): + def is_multipart(self): + return False + +class FakeFormSetNew(FakeFormSet): + form = FakeFormNew + empty_form = FakeFormNew + +class ApplicationGroupFormSet(FakeFormSetNew): + # this probably works, but usually you'd point it at a template file. + template = Template(''' +

Debug applied application groups (save to see the update!): + {% for key, value in inline_admin_formset.formset.get_applications.items %} +

applicatio "{{ key }}":
+ {{ value }} +

+ {% endfor %} + + +

+ ''') + + def get_applications(self): + user = self.instance + + from janus.views import ProfileView + pv = ProfileView() + + ret = {} + + from oauth2_provider.models import Application as Application2 + + applications = Application2.objects.all() + + for application in applications: + ret[application.name] = pv.get_group_list(user, application) + + return ret + + +class ApplicationGroups(FakeInline): + formset = ApplicationGroupFormSet + + # Define a new User admin # noinspection PyRedeclaration class UserAdmin(UserAdmin): - inlines = (ProfileInline,) + inlines = (ProfileInline, ApplicationGroups) list_display = UserAdmin.list_display + ('profile_groups',) diff --git a/janus/views.py b/janus/views.py index cc3eb1a..4855a42 100644 --- a/janus/views.py +++ b/janus/views.py @@ -122,6 +122,15 @@ def get_profile_personal_memberships(self, user, application): return group_list + def get_group_list(self, user, application): + + groups = set() + groups = groups.union(self.get_profile_group_memberships(user, application)) + groups = groups.union(self.get_profile_personal_memberships(user, application)) + + return list(groups) + + def get(self, request): if request.resource_owner: user = request.resource_owner @@ -134,13 +143,15 @@ def get(self, request): access_token = access_token.replace("Bearer ", "") token = AccessToken.objects.filter(token=access_token).first() + user = token.user + application = token.application if not token: return self.error_response(OAuthToolkitError("No access token")) - is_superuser, can_authenticate = self.get_group_permissions(token.user, token.application) + is_superuser, can_authenticate = self.get_group_permissions(user, application) # if set the personal settings overwrite the user settings - pp_superuser, pp_authenticate = self.get_personal_permissions(token.user, token.application) + pp_superuser, pp_authenticate = self.get_personal_permissions(user, application) if pp_superuser is not None: if type(pp_superuser) is bool: is_superuser = pp_superuser @@ -149,9 +160,7 @@ def get(self, request): if type(pp_authenticate) is bool: can_authenticate = pp_authenticate - groups = set() - groups.union(self.get_profile_group_memberships(token.user, token.application)) - groups.union(self.get_profile_personal_memberships(token.user, token.application)) + groups = self.get_group_list(user, application) json_data = ( { @@ -164,7 +173,7 @@ def get(self, request): 'email_verified': True, 'is_superuser': is_superuser, 'can_authenticate': can_authenticate, - 'groups': list(groups), + 'groups': groups, } ) json_data = self._replace_json_ids(json_data, token) diff --git a/setup.py b/setup.py index 89518b0..bca6dc2 100644 --- a/setup.py +++ b/setup.py @@ -19,6 +19,7 @@ "django-cors-middleware>=1.3.1", "django_python3_ldap>=0.11.2", "django-allauth>=0.38.0", + "django-fakeinline==0.1.1", ], classifiers=[ "Development Status :: 5 - Production/Stable",