Esempio n. 1
0
def get_user_info(user):
    """Get the user information
    """
    user = api.get_user(user)
    current = api.get_current_user()

    if api.is_anonymous():
        return {
            "username":
            current.getUserName(),
            "authenticated":
            False,
            "roles":
            current.getRoles(),
            "api_url":
            api.url_for("plone.jsonapi.routes.users", username="******"),
        }

    # nothing to do
    if user is None:
        logger.warn("No user found for {}".format(user))
        return None

    # plone user
    pu = user.getUser()

    info = {
        "username":
        user.getUserName(),
        "roles":
        user.getRoles(),
        "groups":
        pu.getGroups(),
        "authenticated":
        current == user,
        "api_url":
        api.url_for("plone.jsonapi.routes.users", username=user.getId()),
    }

    for k, v in api.get_user_properties(user).items():
        if api.is_date(v):
            v = api.to_iso_date(v)
        if not api.is_json_serializable(v):
            logger.warn(
                "User property '{}' is not JSON serializable".format(k))
            continue
        info[k] = v

    return info
def get(context, request, username=None):
    """ NSK users route
    """

    items = []

    # list all users if no username was given
    if username is None:
        users = ploneapi.user.get_users()

        for user in users:
            items.append(get_user_info(user.getId()))

    # special user 'current' which retrieves the current user infos
    elif username == "current":
        items.append(get_user_info(short=False))

    # we have a username, go and get the infos for it
    else:
        info = get_user_info(username, short=False)
        items.append(info)

    return {
        "url":   url_for("users"),
        "count": len(items),
        "items": items
    }
Esempio n. 3
0
def get_user_info(username=None, short=True):
    """ return the user informations
    """

    # XXX: refactoring needed in this function

    user = None
    anon = ploneapi.user.is_anonymous()
    current = ploneapi.user.get_current()

    # no username, go and get the current user
    if username is None:
        user = current
    else:
        user = ploneapi.user.get(username)

    if not user:
        raise KeyError('User not found')

    info = {
        "id": user.getId(),
        "username": user.getUserName(),
        "url": url_for("users", username=user.getUserName())
    }

    # return base info
    if short or anon:
        return info

    # try to get extended infos
    pu = user.getUser()
    properties = {}
    if "mutable_properties" in pu.listPropertysheets():
        mp = pu.getPropertysheet("mutable_properties")
        properties = dict(mp.propertyItems())

    def to_iso8601(dt=None):
        if dt is None:
            return ""
        return dt.ISO8601()

    # include mutable properties if short==False
    info.update({
        "email":
        properties.get("email"),
        "fullname":
        properties.get("fullname"),
        "login_time":
        to_iso8601(properties.get("login_time")),
        "last_login_time":
        to_iso8601(properties.get("last_login_time")),
        "roles":
        user.getRoles(),
        "groups":
        pu.getGroups(),
        "authenticated":
        current == user and not anon,
    })

    return info
Esempio n. 4
0
def action(context, request, action=None, resource=None, uid=None):
    """Various HTTP POST actions

    Current supported actions:

      - create
      - update
      - delete
      - cut
      - copy
      - paste
    """
    # supported actions (see API function <action>_items(...))
    actions = ["create", "update", "delete", "cut", "copy", "paste"]
    if action not in actions:
        api.fail(401, "Action '{}' is not supported".format(action))

    # Fetch and call the action function of the API
    func_name = "{}_items".format(action)
    action_func = getattr(api, func_name, None)
    if action_func is None:
        api.fail(500, "API has no member named '{}'".format(func_name))

    portal_type = api.resource_to_portal_type(resource)
    items = action_func(portal_type=portal_type, uid=uid)

    return {
        "count": len(items),
        "items": items,
        "url": api.url_for("plone.jsonapi.routes.action", action=action),
    }
Esempio n. 5
0
def get(context, request, username=None):
    """ Plone users route
    """

    items = []

    if ploneapi.user.is_anonymous():
        raise RuntimeError("Not allowed for anonymous users")

    # list all users if no username was given
    if username is None:
        users = ploneapi.user.get_users()

        for user in users:
            items.append(get_user_info(user.getId()))

    # special user 'current' which retrieves the current user infos
    elif username == "current":
        items.append(get_user_info(short=False))

    # we have a username, go and get the infos for it
    else:
        info = get_user_info(username, short=False)
        items.append(info)

    return {
        "url": url_for("users"),
        "count": len(items),
        "items": items,
        "success": True
    }
Esempio n. 6
0
def get(context, request, username=None):
    """ Plone users route
    """

    items = []

    # don't allow anonymous to see other accounts
    if ploneapi.user.is_anonymous():
        username = "******"

    # list all users if no username was given
    if username is None:
        users = ploneapi.user.get_users()

        for user in users:
            items.append(get_user_info(user.getId()))

    # special user 'current' which retrieves the current user infos
    elif username == "current":
        items.append(get_user_info(short=False))

    # we have a username, go and get the infos for it
    else:
        info = get_user_info(username, short=False)
        items.append(info)

    return {"url": url_for("users"), "count": len(items), "items": items}
Esempio n. 7
0
def apiversion(context, request):
    """ get the current version of this package
    """
    return {
        "url":     api.url_for("plone.jsonapi.routes.version"),
        "version": __version__,
        "date":    __date__,
    }
Esempio n. 8
0
def apiversion(context, request):
    """ get the current version of this package
    """
    return {
        "url": api.url_for("plone.jsonapi.routes.version"),
        "version": __version__,
        "date": __date__,
    }
def get(context, request, uid=None):
    """ get events
    """
    items = get_items("Event", request, uid=uid, endpoint="events")
    return {
        "url": url_for("events"),
        "count": len(items),
        "items": items,
    }
Esempio n. 10
0
def delete(context, request, uid=None):
    """ delete wccdocument
    """
    items = delete_items("wcc.document.document", request, uid=uid, endpoint="wccdocument")
    return {
        "url": url_for("wccdocument_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 11
0
def delete(context, request, uid=None):
    """ delete newsitems
    """
    items = delete_items("News Item", uid=uid, endpoint="newsitems")
    return {
        "url": url_for("newsitems_delete"),
        "count": len(items),
        "items": items,
    }
def delete(context, request, uid=None):
    """ delete folders
    """
    items = delete_items("Folder", request, uid=uid, endpoint="folders")
    return {
        "url": url_for("folders_delete"),
        "count": len(items),
        "items": items,
    }
def get(context, request, uid=None):
    """ get topics
    """
    items = get_items("Topic", request, uid=uid, endpoint="topics")
    return {
        "url": url_for("topics"),
        "count": len(items),
        "items": items,
    }
Esempio n. 14
0
def update(context, request, uid=None):
    """ update files
    """
    items = update_items("File", request, uid=uid, endpoint="files")
    return {
        "url": url_for("files_update"),
        "count": len(items),
        "items": items,
    }
Esempio n. 15
0
def delete(context, request, uid=None):
    """ delete links
    """
    items = delete_items("Link", uid=uid, endpoint="links")
    return {
        "url": url_for("links_delete"),
        "count": len(items),
        "items": items,
    }
def delete(context, request, uid=None):
    """ delete collections
    """
    items = delete_items("Collection", uid=uid, endpoint="collections")
    return {
        "url": url_for("collections_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 17
0
def delete(context, request, uid=None):
    """ delete content
    """
    items = delete_items(uid=uid)
    return {
        "url": url_for("delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 18
0
def update(context, request, uid=None):
    """ update documents
    """
    items = update_items("Document", uid=uid, endpoint="documents")
    return {
        "url": url_for("documents_update"),
        "count": len(items),
        "items": items,
    }
Esempio n. 19
0
def delete(context, request, uid=None):
    """ delete events
    """
    items = delete_items("Event", uid=uid, endpoint="events")
    return {
        "url": url_for("events_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 20
0
def delete(context, request, uid=None):
    """ delete topics
    """
    items = delete_items("Topic", uid=uid, endpoint="topics")
    return {
        "url": url_for("topics_delete"),
        "count": len(items),
        "items": items,
    }
def get(context, request, uid=None):
    """ get collections
    """
    items = get_items("Collection", request, uid=uid, endpoint="collections")
    return {
        "url": url_for("collections"),
        "count": len(items),
        "items": items,
    }
Esempio n. 22
0
def delete(context, request, uid=None):
    """ delete links
    """
    items = delete_items("Link", uid=uid, endpoint="links")
    return {
        "url": url_for("links_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 23
0
def delete(context, request, uid=None):
    """ delete newsitems
    """
    items = delete_items("News Item", uid=uid, endpoint="newsitems")
    return {
        "url": url_for("newsitems_delete"),
        "count": len(items),
        "items": items,
    }
def get(context, request, uid=None):
    """ get links
    """
    items = get_items("Link", request, uid=uid, endpoint="links")
    return {
        "url": url_for("links"),
        "count": len(items),
        "items": items,
    }
Esempio n. 25
0
def copy(context, request, uid=None):
    """ copy content
    """
    items = copy_items(uid=uid, request=request)
    return {
        "url": url_for("copy"),
        "count": len(items),
        "items": items,
    }
def update(context, request, uid=None):
    """ update files
    """
    items = update_items("File", request, uid=uid, endpoint="files")
    return {
        "url": url_for("files_update"),
        "count": len(items),
        "items": items,
    }
Esempio n. 27
0
 def inner(*args, **kwargs):
     result = func(*args, **kwargs)
     if type(result) is types.ListType:
         return {
             "url": url_for(endpoint),
             "count": len(result),
             "items": result,
         }
     return result
Esempio n. 28
0
def paste(context, request, uid=None):
    """ paste content
    """
    items = paste_items(uid=uid, request=request)
    return {
        "url": url_for("paste"),
        "count": len(items),
        "items": items,
    }
def get(context, request, uid=None):
    """ get files
    """
    items = get_items("File", request, uid=uid, endpoint="files")
    return {
        "url": url_for("files"),
        "count": len(items),
        "items": items,
    }
Esempio n. 30
0
def update(context, request, uid=None):
    """ update documents
    """
    items = update_items("Document", uid=uid, endpoint="documents")
    return {
        "url": url_for("documents_update"),
        "count": len(items),
        "items": items,
    }
Esempio n. 31
0
def get(context, request, uid=None):
    """ get wccdocument
    """
    items = get_items("wcc.document.document", request, uid=uid, endpoint="wccdocument")
    return {
        "url": url_for("wccdocument"),
        "count": len(items),
        "items": items,
    }
Esempio n. 32
0
def delete(context, request, uid=None):
    """ delete folders
    """
    items = delete_items("Folder", uid=uid, endpoint="folders")
    return {
        "url": url_for("folders_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 33
0
def delete(context, request, uid=None):
    """ delete todos
    """
    items = delete_items("plone.todos.todo", request, uid=uid, endpoint="todos")
    return {
        "url": url_for("todos_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 34
0
def delete(context, request, uid=None):
    """ delete images
    """
    items = delete_items("Image", uid=uid, endpoint="images")
    return {
        "url": url_for("images_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 35
0
def delete(context, request, uid=None):
    """ delete collections
    """
    items = delete_items("Collection", uid=uid, endpoint="collections")
    return {
        "url": url_for("collections_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 36
0
def delete(context, request, uid=None):
    """ delete content
    """
    items = delete_items(uid=uid)
    return {
        "url": url_for("delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 37
0
def get(context, request, uid=None):
    """ get todos
    """
    items = get_items("plone.todos.todo", request, uid=uid, endpoint="todos")
    return {
        "url": url_for("todos"),
        "count": len(items),
        "items": items,
    }
Esempio n. 38
0
def update(context, request, uid=None):
    """ update topics
    """
    items = update_items("Topic", uid=uid, endpoint="topics")
    return {
        "url": url_for("topics_update"),
        "count": len(items),
        "items": items,
    }
Esempio n. 39
0
def delete(context, request, uid=None):
    """ delete story
    """
    items = delete_items("sinarngo.story.story", request, uid=uid, endpoint="story")
    return {
        "url": url_for("story_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 40
0
def get(context, request, uid=None):
    """ get story
    """
    items = get_items("sinarngo.story.story", request, uid=uid, endpoint="story")
    return {
        "url": url_for("story"),
        "count": len(items),
        "items": items,
    }
Esempio n. 41
0
def logout(context, request):
    """ Logout Route
    """
    logger.info("*** LOGOUT ***")

    acl_users = ploneapi.portal.get_tool("acl_users")
    acl_users.logout(request)

    return {"url": url_for("users"), "success": True}
Esempio n. 42
0
def delete(context, request, uid=None):
    """ delete events
    """
    items = delete_items("Event", uid=uid, endpoint="events")
    return {
        "url": url_for("events_delete"),
        "count": len(items),
        "items": items,
    }
Esempio n. 43
0
def logout(context, request):
    """ Logout Route
    """
    logger.info("*** LOGOUT ***")

    acl_users = ploneapi.portal.get_tool("acl_users")
    acl_users.logout(request)

    return {
        "url":     url_for("users"),
        "success": True
    }
Esempio n. 44
0
def get_user_info(username=None, short=True):
    """ return the user informations
    """

    # XXX: refactoring needed in this function

    user = None
    anon = ploneapi.user.is_anonymous()
    current = ploneapi.user.get_current()

    # no username, go and get the current user
    if username is None:
        user = current
    else:
        user = ploneapi.user.get(username)

    if not user:
        raise KeyError('User not found')

    info = {
        "id":       user.getId(),
        "username": user.getUserName(),
        "url":      url_for("users", username=user.getUserName())
    }

    # return base info
    if short or anon:
        return info

    # try to get extended infos
    pu = user.getUser()
    properties = {}
    if "mutable_properties" in pu.listPropertysheets():
        mp = pu.getPropertysheet("mutable_properties")
        properties = dict(mp.propertyItems())

    def to_iso8601(dt=None):
        if dt is None:
            return ""
        return dt.ISO8601()

    # include mutable properties if short==False
    info.update({
        "email":           properties.get("email"),
        "fullname":        properties.get("fullname"),
        "login_time":      to_iso8601(properties.get("login_time")),
        "last_login_time": to_iso8601(properties.get("last_login_time")),
        "roles":           user.getRoles(),
        "groups":          pu.getGroups(),
        "authenticated":   current == user and not anon,
    })

    return info
Esempio n. 45
0
def get_user_info(user):
    """Get the user information
    """
    user = api.get_user(user)
    current = api.get_current_user()

    if api.is_anonymous():
        return {
            "username": current.getUserName(),
            "authenticated": False,
            "roles": current.getRoles(),
            "api_url": api.url_for("plone.jsonapi.routes.users", username="******"),
        }

    # nothing to do
    if user is None:
        logger.warn("No user found for {}".format(user))
        return None

    # plone user
    pu = user.getUser()

    info = {
        "username": user.getUserName(),
        "roles": user.getRoles(),
        "groups": pu.getGroups(),
        "authenticated": current == user,
        "api_url": api.url_for("plone.jsonapi.routes.users", username=user.getId()),
    }

    for k, v in api.get_user_properties(user).items():
        if api.is_date(v):
            v = api.to_iso_date(v)
        if not api.is_json_serializable(v):
            logger.warn("User property '{}' is not JSON serializable".format(k))
            continue
        info[k] = v

    return info
Esempio n. 46
0
def action(context, request, action=None, resource=None, uid=None):
    """Various HTTP POST actions
    """

    # allow to set the method via the header
    if action is None:
        action = request.get_header("HTTP_X_HTTP_METHOD_OVERRIDE", "CREATE").lower()

    # Fetch and call the action function of the API
    func_name = "{}_items".format(action)
    action_func = getattr(api, func_name, None)
    if action_func is None:
        api.fail(500, "API has no member named '{}'".format(func_name))

    portal_type = api.resource_to_portal_type(resource)
    items = action_func(portal_type=portal_type, uid=uid)

    return {
        "count": len(items),
        "items": items,
        "url": api.url_for("plone.jsonapi.routes.action", action=action),
    }
Esempio n. 47
0
def action(context, request, action=None, resource=None, uid=None):
    """Various HTTP POST actions
    """

    # allow to set the method via the header
    if action is None:
        action = request.get_header("HTTP_X_HTTP_METHOD_OVERRIDE",
                                    "CREATE").lower()

    # Fetch and call the action function of the API
    func_name = "{}_items".format(action)
    action_func = getattr(api, func_name, None)
    if action_func is None:
        api.fail(500, "API has no member named '{}'".format(func_name))

    portal_type = api.resource_to_portal_type(resource)
    items = action_func(portal_type=portal_type, uid=uid)

    return {
        "count": len(items),
        "items": items,
        "url": api.url_for("plone.jsonapi.routes.action", action=action),
    }
Esempio n. 48
0
def action(context, request, action=None, resource=None, uid=None):
    """Various HTTP POST actions

    Case 1: <action>
    <Plonesite>/@@API/plone/api/1.0/<action>

    Case 2: <action>/<uid>
    -> The actions (cut, copy, update, delete) will performed on the object identified by <uid>
    -> The actions (create, paste) will use the <uid> as the parent folder
    <Plonesite>/@@API/plone/api/1.0/<action>/<uid>

    Case 3: <resource>/<action>
    -> The "target" object will be located by a location given in the request body (uid, path, parent_path + id)
    -> The actions (cut, copy, update, delete) will performed on the target object
    -> The actions (create) will use the target object as the container
    <Plonesite>/@@API/plone/api/1.0/<resource>/<action>

    Case 4: <resource>/<action>/<uid>
    -> The actions (cut, copy, update, delete) will performed on the object identified by <uid>
    -> The actions (create) will use the <uid> as the parent folder
    <Plonesite>/@@API/plone/api/1.0/<resource>/<action>
    """

    # Fetch and call the action function of the API
    func_name = "{}_items".format(action)
    action_func = getattr(api, func_name, None)
    if action_func is None:
        api.fail(500, "API has no member named '{}'".format(func_name))

    portal_type = api.resource_to_portal_type(resource)
    items = action_func(portal_type=portal_type, uid=uid)

    return {
        "count": len(items),
        "items": items,
        "url": api.url_for("plone.jsonapi.routes.action", action=action),
    }
Esempio n. 49
0
 def test_url_for(self):
     endpoint = "plonesites"
     uid = "0"
     self.assertEqual(
         api.url_for(endpoint, uid=uid),
         "http://foo/plone/api/1.0/plonesites/0")
Esempio n. 50
0
 def test_url_for(self):
     endpoint = "plonesites"
     uid = "0"
     self.assertEqual(api.url_for(endpoint, uid=uid),
                      "http://foo/plone/api/1.0/plonesites/0")