PageRenderTime 73ms CodeModel.GetById 59ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/magehelp/application/models/acl/permission.php

https://bitbucket.org/jit_bec/shopifine
PHP | 291 lines | 166 code | 57 blank | 68 comment | 15 complexity | ef57b1060246fee5f0f7a05c8b4e502c MD5 | raw file
  1<?php
  2
  3/*
  4 * To change this template, choose Tools | Templates
  5 * and open the template in the editor.
  6 */
  7
  8/**
  9 * Description of resource
 10 *
 11 * @author abhijit
 12 */
 13class Permission extends Base_model {	
 14	
 15//        function insert($role_data,$parents = array())
 16//	{
 17//            //$this->db->insert('invoice',$invoice_data);
 18//            //return $this->db->insert('roles',$role_data);
 19//            $this->db->trans_start();
 20//            $this->db->insert('roles',$role_data);
 21//            $id = $this->db->insert_id();
 22//            if (!empty($parents)){
 23//                foreach ($parents as $parent){
 24//                    $parent_name = $this->getName($parent);
 25//                    if (!empty($parent_name)){
 26//                        $role_inherit_data = array('role_id'=>$id,'role_name'=>$role_data['role_name'],
 27//                            'parent_role_id'=>$parent,'parent_role_name'=>$parent_name);
 28//                        $this->db->insert('role_inheritance_mapping',$role_inherit_data);
 29//                    }
 30//                }
 31//            }
 32//            $this->db->trans_complete();
 33//            if ($this->db->trans_status() === FALSE)
 34//            {
 35//                //echo $this->db->_error_message();
 36//                die( 'Shipping  Failed.Please check log ');
 37//            }
 38//            else {
 39//                $success = true;
 40//            }
 41//	}
 42        
 43        function save($permission_data,$id=false){
 44            $success=false;
 45            //Run these queries as a transaction, we want to make sure we do all or nothing
 46            if (!empty($permission_data)){
 47                try {
 48                    if (!$id or !$this->permissionExistsById($id)){
 49                        $success = $this->db->insert('acl_role_resource_permission_mapping',$permission_data);
 50                    }
 51                    else{
 52
 53                        $this->db->where('id', $id);
 54                        $success = $this->db->update('acl_role_resource_permission_mapping',$permission_data);
 55
 56                    }
 57                    if ($success){
 58                        log_message('debug','Permission Suceesfully Created');
 59                    }
 60                }
 61                catch (Exception $e){
 62                    log_message('Permission Creation Failed '.$this->db->_error_message() );
 63                    throw new Exception('RePermissionsource Creation Failed' );
 64                }
 65                
 66            }
 67            
 68            
 69            return $success;
 70        }
 71        
 72        function getRoleResourcePermissionMapping($where_clause=null){
 73            $this->db->select('*');
 74            if (!empty($where_clause)){
 75                $this->db->where($where_clause);
 76            }
 77            $query = $this->db->get('acl_role_resource_permission_mapping');
 78            return $query->result_array();
 79        }
 80        
 81	
 82        function getParent($child){
 83            $sql = "SELECT parent.resource parentresource FROM " .$this->db->dbprefix
 84               ."acl_resources child left join ".$this->db->dbprefix."acl_resources parent on 
 85                child.parent_id=parent.id where child.resource =?";
 86            $query = $this->db->query($sql,$child);
 87            
 88            if($query->num_rows()>0){
 89                $row = $query->row();
 90                return $row->parentresource;
 91            }
 92            
 93            
 94        }
 95        
 96        function getId($role_name){
 97            $this->db->select('id');
 98            $this->db->where('role_name',$role_name);
 99            $query = $this->db->get('roles');
100            if ($query->num_rows() > 0)
101            {
102                $row = $query->row_array(); 
103
104                return  $row['id'];
105            
106            } 
107            return null;
108        }
109        
110        function getPermissionName($id){
111            $this->db->select('permission');
112            $this->db->where('id',$id);
113            $query = $this->db->get('acl_permissions');
114            if ($query->num_rows() > 0)
115            {
116                $row = $query->row_array(); 
117
118                return  $row['permission'];
119            
120            } 
121            return null;
122        }
123        
124//        function getMenu($userName){
125//            $menu ="-menu";
126//            $sql = "SELECT resource.id,resource.resource, resource.ui_display_name, resource.relative_path_link, user.username
127//                FROM ".$this->db->dbprefix."acl_resources AS resource
128//                LEFT JOIN ".$this->db->dbprefix."acl_role_resource_permission_mapping AS permission ON resource.resource = permission.resource_name
129//                LEFT JOIN ".$this->db->dbprefix."acl_users AS user ON permission.role_id = user.role_id
130//                WHERE user.username = ?
131//                AND permission.isAllowed =1
132//                AND resource.resource LIKE '%".$this->db->escape_like_str($menu)."%' order by relative_order_in_category";
133//            $query = $this->db->query($sql,array($userName));
134//            log_message('debug', $this->db->last_query());
135//            //echo $this->db->last_query();
136//            return $query->result();
137//        }
138        
139        function getMenu($userName){
140            $menu ="-menu";
141            $sql = "SELECT id,resource, ui_display_name,relative_path_link, relative_order_in_category,username,isAllowed,0 as relative_order_parent
142                FROM ".$this->db->dbprefix."user_role_permission_mapping  WHERE username = ?  AND isAllowed is not null
143                AND resource LIKE '%".$this->db->escape_like_str($menu)."%' UNION ".
144                    "SELECT id,resource, ui_display_name,relative_path_link, relative_order_in_category,username,isAllowed,relative_order_parent
145                FROM ".$this->db->dbprefix."user_parentrole_permission_mapping  WHERE username = ?  AND isAllowed is not null
146                AND resource LIKE '%-menu%' ".
147                " order by `relative_order_parent` desc ";
148            $query = $this->db->query($sql,array($userName,$userName));
149            //log_message('debug', $this->db->last_query());
150            //echo $this->db->last_query();
151            return $query->result_array();
152        }
153        
154//        function getAdminMenu($userName){
155//            $menu ="-adminmenu";
156//            $sql = "SELECT resource.id,resource.resource, resource.ui_display_name, resource.relative_path_link, user.username
157//                FROM ".$this->db->dbprefix."acl_resources AS resource
158//                LEFT JOIN ".$this->db->dbprefix."acl_role_resource_permission_mapping AS permission ON resource.resource = permission.resource_name
159//                LEFT JOIN ".$this->db->dbprefix."acl_users AS user ON permission.role_id = user.role_id
160//                WHERE user.username = ?
161//                AND permission.isAllowed =1
162//                AND resource.resource LIKE '%".$this->db->escape_like_str($menu)."%' order by relative_order_in_category";
163//            $query = $this->db->query($sql,array($userName));
164//            //echo $this->db->last_query();
165//            return $query->result();
166//        }
167        function getAdminMenu($userName){
168            $menu ="-adminmenu";
169            $sql = "SELECT id,resource, ui_display_name,relative_path_link, relative_order_in_category,username,isAllowed,0 as relative_order_parent
170                FROM ".$this->db->dbprefix."user_role_permission_mapping  WHERE username = ? AND isAllowed is not null
171                AND resource LIKE '%".$this->db->escape_like_str($menu)."%' UNION ".
172                    "SELECT id,resource, ui_display_name,relative_path_link, relative_order_in_category,username,isAllowed,relative_order_parent
173                FROM ".$this->db->dbprefix."user_parentrole_permission_mapping  WHERE username = ?  AND isAllowed is not null
174                AND resource LIKE '%".$this->db->escape_like_str($menu)."%' ".
175                " order by `relative_order_parent` desc ";
176            $query = $this->db->query($sql,array($userName,$userName));
177             //log_message('debug', 'admin menu ' .$this->db->last_query());
178            return $query->result_array();
179        }
180        function getSubMenu($userName,$parent_resource_id){
181            $menu ="-submenu";
182            $sql = "SELECT id,resource, ui_display_name,relative_path_link, relative_order_in_category,username,isAllowed,0 as relative_order_parent
183                FROM ".$this->db->dbprefix."user_role_permission_mapping  WHERE username = ?  AND parent_id = ? AND isAllowed is not null
184                AND resource LIKE '%".$this->db->escape_like_str($menu)."%' UNION ".
185                    "SELECT id,resource, ui_display_name,relative_path_link,relative_order_in_category, username,isAllowed,relative_order_parent
186                FROM ".$this->db->dbprefix."user_parentrole_permission_mapping  WHERE username = ?  AND parent_id = ? AND isAllowed is not null
187                AND resource LIKE '%".$this->db->escape_like_str($menu)."%' "." order by `relative_order_parent` desc ";
188            $query = $this->db->query($sql,array($userName,$parent_resource_id,$userName,$parent_resource_id));
189            //log_message('debug', 'sub menu'.$this->db->last_query());
190            
191            return $query->result_array();
192        }
193        
194        function getAllPermissionTypes (){
195             $this->db->select('*');
196             $query =$this->db->get('acl_permissions');
197             return $query->result_array();
198        }
199        
200        function getAllPermissionMapping($csv = false,$whereClause=null,$order_limit_clause=array(),$like_fields_array=null,$or_where_clause_array=null){
201            $orderBy = 'id';
202            $orderDir= 'desc';
203            $startLimit = 0;
204            $limit = 1000;
205            
206            if (!empty($order_limit_clause['orderBy'])){
207                $orderBy = $order_limit_clause['orderBy'];
208            }
209            if (!empty($order_limit_clause['orderDir'])){
210                $orderDir = $order_limit_clause['orderDir'];
211            }
212            if (!empty($order_limit_clause['startLimit'])){
213                $startLimit = $order_limit_clause['startLimit'];
214            }
215            if (!empty($order_limit_clause['limit'])){
216                $limit = $order_limit_clause['limit'];
217            }
218        
219            $this->load->dbutil();  
220            $this->db->select('*');
221            if (!empty($whereClause)){
222                $this->db->where($whereClause);
223            }
224            
225            if (!empty($or_where_clause_array)){
226                $this->db->or_where($or_where_clause_array);
227            }
228            
229            if (!empty($like_fields_array)){
230                $this->db->like($like_fields_array);
231            }
232            $this->db->order_by($orderBy,$orderDir);
233            $this->db->limit($limit,$startLimit);
234            $query = $this->db->get('acl_role_resource_permission_mapping');
235            
236            if ($csv){
237                return $this->dbutil->csv_from_result($query);
238            }
239            return $query->result_array();
240        }
241        
242        
243        function totalNoOfRowsPermissionMapping () {
244            
245            $this->db->from('acl_role_resource_permission_mapping');
246            return $this->db->count_all_results() ;
247            
248            
249        }
250        
251        function permissionExists($roleid,$resourceid,$permissionid) {
252            
253            $this->db->from('acl_role_resource_permission_mapping');
254            $this->db->where('role_id',$roleid);
255            $this->db->where('resource_id',$resourceid);
256            $this->db->where('permission_id',$permissionid);
257            
258            $query = $this->db->get();
259            return ($query->num_rows()==1);
260            
261        }
262        
263        function permissionExistsById($id) {
264            
265            $this->db->from('acl_role_resource_permission_mapping');
266            $this->db->where('id',$id);
267            
268            
269            $query = $this->db->get();
270            return ($query->num_rows()==1);
271            
272        }
273        
274        function anyPermissionExistsForResource($resource) {
275            
276            $this->db->from('acl_role_resource_permission_mapping');
277            
278            $this->db->where('resource_name',$resource);
279            
280            $query = $this->db->get();
281            return ($query->num_rows()>0);
282            
283        }
284        
285        
286        
287	
288}
289
290
291?>