CoreFiles by mySites.guru
FieldsController.php189 lines, 5,030 bytes, from Joomla_6.1.4-Stable-Full_Package.zip
Raw History Compare with 6.1.3
md5
bc2f0482e6375bc0775f9950dcd2083b
sha1
c669c01848282755f37191b671caf5a9fb5a56fc
sha256
078782eafc3b1d4253867c1648bbae7fb4d701f74ed2522b1e347baa58e59def
<?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   5.4.7
     * @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   5.4.7
     * @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   5.4.8
     * @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 7 releases, Joomla 6.1.4-rc1 to 6.2.0. See them all, or every version of this file.