Welcome Guest, Not a member yet? Register   Sign In
Variable should be set but its not!
#1

Hello, i'm making a blog (basic stuff) and i'm trying to set the permission levels. Everything inside the model and the controller is working fine except the visual stuff. The problem started when i was trying to find the most efficient way to remove stuff like the edit or delete button then the user level i lowest than the accepted.

For example:
if a normal users tries to edit a post from the url he will be redirected back. The edit button is disabled. Same for other dangerous actions.

Code:

Code:
function post($postID)
    {
                //which post are your trying to edit son?
        if(!$this->post->get_post($postID))
        {
            redirect(base_url().'posts');
        }
        else
        {
                         //call the function
            $this->correct_permissions('user');
            /*
            if(!$this->correct_permissions('author'))
            {
                $data['has_edit_perms'] = false;
            }
            else {
                $data['has_edit_perms'] = true;
            }
            */
                        //array
            $data['post'] = $this->post->get_post($postID);
                        //header stuff
            $this->load->view('__head__');
                        //pack the view and pass the data
            $this->load->view('post', $data);
        }
    }

        // no comment
    function correct_permissions($required)
    {
        $user_type = $this->session->userdata('user_type');
        if($required == 'user')
        {
            $data['perm_level'] = 1;
            if($user_type)
            {
                
                return true;
            }    
        }
        elseif($required == 'subscriber')
        {
            $data['perm_level'] = 2;
            if($user_type == 'admin' || $user_type == 'author' || $user_type == 'subscriber')
            {
                
                return true;
            }
        }
        elseif($required == 'author')
        {
            $data['perm_level'] = 3;
            if($user_type == 'admin' || $user_type == 'author')
            {
                
                return true;
            }
        }
        elseif($required == 'admin')
        {
            $data['perm_level'] = 4;
            if($user_type == 'admin')
            {
                
                return true;
            }
        }
    }

If i call the correct_permissions() function it will work and it will tell me if the user has the required rank BUT i will not be able to access the value of $data['perm_level'] inside the view.

Used in the code:
Code:
<?php if($perm_level > 3): ?>
<a href="<?= base_url() ?>posts/new_post"> Create new post </a>
<?php endif; ?>

(if i use the commented code instead it will work but i want to make more dynamic thats why im using the other way)

Anyone knows why the 'perm_level' is not working?
Reply
#2

the problem is that the array: $data in your function: correct_permission is only scoped to that function hence why it is not available to your views.

declare your $data array as a class level variable. then whereever you access/set data to it use: $this->data instead of: $data
Reply
#3

Hmm. That messes up everything.
Reply
#4

an alternative would be in your correct_permissions function have it return the perm_level value to your post function so instead of:

$this->correct_permissions('user');

you would do:

$data['perm_level'] = $this->correct_permissions('user');
Reply
#5

If you need to set a variable so that your view can see it you can do the following:

Code:
$this->load->vars($data);
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

lol at myself, i totally forgot about that one. Tongue
Reply




Theme © iAndrew 2016 - Forum software by © MyBB