map roles and db permissions

in the db, we have several rows for single permissions - we separate
`send_messages` into `send_texts`, `send_emails` and `send_letters`,
and also `manage_service` into `manage_users` and `manage_settings`.

But on the front end we don't do anything with this distinction. It's
unhelpful for us to have to think about permissions as groups of things
when we can never split them up at all. So we should combine them. This
commit makes sure:
* when user models are read  (from JSON direct from the API), we
  should transform them from db permissions into roles.
* when permissions are persisted (editing permissions, and creating
  invites), we should send db permissions to the API.

All other interaction with permissions (should just be the endpoint
decorator and checks in html templates generally) should use admin
roles.
This commit is contained in:
Leo Hemsted
2018-02-28 15:37:49 +00:00
parent bd54dbb40c
commit 17061e0d06
7 changed files with 117 additions and 22 deletions

View File

@@ -1,7 +1,10 @@
from notifications_python_client.errors import HTTPError
from app.notify_client import NotifyAdminAPIClient
from app.notify_client.models import User
from app.notify_client.models import (
User,
translate_permissions_from_admin_roles_to_db,
)
ALLOWED_ATTRIBUTES = {
'name',
@@ -142,8 +145,9 @@ class UserApiClient(NotifyAdminAPIClient):
return [User(data) for data in resp['data']]
def add_user_to_service(self, service_id, user_id, permissions):
# permissions passed in are the combined admin roles, not db permissions
endpoint = '/service/{}/users/{}'.format(service_id, user_id)
data = [{'permission': x} for x in permissions]
data = [{'permission': x} for x in translate_permissions_from_admin_roles_to_db(permissions)]
resp = self.post(endpoint, data=data)
return User(resp['data'], max_failed_login_count=self.max_failed_login_count)
@@ -152,7 +156,8 @@ class UserApiClient(NotifyAdminAPIClient):
return User(resp['data'], max_failed_login_count=self.max_failed_login_count)
def set_user_permissions(self, user_id, service_id, permissions):
data = [{'permission': x} for x in permissions]
# permissions passed in are the combined admin roles, not db permissions
data = [{'permission': x} for x in translate_permissions_from_admin_roles_to_db(permissions)]
endpoint = '/user/{}/service/{}/permission'.format(user_id, service_id)
self.post(endpoint, data=data)