- md5
- 090685bef48cfd4655afb4fe9490cfe3
- sha1
- b8f2680328b7f64024f2a2b11deb3c8f3984b864
- sha256
- 9c1aaa766cbfc807529a21c6418552b0930ce28f64ad705a6a58fd776c2bb5e4
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
<?php
/**
* @package Joomla.API
* @subpackage com_menus
*
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Menus\Api\Controller;
use Joomla\CMS\Access\Exception\NotAllowed;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\ApiController;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\Component\Menus\Api\View\Items\JsonapiView;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* The items controller
*
* @since 4.0.0
*/
class ItemsController extends ApiController
{
/**
* The content type of the item.
*
* @var string
* @since 4.0.0
*/
protected $contentType = 'items';
/**
* The default view for the display method.
*
* @var string
* @since 3.0
*/
protected $default_view = 'items';
/**
* Basic display of an item view
*
* @param integer $id The primary key to display. Leave empty if you want to retrieve data from the request
*
* @return static A \JControllerLegacy object to support chaining.
*
* @since 4.0.0
*/
public function displayItem($id = null)
{
$this->modelState->set('filter.client_id', $this->getClientIdFromInput());
return parent::displayItem($id);
}
/**
* Basic display of a list view
*
* @return static A \JControllerLegacy object to support chaining.
*
* @since 4.0.0
*/
public function displayList()
{
$apiFilterInfo = $this->input->get('filter', [], 'array');
$filter = InputFilter::getInstance();
if (\array_key_exists('menutype', $apiFilterInfo)) {
$this->modelState->set('filter.menutype', $filter->clean($apiFilterInfo['menutype'], 'STRING'));
}
$this->modelState->set('filter.client_id', $this->getClientIdFromInput());
return parent::displayList();
}
/**
* Method to add a new record.
*
* @return void
*
* @since 4.0.0
* @throws NotAllowed
* @throws \RuntimeException
*/
public function add()
{
$data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');
if (isset($data['menutype'])) {
$this->input->set('menutype', $data['menutype']);
$this->input->set('com_menus.items.menutype', $data['menutype']);
}
isset($data['type']) && $this->input->set('type', $data['type']);
isset($data['parent_id']) && $this->input->set('parent_id', $data['parent_id']);
isset($data['link']) && $this->input->set('link', $data['link']);
$this->input->set('id', '0');
parent::add();
}
/**
* Method to edit an existing record.
*
* @return static A \JControllerLegacy object to support chaining.
*
* @since 4.0.0
*/
public function edit()
{
$data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');
if (isset($data['menutype'])) {
$this->input->set('menutype', $data['menutype']);
$this->input->set('com_menus.items.menutype', $data['menutype']);
}
isset($data['type']) && $this->input->set('type', $data['type']);
isset($data['parent_id']) && $this->input->set('parent_id', $data['parent_id']);
isset($data['link']) && $this->input->set('link', $data['link']);
return parent::edit();
}
/**
* Method to check if it's allowed to add a new menu item.
*
* Like in the administrator, create permission is checked on the menu type of the new item and the protected
* administrator menu "main" can not be used.
*
* @param array $data An array of input data.
*
* @return boolean
*
* @since 6.2.1
*/
protected function allowAdd($data = [])
{
return $this->authoriseMenuType('core.create', $this->input->getCmd('menutype', $data['menutype'] ?? ''));
}
/**
* Method to check if it's allowed to edit an existing menu item.
*
* Like in the administrator, edit permission is checked on the menu type of the item and items of the protected
* administrator menu "main" can not be edited. Moving an item to another menu requires create permission on
* the target menu.
*
* @param array $data An array of input data.
* @param string $key The name of the key for the primary key; default is id.
*
* @return boolean
*
* @since 6.2.1
*/
protected function allowEdit($data = [], $key = 'id')
{
$table = $this->getModel('item', '', ['ignore_request' => true])->getTable();
if (!isset($data[$key]) || !$table->load((int) $data[$key])) {
return false;
}
if (!$this->authoriseMenuType('core.edit', (string) $table->menutype)) {
return false;
}
// Moving the item to another menu
$newMenuType = $this->input->getCmd('menutype', '');
if ($newMenuType !== '' && $newMenuType !== $table->menutype) {
return $this->authoriseMenuType('core.create', $newMenuType);
}
return true;
}
/**
* Method to check if it's allowed to delete a menu item.
*
* Like in the administrator, delete permission is checked on the menu type of the item and items of the
* protected administrator menu "main" can not be deleted.
*
* @return boolean
*
* @since 6.2.1
*/
protected function allowDelete(): bool
{
$table = $this->getModel('item', '', ['ignore_request' => true])->getTable();
if (!$table->load($this->input->get('id', 0, 'int'))) {
return false;
}
return $this->authoriseMenuType('core.delete', (string) $table->menutype);
}
/**
* Check a permission on the asset of a menu type. The protected administrator menu "main" is always denied.
*
* @param string $action The action to check, e.g. core.edit
* @param string $menuType The menu type
*
* @return boolean
*
* @since 6.2.1
*/
private function authoriseMenuType(string $action, string $menuType): bool
{
// Protected menu type, do not allow any change
if ($menuType === '' || $menuType === 'main') {
return false;
}
$menuTypeTable = $this->getModel('item', '', ['ignore_request' => true])->getTable('MenuType');
$menuTypeTable->load(['menutype' => $menuType]);
if (!$menuTypeTable->id) {
return false;
}
return $this->app->getIdentity()->authorise($action, 'com_menus.menu.' . (int) $menuTypeTable->id);
}
/**
* Return menu items types
*
* @return static A \JControllerLegacy object to support chaining.
*
* @since 4.0.0
*/
public function getTypes()
{
$viewType = $this->app->getDocument()->getType();
$viewName = $this->input->get('view', $this->default_view);
$viewLayout = $this->input->get('layout', 'default', 'string');
try {
/** @var JsonapiView $view */
$view = $this->getView(
$viewName,
$viewType,
'',
['base_path' => $this->basePath, 'layout' => $viewLayout, 'contentType' => $this->contentType]
);
} catch (\Exception $e) {
throw new \RuntimeException($e->getMessage());
}
/** @var ListModel $model */
$model = $this->getModel('menutypes', '', ['ignore_request' => true]);
if (!$model) {
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
}
$model->setState('client_id', $this->getClientIdFromInput());
$view->setModel($model, true);
$view->document = $this->app->getDocument();
$view->displayListTypes();
return $this;
}
/**
* Get client id from input
*
* @return string
*
* @since 4.0.0
*/
private function getClientIdFromInput()
{
return $this->input->exists('client_id') ?
$this->input->get('client_id') : $this->input->post->get('client_id');
}
}
No other release has this exact content. See every version of this file.