Welcome Guest, Not a member yet? Register   Sign In
how to show parts of my view, if user does have access?
#1

[eluser]K.OS[/eluser]
Hi,

quick question, how would you approach this problem:

I have one view, lets say a view called 'nav'. This view contains the complete menu structure for my site. I have some Users with different roles and not all of them are allowed to access all function through the menu.

i blocked access for some controller functions with this function:
Code:
function requireUserRole($role = 3)
    {
      if ($role >= $this->session->userdata['role']) {
     return true;
      }else{
      return false;
      }

    }

only if this function call is true, certain other function will be executed… this works well to block access to functions in my controller.

But because of the MVC structure i want to avoid calling this functions in my controller as well. How would you filter your view to show just some entries?

Code:
<ul>
  &lt;!-- this should be available to every user --&gt;
  <li>
      <a href="&lt;?= site_url('presentation/add') ?&gt;">neue Präsentation</a>
  </li>
  &lt;!-- this item should just be available to certain users with certain roles --&gt;
  <li>
      <a href="&lt;?= site_url('presentation/list_all') ?&gt;">alle Präsentationen</a>
      <a href="&lt;?= site_url('presentation/list_all/xml') ?&gt;">xml</a>
  </li>
</ul>
#2

[eluser]Clifford James[/eluser]
Something like this?

Code:
<ul>
  &lt;!-- this should be available to every user --&gt;
  <li>
      <a href="&lt;?= site_url('presentation/add') ?&gt;">neue Präsentation</a>
  </li>
  &lt;!-- this item should just be available to certain users with certain roles --&gt;
  &lt;? if (requireUserRole($user->role)) : ?&gt;
  <li>
      <a href="&lt;?= site_url('presentation/list_all') ?&gt;">alle Präsentationen</a>
      <a href="&lt;?= site_url('presentation/list_all/xml') ?&gt;">xml</a>
  </li>
  &lt;? endif ?&gt;
</ul>
#3

[eluser]K.OS[/eluser]
but as far as i know you should not load a controller method in view?! correct me if I am wrongSmile
#4

[eluser]mddd[/eluser]
You can put the 'user check' functions in a model (let's call it user_role_model). Then if you pass a reference to that model to the view like this:
Code:
$this->load->model('user_role_model');
$this->load->view('my_view', array('user_roles'=>$this->user_role_model));
Now you can call methods from this model in the view:
Code:
&lt;? if ($user_roles->requireUserRole($user->role)) : ?&gt;
  <li>
      <a href="&lt;?= site_url('presentation/list_all') ?&gt;">alle Präsentationen</a>
      <a href="&lt;?= site_url('presentation/list_all/xml') ?&gt;">xml</a>
  </li>
  &lt;? endif ?&gt;
(You probably don't really need to pass the model to the view because things that are loaded in the controller are usually also present in the view. But I think it good to make it explicit what you are passing and using in the view).




Theme © iAndrew 2016 - Forum software by © MyBB