Ejemplo n.º 1
0
def schema_objectclass(ds, objectclass_name):
    if request.method == 'OPTIONS':
        #
        # Return a list of the allowed methods
        #
        msg = {'allow': 'GET PUT DELETE OPTIONS HEAD'}
        return jsonify(msg)

    try:
        objectclass = ds.schema.query_objectclass(objectclass_name)
    except ldap.LDAPError as e:
        return jsonResponse("Error getting objectclass: " % str(e), 403,
                            request.url)

    # This does the right thing regardless of resource type

    if request.method == 'HEAD':
        #
        # Return the HEAD Response
        #
        if objectclass is None:
            return jsonResponse("", 404, request.url)
        return jsonResponse("", 200, request.url)

    if request.method == 'GET':
        if objectclass is None:
            return jsonResponse("Failed to find objectclass", 404, request.url)
        results = {
            'names': objectclass.names,
            'desc': objectclass.desc,
            'kind': objectclass.kind,
            'may': objectclass.may,
            'must': objectclass.must,
            'obsolete': objectclass.obsolete,
            'oid': objectclass.oid,
            'schema_attribute': objectclass.schema_attribute,
            'sup': objectclass.sup,
        }
        return jsonResponse(results, 200, request.url)

    if request.method == 'PUT':
        json_req = request.get_json(force=True)
        if not json_req:
            return jsonResponse("JSON representation missing", 403,
                                request.url)
        newoc = jsonOCToStr(json_req)

        attrObj = Schema(ds)
        try:
            attrObj.add_objectclass(newoc)
        except ldap.TYPE_OR_VALUE_EXISTS:
            # idempotent - return the entry as is
            return jsonResponse(json_req, 201,
                                request.url + '/' + json_req['names'][0])
        except ldap.LDAPError as e:
            return jsonResponse('Failed to add objectclass: %s' % str(e), 403,
                                request.url)

        return jsonResponse(json_req, 201,
                            request.url + '/' + json_req['names'][0])

    if request.method == 'DELETE':
        schemaObj = Schema(ds)
        try:
            delete_val = None
            name_cmp = "'%s'" % objectclass_name

            entry = schemaObj.get_entry()
            ocs = entry.getValues('objectClasses')
            for val in ocs:
                if name_cmp.lower() in val.lower():
                    delete_val = val
                    break

            if delete_val:
                ds.modify_s(DN_SCHEMA, [(ldap.MOD_DELETE, 'objectClasses',
                            delete_val)])
                return jsonResponse('', 200, request.url)
            else:
                # idempotent, return success
                return jsonResponse('', 200, request.url)

        except ldap.LDAPError as e:
            return jsonResponse('Failed to delete objectclass: %s' % str(e),
                                403, request.url)
Ejemplo n.º 2
0
def schema_attribute(ds, attribute_name):

    if request.method == 'OPTIONS':
        #
        # Return a list of the allowed methods
        #
        msg = {'allow': 'GET PUT DELETE OPTIONS HEAD'}
        return jsonify(msg)

    try:
        result = ds.schema.query_attributetype(attribute_name)
    except ldap.LDAPError as e:
        return jsonResponse("Error getting attribute: " % str(e), 403,
                            request.url)

    if request.method == 'HEAD':
        #
        # Return the HEAD Response
        #
        if result is None:
            return jsonResponse("", 404, request.url)
        return jsonResponse("", 200, request.url)

    if request.method == 'GET':
        if result is None:
            return jsonResponse("Failed to find attribute", 404, request.url)
        # Split the tuple
        attribute, must, may = result
        results = {'names': attribute.names,
                   'desc': attribute.desc,
                   'oid': attribute.oid,
                   'ordering': attribute.ordering,
                   'single_value': attribute.single_value,
                   'equality': attribute.equality,
                   'substr': attribute.substr,
                   'syntax': attribute.syntax,
                   'usage': attribute.usage,
                   'obsolete': attribute.obsolete,
                   'no_user_mod': attribute.no_user_mod,
                   'sup': attribute.sup,
                   'must': map(lambda x: x.names[0], must),
                   'may': map(lambda x: x.names[0], may)}
        return jsonResponse(results, 200, request.url)

    if request.method == 'PUT':
        json_req = request.get_json(force=True)
        if not json_req:
            return jsonResponse("JSON representation missing", 403,
                                request.url)
        newattr = jsonAttrToStr(json_req)

        attrObj = Schema(ds)
        try:
            attrObj.add_attribute(newattr)
        except ldap.TYPE_OR_VALUE_EXISTS:
            # idempotent - return the entry as is
            return jsonResponse(json_req, 201,
                                request.url + '/' + json_req['names'][0])

        except ldap.LDAPError as e:
            return jsonResponse('Failed to add attribute: %s' % str(e), 403,
                                request.url)

        return jsonResponse(json_req, 201,
                            request.url + '/' + json_req['names'][0])

    if request.method == 'DELETE':
        schemaObj = Schema(ds)
        try:
            delete_val = None
            name_cmp = "'%s'" % attribute_name

            entry = schemaObj.get_entry()
            attributes = entry.getValues('attributeTypes')
            for val in attributes:
                if name_cmp.lower() in val.lower():
                    delete_val = val
                    break

            if delete_val:
                ds.modify_s(DN_SCHEMA, [(ldap.MOD_DELETE, 'attributeTypes',
                            delete_val)])
                return jsonResponse('', 200, request.url)
            else:
                # Idempotent - no val, already deleted, return success
                return jsonResponse('', 200, request.url)

        except ldap.LDAPError as e:
            return jsonResponse('Failed to delete attribute: %s' % str(e), 403,
                                request.url)