Hide Add/Edit panel part if role and template equals

Trying to prevent certain user roles from not adding or editing pages on specific templates. Any ideas what I am doing wrong? I get a really weird and long php error.

file edited: /panel/app/snippets/pages/sidebar/subpages.php

<?php if( $user = $site->user() and $user->hasRole('creative') && !$page->template() == 'default'  ) { ?>
    <?php // hide and show add edit here based on user role and template ?>
<?php } ?>

error: http://pastebin.com/6YitAM2u

Try this (in subpage.php, not subpages.php)

<?php $user = site()->user(); if($user->hasRole('admin') && $subpage->template() != 'default'): ?>
<li>
  <a class="draggable" data-helper="<?php echo esc($subpage->title(), 'attr') ?>" data-text="<?php echo esc(dragText($subpage), 'attr') ?>" href="<?php _u($subpage, 'show') ?>">
    <?php i($subpage) ?><span><?php __($subpage->title()) ?></span>
    <small class="marginalia shiv shiv-left shiv-white"><?php __(n($subpage)) ?></small>
  </a>
</li>
<?php endif; ?>

Thanks but not what i wanted… this ends up hiding the page all together from the panel. I just need to remove the abilty to add or edit pages in certain templates.

I guess I misread the issue.

Wrap the buttons in the subpages.php in a similar if statement as above. The issue above was your code, so the same principle applies. You were declaring your $user variable inside your if statement. Notice the examples I’ve shared declare it before starting the if statement. Also $site didn’t seem to exist, so I used site(). You could also use $site = site(); $user = $site->user(); if you wanted it available on the page for other reasons.

  <?php $user = site()->user(); if($user->hasRole('admin') && $page->template() != 'default'): ?>
  <span class="hgroup-options shiv shiv-dark shiv-left">
    <span class="hgroup-option-right">
      <a title="<?php _l('pages.show.subpages.edit') ?>" href="<?php _u('subpages/index/' . $page->id()) ?>">
        <?php i('pencil', 'left') ?><span><?php _l('pages.show.subpages.edit') ?></span>
      </a>
      <?php if($addbutton): ?>
      <a title="+" data-shortcut="+" href="<?php _u($page, 'add') ?>">
        <?php i('plus-circle', 'left') ?><span><?php _l('pages.show.subpages.add') ?></span>
      </a>
      <?php endif ?>
    </span>
  </span>
  <?php endif; ?>

Ah I see, just some syntax errors. Thats how it was written in kirbys docs, maybe they should update it:
http://getkirby.com/docs/cheatsheet/user/has-role

Your a life saver, thanks for your help.

The example they provided is meant to be used in templates, where $user and $site are already made available by Kirby. So it is checking if $user is equal to $site->user() and if it’s role is “editor”.

Within the snippet in question though, those variables weren’t declared. So declaring them rather than double checking that they’re declared was the solution.

There might also be an officially supported way for this coming up.

1 Like

Hi! Can anyone help me with this :wink: It is solved but for the newbies (like me) there is still allot to learn. And I really need this ’ function’.

@Luke can you explain a little bit more?

For example. What do I need to put where when I want one template to load for a specific user role. I am still learning PHP so please forgive me for the newbie questions.

@cirstenkot, do you want certain pages of your site to only be visible if a user is logged in and has a certain role?

If so, click here for more information on restricting content/templates by user roles

There’s a lot of other information about the authentication system there as well, such as how to handle user log ins from within the site itself (not the panel), creating custom roles, etc.

This subject was more about modifying the panel, which from the sounds of it wasn’t what you really need. If I’m wrong about that let me know and perhaps you could explain exactly what you’re trying to achieve.

@Luke tnx for the reply! Your fast :smile:

What I need. For a newbie php developer it is a big big big dream :wink: (Ok I will stop using emoticons…)

I want a client to register --> role is client --> I want the clients to add content, the best way is that the clients only will have one certain blueprint / template. --> user (best scenario) is not able to see content from other users even if they have the same role.

I don’t think it is even possible for now.

Thank you again for taking time!

I believe this is definitely possible, but it would involve some development effort on your part. I’ve wrote a frontend registration/login/authentication/profile/etc setup for Kirby 1 a long time ago, but it utilized MySQL and didn’t integrate with the panel.

To summarize…

User visits …/registration page, fills out form. This is a front end template. Form utilizes Kirby’s $users->create() to create user using form inputs and set custom role w/o panel access. See the authentication docs from my last post for info on creating custom roles.

User visits …/login page, logs in. Redirects to …/create-content page. Again, both front end templates.

The “create-content” page has whatever form fields necessary to create your page. Form inputs feed into $pages->create() to create the page from the front end.

Doing it this way means you don’t have to modify the core panel files at all as well.

1 Like

Ok I will give it a try. Can I find your code somewhere?

I think this is the best option possible. Because changing core will be a bad idea ; )

Tnx again!

My code belongs to a corporation and is in a private repository. It wouldn’t be of much help here anyway. 1) It’s outdated Kirby 1 code. 2) it uses mysql databases and doesn’t tie in with the panel/kirby user setup. 3) it doesn’t use either the $users->create() or $pages->create() features available in Kirby 2. It actually doesn’t create pages at all… It only manages user profile information, test results, certification status, etc by communicating with MySQL.

1 Like

ok wel I will give it a try just bij using the Kirby 2 code. If I manage to come up with something I will let you know! Thnx again for the workflow! It is a way to to start.