Example #1
0
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))
Example #2
0
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 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))
Example #4
0
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))
Example #5
0
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))
Example #6
0
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 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_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))
Example #9
0
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))
Example #10
0
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))