# Conditional application rules

**URL:** <https://discourse.cakephp.org/t/conditional-application-rules/1606>\
**Category:** Uncategorized\
**Created:** [November 25, 2016, 11:56am UTC](https://discourse.cakephp.org/t/conditional-application-rules/1606 "2016-11-25T11:56:55Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![markstory](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/markstory/32/182_2.png) [@markstory](https://discourse.cakephp.org/u/markstory)\
**Post date:** [November 26, 2016, 4:11pm UTC](https://discourse.cakephp.org/t/conditional-application-rules/1606/5 "2016-11-26T16:11:25Z")

</div>

Instead of Copy/Pasting you can make standalone Rules objects that decorate other rules apply their rules based on the state in the entities/relations that are being persisted.

```
class AdminAllowedRule
{
    public function __construct($callable)
    {
            $this->callable = $callable;
    }

    public function __invoke(EntityInterface $entity, $options)
    {
            if ($entity->user->is_admin) {
                    return true;
            }
            $cb = $this->callable;
            return $cb($entity, $options);
    }
}

```

This decorator approach would allow you do decorate your rules with the ‘admin allowed’ logic.

```
    $rules->add(new AdminAllowedRule(function ($entity, $options) {
            return $entity->created < time();
    });

```

This is pretty rough code, but with some polish it could work.

---

_[View the full topic](https://discourse.cakephp.org/t/conditional-application-rules/1606)._
