cakephp - View link if depended on user rights -
i working cakephp 2.x. have 3 columns:
user | course | usercourserole
each user can edit multiple courses , 1 course can edited multiple users. far good.
if user wants see index of courses i want show 'edit'-link next courses can in fact edit. how can realize this? figured have set sort of field inside coursecontroller , check field inside view. right way go?
my current code is
coursecontroller.php
... public function index() { $courses = $this->course->find('all', array('recursive' => 2)); $this->set('courses', $courses); } ...
courses/index.ctp
<!-- file: /app/view/courses/index.ctp --> ... <?php foreach ($courses $course):?> ... <?php echo $this->html->link('edit', array('action' => 'edit', $course['course']['id'])); ?> ...
in beforerender() or beforefilter() set $this->auth->user() variable view, example userdata.
$this->set('userdata', $this->auth->user());
implement (auth)helper uses variable (you can make configurable helper setting) , checks like:
if ($this->auth->hasrole($course['course']['role']) { /* ... */ } if ($this->auth->isloggedin() { /* ... */ } if ($this->auth->isme($course['course']['user_id']) { /* ... */ }
implement hasrole() method according whatever specific requirements are.
doing helper bunch of advantages, easy reuse, overload , adapt whatever checks , don't use component in view plus should avoid calling statics , singletons lot in app. pretty easy read , understand code does.
Comments
Post a Comment