- md5
- 6636f5fabfe16c3bb50283eb0534a408
- sha1
- 4d23ac0ffaf1f3102f6c5b03eadda74852866037
- sha256
- b4e67610518b9508193088b7c92923e5bfa9acd1c65a04f51b1c78193eced1c4
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
<?php
/**
* @package Joomla.API
* @subpackage com_fields
*
* @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\Fields\Api\Controller;
use Doctrine\Inflector\InflectorFactory;
use Joomla\CMS\MVC\Controller\ApiController;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* The fields controller
*
* @since 4.0.0
*/
class FieldsController extends ApiController
{
/**
* The content type of the item.
*
* @var string
* @since 4.0.0
*/
protected $contentType = 'fields';
/**
* The default view for the display method.
*
* @var string
* @since 3.0
*/
protected $default_view = 'fields';
/**
* 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.context', $this->getContextFromInput());
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()
{
$this->modelState->set('filter.context', $this->getContextFromInput());
return parent::displayList();
}
/**
* Get extension from input
*
* @return string
*
* @since 4.0.0
*/
private function getContextFromInput()
{
return $this->input->exists('context') ?
$this->input->get('context') : $this->input->post->get('context');
}
/**
* Method to check if you can add a new record.
*
* We don't allow adding from API
*
* @param array $data An array of input data.
*
* @return boolean
*
* @since 6.1.2
*/
protected function allowAdd($data = [])
{
$user = $this->app->getIdentity();
[$option] = explode('.', $this->getContextFromInput());
// Require generic management permissions for the component
if (!$user->authorise('core.manage', $option)) {
return false;
}
return $user->authorise('core.create', $option);
}
/**
* Method to check if you can edit an existing record.
*
* We don't allow editing from API (yet?)
*
* @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.1.2
*/
protected function allowEdit($data = [], $key = 'id')
{
$recordId = isset($data[$key]) ? (int) $data[$key] : 0;
$user = $this->app->getIdentity();
[$option] = explode('.', $this->getContextFromInput());
// Require generic management permissions for the component
if (!$user->authorise('core.manage', $option)) {
return false;
}
// Zero record (id:0), return component edit permission by calling parent controller method
if (!$recordId) {
return $user->authorise('core.edit', $option);
}
// Get existing record
$inflector = InflectorFactory::create()->build();
$record = $this->getModel($inflector->singularize($this->contentType))->getItem($recordId);
if (empty($record)) {
return false;
}
[$recordOption] = explode('.', $record->context);
// Validate request context and field context match
if ($recordOption !== $option) {
return false;
}
// Check edit on the record asset (explicit or inherited)
if ($user->authorise('core.edit', $option . '.field.' . $recordId)) {
return true;
}
// Check edit own on the record asset (explicit or inherited)
if ($user->authorise('core.edit.own', $option . '.field.' . $recordId)) {
// Grant if current user is owner of the record
return $user->id == $record->created_user_id;
}
return false;
}
/**
* Method to check if it's allowed to delete a record
*
* @return boolean
*
* @since 6.1.3
*/
protected function allowDelete(): bool
{
[$option] = explode('.', $this->getContextFromInput());
$user = $this->app->getIdentity();
// Require generic management permissions for the component
if (!$user->authorise('core.manage', $option)) {
return false;
}
return $user->authorise('core.delete', $option);
}
}
The same content is in 2 releases, Joomla 6.1.3 to 6.2.0-beta1. See them all, or every version of this file.