Ejemplo n.º 1
0
    def get(self, root_node_id):
        args = self.get_parser.parse_args()

        max_depth = args['max_depth']
        specific_resources_filter = jsonify_argument(args['sections_filter']) or {}
        annotations_filter = jsonify_argument(args['annotations_filter']) or {}

        root_node_projection = projection_helper.modified_projection(
            jsonify_argument(args['root_node_projection']), mandatory_attrs=['_id'])
        _validate_projection(root_node_projection)
        specific_resources_projection = projection_helper.modified_projection(
            jsonify_argument(args['sections_projection']), mandatory_attrs=['_id'])
        _validate_projection(specific_resources_projection)
        annotations_projection = projection_helper.modified_projection(
            jsonify_argument(args['annotations_projection']), mandatory_attrs=['_id'])
        _validate_projection(annotations_projection)

        try:
            tree = objstore_helper.read_tree(
                g.objstore_colln, root_node_id, max_depth, current_token.user_id, current_token.group_ids,
                specific_resources_filter=specific_resources_filter,
                annotations_filter=annotations_filter,
                root_node_projection=root_node_projection,
                specific_resources_projection=specific_resources_projection,
                annotations_projection=annotations_projection
            )
        except ObjModelException as e:
            return error_response(message=e.message, code=e.http_response_code)

        return tree
Ejemplo n.º 2
0
    def get(self, user_id):
        args = self.get_parser.parse_args()
        user_selector_doc = users_helper.get_user_selector_doc(_id=user_id)

        projection = jsonify_argument(args.get('projection', None),
                                      key='projection')
        check_argument_type(projection, (dict, ),
                            key='projection',
                            allow_none=True)
        _validate_projection(projection)
        projection = projection_helper.modified_projection(
            projection, mandatory_attrs=["_id", "jsonClass"])

        user = JsonObject.make_from_dict(
            g.users_colln.find_one(user_selector_doc, projection=None))
        if user is None:
            return error_response(message='user not found', code=404)

        if not PermissionResolver.resolve_permission(
                user, ObjectPermissions.READ, current_token.user_id,
                current_token.group_ids, g.users_colln):
            return error_response(message='permission denied', code=403)

        user_json = user.to_json_map()
        projected_user_json = users_helper.project_user_json(
            user_json, projection=projection)
        return projected_user_json
Ejemplo n.º 3
0
    def post(self):
        args = self.post_parser.parse_args()

        if g.current_user_id:
            return error_response(message='you are already registered',
                                  code=403)

        user_json = jsonify_argument(args['user_json'],
                                     key='user_json')  # type: dict
        check_argument_type(user_json, (dict, ), key='user_json')

        return_projection = jsonify_argument(args.get('return_projection',
                                                      None),
                                             key='return_projection')
        check_argument_type(return_projection, (dict, ),
                            key='return_projection',
                            allow_none=True)
        _validate_projection(return_projection)
        return_projection = projection_helper.modified_projection(
            return_projection, mandatory_attrs=['_id', 'jsonClass'])

        try:
            new_user_id = users_helper.create_new_user(
                g.users_colln, user_json, initial_agents=g.initial_agents)
        except ObjModelException as e:
            return error_response(message=e.message, code=e.http_response_code)
        except ValidationError as e:
            return error_response(message='invalid schema for user_json',
                                  code=403,
                                  details={"error": str(e)})

        new_user_json = g.users_colln.get(new_user_id,
                                          projection=return_projection)
        return new_user_json
Ejemplo n.º 4
0
    def get(self):
        args = self.get_parser.parse_args()

        projection = jsonify_argument(args.get('projection', None),
                                      key='projection')
        check_argument_type(projection, (dict, ),
                            key='projection',
                            allow_none=True)
        _validate_projection(projection)
        projection = projection_helper.modified_projection(
            projection, mandatory_attrs=["_id", "jsonClass"])

        args_copy = args.copy()
        args_copy.pop('projection', None)
        user_jsons = get_requested_agents(args_copy,
                                          g.users_colln,
                                          current_token.user_id,
                                          current_token.group_ids,
                                          filter_doc={"jsonClass": "User"})

        projected_user_jsons = [
            users_helper.project_user_json(uj, projection=projection)
            for uj in user_jsons
        ]
        return projected_user_jsons
Ejemplo n.º 5
0
    def post(self):
        args = self.post_parser.parse_args()
        args = parse_json_args(args, self.post_json_parse_directives)

        resource_jsons = args['resource_jsons']
        return_projection = args['return_projection']

        created_resource_jsons = []
        for n, rj in enumerate(resource_jsons):
            try:
                if 'jsonClass' not in rj:
                    raise ObjModelException('jsonClass attribute should exist for update/creation', 403)

                created_resource_id = objstore_helper.create_or_update(
                    g.objstore_colln, rj,
                    current_token.user_id, current_token.group_ids, initial_agents=g.initial_agents)

                if created_resource_id is None:
                    created_resource_jsons.append(None)
                    continue
                created_resource_json = g.objstore_colln.get(
                    created_resource_id,
                    projection=projection_helper.modified_projection(return_projection, ["_id", "jsonClass"]))
                created_resource_jsons.append(created_resource_json)

            except ObjModelException as e:
                return error_response(
                    message='action not allowed at resource {}'.format(n),
                    code=e.http_response_code, details={"error": e.message})
            except (ValidationError, TypeError) as e:
                return error_response(
                    message='schema validation error at resource {}'.format(n),
                    code=400, details={"error": str(e)})

        return created_resource_jsons
Ejemplo n.º 6
0
    def get(self, group_identifier):
        args = self.get_parser.parse_args()

        identifier_type = args.get('identifier_type', '_id')
        group_selector_doc = self._group_selector_doc(group_identifier,
                                                      identifier_type)

        projection = jsonify_argument(args.get('projection', None),
                                      key='projection')
        check_argument_type(projection, (dict, ),
                            key='projection',
                            allow_none=True)
        _validate_projection(projection)
        projection = projection_helper.modified_projection(
            projection, mandatory_attrs=["_id", "jsonClass"])

        group = JsonObject.make_from_dict(
            g.users_colln.find_one(group_selector_doc, projection=None))
        if group is None:
            return error_response(message='group not found', code=404)

        if not PermissionResolver.resolve_permission(
                group, ObjectPermissions.READ, current_token.user_id,
                current_token.group_ids, g.users_colln):
            return error_response(message='permission denied', code=403)

        group_json = group.to_json_map()
        projected_group_json = projection_helper.project_doc(
            group_json, projection)
        return projected_group_json
Ejemplo n.º 7
0
    def post(self, group_identifier):
        args = self.get_parser.parse_args()

        identifier_type = args.get('identifier_type', '_id')
        group_selector_doc = self._group_selector_doc(group_identifier,
                                                      identifier_type)

        update_doc = jsonify_argument(args['update_doc'], key='update_doc')
        check_argument_type(update_doc, (dict, ), key='update_doc')

        if 'jsonClass' not in update_doc:
            update_doc['jsonClass'] = 'UsersGroup'
        if update_doc['jsonClass'] != 'UsersGroup':
            return error_response(message='invalid jsonClass', code=403)

        if identifier_type == '_id':
            update_doc.pop('groupName', None)
            if update_doc['_id'] != group_identifier:
                return error_response(message='invalid user_id', code=403)
        else:
            group_id = groups_helper.get_group_id(g.users_colln,
                                                  group_identifier)
            if not group_id:
                return error_response(message='no group with group_name {}'.
                                      format(group_identifier))
            update_doc['_id'] = group_id

        return_projection = jsonify_argument(args.get('return_projection',
                                                      None),
                                             key='return_projection')
        check_argument_type(return_projection, (dict, ),
                            key='return_projection',
                            allow_none=True)
        _validate_projection(return_projection)
        return_projection = projection_helper.modified_projection(
            return_projection, mandatory_attrs=['_id', 'jsonClass'])

        try:
            group_update = JsonObject.make_from_dict(update_doc)
            updated_group_id = objstore_helper.update_resource(
                g.users_colln,
                group_update.to_json_map(),
                current_token.user_id,
                current_token.group_ids,
                not_allowed_attributes=['members', 'groupName'])
            if updated_group_id is None:
                raise ObjModelException('group not exist', 404)
        except ObjModelException as e:
            return error_response(message=e.message, code=e.http_response_code)
        except ValueError as e:
            return error_response(message='schema validation error',
                                  code=403,
                                  details={"error": str(e)})

        group_json = g.users_colln.find_one(group_selector_doc,
                                            projection=return_projection)
        return group_json
Ejemplo n.º 8
0
def project_user_json(user_json, projection=None):
    user_exposed_projection = projection_helper.modified_projection(
        user_json.get('exposed_projection', None), mandatory_attrs=['_id', 'jsonClass']) or {}
    user_exposed_projection = projection_helper.get_restricted_projection(
        user_exposed_projection, {"hashedPassword": 0}
    )
    projected_user_json = projection_helper.project_doc(
        user_json, projection_helper.get_restricted_projection(projection, user_exposed_projection))
    return projected_user_json
Ejemplo n.º 9
0
def get_requested_agents(args, colln, user_id, group_ids, filter_doc=None):
    selector_doc = jsonify_argument(args.get('selector_doc', None),
                                    key='selector_doc') or {}
    check_argument_type(selector_doc, (dict, ), key='selector_doc')
    if filter_doc is not None:
        selector_doc.update(filter_doc)

    projection = jsonify_argument(args.get('projection', None),
                                  key='projection')
    check_argument_type(projection, (dict, ),
                        key='projection',
                        allow_none=True)
    _validate_projection(projection)
    projection = projection_helper.modified_projection(
        projection, mandatory_attrs=["_id", "jsonClass"])

    lrs_request_doc = jsonify_argument(args.get('linked_resources', None),
                                       'linked_resources')
    check_argument_type(lrs_request_doc, (dict, ),
                        key='linked_resources',
                        allow_none=True)

    sort_doc = jsonify_argument(args.get('sort_doc', None), key='sort_doc')
    check_argument_type(sort_doc, (dict, list),
                        key='sort_doc',
                        allow_none=True)

    ops = OrderedDict()
    if sort_doc is not None:
        ops['sort'] = [sort_doc]
    if args.get('start', None) is not None and args.get('count',
                                                        None) is not None:
        ops['skip'] = [args['start']]
        ops['limit'] = [args['count']]

    try:
        resource_repr_jsons = objstore_helper.get_read_permitted_resource_jsons(
            colln,
            user_id,
            group_ids,
            selector_doc,
            projection=projection,
            ops=ops)
    except (TypeError, ValueError):
        error = error_response(message='arguments to operations seems invalid',
                               code=400)
        abort_with_error_response(error)

    if lrs_request_doc is not None:
        # noinspection PyUnboundLocalVariable
        for rj in resource_repr_jsons:
            linked_resources = objstore_helper.get_linked_resource_ids(
                colln, rj['_id'], lrs_request_doc)
            rj['linked_resources'] = linked_resources
    return resource_repr_jsons
Ejemplo n.º 10
0
    def post(self, user_id):
        '''
        if not current_token.user_id:
            return error_response(message='not authorized', code=401)
        '''
        args = self.post_parser.parse_args()
        user_selector_doc = users_helper.get_user_selector_doc(_id=user_id)

        update_doc = jsonify_argument(args['update_doc'], key='update_doc')
        check_argument_type(update_doc, (dict, ), key='update_doc')
        if '_id' not in update_doc:
            update_doc['_id'] = user_id
        if 'jsonClass' not in update_doc:
            update_doc['jsonClass'] = 'User'

        if update_doc['_id'] != user_id:
            return error_response(message='invalid user_id', code=403)
        if update_doc['jsonClass'] != 'User':
            return error_response(message='invalid jsonClass', code=403)

        return_projection = jsonify_argument(args.get('return_projection',
                                                      None),
                                             key='return_projection')
        check_argument_type(return_projection, (dict, ),
                            key='return_projection',
                            allow_none=True)
        _validate_projection(return_projection)
        return_projection = projection_helper.modified_projection(
            return_projection, mandatory_attrs=['_id', 'jsonClass'])

        try:
            user_update = JsonObject.make_from_dict(update_doc)
            updated_user_id = objstore_helper.update_resource(
                g.users_colln,
                user_update.to_json_map(),
                current_token.user_id,
                current_token.group_ids,
                not_allowed_attributes=[
                    'hashedPassword', 'externalAuthentications', 'password'
                ])
            if updated_user_id is None:
                raise ObjModelException('user not exist', 404)
        except ObjModelException as e:
            return error_response(message=e.message, code=e.http_response_code)
        except (ValueError, ValidationError, TypeError) as e:
            return error_response(message='schema validation error',
                                  code=403,
                                  details={"error": str(e)})

        user_json = g.users_colln.find_one(user_selector_doc,
                                           projection=return_projection)
        return user_json
Ejemplo n.º 11
0
    def post(self):
        args = self.post_parser.parse_args()

        root_node_return_projection = projection_helper.modified_projection(
            jsonify_argument(args['root_node_return_projection']), mandatory_attrs=['_id'])
        _validate_projection(root_node_return_projection)
        specific_resources_return_projection = projection_helper.modified_projection(
            jsonify_argument(args['sections_return_projection']), mandatory_attrs=['_id'])
        _validate_projection(specific_resources_return_projection)
        annotations_return_projection = projection_helper.modified_projection(
            jsonify_argument(args['annotations_return_projection']), mandatory_attrs=['_id'])
        _validate_projection(annotations_return_projection)

        trees = jsonify_argument(args['trees'], key='trees')
        check_argument_type(trees, (list,), key='trees')

        result_trees = []
        try:
            for i, tree in enumerate(trees):
                result_tree = objstore_helper.update_tree(
                    g.objstore_colln, current_token.user_id, current_token.group_ids, tree, 'tree{}'.format(i), None,
                    root_node_return_projection=root_node_return_projection,
                    specific_resources_return_projection=specific_resources_return_projection,
                    annotations_return_projection=annotations_return_projection,
                    initial_agents=g.initial_agents)
                result_trees.append(result_tree)
        except objstore_helper.TreeValidationError as e:
            return error_response(
                message="error in posting tree",
                code=e.http_status_code,
                invalid_node_path=e.invalid_node_path,
                invalid_node_json=e.invalid_node_json,
                error=str(e.error),
                succeded_trees=result_trees
            )
        except (ValidationError, TypeError) as e:
            return error_response(message='schema validation error', error=str(e), code=400)

        return result_trees
Ejemplo n.º 12
0
    def get(self):
        user_id = resolve_user_id()
        args = self.get_parser.parse_args()

        projection = jsonify_argument(args.get('projection', None), key='projection')
        check_argument_type(projection, (dict,), key='projection', allow_none=True)
        _validate_projection(projection)
        projection = projection_helper.modified_projection(projection, mandatory_attrs=["_id", "jsonClass"])

        current_user_json = g.users_colln.find_one(
            users_helper.get_user_selector_doc(_id=user_id), projection=projection)
        if current_user_json is None:
            sign_out_user(g.current_org_name)
            return error_response(message='not authorized', code=401)
        return current_user_json, 200
Ejemplo n.º 13
0
def get_user_groups(groups_colln, user_id, groups_projection=None):
    groups_projection = projection_helper.modified_projection(
        groups_projection, mandatory_attrs=["_id", "source"]
    )

    group_id_jsons_map = {}
    explicit_group_jsons = groups_colln.find(
        {"jsonClass": UsersGroup.json_class, "members": user_id}, projection=groups_projection
    )
    group_id_jsons_map.update(dict((group_json['_id'], group_json) for group_json in explicit_group_jsons))

    for group_json in explicit_group_jsons:
        group_hierarchy = get_group_hierarchy(groups_colln, group_json, groups_projection=groups_projection)
        group_id_jsons_map.update(dict((group_json['_id'], group_json) for group_json in group_hierarchy))

    return list(group_id_jsons_map.values())
Ejemplo n.º 14
0
    def post(self):
        user_id = resolve_user_id()
        group_ids = [
            group['_id'] for group in groups_helper.get_user_groups(
                g.users_colln, user_id, groups_projection={"_id": 1})
        ]

        args = self.post_parser.parse_args()
        update_doc = jsonify_argument(args['update_doc'], key='update_doc')
        check_argument_type(update_doc, (dict,), key='update_doc')
        if '_id' not in update_doc:
            update_doc['_id'] = user_id
        if 'jsonClass' not in update_doc:
            update_doc['jsonClass'] = 'User'

        if update_doc['_id'] != user_id:
            return error_response(message='invalid _id', code=403)
        if update_doc['jsonClass'] != 'User':
            return error_response(message='invalid jsonClass', code=403)

        if 'password' in update_doc:
            update_doc['hashedPassword'] = bcrypt.hashpw(
                update_doc['password'].encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
            update_doc.pop('password')

        return_projection = jsonify_argument(args.get('return_projection', None), key='return_projection')
        check_argument_type(return_projection, (dict,), key='return_projection', allow_none=True)
        _validate_projection(return_projection)
        return_projection = projection_helper.modified_projection(
            return_projection, mandatory_attrs=['_id', 'jsonClass'])

        try:
            user_update = JsonObject.make_from_dict(update_doc)
            updated_user_id = objstore_helper.update_resource(
                g.users_colln, user_update.to_json_map(), user_id, group_ids,
                not_allowed_attributes=('externalAuthentications', 'password'))
            if updated_user_id is None:
                raise ObjModelException('user not exist', 404)
        except ObjModelException as e:
            return error_response(message=e.message, code=e.http_response_code)
        except ValueError as e:
            return error_response(message='schema validation error', code=403, details={"error": str(e)})

        user_json = g.users_colln.get(user_id, projection=return_projection)
        return user_json
Ejemplo n.º 15
0
def get_group_hierarchy(groups_colln, group_json, groups_projection=None):
    groups_projection = projection_helper.modified_projection(
        groups_projection, mandatory_attrs=["_id", "source"]
    )

    group_hierarchy = []
    parent_group_id = group_json.get('source', None)
    if not parent_group_id:
        return group_hierarchy

    parent_group_json = groups_colln.find_one(
        get_group_selector_doc(_id=parent_group_id), projection=groups_projection)
    if parent_group_json is None:
        return group_hierarchy

    group_hierarchy.append(parent_group_json)

    parent_group_hierarchy = get_group_hierarchy(
        groups_colln, parent_group_json, groups_projection=groups_projection)
    group_hierarchy.extend(parent_group_hierarchy)

    return group_hierarchy
Ejemplo n.º 16
0
def get_group(groups_colln, group_selector_doc, projection=None):
    projection = projection_helper.modified_projection(projection, mandatory_attrs=['jsonClass'])
    group_json = groups_colln.find_one(group_selector_doc, projection=projection)
    return JsonObject.make_from_dict(group_json)
Ejemplo n.º 17
0
def get_client(oauth_colln, client_selector_doc, projection=None):
    projection = projection_helper.modified_projection(
        projection, mandatory_attrs=["jsonClass"])
    user_json = oauth_colln.find_one(client_selector_doc,
                                     projection=projection)
    return JsonObject.make_from_dict(user_json)