Exemplo n.º 1
0
def graphs_advanced_search_ajax_api(request):
	"""
	Handles any request sent to following urls:
		/ajax/graphs

	Parameters
	----------
	request - HTTP Request

	Returns
	-------
	response : JSON Response

	"""
	if request.META.get('HTTP_ACCEPT', None) == 'application/json':
		if request.method == "POST":
			querydict = QueryDict('', mutable=True)
			querydict.update(request.GET)
			queryparams = querydict

			# Validate search graphs API request
			user_role = authorization.user_role(request)
			if user_role == authorization.UserRole.LOGGED_IN:
				if queryparams.get('owner_email', None) is None \
						and queryparams.get('member_email', None) is None \
						and queryparams.get('is_public', None) != '1':
					raise BadRequest(request, error_code=ErrorCodes.Validation.IsPublicNotSet)
				if queryparams.get('is_public', None) != '1':
					if get_request_user(request) != queryparams.get('member_email', None) \
							and get_request_user(request) != queryparams.get('owner_email', None):
						raise BadRequest(request, error_code=ErrorCodes.Validation.NotAllowedGraphAccess,
						                 args=queryparams.get('owner_email', None))

			total, graphs_list = graphs.search_graphs1(request,
			                                           owner_email=queryparams.get('owner_email', None),
			                                           member_email=queryparams.get('member_email', None),
			                                           names=list(filter(None, queryparams.getlist('names[]', []))),
			                                           is_public=queryparams.get('is_public', None),
			                                           nodes=list(filter(None, queryparams.getlist('nodes[]', []))),
			                                           edges=list(filter(None, queryparams.getlist('edges[]', []))),
			                                           tags=list(filter(None, queryparams.getlist('tags[]', []))),
			                                           limit=queryparams.get('limit', 20),
			                                           offset=queryparams.get('offset', 0),
			                                           order=queryparams.get('order', 'desc'),
			                                           sort=queryparams.get('sort', 'name'),
			                                           query=json.loads(request.body))

			return HttpResponse(json.dumps({
				'total': total,
				'graphs': [utils.serializer(graph, summary=True) for graph in graphs_list]
			}), content_type="application/json", status=200)
		else:
			raise MethodNotAllowed(request)  # Handle other type of request methods like GET, OPTIONS etc.
	else:
		raise BadRequest(request)
Exemplo n.º 2
0
def _add_graph(request, graph={}):
    """
	Graph Parameters
	----------
	name : string
		Name of group. Required
	owner_email : string
		Email of the Owner of the graph. Required
	tags: list of strings
		List of tags to be attached with the graph. Optional


	Parameters
	----------
	graph : dict
		Dictionary containing the data of the graph being added.
	request : object
		HTTP POST Request.

	Returns
	-------
	graph : object
		Newly created graph object.

	Raises
	------
	BadRequest - Cannot create graph for user other than the requesting user.

	Notes
	------

	"""

    # Validate add graph API request
    user_role = authorization.user_role(request)
    if user_role == authorization.UserRole.LOGGED_IN:
        if get_request_user(request) != graph.get('owner_email', None):
            raise BadRequest(
                request,
                error_code=ErrorCodes.Validation.CannotCreateGraphForOtherUser,
                args=graph.get('owner_email', None))
    elif user_role == authorization.UserRole.LOGGED_OFF and graph.get(
            'owner_email', None) is not None:
        raise BadRequest(
            request,
            error_code=ErrorCodes.Validation.CannotCreateGraphForOtherUser,
            args=graph.get('owner_email', None))

    return utils.serializer(
        graphs.add_graph(request,
                         name=graph.get('name', None),
                         is_public=graph.get('is_public', None),
                         graph_json=graph.get('graph_json', None),
                         style_json=graph.get('style_json', None),
                         tags=graph.get('tags', None),
                         owner_email=graph.get('owner_email', None)))
Exemplo n.º 3
0
def _add_layout(request, graph_id, layout={}):
    """
	Layout Parameters
	----------
	name : string
		Name of the layout. Required
	owner_email : string
		Email of the Owner of the graph. Required
	graph_id : string
		Unique ID of the graph for the layout. Required


	Parameters
	----------
	layout : dict
		Dictionary containing the data of the layout being added.
	request : object
		HTTP POST Request.

	Returns
	-------
	layout : object
		Newly created layout object.

	Raises
	------

	Notes
	------

	"""
    authorization.validate(request, permission='GRAPH_READ', graph_id=graph_id)

    # Validate add graph API request
    user_role = authorization.user_role(request)
    if user_role == authorization.UserRole.LOGGED_IN:
        if get_request_user(request) != layout.get('owner_email', None):
            raise BadRequest(request,
                             error_code=ErrorCodes.Validation.
                             CannotCreateLayoutForOtherUser,
                             args=layout.get('owner_email', None))

    return utils.serializer(
        graphs.add_layout(
            request,
            owner_email=layout.get('owner_email', None),
            name=layout.get('name', None),
            graph_id=layout.get('graph_id', None),
            is_shared=layout.get('is_shared', None),
            positions_json=layout.get('positions_json', None),
            style_json=layout.get('style_json', None),
        ))
Exemplo n.º 4
0
def _add_graph(request, graph={}):
	"""
	Graph Parameters
	----------
	name : string
		Name of group. Required
	owner_email : string
		Email of the Owner of the graph. Required
	tags: list of strings
		List of tags to be attached with the graph. Optional


	Parameters
	----------
	graph : dict
		Dictionary containing the data of the graph being added.
	request : object
		HTTP POST Request.

	Returns
	-------
	graph : object
		Newly created graph object.

	Raises
	------
	BadRequest - Cannot create graph for user other than the requesting user.

	Notes
	------

	"""

	# Validate add graph API request
	user_role = authorization.user_role(request)
	if user_role == authorization.UserRole.LOGGED_IN:
		if get_request_user(request) != graph.get('owner_email', None):
			raise BadRequest(request, error_code=ErrorCodes.Validation.CannotCreateGraphForOtherUser,
			                 args=graph.get('owner_email', None))
	elif user_role == authorization.UserRole.LOGGED_OFF and graph.get('owner_email', None) is not None:
		raise BadRequest(request, error_code=ErrorCodes.Validation.CannotCreateGraphForOtherUser,
		                 args=graph.get('owner_email', None))

	return utils.serializer(graphs.add_graph(request,
	                                         name=graph.get('name', None),
	                                         is_public=graph.get('is_public', None),
	                                         graph_json=graph.get('graph_json', None),
	                                         style_json=graph.get('style_json', None),
	                                         tags=graph.get('tags', None),
	                                         owner_email=graph.get('owner_email', None)))
Exemplo n.º 5
0
def _add_layout(request, graph_id, layout={}):
	"""
	Layout Parameters
	----------
	name : string
		Name of the layout. Required
	owner_email : string
		Email of the Owner of the graph. Required
	graph_id : string
		Unique ID of the graph for the layout. Required


	Parameters
	----------
	layout : dict
		Dictionary containing the data of the layout being added.
	request : object
		HTTP POST Request.

	Returns
	-------
	layout : object
		Newly created layout object.

	Raises
	------

	Notes
	------

	"""
	authorization.validate(request, permission='GRAPH_READ', graph_id=graph_id)

	# Validate add graph API request
	user_role = authorization.user_role(request)
	if user_role == authorization.UserRole.LOGGED_IN:
		if get_request_user(request) != layout.get('owner_email', None):
			raise BadRequest(request, error_code=ErrorCodes.Validation.CannotCreateLayoutForOtherUser,
			                 args=layout.get('owner_email', None))

	return utils.serializer(graphs.add_layout(request,
	                                          owner_email=layout.get('owner_email', None),
	                                          name=layout.get('name', None),
	                                          graph_id=layout.get('graph_id', None),
	                                          is_shared=layout.get('is_shared', None),
	                                          positions_json=layout.get('positions_json', None),
	                                          style_json=layout.get('style_json', None),
	                                          ))
Exemplo n.º 6
0
def _add_group(request, group={}):
    """
	Group Parameters
	----------
	name : string
		Name of group. Required
	description : string
		Description of the group. Optional
	owner_email : string
		Email of the Owner of the groups. Required


	Parameters
	----------
	group : dict
		Dictionary containing the data of the group being added.
	request : object
		HTTP POST Request.

	Returns
	-------
	group : object
		Newly created group object.

	Raises
	------

	Notes
	------

	"""

    # Validate add graph API request
    user_role = authorization.user_role(request)
    if user_role == authorization.UserRole.LOGGED_IN:
        if get_request_user(request) != group.get('owner_email', None):
            raise BadRequest(
                request,
                error_code=ErrorCodes.Validation.CannotCreateGroupForOtherUser,
                args=group.get('owner_email', None))

    return utils.serializer(
        users.add_group(request,
                        name=request.POST.get('name', None),
                        description=group.get('description', None),
                        owner_email=group.get('owner_email', None)))
Exemplo n.º 7
0
def _add_group(request, group={}):
	"""
	Group Parameters
	----------
	name : string
		Name of group. Required
	description : string
		Description of the group. Optional
	owner_email : string
		Email of the Owner of the groups. Required


	Parameters
	----------
	group : dict
		Dictionary containing the data of the group being added.
	request : object
		HTTP POST Request.

	Returns
	-------
	group : object
		Newly created group object.

	Raises
	------

	Notes
	------

	"""

	# Validate add graph API request
	user_role = authorization.user_role(request)
	if user_role == authorization.UserRole.LOGGED_IN:
		if get_request_user(request) != group.get('owner_email', None):
			raise BadRequest(request, error_code=ErrorCodes.Validation.CannotCreateGroupForOtherUser,
							 args=group.get('owner_email', None))

	return utils.serializer(users.add_group(request,
											name=request.POST.get('name', None),
											description=group.get('description', None),
											owner_email=group.get('owner_email', None)))
Exemplo n.º 8
0
def user_page(request):
	"""
		Wrapper view for the user profile page.

		:param request: HTTP GET Request.
	"""
	if 'GET' == request.method:
		context = RequestContext(request, {})
		user_email = get_request_user(request)
		user_object, auth_token = users.get_user_profile(request, user_email) if user_email is not None else None
		context.push({
			"user": {
				"id": user_object.id,
				"email": user_object.email,
				"auth_token": auth_token
			}
		})
		return render(request, 'user_profile/index.html', context)
	else:
		raise MethodNotAllowed(request)  # Handle other type of request methods like POST, PUT, UPDATE.
Exemplo n.º 9
0
def user_role(request):
	"""
	Returns the user role for the user making the request.

	Parameters
	----------
	request: HTTP request

	Returns
	-------
	Returns UserRole
	"""
	user_email = get_request_user(request)
	user = users.controllers.get_user(request, user_email) if user_email is not None else None
	if user is None:
		return UserRole.LOGGED_OFF
	elif user.is_admin:
		return UserRole.ADMIN
	else:
		return UserRole.LOGGED_IN
Exemplo n.º 10
0
def user_role(request):
    """
	Returns the user role for the user making the request.

	Parameters
	----------
	request: HTTP request

	Returns
	-------
	Returns UserRole
	"""
    user_email = get_request_user(request)
    user = users.controllers.get_user(
        request, user_email) if user_email is not None else None
    if user is None:
        return UserRole.LOGGED_OFF
    elif user.is_admin:
        return UserRole.ADMIN
    else:
        return UserRole.LOGGED_IN
Exemplo n.º 11
0
def graphs_advanced_search_ajax_api(request):
    """
	Handles any request sent to following urls:
		/ajax/graphs

	Parameters
	----------
	request - HTTP Request

	Returns
	-------
	response : JSON Response

	"""
    if request.META.get('HTTP_ACCEPT', None) == 'application/json':
        if request.method == "POST":
            querydict = QueryDict('', mutable=True)
            querydict.update(request.GET)
            queryparams = querydict

            # Validate search graphs API request
            user_role = authorization.user_role(request)
            if user_role == authorization.UserRole.LOGGED_IN:
                if queryparams.get('owner_email', None) is None \
                  and queryparams.get('member_email', None) is None \
                  and queryparams.get('is_public', None) != '1':
                    raise BadRequest(
                        request,
                        error_code=ErrorCodes.Validation.IsPublicNotSet)
                if queryparams.get('is_public', None) != '1':
                    if get_request_user(request) != queryparams.get('member_email', None) \
                      and get_request_user(request) != queryparams.get('owner_email', None):
                        raise BadRequest(request,
                                         error_code=ErrorCodes.Validation.
                                         NotAllowedGraphAccess,
                                         args=queryparams.get(
                                             'owner_email', None))

            total, graphs_list = graphs.search_graphs1(
                request,
                owner_email=queryparams.get('owner_email', None),
                member_email=queryparams.get('member_email', None),
                names=list(filter(None, queryparams.getlist('names[]', []))),
                is_public=queryparams.get('is_public', None),
                nodes=list(filter(None, queryparams.getlist('nodes[]', []))),
                edges=list(filter(None, queryparams.getlist('edges[]', []))),
                tags=list(filter(None, queryparams.getlist('tags[]', []))),
                limit=queryparams.get('limit', 20),
                offset=queryparams.get('offset', 0),
                order=queryparams.get('order', 'desc'),
                sort=queryparams.get('sort', 'name'),
                query=json.loads(request.body))

            return HttpResponse(json.dumps({
                'total':
                total,
                'graphs': [
                    utils.serializer(graph, summary=True)
                    for graph in graphs_list
                ]
            }),
                                content_type="application/json",
                                status=200)
        else:
            raise MethodNotAllowed(
                request
            )  # Handle other type of request methods like GET, OPTIONS etc.
    else:
        raise BadRequest(request)
Exemplo n.º 12
0
def _get_layouts(request, graph_id, query=dict()):
	"""
	Query Parameters
	----------
	owner_email : string
		Email of the Owner of the groups.
	limit : integer
		Number of entities to return. Default value is 20.
	offset : integer
		Offset the list of returned entities by this number. Default value is 0.
	name : string
		Search for groups with given name. In order to search for layouts with given name as a substring, wrap the name with percentage symbol. For example, %xyz% will search for all layouts with xyz in their name.
	order : string
		Defines the column sort order, can only be 'asc' or 'desc'.
	sort : string
		Defines which column will be sorted.

	Parameters
	----------
	query : dict
		Dictionary of query parameters.
	request : object
		HTTP GET Request.

	Returns
	-------
	total : integer
		Number of groups matching the request.
	groups : List of Layouts.
		List of Layout Objects with given limit and offset.

	Raises
	------

	Notes
	------
	"""
	authorization.validate(request, permission='GRAPH_READ', graph_id=graph_id)

	querydict = QueryDict('', mutable=True)
	querydict.update(query)
	query = querydict

	# Validate search layouts API request
	user_role = authorization.user_role(request)
	if user_role == authorization.UserRole.LOGGED_IN:
		if get_request_user(request) != query.get('owner_email', None) \
				and (query.get('is_shared', None) is None or int(query.get('is_shared', 0)) != 1):
			raise BadRequest(request, error_code=ErrorCodes.Validation.NotAllowedLayoutAccess, args=get_request_user(request))

	total, layouts = graphs.search_layouts(request,
	                                       owner_email=query.get('owner_email', None),
	                                       name=query.get('name', None),
	                                       is_shared=query.get('is_shared', None),
	                                       graph_id=graph_id,
	                                       limit=query.get('limit', 20),
	                                       offset=query.get('offset', 0),
	                                       order=query.get('order', 'desc'),
	                                       sort=query.get('sort', 'name'))

	return {
		'total': total,
		'layouts': [utils.serializer(layout) for layout in layouts]
	}
Exemplo n.º 13
0
def _get_graph_groups(request, graph_id, query={}):
	"""

	Query Parameters
	----------
	owner_email : string
		Email of the Owner of the groups.
	member_email: string
		Email of the member of the groups.
	limit : integer
		Number of entities to return. Default value is 20.
	offset : integer
		Offset the list of returned entities by this number. Default value is 0.
	name : string
		Search for groups with given name. In order to search for groups with given name as a substring, wrap the name with percentage symbol. For example, %xyz% will search for all groups with xyz in their name.
	description : string
		Search for groups with given description. In order to search for groups with given description as a substring, wrap the description with percentage symbol. For example, %xyz% will search for all groups with xyz in their description.
	order : string
		Defines the column sort order, can only be 'asc' or 'desc'.
	sort : string
		Defines which column will be sorted.


	Parameters
	----------
	request : object
		HTTP GET Request.
	graph_id : string
		Unique ID of the graph.

	Returns
	-------
	total : integer
		Number of groups matching the request.
	groups : List of Groups.
		List of Group Objects with given limit and offset.

	Raises
	------
	BadRequest: If the user is not admin and tries to access groups where user is neither owner or member.

	Notes
	------

	"""
	authorization.validate(request, permission='GRAPH_READ', graph_id=graph_id)

	# Validate search graph groups API request
	user_role = authorization.user_role(request)
	if user_role == authorization.UserRole.LOGGED_IN:
		if query.get('is_public', None) is not True:
			if get_request_user(request) != query.get('member_email', None) \
					and get_request_user(request) != query.get('owner_email', None):
				raise BadRequest(request, error_code=ErrorCodes.Validation.NotAllowedGroupAccess,
				                 args=get_request_user(request))

	total, groups = users.search_groups(request,
	                                    graph_ids=[graph_id],
	                                    owner_email=query.get('owner_email', None),
	                                    member_email=query.get('member_email', None),
	                                    name=query.get('name', None),
	                                    description=query.get('description', None),
	                                    limit=query.get('limit', 20),
	                                    offset=query.get('offset', 0),
	                                    order=query.get('order', 'desc'),
	                                    sort=query.get('sort', 'name'))

	return {
		'total': total,
		'groups': [utils.serializer(group) for group in groups]
	}
Exemplo n.º 14
0
def _get_graphs(request, query=dict()):
	"""
	Query Parameters
	----------
	owner_email : string
		Email of the Owner of the graphs. Required if member_email is not provided, user is not admin and is_public is not set to True.
	member_email : string
		Email of the User with which the graphs are shared. Required if owner_email is not provided, user is not admin and is_public is not set to True.
	limit : integer
		Number of entities to return. Default value is 20.
	offset : integer
		Offset the list of returned entities by this number. Default value is 0.
	is_public: integer
		Search for graphs with given visibility. In order to search for public graphs set is_public to 1. Required if member_email & owner_email are not provided.
		In order to search for private graphs set is_public to 0. In order to search for all graphs set is_public to None.
	names : list of strings
		Search for graphs with given list of names. In order to search for graphs with given name as a substring, wrap the name with percentage symbol. For example, %xyz% will search for all graphs with xyz in their name.
	nodes : list of strings
		Search for graphs with given given list of node names. In order to search for graphs with given node name as a substring, wrap the name with percentage symbol. For example, %xyz% will search for all graphs with xyz in their node name.
	edges : list of strings
		Search for graphs with the edge between given given list of node names separated by colon. In order to search for graphs with given edge as a substring, wrap the name of the nodes with percentage symbol. For example, %xyz%:%abc% will search for all graphs with edge between nodes with 'xyz' and 'abc' in their node names.
	tags : list of strings
		Search for graphs with the given given list of tag names. In order to search for graphs with given tag as a substring, wrap the name of the tag with percentage symbol. For example, %xyz% will search for all graphs with 'xyz' in their tag names.
	order : string
		Defines the column sort order, can only be 'asc' or 'desc'.
	sort : string
		Defines which column will be sorted.

	Parameters
	----------
	query : dict
		Dictionary of query parameters.
	request : object
		HTTP GET Request.
	owner_email : string
		Email of the Owner of the groups.

	Returns
	-------
	total : integer
		Number of groups matching the request.
	groups : List of Groups.
		List of Group Objects with given limit and offset.

	Raises
	------
	BadRequest - `is_public` is required to be set to True when `owner_email` and `member_email` are not provided.

	BadRequest - `User is not authorized to access private graphs created by given owner. This means either the graph belongs to a different owner
	or graph is not shared with the user.

	Notes
	------
	"""

	querydict = QueryDict('', mutable=True)
	querydict.update(query)
	query = querydict

	# Validate search graphs API request
	user_role = authorization.user_role(request)
	if user_role == authorization.UserRole.LOGGED_IN:
		if query.get('owner_email', None) is None \
				and query.get('member_email', None) is None \
				and query.get('is_public', None) != '1':
			raise BadRequest(request, error_code=ErrorCodes.Validation.IsPublicNotSet)
		if query.get('is_public', None) != '1':
			if get_request_user(request) != query.get('member_email', None) \
					and get_request_user(request) != query.get('owner_email', None):
				raise BadRequest(request, error_code=ErrorCodes.Validation.NotAllowedGraphAccess,
				                 args=query.get('owner_email', None))

	total, graphs_list = graphs.search_graphs(request,
	                                          owner_email=query.get('owner_email', None),
	                                          member_email=query.get('member_email', None),
	                                          names=list(filter(None, query.getlist('names[]', []))),
	                                          is_public=query.get('is_public', None),
	                                          nodes=list(filter(None, query.getlist('nodes[]', []))),
	                                          edges=list(filter(None, query.getlist('edges[]', []))),
	                                          tags=list(filter(None, query.getlist('tags[]', []))),
	                                          limit=query.get('limit', 20),
	                                          offset=query.get('offset', 0),
	                                          order=query.get('order', 'desc'),
	                                          sort=query.get('sort', 'name'))

	return {
		'total': total,
		'graphs': [utils.serializer(graph, summary=True) for graph in graphs_list]
	}
Exemplo n.º 15
0
def validate(request,
             permission,
             graph_id=None,
             group_id=None,
             layout_id=None):
    """
	Validates if the user has the given permissions based on information like graph id, group id or layout id.

	Returns
	-------
	Nothing

	Raises
	-------
	UserNotAuthorized - if user doesnt have the given permission.

	"""

    # TODO: Each application module should implement a validate method.
    # Then this validate method can plug into the implemented validate method to expose overall validation functionality for the project.

    if graph_id is not None:
        if permission == 'GRAPH_READ' and not graphs.controllers.is_user_authorized_to_view_graph(
                request, username=get_request_user(request),
                graph_id=graph_id):
            raise UserNotAuthorized(request)
        if permission == 'GRAPH_UPDATE' and not graphs.controllers.is_user_authorized_to_update_graph(
                request, username=get_request_user(request),
                graph_id=graph_id):
            raise UserNotAuthorized(request)
        if permission == 'GRAPH_DELETE' and not graphs.controllers.is_user_authorized_to_delete_graph(
                request, username=get_request_user(request),
                graph_id=graph_id):
            raise UserNotAuthorized(request)
        if permission == 'GRAPH_SHARE' and not graphs.controllers.is_user_authorized_to_share_graph(
                request, username=get_request_user(request),
                graph_id=graph_id):
            raise UserNotAuthorized(request)
    if group_id is not None:
        if permission == 'GROUP_READ' and not users.controllers.is_user_authorized_to_view_group(
                request, username=get_request_user(request),
                group_id=group_id):
            raise UserNotAuthorized(request)
        if permission == 'GROUP_UPDATE' and not users.controllers.is_user_authorized_to_update_group(
                request, username=get_request_user(request),
                group_id=group_id):
            raise UserNotAuthorized(request)
        if permission == 'GROUP_DELETE' and not users.controllers.is_user_authorized_to_delete_group(
                request, username=get_request_user(request),
                group_id=group_id):
            raise UserNotAuthorized(request)
        if permission == 'GROUP_SHARE' and not users.controllers.is_user_authorized_to_share_with_group(
                request, username=get_request_user(request),
                group_id=group_id):
            raise UserNotAuthorized(request)
    if layout_id is not None:
        if permission == 'LAYOUT_READ' and not graphs.controllers.is_user_authorized_to_view_layout(
                request, username=get_request_user(request),
                layout_id=layout_id):
            raise UserNotAuthorized(request)
        if permission == 'LAYOUT_UPDATE' and not graphs.controllers.is_user_authorized_to_update_layout(
                request, username=get_request_user(request),
                layout_id=layout_id):
            raise UserNotAuthorized(request)
        if permission == 'LAYOUT_DELETE' and not graphs.controllers.is_user_authorized_to_delete_layout(
                request, username=get_request_user(request),
                layout_id=layout_id):
            raise UserNotAuthorized(request)
    return
Exemplo n.º 16
0
def _get_layouts(request, graph_id, query=dict()):
    """
	Query Parameters
	----------
	owner_email : string
		Email of the Owner of the groups.
	limit : integer
		Number of entities to return. Default value is 20.
	offset : integer
		Offset the list of returned entities by this number. Default value is 0.
	name : string
		Search for groups with given name. In order to search for layouts with given name as a substring, wrap the name with percentage symbol. For example, %xyz% will search for all layouts with xyz in their name.
	order : string
		Defines the column sort order, can only be 'asc' or 'desc'.
	sort : string
		Defines which column will be sorted.

	Parameters
	----------
	query : dict
		Dictionary of query parameters.
	request : object
		HTTP GET Request.

	Returns
	-------
	total : integer
		Number of groups matching the request.
	groups : List of Layouts.
		List of Layout Objects with given limit and offset.

	Raises
	------

	Notes
	------
	"""
    authorization.validate(request, permission='GRAPH_READ', graph_id=graph_id)

    querydict = QueryDict('', mutable=True)
    querydict.update(query)
    query = querydict

    # Validate search layouts API request
    user_role = authorization.user_role(request)
    if user_role == authorization.UserRole.LOGGED_IN:
        if get_request_user(request) != query.get('owner_email', None) \
          and (query.get('is_shared', None) is None or int(query.get('is_shared', 0)) != 1):
            raise BadRequest(
                request,
                error_code=ErrorCodes.Validation.NotAllowedLayoutAccess,
                args=get_request_user(request))

    total, layouts = graphs.search_layouts(
        request,
        owner_email=query.get('owner_email', None),
        name=query.get('name', None),
        is_shared=query.get('is_shared', None),
        graph_id=graph_id,
        limit=query.get('limit', 20),
        offset=query.get('offset', 0),
        order=query.get('order', 'desc'),
        sort=query.get('sort', 'name'))

    return {
        'total': total,
        'layouts': [utils.serializer(layout) for layout in layouts]
    }
Exemplo n.º 17
0
def _get_graph_groups(request, graph_id, query={}):
    """

	Query Parameters
	----------
	owner_email : string
		Email of the Owner of the groups.
	member_email: string
		Email of the member of the groups.
	limit : integer
		Number of entities to return. Default value is 20.
	offset : integer
		Offset the list of returned entities by this number. Default value is 0.
	name : string
		Search for groups with given name. In order to search for groups with given name as a substring, wrap the name with percentage symbol. For example, %xyz% will search for all groups with xyz in their name.
	description : string
		Search for groups with given description. In order to search for groups with given description as a substring, wrap the description with percentage symbol. For example, %xyz% will search for all groups with xyz in their description.
	order : string
		Defines the column sort order, can only be 'asc' or 'desc'.
	sort : string
		Defines which column will be sorted.


	Parameters
	----------
	request : object
		HTTP GET Request.
	graph_id : string
		Unique ID of the graph.

	Returns
	-------
	total : integer
		Number of groups matching the request.
	groups : List of Groups.
		List of Group Objects with given limit and offset.

	Raises
	------
	BadRequest: If the user is not admin and tries to access groups where user is neither owner or member.

	Notes
	------

	"""
    authorization.validate(request, permission='GRAPH_READ', graph_id=graph_id)

    # Validate search graph groups API request
    user_role = authorization.user_role(request)
    if user_role == authorization.UserRole.LOGGED_IN:
        if query.get('is_public', None) is not True:
            if get_request_user(request) != query.get('member_email', None) \
              and get_request_user(request) != query.get('owner_email', None):
                raise BadRequest(
                    request,
                    error_code=ErrorCodes.Validation.NotAllowedGroupAccess,
                    args=get_request_user(request))

    total, groups = users.search_groups(
        request,
        graph_ids=[graph_id],
        owner_email=query.get('owner_email', None),
        member_email=query.get('member_email', None),
        name=query.get('name', None),
        description=query.get('description', None),
        limit=query.get('limit', 20),
        offset=query.get('offset', 0),
        order=query.get('order', 'desc'),
        sort=query.get('sort', 'name'))

    return {
        'total': total,
        'groups': [utils.serializer(group) for group in groups]
    }
Exemplo n.º 18
0
def _get_graphs(request, query=dict()):
    """
	Query Parameters
	----------
	owner_email : string
		Email of the Owner of the graphs. Required if member_email is not provided, user is not admin and is_public is not set to True.
	member_email : string
		Email of the User with which the graphs are shared. Required if owner_email is not provided, user is not admin and is_public is not set to True.
	limit : integer
		Number of entities to return. Default value is 20.
	offset : integer
		Offset the list of returned entities by this number. Default value is 0.
	is_public: integer
		Search for graphs with given visibility. In order to search for public graphs set is_public to 1. Required if member_email & owner_email are not provided.
		In order to search for private graphs set is_public to 0. In order to search for all graphs set is_public to None.
	names : list of strings
		Search for graphs with given list of names. In order to search for graphs with given name as a substring, wrap the name with percentage symbol. For example, %xyz% will search for all graphs with xyz in their name.
	nodes : list of strings
		Search for graphs with given given list of node names. In order to search for graphs with given node name as a substring, wrap the name with percentage symbol. For example, %xyz% will search for all graphs with xyz in their node name.
	edges : list of strings
		Search for graphs with the edge between given given list of node names separated by colon. In order to search for graphs with given edge as a substring, wrap the name of the nodes with percentage symbol. For example, %xyz%:%abc% will search for all graphs with edge between nodes with 'xyz' and 'abc' in their node names.
	tags : list of strings
		Search for graphs with the given given list of tag names. In order to search for graphs with given tag as a substring, wrap the name of the tag with percentage symbol. For example, %xyz% will search for all graphs with 'xyz' in their tag names.
	order : string
		Defines the column sort order, can only be 'asc' or 'desc'.
	sort : string
		Defines which column will be sorted.

	Parameters
	----------
	query : dict
		Dictionary of query parameters.
	request : object
		HTTP GET Request.
	owner_email : string
		Email of the Owner of the groups.

	Returns
	-------
	total : integer
		Number of groups matching the request.
	groups : List of Groups.
		List of Group Objects with given limit and offset.

	Raises
	------
	BadRequest - `is_public` is required to be set to True when `owner_email` and `member_email` are not provided.

	BadRequest - `User is not authorized to access private graphs created by given owner. This means either the graph belongs to a different owner
	or graph is not shared with the user.

	Notes
	------
	"""

    querydict = QueryDict('', mutable=True)
    querydict.update(query)
    query = querydict

    # Validate search graphs API request
    user_role = authorization.user_role(request)
    if user_role == authorization.UserRole.LOGGED_IN:
        if query.get('owner_email', None) is None \
          and query.get('member_email', None) is None \
          and query.get('is_public', None) != '1':
            raise BadRequest(request,
                             error_code=ErrorCodes.Validation.IsPublicNotSet)
        if query.get('is_public', None) != '1':
            if get_request_user(request) != query.get('member_email', None) \
              and get_request_user(request) != query.get('owner_email', None):
                raise BadRequest(
                    request,
                    error_code=ErrorCodes.Validation.NotAllowedGraphAccess,
                    args=query.get('owner_email', None))

    total, graphs_list = graphs.search_graphs(
        request,
        owner_email=query.get('owner_email', None),
        member_email=query.get('member_email', None),
        names=list(filter(None, query.getlist('names[]', []))),
        is_public=query.get('is_public', None),
        nodes=list(filter(None, query.getlist('nodes[]', []))),
        edges=list(filter(None, query.getlist('edges[]', []))),
        tags=list(filter(None, query.getlist('tags[]', []))),
        limit=query.get('limit', 20),
        offset=query.get('offset', 0),
        order=query.get('order', 'desc'),
        sort=query.get('sort', 'name'))

    return {
        'total': total,
        'graphs':
        [utils.serializer(graph, summary=True) for graph in graphs_list]
    }
Exemplo n.º 19
0
def validate(request, permission, graph_id=None, group_id=None, layout_id=None):
	"""
	Validates if the user has the given permissions based on information like graph id, group id or layout id.

	Returns
	-------
	Nothing

	Raises
	-------
	UserNotAuthorized - if user doesnt have the given permission.

	"""

	# TODO: Each application module should implement a validate method.
	# Then this validate method can plug into the implemented validate method to expose overall validation functionality for the project.

	if graph_id is not None:
		if permission == 'GRAPH_READ' and not graphs.controllers.is_user_authorized_to_view_graph(request, username=get_request_user(request), graph_id = graph_id):
			raise UserNotAuthorized(request)
		if permission == 'GRAPH_UPDATE' and not graphs.controllers.is_user_authorized_to_update_graph(request, username=get_request_user(request), graph_id = graph_id):
			raise UserNotAuthorized(request)
		if permission == 'GRAPH_DELETE' and not graphs.controllers.is_user_authorized_to_delete_graph(request, username=get_request_user(request), graph_id = graph_id):
			raise UserNotAuthorized(request)
		if permission == 'GRAPH_SHARE' and not graphs.controllers.is_user_authorized_to_share_graph(request, username=get_request_user(request), graph_id = graph_id):
			raise UserNotAuthorized(request)
	if group_id is not None:
		if permission == 'GROUP_READ' and not users.controllers.is_user_authorized_to_view_group(request, username=get_request_user(request), group_id = group_id):
			raise UserNotAuthorized(request)
		if permission == 'GROUP_UPDATE' and not users.controllers.is_user_authorized_to_update_group(request, username=get_request_user(request), group_id = group_id):
			raise UserNotAuthorized(request)
		if permission == 'GROUP_DELETE' and not users.controllers.is_user_authorized_to_delete_group(request, username=get_request_user(request), group_id = group_id):
			raise UserNotAuthorized(request)
		if permission == 'GROUP_SHARE' and not users.controllers.is_user_authorized_to_share_with_group(request, username=get_request_user(request), group_id = group_id):
			raise UserNotAuthorized(request)
	if layout_id is not None:
		if permission == 'LAYOUT_READ' and not graphs.controllers.is_user_authorized_to_view_layout(request, username=get_request_user(request), layout_id = layout_id):
			raise UserNotAuthorized(request)
		if permission == 'LAYOUT_UPDATE' and not graphs.controllers.is_user_authorized_to_update_layout(request, username=get_request_user(request), layout_id = layout_id):
			raise UserNotAuthorized(request)
		if permission == 'LAYOUT_DELETE' and not graphs.controllers.is_user_authorized_to_delete_layout(request, username=get_request_user(request), layout_id = layout_id):
			raise UserNotAuthorized(request)
	return