Welcome Guest, Not a member yet? Register   Sign In
Filters and $_POST (or $request post vars)
#1

I want to automatically add variables to each incoming POST, so I thought filters would be helpful, however, I can't see a way to get to the POST vars in the filter class

PHP Code:
class Filters extends BaseConfig
{
 
 public 
$aliases = [
 
'csrf'    => CSRF::class,
 
'toolbar'  => DebugToolbar::class,
 
'honeypot' => Honeypot::class,
 'postfilter' => PostFilter::class
 ];
 public 
$globals = [
 
'before' => [
 
// 'honeypot',
 // 'csrf',
 
],
 
'after'  => [
 
'toolbar',
 
// 'honeypot',
 
],
 ];

 public 
$methods = [
 
    'post' => ['postfilter']
    ];

 public 
$filters = [];


the PostFilters class:

PHP Code:
class PostFilter implements FilterInterface
{
    public function before(RequestInterface $request$arguments null)
    {
        $_POST['Maint_Userid'] = "plugh";
        $_POST['Create_Userid'] = "plugh";
        return $request;
    }

    public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {
        // Do something here
    }


I've also tried:
PHP Code:
public function before(RequestInterface $request$arguments null)
    {
        $post_request = \Config\Services::request();
        $post_request->Maint_Userid "plugh";
        $post_request->Create_Userid "plugh";
        return $request;
    
PHP Code:
public function before(RequestInterface $request$arguments null)
    {
        $request['Maint_Userid'] = "plugh";
        $request['Create_Userid'] = "plugh";

        return $request;
    
(and a few other variations of the above)

It is adding the varaibles to the RequestInterface $request (from log_messages, I see this). Even the direct $_POST didn't add it to the post vars in the controller.
Where should I be doing this (or how do I make the filters see the $_POST vars and add to them)?
Thanks.
Reply
#2

(This post was last modified: 07-29-2021, 02:24 PM by Gary.)

Try using $_REQUEST['Maint_Userid']... or whatever the form/post variable's name it is you want to access from inside the Filter.

Code:
public function before(RequestInterface $request, $arguments = null)
    {
        $maint_Userid = $_REQUEST['Maint_Userid'];
        $create_Userid = $_REQUEST['Create_Userid'];
        ...
Reply
#3

(07-29-2021, 02:19 PM)Gary Wrote: Try using $_REQUEST['Maint_Userid']... or whatever the form/post variable's name it is you want to access from inside the Filter.

Code:
public function before(RequestInterface $request, $arguments = null)
    {
        $maint_Userid = $_REQUEST['Maint_Userid'];
        $create_Userid = $_REQUEST['Create_Userid'];
        ...

That would get me the values in the POST. I'm looking to *add* values to the POST for processing (as in, for each POST, I want to add the user's ID for both the create and maint fields)
Reply
#4

[quote pid="389059" dateline="1627911401"]
you need api ctl other ctl extend from it  after log in call $userId
[/quote]
you dont need add data to post request

PHP Code:
<?php

namespace Modules\Shared\Controllers;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *    class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 *
 * @package CodeIgniter
 */


use CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\RESTful\ResourceController;
use 
Myth\Auth\AuthTrait;
use 
Psr\Log\LoggerInterface;
use  Modules\Shared\Interfaces\UrlQueryParamInterface;
use  Modules\Shared\Libraries\UrlQueryParam;

class 
ApiController extends ResourceController
{
    use AuthTrait;

    protected $format "";

    public int $userId;
    public object $userObject;
    public array $userGroup;
    public UrlQueryParamInterface $urlQueryParam;

    /**
    * An array of helpers to be loaded automatically upon
    * class instantiation. These helpers will be available
    * to all other controllers that extend BaseController.
    *
    * @var array
    */
    protected $helpers = ['cookie''url''from''filesystem','text'];

    /**
    * Constructor.
    *
    * @param RequestInterface $request
    * @param ResponseInterface $response
    * @param LoggerInterface $logger
    */


    /**
    * @var string
    * Holds the session instance
    */
    protected $session;

    public function __construct()
    {
        $this->userId 0;
        $this->userGroup = [];
        $this->userObject = (object)[];
    }

    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);

        try {
            $this->setupAuthClasses();
            if ($this->authenticate->check()) {
                $this->userId $this->authenticate->id();
                $this->userObject $this->authenticate->user();
                $groupModel = new \Myth\Auth\Authorization\GroupModel();
                $this->userGroup $groupModel->getGroupsForUser($this->userId)[0];

            }
            $this->urlQueryParam = new UrlQueryParam();
            $this->urlQueryParam->initParameters($request);

        } catch (\Exception $e) {

        }
    }


Enlightenment  Is  Freedom
Reply
#5

Actually, I've sorted out the issue that I needed to sort out. I really didn't need the vars as part of the POST, I needed them to be automatically filled in when I submitted my records for insert or update to the database, so, the database beforeInsert and beforeUpdate fixed the issue (in the model):

PHP Code:
namespace App\Models;

use 
CodeIgniter\Model;

class 
EmployeeCOIModel extends OurModel
{
    protected $DBGroup      'db_group1';
    protected $table        'Employee_COI';
    protected $allowedFields = [
 // our fields go here
 
      'Maint_Userid',
        'Maint_Date',
        'Create_Userid',
        'Create_Date'              
    
];

    protected $beforeInsert = ['setInsertFields'];
    protected $beforeUpdate = ['setUpdateFields'];
    protected $returnType    'App\Entities\EmployeeCOI';

    protected function setInsertFields(array $data)
    {
        if (!isset($data['data']['Create_Userid'])) {
            helper("user");
            $request = \Config\Services::request();
            $current_user get_user($request->getServer('AUTH_USER'));
            $data['data']['Create_Userid'] = get_username($current_user);
            $data['data']['Maint_Userid'] = get_username($current_user);
        }
        return $data;
    }
    protected function setUpdateFields(array $data)
    {
        if (!isset($data['data']['Maint_Userid'])) {
            helper("user");
            $request = \Config\Services::request();
            $current_user get_user($request->getServer('AUTH_USER'));
            $data['data']['Maint_Userid'] = get_username($current_user);
        }
        return $data;
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB