Changeset 279

Show
Ignore:
Timestamp:
05/31/07 10:48:06
Author:
grahack
Message:

security enhancements: control access to edit/delete functions in admins and users controllers (fixes #56) the link flags are now set in the controllers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • FreakAuth/trunk/www/system/application/controllers/admin/admins.php

    r244 r279  
    118118                 
    119119                if ($query->num_rows()>0) 
    120                 {  
    121                         $i=1; 
    122                         foreach ($query->result() as $row) 
     120                { 
     121                        $i=1; 
     122                        foreach ($query->result() as $row) 
    123123                        { 
     124                // when do we display links for editing or deleting a user ? 
     125                // note: if we are in the admins controller, we are a superadmin 
     126                // we display the edit link if 
     127                // - the user in the table is not a superadmin 
     128                // - we are a superadmin editing himself 
     129                $data['user'][$i]['show_edit_link'] = 
     130                    ($row->role != 'superadmin' OR $row->id == getUserProperty('id')); 
     131                // we display the delete link if 
     132                // - the user in the table is not a superadmin 
     133                $data['user'][$i]['show_delete_link'] = 
     134                    ($row->role != 'superadmin'); 
     135                 
     136                // then we just fill the infos 
    124137                                $data['user'][$i]['id']= $row->id; 
    125138                                $data['user'][$i]['user_name']= $row->user_name; 
     
    168181                if ($query->num_rows() == 1) 
    169182        { 
    170                         $row = $query->row(); 
     183            $row = $query->row(); 
     184             
     185            // initializing two flags, for the edit and delete links 
     186            // we can edit the displayed admin if 
     187            //  - we are a superadmin 
     188            //  - OR the displayed user is not a superadmin 
     189            $data['can_edit_user'] = ( 
     190                getUserProperty('role') != 'superadmin' 
     191                OR $row->id == getUserProperty('id') 
     192                OR $row->role != 'superadmin' 
     193                ); 
     194            // we cannot delete a superadmin 
     195            $data['can_delete_user'] = 
     196                ($row->role != 'superadmin'); 
     197             
    171198                        $data['user']['id']= $row->id; 
    172199                        $data['user']['user_name']= $row->user_name; 
     
    312339    /** 
    313340     * Manages the edit 
    314      *  
     341     * superadmins can only be edited by themselves 
     342     * 
    315343     * @access public 
    316      * 
     344     * @param integer $id the id of the admin 
    317345     */ 
    318     function edit(
     346    function edit($id
    319347    { 
    320         $id = $this->uri->segment(4); 
     348        // we only allow edition of this admin if he/she is 
     349        // - not a superadmin OR 
     350        // - a superadmin editing himself 
     351        $allowed = ( 
     352            (getUserPropertyFromId($id, 'role') != 'superadmin') 
     353            OR (getUserProperty('id') == $id) 
     354        ); 
     355        if (!$allowed) $this->freakauth_light->denyAccess(getUserProperty('role')); 
    321356         
    322357        //set validation rules 
     
    450485     
    451486    /** 
    452      * Displays the registration form. 
    453      *  
     487     * Deletes an administrator, but not a superadmin 
     488     * 
    454489     * @access public 
    455      * 
     490     * @param integer $id the id of the admin 
    456491     */ 
    457492    function del($id) 
    458493    { 
    459         //CHECK IF ADMIN#1 OR ONLY ONE ADMIN LEFT 
    460         $fields = 'id'; 
    461         $query=$this->usermodel->getAdmins($fields); 
    462  
    463         //first system admin     
    464         if ($id==1) 
    465         { 
    466                 //set a flash message 
    467                 $msg = "It's not allowed to delete the system administrator #1"; 
    468          
    469         } 
    470         //last admin left 
    471         elseif ($id!=1 AND $query->num_rows()<1) 
    472         { 
    473                 //set a flash message 
    474                 $msg = "It's not allowed to delete the last system administrator left!";         
    475         } 
    476         else  
    477         { 
    478                 $this->usermodel->deleteUser($id); 
    479          
    480                 if ($this->config->item('FAL_create_user_profile')==TRUE) 
    481                         { 
    482                                 $this->load->model('Userprofile'); 
    483                                 $this->Userprofile->deleteUserProfile($id); 
    484                         } 
    485                 //set a flash message 
    486                         $msg = $this->db->affected_rows().$this->lang->line('FAL_user_deleted'); 
    487                 flashMsg($msg); 
    488                 $this->usermodel->deleteAdmin($id) ; 
    489                 //set a flash message 
    490                 $msg = $this->db->affected_rows().' administrator successfully deleted!'; 
    491                 redirect('admin/admins', 'location');      
    492             } 
    493                  
     494        // we only allow edition of this admin if if he/she is 
     495        // - not a superadmin 
     496        $allowed = (getUserPropertyFromId($id, 'role') != 'superadmin'); 
     497             
     498        if (!$allowed) $this->freakauth_light->denyAccess(getUserProperty('role')); 
     499         
     500        $this->usermodel->deleteUser($id); 
     501        //set a flash message 
     502        $msg = $this->db->affected_rows().$this->lang->line('FAL_user_deleted'); 
     503         
     504        if ($this->config->item('FAL_create_user_profile')==TRUE) 
     505        { 
     506            $this->load->model('Userprofile'); 
     507            $this->Userprofile->deleteUserProfile($id); 
     508        } 
     509        $this->usermodel->deleteUser($id) ; 
     510                 
     511        flashMsg($msg); 
     512        redirect('admin/admins', 'location'); 
    494513    } 
    495514     
     
    562581        return false; 
    563582    } 
     583  
    564584} 
    565585?> 
  • FreakAuth/trunk/www/system/application/controllers/admin/users.php

    r245 r279  
    121121                         foreach ($query->result() as $row) 
    122122                        { 
     123                // when do we display links for editing or deleting a user ? 
     124                // we display the edit and delete links if 
     125                // the user in the table is neither a superadmin nor an admin 
     126                $data['user'][$i]['show_edit_link'] = 
     127                    ($row->role != 'admin' AND $row->role != 'superadmin'); 
     128                $data['user'][$i]['show_delete_link'] = 
     129                    ($row->role != 'admin' AND $row->role != 'superadmin'); 
     130                 
     131                // then we just fill the infos 
    123132                                $data['user'][$i]['id']= $row->id; 
    124133                                $data['user'][$i]['user_name']= $row->user_name; 
     
    168177                if ($query->num_rows() == 1) 
    169178        { 
    170                         $row = $query->row(); 
     179            $row = $query->row(); 
     180             
     181            // initializing two flags, for the edit and delete links 
     182            // we can edit the displayed user if 
     183            //  - we are an admin 
     184            //  - OR the displayed user is not a superadmin 
     185            $data['can_edit_user'] = 
     186                ($row->role != 'superadmin' AND $row->role != 'admin'); 
     187            // we cannot delete a superadmin or an admin 
     188            $data['can_delete_user'] = 
     189                ($row->role != 'superadmin' AND $row->role != 'admin'); 
     190                 
    171191                        $data['user']['id']= $row->id; 
    172192                        $data['user']['user_name']= $row->user_name; 
     
    329349     * 
    330350     */ 
    331     function edit(
     351    function edit($id
    332352    { 
    333         $id = $this->uri->segment(4); 
    334          
    335353        // security check: 
    336         // we need to be a superadmin 
    337         // OR the target user must not be a superadmin 
    338         $this->_check_superadmin_handling($id); 
     354        // admins or superadmins cannot be edited in the users controller 
     355        $edited_role = getUserPropertyFromId($id, 'role'); 
     356        $allowed = ($edited_role != 'admin' AND $edited_role != 'superadmin'); 
     357        if (!$allowed) $this->freakauth_light->denyAccess(getUserProperty('role')); 
    339358         
    340359        //set validation rules 
     
    477496    { 
    478497        // security check: 
    479         // we need to be a superadmin 
    480         // OR the target user must not be a superadmin 
    481         $this->_check_superadmin_handling($id); 
     498        // admins or superadmins cannot be deleted in the users controller 
     499        $edited_role = getUserPropertyFromId($id, 'role'); 
     500        $allowed = ($edited_role != 'admin' AND $edited_role != 'superadmin'); 
     501        if (!$allowed) $this->freakauth_light->denyAccess(getUserProperty('role')); 
    482502         
    483503        $this->usermodel->deleteUser($id); 
     
    564584        return false; 
    565585    } 
    566      
    567     /** 
    568      *  security check used in edit and del functions 
    569      *  we need to be a superadmin 
    570      *  OR the target user must not be a superadmin 
    571      * 
    572      * @return boolean 
    573      */ 
    574     function _check_superadmin_handling($id) 
    575     { 
    576         $allowed = 
    577             (getUserProperty('role') == 'superadmin' 
    578             OR getUserPropertyFromId($id, 'role') != 'superadmin'); 
    579              
    580         if (!$allowed) $this->freakauth_light->denyAccess(getUserProperty('role')); 
    581     } 
    582586} 
    583587?> 
  • FreakAuth/trunk/www/system/application/views/FreakAuth_light/template_admin/users/detail.php

    r271 r279  
    3535 
    3636<?php if (isset($user)) 
    37 
    38     // initializing a flag 
    39     // we can handle the displayed user if 
    40     // we are a superadmin OR the displayed user is not a superadmin 
    41     $can_handle_user = (getUserProperty('role') == 'superadmin' OR $user['role'] != 'superadmin'); 
    42     ?> 
     37{?> 
    4338        <table> 
    4439          <tr> 
     
    5651         
    5752        <?php 
    58             if ($can_handle_user) 
     53            if ($can_edit_user OR $can_delete_user) 
    5954            {?> 
    6055                <th scope="col">&nbsp;</th> 
     
    7570            }?> 
    7671             
    77         <?php 
    78             if ($can_handle_user) 
    79             {?> 
     72        <?php if ($can_edit_user OR $can_delete_user):?> 
    8073                <td> 
     74        <?php endif;?> 
     75            <?php if ($can_edit_user):?> 
    8176                    <?=anchor('admin/'.$controller.'/edit/'.$user['id'], '<img src="'.base_url().$this->config->item('FAL_assets_admin').'/'.$this->config->item('FAL_images').'/pencil.png" alt="edit" title="edit">', array('title' => 'edit'));?> 
     77            <?php endif;?> 
     78            <?php if ($can_delete_user):?> 
    8279                    <?=anchor('admin/'.$controller.'/del/'.$user['id'], '<img src="'.base_url().$this->config->item('FAL_assets_admin').'/'.$this->config->item('FAL_images').'/cross.png" alt="delete" title="delete">', array('onCLick' => "return confirm('Are you SURE you want to delete this record?')", 'title'=>'delete'));?> 
     80            <?php endif;?> 
     81        <?php if ($can_edit_user OR $can_delete_user):?> 
    8382                </td> 
    84         <?php 
    85             }?> 
    86           </tr> 
     83        <?php endif;?> 
     84        </tr> 
    8785        </table> 
    8886<?php 
  • FreakAuth/trunk/www/system/application/views/FreakAuth_light/template_admin/users/list.php

    r261 r279  
    2828    <td><?=$user[$key]['user_name'];?></td> 
    2929    <td><?=$user[$key]['role']?></td> 
    30     <td>   
    31                     <?=anchor('admin/'.$controller.'/show/'.$user[$key]['id'], '<img src="'.base_url().$this->config->item('FAL_assets_admin').'/'.$this->config->item('FAL_images').'/zoom.png" alt="view" title="view">', array('title' => 'view'));?> 
    32                     <?php 
    33                         // now let's control when we display links for modifying or deleting a user 
    34                         // we display the links to superadmins or if the user in the table is not a superadmin or an admin 
    35                         $admins_list = ($controller == 'admins'); // if we are in the admins controller, we are a superadmin 
    36                         $user_is_not_an_admin = ($user[$key]['role']!='superadmin' AND $user[$key]['role']!='admin'); 
    37                 if ($admins_list OR $user_is_not_an_admin) 
    38                 {?> 
    39                     <?=anchor('admin/'.$controller.'/edit/'.$user[$key]['id'], '<img src="'.base_url().$this->config->item('FAL_assets_admin').'/'.$this->config->item('FAL_images').'/pencil.png" alt="edit" title="edit">', array('title' => 'edit'));?> 
    40                 <?=anchor('admin/'.$controller.'/del/'.$user[$key]['id'], '<img src="'.base_url().$this->config->item('FAL_assets_admin').'/'.$this->config->item('FAL_images').'/cross.png" alt="delete" title="delete">', array('onCLick' => "return confirm('Are you SURE you want to delete this record?')", 'title' => 'delete'));?> 
    41                     <?php 
    42                 } 
    43                 ?> 
     30    <td> 
     31        <?=anchor('admin/'.$controller.'/show/'.$user[$key]['id'], '<img src="'.base_url().$this->config->item('FAL_assets_admin').'/'.$this->config->item('FAL_images').'/zoom.png" alt="view" title="view">', array('title' => 'view'));?> 
     32        <?php 
     33        if ($user[$key]['show_edit_link']) 
     34            echo anchor('admin/'.$controller.'/edit/'.$user[$key]['id'], '<img src="'.base_url().$this->config->item('FAL_assets_admin').'/'.$this->config->item('FAL_images').'/pencil.png" alt="edit" title="edit">', array('title' => 'edit')); 
     35        if ($user[$key]['show_delete_link']) 
     36            echo anchor('admin/'.$controller.'/del/'.$user[$key]['id'], '<img src="'.base_url().$this->config->item('FAL_assets_admin').'/'.$this->config->item('FAL_images').'/cross.png" alt="delete" title="delete">', array('onCLick' => "return confirm('Are you SURE you want to delete this record?')", 'title' => 'delete')); 
     37        ?> 
    4438    </td> 
    4539  </tr>