PHP code example of devtools-marvellous / security-request

1. Go to this page and download the library: Download devtools-marvellous/security-request library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devtools-marvellous / security-request example snippets


    $this->action; //Массив дейсвия.
    $this->actionName; //Имя действия. Находится в ключе.

class CategoryRequest extends FormRequest
{
    use SecurityRequest;
     
    /**
     * Actions.
     *
     * @var array
     */
    protected $actions = [
        'view' => [
            'methods'    => [ 'GET' ],
            'permission' => 'default',
        ],
        'add'  => [
            'methods'    => [ 'POST' ],
            'permission' => 'default',
        ],
        'edit' => [
            'methods'    => [ 'PUT', 'PATCH' ],
            'permission' => 'default',
        ],
        'delete' => [
            'methods'    => [ 'DELETE' ],
            'permission' => 'default',
        ]
    ];

    /**
     * Rules array.
     *
     * @return array
     */
    public function rulesArray()
    {
        $rules = [
            'uri'        => 'in', 'Name must be at least :min characters in length.', [ ':min' => 3 ]),
            'name_*.max'        => trans_db(app('translations'), 'validation-categories-name-max', 'Name must be maximum :max characters in length.', [ ':max' => 255 ]),
        ];
    }

    /**
     * @return array
     */
    protected function postActionMessages()
    {
        return $this->messagesArray();
    }

    /**
     * Get action rules
     *
     * @return array
     */
    protected function getAction()
    {
        return [ ];
    }

    /**
     * Post action rules
     *
     * @return array
     */
    protected function postAction()
    {
        return $this->rulesArray();
    }

    /**
     * Put action rules
     *
     * @return array
     */
    protected function putAction()
    {
        $rules = $this->rulesArray();
        $category_id = $this->route('category');
        $rules['uri'] = [ '

'delete' => [
    'methods'    => [ 'DELETE' ],
    'permission' => 'change-log-delete',
],

'edit'    => [
    'methods'    => [ 'PUT', 'PATCH' ],
    'route'      => 'projects/*/groups/*',
    'permission' => 'default',
],
'move-to' => [
    'methods'    => [ 'PUT', 'PATCH' ],
    'route'      => 'projects/*/groups/move',
    'permission' => 'group-task-actions',
],

$this->is('projects/*/groups/move')

/**
* Put method rules apply.
*/
protected function putEditAction()
{
    return [];
}

/**
* Put method rules apply.
*/
protected function putMoveToAction()
{
    return [];
}

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    $group_id = $this->route('group');
    $user = detectUser()->user;
    if (
        ! $this->project->isTeammates([ detectUser()->user ]) ||
        ($group_id && ! $this->project->groups->contains('id_task_group', $group_id)) ||
        ($this->actionName == 'edit' && $this->task_group_status_id == 2 && $user->cannot('group-begin')) ||
        ($this->actionName == 'edit' && $this->task_group_status_id == 3 && $user->cannot('group-close'))
    ) {
        return false;
    }

    return parent::authorize();
}