Skip to content

Commit

Permalink
Create super users in WireCloud if the IDM user has the admin role
Browse files Browse the repository at this point in the history
  • Loading branch information
aarranz committed Jun 25, 2018
1 parent 5ff6750 commit efd80a9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/wirecloud/fiware/social_auth_backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2013-2017 Conwet Lab., Universidad Politécnica de Madrid
# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.

# This file is part of Wirecloud.

Expand Down Expand Up @@ -114,11 +115,17 @@ def get_user_details(self, response):
first_name, last_name = name.split(' ', 1)
else:
first_name = name
return {'username': response.get('username'),
'email': response.get('email') or '',
'fullname': name,
'first_name': first_name,
'last_name': last_name}

superuser = any(rol['name'].strip().lower() == "admin" for rol in response.get("roles", []))
return {
'username': response.get('username'),
'email': response.get('email') or '',
'fullname': name,
'first_name': first_name,
'last_name': last_name,
'is_superuser': superuser,
'is_staff': superuser
}

@classmethod
def request_user_info(cls, access_token):
Expand Down
22 changes: 20 additions & 2 deletions src/wirecloud/fiware/tests/social_backend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

# Copyright (c) 2015-2017 Conwet Lab., Universidad Politécnica de Madrid
# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L.

# This file is part of Wirecloud.

Expand Down Expand Up @@ -83,8 +84,9 @@ class TestSocialAuthBackend(WirecloudTestCase):
}]
}

USER_DATA = {"username": "demo", "email": "demo@fiware.org", "fullname": "Demo user", "first_name": "Demo", "last_name": "user"}
USER_DATA_NO_LAST_NAME = {"username": "demo", "email": "demo@fiware.org", "fullname": "Demo", "first_name": "Demo", "last_name": ""}
USER_DATA = {"username": "demo", "email": "demo@fiware.org", "fullname": "Demo user", "first_name": "Demo", "last_name": "user", "is_superuser": False, "is_staff": False}
USER_DATA_ADMIN = {"username": "demo", "email": "demo@fiware.org", "fullname": "Demo user", "first_name": "Demo", "last_name": "user", "is_superuser": True, "is_staff": True}
USER_DATA_NO_LAST_NAME = {"username": "demo", "email": "demo@fiware.org", "fullname": "Demo", "first_name": "Demo", "last_name": "", "is_superuser": False, "is_staff": False}

def setUp(self):
self.social_core = MagicMock()
Expand Down Expand Up @@ -157,6 +159,14 @@ def test_get_user_details_old_version(self):

self.assertEqual(data, self.USER_DATA)

def test_get_user_details_old_version_admin(self):

response = deepcopy(self.OLD_RESPONSE)
response['roles'][0]['name'] = 'admin'
data = self.instance.get_user_details(response)

self.assertEqual(data, self.USER_DATA_ADMIN)

def test_get_user_details_new_version(self):

response = deepcopy(self.NEW_RESPONSE)
Expand All @@ -165,6 +175,14 @@ def test_get_user_details_new_version(self):

self.assertEqual(data, self.USER_DATA)

def test_get_user_details_new_version_admin(self):

response = deepcopy(self.NEW_RESPONSE)
response['roles'][0]['name'] = 'Admin'
data = self.instance.get_user_details(response)

self.assertEqual(data, self.USER_DATA_ADMIN)

def test_get_user_details_no_last_name(self):

response = deepcopy(self.RESPONSE_NO_LAST_NAME)
Expand Down

0 comments on commit efd80a9

Please sign in to comment.