Edit button only in records the user has edit rights?

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
panoss
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Thu Apr 12, 2012 8:30 am

Edit button only in records the user has edit rights?

Post by panoss » Tue Oct 13, 2015 6:34 am

I 'm developing a component.
In my list view I want the edit button to be appeared only on records on which the user has edit rights.
So, if the list shows 5 records, and the user has edit rights only for one record, the edit button to appear only on this record 's row, and not on the others.

In my template I used:

Code: Select all

if(JFactory::getUser()->authorise('core.edit', 'com_custom.adv.' . $item->id)){
   ...show Edit button
}
But it's not working.
In the assets table, field 'rules' things seem ok.
What should I check?

panoss
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Thu Apr 12, 2012 8:30 am

Re: Edit button only in records the user has edit rights?

Post by panoss » Tue Oct 13, 2015 8:16 am

Here is a summary ( ;) ) of my code that works(it's for both edit and delete buttons)!

Code: Select all

<?php foreach ($this->items as $i => $item) : ?>
    <?php $canEdit = $user->authorise('core.edit', 'com_custom'); ?>
    <?php $canDelete = $user->authorise('core.delete', 'com_custom'); ?>

    <?php if (!$canEdit && $user->authorise('core.edit.own', 'com_custom')): ?>
        <?php $canEdit = JFactory::getUser()->id == $item->created_by; ?>
    <?php endif; ?>

    <?php if (!$canDelete && $user->authorise('core.edit.own', 'com_custom')): ?>
        <?php $canDelete = JFactory::getUser()->id == $item->created_by; ?>
    <?php endif; ?>

    <?php if (isset($this->items[0]->id)): ?>
        <td class="center hidden-phone">
            <?php echo (int) $item->id; ?>
        </td>
    <?php endif; ?> 
    <td>
        <?php echo $item->name; ?>
    </td>                
    <td>
        <?php echo $item->content; ?>
    </td>

    <?php if ($canEdit || $canDelete): ?>
        <td class="center">
            <?php if ($canEdit): ?>
                <a href="<?php echo JRoute::_('index.php?option=com_custom&task=adv.edit&id=' . $item->id, false, 2); ?>" class="btn btn-mini" type="button"><i class="icon-edit" ></i></a>
            <?php endif; ?>
            <?php if ($canDelete): ?>
                <button data-item-id="<?php echo $item->id; ?>" class="btn btn-mini delete-button" type="button"><i class="icon-trash" ></i></button>
                <?php endif; ?>
        </td>
    <?php endif; ?>

    </tr>
<?php endforeach; ?>

I 'm not sure this part is correct:

Code: Select all

    <?php if (!$canDelete && $user->authorise('core.edit.own', 'com_custom')): ?>
        <?php $canDelete = JFactory::getUser()->id == $item->created_by; ?>
    <?php endif; ?>
What do you think?

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 30887
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Edit button only in records the user has edit rights?

Post by Per Yngve Berg » Tue Oct 13, 2015 10:21 am

The edit permission is not inherited from a parent level?

panoss
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Thu Apr 12, 2012 8:30 am

Re: Edit button only in records the user has edit rights?

Post by panoss » Tue Oct 13, 2015 10:32 am

No, it 's not inhereted.
Parent is 'Registered', I think.

-Registered
--Author
-Registered
Action: Edit
inhereted
calculated setting: not allowed


-Registered
--Author
Action: Edit
allowed
calculated setting: Allowed
So group 'Registered' inherits 'Edit not allowed'.
Group 'Author' does not inherit 'Edit'. Sets it's own edit right to 'allowed'.

Which results to a calculated setting: 'Allowed' for edit action.

Although I've read much about ACL I'm not sure I understand much.
Are they correct?

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 30887
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Edit button only in records the user has edit rights?

Post by Per Yngve Berg » Tue Oct 13, 2015 11:01 am

You check the Authorisation against the Component Level, not the Item.

panoss
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Thu Apr 12, 2012 8:30 am

Re: Edit button only in records the user has edit rights?

Post by panoss » Tue Oct 13, 2015 11:12 am

In my first post, I think I check against item level.

Code: Select all

JFactory::getUser()->authorise('core.edit', 'com_custom.adv.' . $item->id)
In my second post, I think I check against the component level.

Code: Select all

$canEdit = $user->authorise('core.edit', 'com_custom'); 
If I understand correctly, you 're suggesting that I should check against item level?
(that sounds logical to me but doesn't work, while against component level, it does work. I suppose I have wrong settings in ACL)

Edit: 'Author' is a subgroup of 'Registered'.
So when I changed Author 's setting for 'Edit' from 'inhereted' to 'allowed', joomla should alert me with a message that there is a conflict because it 's parent group ('Registered') has Edit to 'not allowed' (calculated) and new setting cannot be accepted.
Am I wrong?

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 30887
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Edit button only in records the user has edit rights?

Post by Per Yngve Berg » Tue Oct 13, 2015 11:41 am

Code: Select all

JFactory::getUser()->authorise('core.edit', 'com_custom.adv.' . $item->id)
This will work only when you put every Item into the Assets Table.

Code: Select all

<?php if (!$canDelete && $user->authorise('core.edit.own', 'com_custom')): ?>
        <?php $canDelete = JFactory::getUser()->id == $item->created_by; ?>
    <?php endif; ?>
This should work without having every item in the Assets Table.

panoss
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Thu Apr 12, 2012 8:30 am

Re: Edit button only in records the user has edit rights?

Post by panoss » Tue Oct 13, 2015 11:55 am

Per Yngve Berg wrote:

Code: Select all

JFactory::getUser()->authorise('core.edit', 'com_custom.adv.' . $item->id)
This will work only when you put every Item into the Assets Table.
I do put every item in the Assets table.

But it doesn't work.


Locked

Return to “Joomla! 3.x Coding”