Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most simple solution: Add a condition in your controller, so:</p> <pre><code>$this-&gt;set('books', $this-&gt;Book-&gt;find( 'all', array('conditions' =&gt; array('Book.user_id' =&gt; $user['User']['id'])) ); </code></pre> <p>Disadvantages: You will likely create duplicate code here since this check has to happen also in other places. Also when you start testing your model you can only test that it returns books, you cannot test a model method like: getMyBooks($userId). So no, not the preferred solution.</p> <p><strong>Next solution: Check in the model</strong></p> <p>It could be done by a check in for example your books model. You could just check in the afterfind() method whether the returned records are allowed or not. In your beforefind you could also add an additional condition to all queries.</p> <p>In general a model should be fat so I would suggest implementing clear methods there like: getAllBooks, getBooksOfUser($User), getLatestBooksOfUser($User) etc.</p> <p>Why is this a nice implementation? Because you now manage the access levels in a central place. You can test the model and you are sure it does only return books from this user.</p> <p>With beforeSave etc. you can intervene every save attempt and first check: hey, you want to save this but is this really <strong>your</strong> book?</p> <p><strong>ACL solution</strong></p> <p>But in general it could be wise to implement some ACL solution (preferably the built in one) since that makes you application much more future proof. It allows flexibility, for example:</p> <blockquote> <p>Each user may have access to a certain set of books. I don't need various roles.</p> </blockquote> <p>That's true for now but the future can change it. So if you need a quick solution just custom filter the records. But think about the future.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload