Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-90987

As a developer, I can obtain user segments using a headless API

    Details

      Description

      The goal of this story is to expose via a headless API the logic of the SegmentsEntryProvider, which allows retrieving the segments of a user, given his context.

      To try this:

      curl -u username:password -D - http://localhost:8080/o/headless-admin-user/v1.0/sites/{site-id}/user-accounts/{user-id}/segments
      

      For example:

      • As an admin user, I can list the segments of any user. 2019 is test/test admin user and 37004 is a regular user:
        curl -u test@liferay.com:test -D - http://localhost:8080/o/headless-admin-user/v1.0/sites/20123/user-accounts/20139/segments
        
        {
          "items" : [ {
            "active" : true,
            "criteria" : "{\"criteria\":{\"context\":{\"conjunction\":\"and\",\"filterString\":\"(languageId eq 'en_US')\",\"typeValue\":\"context\"},\"user\":{\"conjunction\":\"and\",\"filterString\":\"(emailAddress eq 'test@liferay.com')\",\"typeValue\":\"model\"}}}",
            "dateCreated" : "2019-02-21T17:30:30Z",
            "dateModified" : "2019-02-22T11:28:20Z",
            "id" : 36898,
            "name" : "Test segment",
            "source" : "DEFAULT"
          }, {
            "active" : true,
            "criteria" : "",
            "dateCreated" : "2019-02-21T16:33:20Z",
            "dateModified" : "2019-02-21T16:33:20Z",
            "id" : 23104,
            "name" : "Default Segment",
            "source" : "DEFAULT"
          } ],
          "itemsPerPage" : 20,
          "lastPageNumber" : 1,
          "pageNumber" : 1,
          "totalCount" : 2
        }
        
        curl -u test@liferay.com:test -D - http://localhost:8080/o/headless-admin-user/v1.0/sites/20123/user-accounts/37004/segments
        
        {
          "items" : [ {
            "active" : true,
            "criteria" : "{\"criteria\":{\"user\":{\"conjunction\":\"and\",\"filterString\":\"(firstName eq 'Sarai')\",\"typeValue\":\"model\"}}}",
            "dateCreated" : "2019-02-22T12:20:59Z",
            "dateModified" : "2019-02-22T12:20:59Z",
            "id" : 37003,
            "name" : "Segment not test",
            "source" : "DEFAULT"
          }, {
            "active" : true,
            "criteria" : "",
            "dateCreated" : "2019-02-21T16:33:20Z",
            "dateModified" : "2019-02-21T16:33:20Z",
            "id" : 23104,
            "name" : "Default Segment",
            "source" : "DEFAULT"
          } ],
          "itemsPerPage" : 20,
          "lastPageNumber" : 1,
          "pageNumber" : 1,
          "totalCount" : 2
        }
        
      • As a regular user, I can list my segments but I can't see other user's segments:
        curl -u sarai.diaz@liferay.com:sarai -D - http://localhost:8080/o/headless-admin-user/v1.0/sites/20123/user-accounts/37004/segments
        
        {
          "items" : [ {
            "active" : true,
            "criteria" : "{\"criteria\":{\"user\":{\"conjunction\":\"and\",\"filterString\":\"(firstName eq 'Sarai')\",\"typeValue\":\"model\"}}}",
            "dateCreated" : "2019-02-22T12:20:59Z",
            "dateModified" : "2019-02-22T12:20:59Z",
            "id" : 37003,
            "name" : "Segment not test",
            "source" : "DEFAULT"
          }, {
            "active" : true,
            "criteria" : "",
            "dateCreated" : "2019-02-21T16:33:20Z",
            "dateModified" : "2019-02-21T16:33:20Z",
            "id" : 23104,
            "name" : "Default Segment",
            "source" : "DEFAULT"
          } ],
          "itemsPerPage" : 20,
          "lastPageNumber" : 1,
          "pageNumber" : 1,
          "totalCount" : 2
        }
        
        //Should return 404 because a regular user is trying to get information about another user
        curl -u sarai.diaz@liferay.com:sarai -D - http://localhost:8080/o/headless-admin-user/v1.0/sites/20123/user-accounts/20139/segments
        
        HTTP/1.1 404
        X-Content-Type-Options: nosniff
        X-Frame-Options: SAMEORIGIN
        X-XSS-Protection: 1
        Set-Cookie: JSESSIONID=5F099CB7DC86EFCD15B53FC588D6C97B; Path=/; HttpOnly
        Link: <http://localhost:8080/o/api/doc rel="http://www.w3.org/ns/hydra/core#apiDocumentation">
        Date: Mon, 25 Feb 2019 15:46:10 GMT
        Content-Length: 0
        

      To try this with context, you should add the headers you want to the request:

      curl -X GET \
        http://localhost:8080/o/headless-admin-user/v1.0/sites/20123/user-accounts/20139/segments \
        -u 'test@liferay.com:test' \
        -H 'X-Device-Brand: LG' \
        -H 'X-Device-Model: G7' 
      

      You can add a new header with

      -H '<header-name>: <header-value>'

      These are the available headers:

      • Host (internal note: this is going to map with Context.URL)
      • Accept-Language (internal note: this is going to map with Context.LANGUAGE_ID)
      • User-Agent (internal note: this is going to map with Context.USER_AGENT)
      • X-Browser (internal note: this is going to map with Context.BROWSER)
      • X-Cookies (internal note: this is going to map with Context.COOKIES)
      • X-Device-Brand (internal note: this is going to map with Context.DEVICE_BRAND)
      • X-Device-Model (internal note: this is going to map with Context.DEVICE_MODEL)
      • X-Device-Screen-Resolution-Height (internal note: this is going to map with Context.DEVICE_SCREEN_RESOLUTION_HEIGHT)
      • X-Device-Screen-Resolution-Width (internal note: this is going to map with Context.DEVICE_SCREEN_RESOLUTION_WIDTH)
      • X-Last-Sign-In-Date-Time (internal note: this is going to map with Context.LAST_SIGN_IN_DATE_TIME)
      • X-Signed-In (internal note: this is going to map with Context.SIGNED_IN)
      • (internal note: Context.LOCAL_DATE is always present in the context)

      Tests

      Acceptance Criteria

      • If the user introduces the wrong endpoint, 404 error shows up.
      • If the user introduces the wrong credentials to the request, 403 error shows up.
      • If the user introduces a wrong userId, 404 error shows up with this message: No User exists with the primary key 37055.
      • If the user introduces a wrong siteId, 404 error shows up with this message: Unable to get a valid site with ID 20124.
      • As an admin user, I can list the segments of any user.
      • As a regular user, I can list my segments but I can't see other user's segments.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Packages

                  Version Package
                  Master