Skip to content

Commit 8edd454

Browse files
committed
Updated Docs
Updated the docs with changes made in the 845ad99
1 parent 845ad99 commit 8edd454

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

docs/usage/blade_templates.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ Three directives are available for use within your Blade templates. What you giv
2020
<p>This is visible to users with the given abilities. Gets translated to
2121
\Laratrust::ability('admin,owner', 'create-post,edit-user')</p>
2222
@endability
23+
24+
@canAndOwns('edit-post', $post)
25+
<p>This is visible if the user has the permission and owns the object. Gets translated to
26+
\Laratrust::canAndOwns('edit-post', $post)</p>
27+
@endOwns
28+
29+
@hasRoleAndOwns('admin', $post)
30+
<p>This is visible if the user has the role and owns the object. Gets translated to
31+
\Laratrust::hasRoleAndOwns('admin', $post)</p>
32+
@endOwns

docs/usage/concepts.rst

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ The ``Laratrust`` class has a shortcut to ``ability()`` for the currently logged
252252
253253
Auth::user()->ability('admin,owner', 'create-post,edit-user');
254254
255-
Model's Ownership
255+
Objects's Ownership
256256
-----------------
257257

258-
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``:
259259

260260
.. code-block:: php
261261
@@ -279,14 +279,79 @@ If you want to change the foreign key name to check for, you can pass a second a
279279
...
280280
}
281281
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).
292+
293+
The third parameter is an options array:
294+
295+
.. code-block:: php
296+
297+
$options = [
298+
'requireAll' => true | false (Default: false),
299+
'foreignKeyName' => 'canBeAnyString' (Default: null)
300+
];
301+
302+
Here's an example of the usage of both methods:
303+
304+
.. code-block:: php
305+
306+
$post = Post::find(1);
307+
$user->canAndOwns('edit-post', $post);
308+
$user->canAndOwns(['edit-post', 'delete-post'], $post);
309+
$user->canAndOwns(['edit-post', 'delete-post'], $post, ['requireAll' => false, 'foreignKeyName' => 'writer_id']);
310+
311+
$user->hasRoleAndOwns('admin', $post);
312+
$user->hasRoleAndOwns(['admin', 'writer'], $post);
313+
$user->hasRoleAndOwns(['admin', 'writer'], $post, ['requireAll' => false, 'foreignKeyName' => 'writer_id']);
314+
315+
316+
The ``Laratrust`` class has a shortcut to ``owns()``, ``canAndOwns`` and ``hasRoleAndOwns`` methods for the currently logged in user:
283317

284318
.. code-block:: php
285319
286320
Laratrust::owns($post);
287321
Laratrust::owns($post, 'idUser');
288322
289-
// is identical to
323+
Laratrust::canAndOwns('edit-post', $post);
324+
Laratrust::canAndOwns(['edit-post', 'delete-post'], $post, ['requireAll' => false, 'foreignKeyName' => 'writer_id']);
325+
326+
Laratrust::hasRoleAndOwns('admin', $post);
327+
Laratrust::hasRoleAndOwns(['admin', 'writer'], $post, ['requireAll' => false, 'foreignKeyName' => 'writer_id']);
328+
329+
Ownable Interface
330+
^^^^^^^^^^^^^^^^^
290331

291-
Auth::user()->owns($post);
292-
Auth::user()->owns($post, 'idUser');
332+
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

Comments
 (0)