The client permissions object and related methods.
{{ client_permissions }}
The client_permissions object is available on every page, and contains information about whether or not the user (or developer) has granted permission to use specific features. By default the only feature controlled by this is sessions, but the permission system may be "extended" arbitrarily by the developer to control additional features. The permissions feature requires cookies in order to work. Requests made without cookies (such as by bots or browsers with cookies disabled) will always behave like an initial page-load without existing permission information.
True if the permissions cookie was already set prior to this request. False on the first page load, after the client has cleared their cookies, or on all requests from browsers with cookies disabled
The next date and time that one of the permissions is set to expire. Note that this does not distinguish between "allow" and "deny" permissions and will ignore permissions with no expiration date set. If there are no permissions with expiration dates set this property will be empty
The latest date and time that one of the permissions is set to expire. Note that this does not distinguish between "allow" and "deny" permissions and will ignore permissions with no expiration date set. If there are no permissions with expiration dates set this property will be empty
JSON representation of the client_permissions object, similar to calling {{ client_permissions | inspect: 3, false }}
The client_permissions object is copyable, and when copied using the {% copy_to_dictionary %} method the keys will be the permission names and the values will be the corresponding permission objects. The client_permissions object may also be enumerated using the {% for %} method, which will loop through all of the related permission objects when the for loop is started. Permissions that are added during enumeration will NOT be included, permissions that are removed during enumeration WILL be included, and permissions that are modified during enumeration will be included along with their updated properties.
ExampleClient PermissionsShows how a developer can use different client permission flags control messaging and how to check for expiring permissions.
Liquid
{%- if client_permissions.do_not_track -%}
<p>You have requested that we do not track your information. Consequently we will return a simple generic response even though it may not provide the best experience.</p>
{%- else -%}
{%- if client_permissions.allow_all -%}
{%- comment %}In this example "all" refers to a custom permission defined by the developer{% endcomment -%}
<p>You have granted us unlimited power! You should have the best possible experience.</p>
{%- if client_permissions.all.expires < request.date | add_weeks: 2 -%}
<p>Your permissions will expire on {{ client_permissions.all.expires | date: 'MMM dd' }}. If you want to continue receiving the best possible experience, <a href="#">follow these instructions</a>.</p>
{%- endif -%}
{%- else -%}
{%- if client_permissions.allow_generous -%}
<p>You have allowed "generous" permissions. While not the best, this still allows for a good experience.</p>
{%- elsif client_permissions.allow_limited -%}
<p>You have allowed "limited" permissions. This is only slightly better than a completely anonymous experience.</p>
{%- else -%}
<p>You have not allowed any permissions. Your experience will be fully generic.</p>
{%- endif -%}
<p>To customize your options and improve your experience, <a href="#">click here</a>.</p>
{%- endif -%}
{%- endif -%}
ExampleClient Permissions allow adsDemonstrates how the client_permissions object could be used to control the display of ads on a page.
Liquid
{%- if client_permissions.allow_advertisements -%}
{%- assign allowed_ads = client_permissions.advertisements.value | default: "all" | split: "&" -%}
<ul>
{%- for ad in allowed_ads -%}
<li>{{ad | replace: '-' " " | replace: '_' " " | capitalize }} advertisements are allowed until {{client_permissions.advertisements.expires | date: 'MMMM dd, yyyy, H:mm'}}.</li>
{%- endfor -%}
</ul>
{%- endif -%}
ExampleUnset the client when Do Not Track is presentWhen the Do Not Track header is present, unset the client so no client-specific data is stored.
Liquid
{%- if client_permissions.do_not_track -%}
{%- unset_client -%}
{%- endif -%}
ExampleHow to use the unset_session method
Simple Use Case
Liquid
{%- if client_permissions.do_not_track -%}
{%- unset_session -%}
{%- endif -%}
If the browser sent the "Do Not Track" header, remove all session properties.
Checks if the "ad_setting=false" query parameter was sent with the request, and if it was, explicitly deny the "ads" client permission and unset the three specified session properties. Multiple properties can be unset at once by providing each property name as a separate argument to the unset_session method.
Unset Session Properties Dynamically
Liquid
{%- var props = request.query_params.clearprops | split: ',' -%}
{%- for prop in props -%}
{%- unset_session &prop -%}
{%- endfor -%}
Checks if the "clearprops" query parameter was included in the request, and if it was, unset the specified session properties using the reference variable syntax.
Checks if the "clearprops" query parameter was included in the request, and if it was, converts the comma-delimited list of property names to a space-delimited list and unsets the session properties with those names using the expanded variable syntax.
{{ permission }}
An object containing information about permission allowed or denied in the permissions system and accessed using the {{ client_permissions }} object.
The date that the permission (either allowed or denied) is set to expire
ExampleClient Permissions allow adsDemonstrates how the client_permissions object could be used to control the display of ads on a page.
Liquid
{%- if client_permissions.allow_advertisements -%}
{%- assign allowed_ads = client_permissions.advertisements.value | default: "all" | split: "&" -%}
<ul>
{%- for ad in allowed_ads -%}
<li>{{ad | replace: '-' " " | replace: '_' " " | capitalize }} advertisements are allowed until {{client_permissions.advertisements.expires | date: 'MMMM dd, yyyy, H:mm'}}.</li>
{%- endfor -%}
</ul>
{%- endif -%}
{% set_client_permission %}
Defines whether the client has granted or deined permission for a particular feature (eg: sessions). The only permission defined by default is the session permission (configurable in the site properties). However, the template developer may use this mechanism for their own purposes as well. The permissions defined by this method will be stored in the permissions cookie, which may be read and/or modified by client-side javascript.
Must be one of allow, deny, 1 (alias for allow), or 0 (alias for deny). Defaults to allow
permission_namerequiredstring
The name of the permission to allow or deny. Defaults to the session permission
renewoptionalliteral
If this permission has already been allowed or denied, the original permission expiration date will only be updated if "renew" is specified
valueoptionalstring
The value to store along with the specified permission
alwaysoptionalbool
Specifies that the permission should not expire, which is a shortcut for a 50-year expiration date
foroptionalliteral
Specify in combination with the duration. If specified, the always and until clauses are not allowed
durationoptionalstring
How long until the permission should expire, specified as "num [minutes|hours|days|weeks|months|years]" where num is a positive integer. May either be specified directly in the method or read from a variable
untiloptionalliteral
Specify in combination with the date. If specified, the always and for clauses are not allowed
dateoptionaldate
The date that the permission will expire. If it is in the past, the permission will automatically be denied as expired
If none of the expiration clauses are specified, the permission expiration date will default to 1 year in the future. Remember that the expiration date specified here is for the permission, not for the effects of the permission. In the case of sessions, the expiration date defines how long the user has granted permission to have a session, regardless of the number or duration of sessions during that timeframe. If the permission has a value, the value will be stored along with the permission regardless of whether the permission has been allowed or denied. Permissions will never expire in the middle of a session - if a permission would be set to expire in the middle of a session, it will automatically be extended until the end of the session to prevent odd mid-session permission change bugs.
ExampleHow to use the set_client_permission method to allow or deny permissionsAllow or deny client permissions (e.g. session, ads) and set expiration or store values.
Liquid
{%- if request.query_params.allow_session == "true" -%}
{%- var allow_length = "1 year" -%}
{%- if request.query_params.allow_months -%}
{%- set allow_length = request.query_params.allow_months | append: " months" -%}
{%- endif -%}
{%- set_client_permission allow session renew for allow_length -%}
{%- elsif request.query_params.deny_session == "true" -%}
{%- set_client_permission deny renew -%}
--equivalent to {% set_client_permission deny session renew for 1 year -%}
{%- endif -%}
Allow the session permission for the default timeframe (1 year). If the session has already been allowed, the renew argument will cause it to be updated with the new timeframe. Equivalent to {% set_client_permission allow session renew for 1 year %}
If the "allowed_ids" query parameter was passed in the request, store it's value in the allowed partner_ids permission. If the "denied_ids" query parameter was passed in the request, store it's value in the denied denied_ids permission. For both of these permissions, the expiration date will use the default timeframe (1 year) if the permission was not previously specified, and will remain unchanged if the permission was previously specified.
Dynamically Set the Length of a Permission
Liquid
{%- if request.query_params.allow_session == "true" -%}
{%- var allow_length = "1 year" -%}
{%- if request.query_params.allow_months is_int true and request.query_params.allow_months > 0 -%}
{%- set allow_length = request.query_params.allow_months | append: " months" -%}
{%- endif -%}
{%- set_client_permission allow session renew for allow_length -%}
{%- elsif request.query_params.deny_session == "true" -%}
{%- set_client_permission deny renew -%}
{%- endif -%}
If the "allow_session" query parameter is set to true, sets or renews the session permission. The session permission is allowed for a default of 1 year, but if the "allow_months" query parmeter is a positive number then it will be used to set the length of the session permission. If the "deny_session" query parameter is set to true, updates the session permission to be denied for the default timeframe (1 year).
If event.end_date is a valid date, set a permission with the name of the event to allow until the specified date. The permission will expire at event.end_date.
Set Permissions Dynamically
Liquid
{%- if request.query_params.allow_networks -%}
{%- var networks = request.query_params.allow_networks | split: ',' -%}
{%- for network in networks -%}
{%- if network is_valid -%}
{%- set_client_permission allow network -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
If the "allow_networks" query parameter was passed in the request, set a permission for each network in the list. This works because "network" is a variable containin a string. If it were not a variable on the current scope, then the permission would be named "network" instead.
This code will sometimes work by setting a permission with the name "partner_ids", but if there is also a variable with that name then the value of the variable will be used instead, resulting in unexpected behavior. This can be easily avoided by placing the permission name in single or double quotes.
ExampleHow to use the unset_client_permission method
Simple Use Case
Liquid
{%- if request.query_params.user_confirmation != permissions.external_user.value -%}
{%- unset_client_permission external_user -%}
<p>Some error message about failed confirmation and please try again</p>
{%- endif -%}
If the "user_confirmation" query parameter is not equal to the value of the "external_user" permission, unset the "external_user" permission and display an error message.
Unset Permissions Dynamically
Liquid
{%- var unset_parties = request.query_params.third_party_unknowns | split: ',' -%}
{%- for party in unset_parties -%}
{%- unset_client_permission party -%}
{%- endfor -%}
Loop through a dynamic list of permissions and unset each one.
Unset Permissions Dynamically Using the Expanded Variable Syntax
If the "unset_permissions" query parameter was included in the request, convert the comma-delimited list of permission names to a space-delimited list and unset the permissions with those names using the expanded variable syntax.
Check whether all permissions should be unset or only most by safely unsetting all but the specified values. If not all permission should be unset, then store the values of permissions that should be preserved in separate variables, unset all permissions, and restore the permissions that should be preserved.
ExampleHow to use the unset_session method
Simple Use Case
Liquid
{%- if client_permissions.do_not_track -%}
{%- unset_session -%}
{%- endif -%}
If the browser sent the "Do Not Track" header, remove all session properties.
Checks if the "ad_setting=false" query parameter was sent with the request, and if it was, explicitly deny the "ads" client permission and unset the three specified session properties. Multiple properties can be unset at once by providing each property name as a separate argument to the unset_session method.
Unset Session Properties Dynamically
Liquid
{%- var props = request.query_params.clearprops | split: ',' -%}
{%- for prop in props -%}
{%- unset_session &prop -%}
{%- endfor -%}
Checks if the "clearprops" query parameter was included in the request, and if it was, unset the specified session properties using the reference variable syntax.
Checks if the "clearprops" query parameter was included in the request, and if it was, converts the comma-delimited list of property names to a space-delimited list and unsets the session properties with those names using the expanded variable syntax.
{% unset_client_permission %}
Removes the specified permissions from the permissions cookie. Note that this is not the same as denying permission since there will be no record that permission was either granted or denied after the permission has been unset.
One or more values. May use the variable arguments syntax. The names of the permissions to remove. If not included, all permissions will be removed
ExampleHow to use the unset_client_permission method
Simple Use Case
Liquid
{%- if request.query_params.user_confirmation != permissions.external_user.value -%}
{%- unset_client_permission external_user -%}
<p>Some error message about failed confirmation and please try again</p>
{%- endif -%}
If the "user_confirmation" query parameter is not equal to the value of the "external_user" permission, unset the "external_user" permission and display an error message.
Unset Permissions Dynamically
Liquid
{%- var unset_parties = request.query_params.third_party_unknowns | split: ',' -%}
{%- for party in unset_parties -%}
{%- unset_client_permission party -%}
{%- endfor -%}
Loop through a dynamic list of permissions and unset each one.
Unset Permissions Dynamically Using the Expanded Variable Syntax
If the "unset_permissions" query parameter was included in the request, convert the comma-delimited list of permission names to a space-delimited list and unset the permissions with those names using the expanded variable syntax.
Check whether all permissions should be unset or only most by safely unsetting all but the specified values. If not all permission should be unset, then store the values of permissions that should be preserved in separate variables, unset all permissions, and restore the permissions that should be preserved.