-
Notifications
You must be signed in to change notification settings - Fork 24
isownedby
TripleZoom edited this page Sep 16, 2017
·
1 revision
The IsOwnedBy-Behavior is used to check easily of an entity is owned by a specific user.
You can load the behavior by the following code:
public function initialize(array $config) {
// code
$this->addBehavior('Utils.IsOwnedBy');
}
Default, the behaviors watches the column user_id
to check if the column contains the users user-id.
This can be changed with this code:
public function initialize(array $config) {
// code
$this->addBehavior('Utils.IsOwnedBy', [
'column' => 'created_by'
]);
}
Now, the column created_by
will be used.
Via the method IsOwnedBy
you can validate if the entity belongs to an user. Look at this example:
if($this->Articles->isOwnedBy($this->Articles->get(1), $user)) {
// The article is owned by the user ($user)
} else {
// The article isn't owned by the user ($user)
}
The first parameter can be an Entity
or array
. The second parameter is the user. Use $this->Auth->user();
in your
controller to get the user.