PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 19, 2022

[FIXED] Changing the allowed fields based on user in CodeIgniter 4

 February 19, 2022     codeigniter, codeigniter-4, html, php     No comments   

Issue

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'];
    }
     
  }
}


Answered By - Wordpress user
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing