mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-02 17:48:50 -04:00
Extend user client to count users with permission
One of the things we need to know for a service to go live is whether they have at least two users with the ‘manage service’ permission. So this commit adds a method to the client to count how many users have a given permission. We can do logic on this count later. But having the counting done in the client feels like a cleaner separation of concerns. Meant some refactoring of the way `service_id` is extracted from the request, in order to make it easier to mock.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from itertools import chain
|
||||
from flask_login import UserMixin, AnonymousUserMixin
|
||||
from flask import session
|
||||
from flask import request, session
|
||||
|
||||
|
||||
roles = {
|
||||
@@ -13,6 +13,10 @@ roles = {
|
||||
all_permissions = set(chain.from_iterable(roles.values())) | {'view_activity'}
|
||||
|
||||
|
||||
def _get_service_id_from_view_args():
|
||||
return request.view_args.get('service_id', None)
|
||||
|
||||
|
||||
class User(UserMixin):
|
||||
def __init__(self, fields, max_failed_login_count=3):
|
||||
self._id = fields.get('id')
|
||||
@@ -117,9 +121,8 @@ class User(UserMixin):
|
||||
if admin_override and not permissions:
|
||||
return False
|
||||
|
||||
from flask import request
|
||||
# Service id is always set on the request for service specific views.
|
||||
service_id = request.view_args.get('service_id', None)
|
||||
service_id = _get_service_id_from_view_args()
|
||||
if service_id in self._permissions:
|
||||
if any_:
|
||||
return any([x in self._permissions[service_id] for x in permissions])
|
||||
|
||||
@@ -131,6 +131,12 @@ class UserApiClient(NotifyAdminAPIClient):
|
||||
resp = self.get(endpoint)
|
||||
return [User(data) for data in resp['data']]
|
||||
|
||||
def get_count_of_users_with_permission(self, service_id, permission):
|
||||
return len([
|
||||
user for user in self.get_users_for_service(service_id)
|
||||
if user.has_permissions(permission, any_=True)
|
||||
])
|
||||
|
||||
def add_user_to_service(self, service_id, user_id, permissions):
|
||||
endpoint = '/service/{}/users/{}'.format(service_id, user_id)
|
||||
data = [{'permission': x} for x in permissions]
|
||||
|
||||
Reference in New Issue
Block a user