Skip to main content

Using Sulu user roles in conditions

· One min read
Stephan Hochdörfer
Head of IT Business Operations

Sulu CMS is quite flexible out-of-the-box. For example, content properties can be marked as mandatory or optional. By utilizing the jexl syntax, you can even enable or disable properties based on certain conditions.

In the Sulu Docs it is shown how you can use a checkbox to enable or disable other content properties:

<?xml version="1.0" ?>
<template xmlns="http://schemas.sulu.io/template/template"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/template-1.0.xsd">
<!-- ... -->

<properties>
<!-- ... -->

<property name="isCode" type="checkbox">
<!-- ... -->
</property>

<property name="code" type="text_area" visibleCondition="isCode == true">
<!-- ... -->
</property>
</properties>
</template>

You can reference other content properties in the visibleCondition and use basic logic for comparisons.

But that is not all, you can also reference other properties, e.g. the user role of the logged-in user. If you don't want users with the role Editor to edit a specific content property, you can configure the property like this:

<?xml version="1.0" ?>
<template xmlns="http://schemas.sulu.io/template/template"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/template-1.0.xsd">
<!-- ... -->

<properties>
<!-- ... -->

<property name="code" type="text_area" disabledCondition="'ROLE_SULU_EDITOR' in __user.roles">
<!-- ... -->
</property>
</properties>
</template>

If you are curious about what else is possible then have a look at the conditionDataProviders in the AdminBundle.