You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you need to check if the user owns a model you can use the user function ``owns``:
258
+
If you need to check if the user owns an object you can use the user function ``owns``:
259
259
260
260
.. code-block:: php
261
261
@@ -279,14 +279,79 @@ If you want to change the foreign key name to check for, you can pass a second a
279
279
...
280
280
}
281
281
282
-
The ``Laratrust`` class has a shortcut to ``owns()`` method for the currently logged in user:
282
+
Permissions, Roles and Ownership Checks
283
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
284
+
285
+
If you want to check if an user can do something or has a role, and also is the owner of an object you can use the ``canAndOwns`` and ``hasRoleAndOwns`` methods:
286
+
287
+
Both methods accept three parameters:
288
+
289
+
* ``permission`` or ``role`` are the permission or role to check (This can be an array of roles or permissions).
290
+
* ``thing`` is the object used to check the ownership .
291
+
* ``options`` is a set of options to change the method behavior (optional).
If the object ownership is witha a more complex logic you can implement the Ownable interface so you can use the ``owns``, ``canAndOwns`` and ``hasRoleAndOwns`` methods in these cases:
333
+
334
+
.. code-block:: php
335
+
336
+
class SomeOwnedObject implements \Laratrust\Contracts\Ownable
337
+
{
338
+
...
339
+
340
+
public function ownerKey()
341
+
{
342
+
return $this->someRelationship->user->id;
343
+
}
344
+
345
+
...
346
+
}
347
+
348
+
.. NOTE::
349
+
The ``ownerKey`` method **must** return the object's owner id value.
350
+
351
+
And then in your code you can simply do:
352
+
353
+
.. code-block:: php
354
+
355
+
$user = User::find(1);
356
+
$theObject = new SomeOwnedObject;
357
+
$user->owns($theObject); // This will return true or false depending of what the ownerKey method returns
0 commit comments