def create_user(): user = request.json # Verify incoming user: email must not exist yet, groups must exist, role must exist if users.find_one({'email': user['email']}) is not None: return jsonify(success=False, reason='user-already-exists') for group_name in user.get('groups', []): if not _check_group_exists(group_name): return jsonify(success=False, reason='unknown-group') if user.get("role") not in ("user", "administrator"): return jsonify(success=False, reason="invalid-role") new_user = { 'id': str(uuid.uuid4()), 'status': 'invited' if user.get('invitation') else 'active', 'email': user['email'], 'name': user.get('name'), 'role': user['role'], 'created': datetime.datetime.utcnow(), 'last_login': None, 'api_key': str(uuid.uuid4()) } users.insert(new_user) # Add the user to the groups - group membership is stored in the group objet, not in the user for group_name in user.get('groups', []): groups.update({'name': group_name}, {'$addToSet': { 'users': user['email'] }}) new_user['groups'] = user.get('groups', []) return jsonify(success=True, user=sanitize_user(new_user))
def update_site(site_id): new_site = request.json # Verify incoming site. It must exist, groups must exist, plans must exist. site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) for group in new_site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in new_site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if 'groups' in new_site: # Add new groups for group_name in new_site.get('groups', []): if group_name not in site['groups']: groups.update({'name':group_name},{'$addToSet': {'sites': site['url']}}) # Remove old groups for group_name in site['groups']: if group_name not in new_site.get('groups', []): groups.update({'name':group_name},{'$pull': {'sites': site['url']}}) if 'plans' in new_site: # Update the site. At this point we can only update plans. sites.update({'id': site_id}, {'$set': {'plans': new_site.get('plans')}}) # Return the updated site site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) return jsonify(success=True, site=sanitize_site(site))
def create_site(): site = request.json # Verify incoming site: url must be valid, groups must exist, plans must exist if not _check_site_url(site.get("url")): return jsonify(success=False, reason="invalid-url") if not _check_required_fields(site, ["url"]): return jsonify(success=False, reason="missing-required-field") for group in site.get("groups", []): if not _check_group_exists(group): return jsonify(success=False, reason="unknown-group") for plan_name in site.get("plans", []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason="unknown-plan") if sites.find_one({"url": site["url"]}) is not None: return jsonify(success=False, reason="site-already-exists") # Create the site new_site = { "id": str(uuid.uuid4()), "url": site["url"], "plans": site.get("plans", []), "created": datetime.datetime.utcnow(), } sites.insert(new_site) # Add the site to the groups - group membership is stored in the group object, not in the site for group_name in site.get("groups", []): # No need to check if the site is already in the group as we just added the site groups.update({"name": group_name}, {"$addToSet": {"sites": site["url"]}}) new_site["groups"] = site.get("groups", []) # Return the new site return jsonify(success=True, site=sanitize_site(new_site))
def update_site(site_id): new_site = request.json # Verify incoming site. It must exist, groups must exist, plans must exist. site = sites.find_one({"id": site_id}) if not site: return jsonify(success=False, reason="no-such-site") site["groups"] = _find_groups_for_site(site["url"]) for group in new_site.get("groups", []): if not _check_group_exists(group): return jsonify(success=False, reason="unknown-group") for plan_name in new_site.get("plans", []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason="unknown-plan") if "groups" in new_site: # Add new groups for group_name in new_site.get("groups", []): if group_name not in site["groups"]: groups.update({"name": group_name}, {"$addToSet": {"sites": site["url"]}}) # Remove old groups for group_name in site["groups"]: if group_name not in new_site.get("groups", []): groups.update({"name": group_name}, {"$pull": {"sites": site["url"]}}) if "plans" in new_site: # Update the site. At this point we can only update plans. sites.update({"id": site_id}, {"$set": {"plans": new_site.get("plans")}}) # Return the updated site site = sites.find_one({"id": site_id}) if not site: return jsonify(success=False, reason="no-such-site") site["groups"] = _find_groups_for_site(site["url"]) return jsonify(success=True, site=sanitize_site(site))
def create_user(): user = request.json # Verify incoming user: email must not exist yet, groups must exist, role must exist if users.find_one({'email': user['email']}) is not None: return jsonify(success=False, reason='user-already-exists') for group_name in user.get('groups', []): if not _check_group_exists(group_name): return jsonify(success=False, reason='unknown-group') if user.get("role") not in ("user", "administrator"): return jsonify(success=False, reason="invalid-role") new_user = { 'id': str(uuid.uuid4()), 'status': 'invited' if user.get('invitation') else 'active', 'email': user['email'], 'name': user.get('name'), 'role': user['role'], 'created': datetime.datetime.utcnow(), 'last_login': None } users.insert(new_user) # Add the user to the groups - group membership is stored in the group objet, not in the user for group_name in user.get('groups', []): groups.update({'name':group_name},{'$addToSet': {'users': user['email']}}) new_user['groups'] = user.get('groups', []) return jsonify(success=True, user=sanitize_user(new_user))
def create_site(): site = request.json # Verify incoming site: url must be valid, groups must exist, plans must exist if not _check_site_url(site.get('url')): return jsonify(success=False, reason='invalid-url') if not _check_required_fields(site, ['url']): return jsonify(success=False, reason='missing-required-field') for group in site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if sites.find_one({'url': site['url']}) is not None: return jsonify(success=False, reason='site-already-exists') # Create the site new_site = { 'id': str(uuid.uuid4()), 'url': site['url'], 'plans': site.get('plans', []), 'created': datetime.datetime.utcnow() } sites.insert(new_site) # Add the site to the groups - group membership is stored in the group object, not in the site for group_name in site.get('groups', []): # No need to check if the site is already in the group as we just added the site groups.update({'name':group_name},{'$addToSet': {'sites': site['url']}}) new_site['groups'] = site.get('groups', []) # Return the new site return jsonify(success=True, site=sanitize_site(new_site))
def update_group_association(old_email, new_email): """ Update all associations with the old email to the new email. """ groups.update({'users': old_email}, {'$set': {'users.$': new_email}}, upsert=False, multi=True)
def update_site(site_id): new_site = request.json # Verify incoming site. It must exist, groups must exist, plans must exist. site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) for group in new_site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in new_site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if 'groups' in new_site: # Add new groups for group_name in new_site.get('groups', []): if group_name not in site['groups']: groups.update({'name': group_name}, {'$addToSet': { 'sites': site['url'] }}) # Remove old groups for group_name in site['groups']: if group_name not in new_site.get('groups', []): groups.update({'name': group_name}, {'$pull': { 'sites': site['url'] }}) if 'plans' in new_site: # Update the site. At this point we can only update plans. sites.update({'id': site_id}, {'$set': { 'plans': new_site.get('plans') }}) new_verification = new_site['verification'] old_verification = site.get('verification') # if site doesn't have 'verification', do us a favor, update the document as it is outdated! if not old_verification or old_verification['enabled'] != new_verification[ 'enabled']: # to make logic simpler, even if the new request wants to # disable verification, generate a new value anyway. sites.update({'id': site_id}, { '$set': { 'verification': { 'enabled': new_verification['enabled'], 'value': str(uuid.uuid4()) } } }) # Return the updated site site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) return jsonify(success=True, site=sanitize_site(site))
def update_group_association(old_email, new_email): """ Update all associations with the old email to the new email. """ groups.update({'users': old_email}, {'$set': { 'users.$': new_email }}, upsert=False, multi=True)
def delete_user(user_email): user = users.find_one({'email': user_email}) if not user: return jsonify(success=False, reason='no-such-user') # Remove the user users.remove({'email': user_email}) # Remove user group membership for group_name in _find_groups_for_user(user_email): groups.update({'name':group_name},{'$pull': {'users': user_email}}) return jsonify(success=True)
def update_user(user_email): new_user = request.json # Verify the incoming user: user must exist, groups must exist, role must exist old_user = users.find_one({'email': user_email}) if old_user is None: return jsonify(success=False, reason='unknown-user') old_user['groups'] = _find_groups_for_user(user_email) old_user['sites'] = _find_sites_for_user(user_email) if 'groups' in new_user: for group_name in new_user.get('groups', []): if not _check_group_exists(group_name): return jsonify(success=False, reason='unknown-group') if 'role' in new_user: if new_user["role"] not in ("user", "administrator"): return jsonify(success=False, reason="invalid-role") if 'status' in new_user: if new_user['status'] not in ('active', 'banned'): return jsonify(success=False, reason='unknown-status-option') # Update the group memberships if 'groups' in new_user: # Add new groups for group_name in new_user.get('groups', []): if group_name not in old_user['groups']: groups.update({'name': group_name}, {'$addToSet': { 'users': user_email }}) # Remove old groups for group_name in old_user['groups']: if group_name not in new_user.get('groups', []): groups.update({'name': group_name}, {'$pull': { 'users': user_email }}) # Modify the user changes = {} if 'name' in new_user: changes['name'] = new_user['name'] if 'role' in new_user: changes['role'] = new_user['role'] if 'groups' in new_user: changes['groups'] = new_user['groups'] if 'status' in new_user: changes['status'] = new_user['status'] users.update({'email': user_email}, {'$set': changes}) # Return the updated user user = users.find_one({'email': user_email}) if not user: return jsonify(success=False, reason='unknown-user') user['groups'] = _find_groups_for_user(user_email) return jsonify(success=True, user=sanitize_user(user))
def remove_group_association(email): """ Remove all associations with the recipient. This is required for a declined invitation or when a user is banned or deleted. In case we have found a user in the same membership list multiple time (should not happen), we better to pull all the occurences out. Hence why we use $pull over $pop.""" groups.update({'users': email}, {'$pull': {'users': email}}, upsert=False, multi=True)
def delete_invite(id): invitation = invites.find_one({'id': id}) if not invitation: return jsonify(success=False, reason='no-such-invitation') # do not delete users that are not invite pending (bug #123) email = invitation['recipient'] user = users.find_one({'email': email}) if user and user.get('status') == "invited": users.remove(user) # bug #133 delete user associations for group_name in _find_groups_for_user(email): groups.update({'name':group_name}, {'$pull': {'users': email}}) for site in _find_sites_for_user(email): sites.update({'url':site}, {'$pull': {'users': email}}) invites.remove({'id': id}) return jsonify(success=True)
def remove_group_association(email): """ Remove all associations with the recipient. This is required for a declined invitation or when a user is banned or deleted. In case we have found a user in the same membership list multiple time (should not happen), we better to pull all the occurences out. Hence why we use $pull over $pop.""" groups.update({'users': email}, {'$pull': { 'users': email }}, upsert=False, multi=True)
def update_user(user_email): new_user = request.json # Verify the incoming user: user must exist, groups must exist, role must exist old_user = users.find_one({'email': user_email}) if old_user is None: return jsonify(success=False, reason='unknown-user') old_user['groups'] = _find_groups_for_user(user_email) old_user['sites'] = _find_sites_for_user(user_email) if 'groups' in new_user: for group_name in new_user.get('groups', []): if not _check_group_exists(group_name): return jsonify(success=False, reason='unknown-group') if 'role' in new_user: if new_user["role"] not in ("user", "administrator"): return jsonify(success=False, reason="invalid-role") if 'status' in new_user: if new_user['status'] not in ('active', 'banned'): return jsonify(success=False, reason='unknown-status-option') # Update the group memberships if 'groups' in new_user: # Add new groups for group_name in new_user.get('groups', []): if group_name not in old_user['groups']: groups.update({'name':group_name},{'$addToSet': {'users': user_email}}) # Remove old groups for group_name in old_user['groups']: if group_name not in new_user.get('groups', []): groups.update({'name':group_name},{'$pull': {'users': user_email}}) # Modify the user changes = {} if 'name' in new_user: changes['name'] = new_user['name'] if 'role' in new_user: changes['role'] = new_user['role'] if 'groups' in new_user: changes['groups'] = new_user['groups'] if 'status' in new_user: changes['status'] = new_user['status'] users.update({'email': user_email}, {'$set': changes}) # Return the updated user user = users.find_one({'email': user_email}) if not user: return jsonify(success=False, reason='unknown-user') user['groups'] = _find_groups_for_user(user_email) return jsonify(success=True, user=sanitize_user(user))
def update_site(site_id): new_site = request.json # Verify incoming site. It must exist, groups must exist, plans must exist. site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) for group in new_site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in new_site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if 'groups' in new_site: # Add new groups for group_name in new_site.get('groups', []): if group_name not in site['groups']: groups.update({'name':group_name},{'$addToSet': {'sites': site['url']}}) # Remove old groups for group_name in site['groups']: if group_name not in new_site.get('groups', []): groups.update({'name':group_name},{'$pull': {'sites': site['url']}}) if 'plans' in new_site: # Update the site. At this point we can only update plans. sites.update({'id': site_id}, {'$set': {'plans': new_site.get('plans')}}) new_verification = new_site['verification'] old_verification = site.get('verification') # if site doesn't have 'verification', do us a favor, update the document as it is outdated! if not old_verification or old_verification['enabled'] != new_verification['enabled']: # to make logic simpler, even if the new request wants to # disable verification, generate a new value anyway. sites.update({'id': site_id}, {'$set': { 'verification': { 'enabled': new_verification['enabled'], 'value': str(uuid.uuid4())}}}) # Return the updated site site = sites.find_one({'id': site_id}) if not site: return jsonify(success=False, reason='no-such-site') site['groups'] = _find_groups_for_site(site['url']) return jsonify(success=True, site=sanitize_site(site))
def create_site(): site = request.json # Verify incoming site: url must be valid, groups must exist, plans must exist if not _check_site_url(site.get('url')): return jsonify(success=False, reason='invalid-url') if not _check_required_fields(site, ['url']): return jsonify(success=False, reason='missing-required-field') for group in site.get('groups', []): if not _check_group_exists(group): return jsonify(success=False, reason='unknown-group') for plan_name in site.get('plans', []): if not _check_plan_exists(plan_name): return jsonify(success=False, reason='unknown-plan') if sites.find_one({'url': site['url']}) is not None: return jsonify(success=False, reason='site-already-exists') # Create the site new_site = { 'id': str(uuid.uuid4()), 'url': site['url'], 'plans': site.get('plans', []), 'created': datetime.datetime.utcnow() } if site.get('verification', {}).get('enabled', False): new_site['verification'] = { 'enabled': True, 'value': str(uuid.uuid4()) } else: new_site['verification'] = {'enabled': False, 'value': None} sites.insert(new_site) # Add the site to the groups - group membership is stored in the group object, not in the site for group_name in site.get('groups', []): # No need to check if the site is already in the group as we just added the site groups.update({'name': group_name}, {'$addToSet': { 'sites': site['url'] }}) new_site['groups'] = site.get('groups', []) # Return the new site return jsonify(success=True, site=sanitize_site(new_site))
def patch_group(group_name): group = groups.find_one({'name': group_name}) if not group: return jsonify(success=False, reason='no-such-group') # Process the edits. These can probably be done in one operation. patch = request.json for site in patch.get('addSites', []): if isinstance(site, unicode) or isinstance(site, str): groups.update({'name': group_name}, {'$push': {'sites': site}}) for site in patch.get('removeSites', []): if isinstance(site, unicode) or isinstance(site, str): groups.update({'name': group_name}, {'$pull': {'sites': site}}) for user in patch.get('addUsers', []): if isinstance(user, unicode) or isinstance(user, str): groups.update({'name': group_name}, {'$push': {'users': user}}) for user in patch.get('removeUsers', []): if isinstance(user, unicode) or isinstance(user, str): groups.update({'name': group_name}, {'$pull': {'users': user}}) # Return the modified group group = groups.find_one({'name': group_name}) return jsonify(success=True, group=sanitize_group(group))
def patch_group(group_name): group = groups.find_one({"name": group_name}) if not group: return jsonify(success=False, reason="no-such-group") # Process the edits. These can probably be done in one operation. patch = request.json for site in patch.get("addSites", []): if isinstance(site, unicode) or isinstance(site, str): groups.update({"name": group_name}, {"$push": {"sites": site}}) for site in patch.get("removeSites", []): if isinstance(site, unicode) or isinstance(site, str): groups.update({"name": group_name}, {"$pull": {"sites": site}}) for user in patch.get("addUsers", []): if isinstance(user, unicode) or isinstance(user, str): groups.update({"name": group_name}, {"$push": {"users": user}}) for user in patch.get("removeUsers", []): if isinstance(user, unicode) or isinstance(user, str): groups.update({"name": group_name}, {"$pull": {"users": user}}) # Return the modified group group = groups.find_one({"name": group_name}) return jsonify(success=True, group=sanitize_group(group))
def patch_group(group_name): group = groups.find_one({'name': group_name}) if not group: return jsonify(success=False, reason='no-such-group') # Process the edits. These can probably be done in one operation. patch = request.json for site in patch.get('addSites', []): if isinstance(site, unicode): groups.update({'name':group_name},{'$push': {'sites': site}}) for site in patch.get('removeSites', []): if isinstance(site, unicode): groups.update({'name':group_name},{'$pull': {'sites': site}}) for user in patch.get('addUsers', []): if isinstance(user, unicode): groups.update({'name':group_name},{'$push': {'users': user}}) for user in patch.get('removeUsers', []): if isinstance(user, unicode): groups.update({'name':group_name},{'$pull': {'users': user}}) # Return the modified group group = groups.find_one({'name': group_name}) return jsonify(success=True, group=sanitize_group(group))