Search code examples
phphtmlcodeignitercodeigniter-4

Changing the allowed fields based on user in CodeIgniter 4


I am building an inventory system where there is users with different levels of authority changing fields. I am trying to change the "protected $allowedFields" class property within a function inside the model, but it will not allow me. Do I have to create 2 different models, or bake the authentication into the html code? Any advice would be appreciated.


   

    class InventoryModel extends Model
       {
        protected $table = "tcg_main_inv";
        protected $primaryKey = 'id';
    
    
        protected $allowedFields = ['size'];
    
        public function setInvFields(string $userLevel)
        {
            if ($userLevel == "admin") {
                $allowedFields = ['container_number', 'size', 'dealer_cost', 'date_in', 
        'sales_order_number'];
            } else if ($userLevel)
                $allowedFields = ['date_in', 'sales_order_number'];
        }
        

Note: this is just a snippet of my model code.


Solution

  • Thank you to @ViLar, I was able to figure out a solution that seems to work. I made a function inside the model that changes the allowed fields.

    class InventoryModel extends Model
    {
      protected $table = "tcg_main_inv";
      protected $primaryKey = 'id';
    
    
      protected $allowedFields;
    
      public function userAuth(string $user_type)
      {
        if($user_type == "dealer"){
            $this->allowedFields = [ 'container_number'];
        }
        else if ($user_type == "admin"){
            $this->allowedFields = ['size', 'container_number', 'dealer_cost'];
        }
         
      }
    }