- Ensure that a user is an authorized user of the correct company before allowing them to access a page
- Ensure that a user owns a specific product before allowing them to access a page
Usage
These steps will guide you through using thehasAccess function to ensure only people who have purchased a specific product, are allowed to access a page.
- Import the functions
- Check if the user has access
hasAccess function to check if the user has access to a specific product. To do this, we need to pass in an access string and the headers object.
Access strings are a way of specifying what the user needs to have access to, to access the page. Check out the Expressions section to learn more about access strings and what they can contain.
In this example, we are just checking if the user owns a specific product.
- Handle the result
access will return true, and if they are not, access will return false.
We can then show a message to the user based on whether or not they have access.
If you want to use the Company ID, you will need to prefix it with
authorized-. To make this easier, we have created a helper function called
authorizedUserOn. This function will return the correct access string for
you, all you need to do is pass in the company ID.For example:authorizedUserOn("biz_XXXX");will return:authorized-biz_XXXXExpressions
An access string is an<expression-list>
an <expression-list> is a list of <expression>s separated by a -
When evaluating the access string, the entity (user) must satisfy at least one of the expressions in the list. Ie: it is evaluated as an implicit or expression.
An <expression> is either:
- a single access control term (or primitive) EG:
user_123meaning the user is user_123authorized-biz_123(meaning the user is an authorized user on biz_123)exp_123meaning the user has access to this experience
- a
not-operator followed by a single<expression>and terminated with an underscore_- eg:
not-authorized-biz_123meaning the user is not authorized on this company
- eg:
- an
and-operator followed by an<expression-list>and terminated with an underscore_- eg:
and-exp_123-user_123_meaning the user has access to this experience and is user_123
- eg:
- an
or-operator followed by an<expression-list>and terminated with an underscore_- eg:
or-exp_123-user_123_meaning the user has access to this experience or is user_123
- eg:
and and or operators as well as not operators. For example:
and-exp_123-or-not-user_123_-authorized-biz_123__
would parse to:
and(prod_123, or(not(user_123), authorized-biz_123))
