Added p.a.users.add_user_to_group() function

This commit is contained in:
2017-05-12 13:37:12 +02:00
parent 7c5aef033d
commit ad9a981cda
3 changed files with 92 additions and 0 deletions

View File

@@ -1,6 +1,43 @@
import logging
import bson
from flask import current_app
from . import hooks
from .routes import blueprint_api
log = logging.getLogger(__name__)
def add_user_to_group(user_id: bson.ObjectId, group_id: bson.ObjectId):
"""Makes the user member of the given group.
Directly uses MongoDB, so that it doesn't require any special permissions.
"""
from pymongo.results import UpdateResult
assert isinstance(user_id, bson.ObjectId)
assert isinstance(group_id, bson.ObjectId)
log.info('Adding user %s to group %s', user_id, group_id)
users_coll = current_app.db('users')
db_user = users_coll.find_one(user_id, projection={'groups': 1})
if db_user is None:
raise ValueError('user %s not found', user_id, group_id)
groups = set(db_user.get('groups', []))
groups.add(group_id)
# Sort the groups so that we have predictable, repeatable results.
result: UpdateResult = users_coll.update_one(
{'_id': db_user['_id']},
{'$set': {'groups': sorted(groups)}})
if result.matched_count == 0:
raise ValueError('Unable to add user %s to group %s; user not found.')
def setup_app(app, api_prefix):
app.on_pre_GET_users += hooks.check_user_access

View File

@@ -359,6 +359,11 @@ class AbstractPillarTest(TestMinimal):
proj_coll = self.app.db()['projects']
return proj_coll.find_one(project_id)
def fetch_user_from_db(self, user_id=ctd.EXAMPLE_USER['_id']):
with self.app.app_context():
users_coll = self.app.db('users')
return users_coll.find_one(user_id)
@staticmethod
def join_url_params(params):
"""Constructs a query string from a dictionary and appends it to a url.