Skip to content

Commit

Permalink
fix error and add admin debug view
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Leinfelder committed Feb 1, 2019
1 parent 7d310e0 commit b622966
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
2 changes: 1 addition & 1 deletion janus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@


__version__ = VERSION = (1, 0, 0)
__version__ = VERSION = (1, 0, 1)
52 changes: 51 additions & 1 deletion janus/admin.py
Original file line number Diff line number Diff line change
@@ -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, \
Expand All @@ -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('''
<p>Debug applied application groups (save to see the update!):
{% for key, value in inline_admin_formset.formset.get_applications.items %}
<p>applicatio "{{ key }}":<br/>
{{ value }}
</p>
{% endfor %}
</p>
''')

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',)

Expand Down
21 changes: 15 additions & 6 deletions janus/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = (
{
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b622966

Please sign in to comment.