Exemplo n.º 1
0
 function beforeSave(&$data, &$params, $mode)
 {
     parent::beforeSave($data, $params, $mode);
     if ($mode == 'update' and empty($data['password']) and array_key_exists('password', $data)) {
         unset($data['password']);
     }
     if (!empty($data['password'])) {
         $data['password'] = \GCore\Libs\Authenticate::hash_password($data['password']);
     }
     if ($mode == 'update' and !empty($data['id']) and (int) $data['id'] == 1) {
         $user = \GCore\Libs\Base::getUser();
         if ($user['id'] != 1) {
             $session = \GCore\Libs\Base::getSession();
             $session->setFlash('error', l_('USERS_CANT_UPDATE_SITE_OWNER'));
             return false;
         }
     }
 }
Exemplo n.º 2
0
 function _initialize()
 {
     //$this->area = 'front';
     $session = \GCore\Libs\Base::getSession();
     if ($this->get('ccname', null)) {
         $this->data['ccname'] = $this->get('ccname');
     }
     if (!empty($this->data['ccname'])) {
         $this->connection = $connection = $this->Connection->find('first', array('conditions' => array('title' => $this->data['ccname'], 'published' => 1)));
         if (!empty($connection['Connection']['extras']['models']['name'][1]) and !empty($connection['Connection']['extras']['models']['tablename'][1])) {
             $dbo_config = array();
             if (!empty($connection['Connection']['extras']['ndb']['enabled'])) {
                 $dbo_config = array('type' => $connection['Connection']['extras']['ndb']['driver'], 'host' => $connection['Connection']['extras']['ndb']['host'], 'name' => $connection['Connection']['extras']['ndb']['database'], 'user' => $connection['Connection']['extras']['ndb']['user'], 'pass' => $connection['Connection']['extras']['ndb']['password'], 'prefix' => $connection['Connection']['extras']['ndb']['prefix']);
             }
             //primary model available
             \GCore\Libs\GModel::generateModel(trim($connection['Connection']['extras']['models']['name'][1]), array('tablename' => trim($connection['Connection']['extras']['models']['tablename'][1]), 'dbo_config' => $dbo_config));
             $pmodel = '\\GCore\\Models\\' . $connection['Connection']['extras']['models']['name'][1];
             $this->pmodel = $pmodel::getInstance();
             $this->connection_models[$this->pmodel->alias] = $this->pmodel;
             //find secondary models if available
             foreach ($this->connection['Connection']['extras']['models']['name'] as $k => $name) {
                 if ($k != 1) {
                     $name = trim($this->connection['Connection']['extras']['models']['name'][$k]);
                     $tablename = trim($this->connection['Connection']['extras']['models']['tablename'][$k]);
                     $pkey = !empty($this->connection['Connection']['extras']['models']['pkey'][$k]) ? trim($this->connection['Connection']['extras']['models']['pkey'][$k]) : NULL;
                     if (!empty($name) and !empty($tablename)) {
                         \GCore\Libs\GModel::generateModel($name, array('tablename' => $tablename, 'pkey' => $pkey, 'dbo_config' => $dbo_config));
                         //setup relation to primary if exists
                         $relation = $this->connection['Connection']['extras']['models']['relation'][$k];
                         if (!empty($relation)) {
                             $relation_settings = array('className' => '\\GCore\\Models\\' . $name);
                             if (!empty($this->connection['Connection']['extras']['models']['foreignKey'][$k])) {
                                 $relation_settings['foreignKey'] = trim($this->connection['Connection']['extras']['models']['foreignKey'][$k]);
                             }
                             if (!empty($this->connection['Connection']['extras']['models']['assoc_save'][$k])) {
                                 $relation_settings['save_on_save'] = true;
                             }
                             if (!empty($this->connection['Connection']['extras']['models']['assoc_delete'][$k])) {
                                 $relation_settings['delete_on_delete'] = true;
                             }
                             if (!empty($this->connection['Connection']['extras']['models']['conditions'][$k])) {
                                 $conditions = eval('?>' . $this->connection['Connection']['extras']['models']['conditions'][$k]);
                                 $relation_settings['conditions'] = is_array($conditions) ? $conditions : array();
                             }
                             if (!empty($this->connection['Connection']['extras']['models']['join_conditions'][$k])) {
                                 $join_conditions = eval('?>' . $this->connection['Connection']['extras']['models']['join_conditions'][$k]);
                                 $relation_settings['join_conditions'] = is_array($join_conditions) ? $join_conditions : array();
                             }
                             if (!empty($this->connection['Connection']['extras']['models']['join_type'][$k])) {
                                 $relation_settings['type'] = $this->connection['Connection']['extras']['models']['join_type'][$k];
                             }
                             if (!empty($this->connection['Connection']['extras']['models']['fields'][$k])) {
                                 $relation_settings['fields'] = $this->_process_fields_list($this->connection['Connection']['extras']['models']['fields'][$k]);
                             }
                             if (!empty($this->connection['Connection']['extras']['models']['order'][$k])) {
                                 $relation_settings['order'] = $this->_process_fields_list($this->connection['Connection']['extras']['models']['order'][$k]);
                             }
                             if (!empty($this->connection['Connection']['extras']['models']['group'][$k])) {
                                 $relation_settings['group'] = $this->_process_fields_list($this->connection['Connection']['extras']['models']['group'][$k]);
                             }
                             if (empty($this->connection['Connection']['extras']['models']['associated_model'][$k])) {
                                 $this->pmodel->bindModels($relation, array($name => $relation_settings));
                                 $this->connection_models[$name] = $this->pmodel->{$name};
                             } else {
                                 $associated_model = $this->connection['Connection']['extras']['models']['associated_model'][$k];
                                 $associated_model_class = '\\GCore\\Models\\' . $this->connection['Connection']['extras']['models']['associated_model'][$k];
                                 if ($associated_model == $this->pmodel->alias) {
                                     $this->pmodel->bindModels($relation, array($name => $relation_settings));
                                     $this->connection_models[$name] = $this->pmodel->{$name};
                                 } else {
                                     $check_model = $this->pmodel;
                                     check_again:
                                     if (in_array($associated_model, array_keys($check_model->associated_models))) {
                                         $check_model->{$associated_model}->bindModels($relation, array($name => $relation_settings));
                                         $this->connection_models[$name] = $check_model->{$associated_model}->{$name};
                                     } else {
                                         $sub_models = array_keys($check_model->associated_models);
                                         foreach ($sub_models as $sub_model) {
                                             $check_model = $sub_model;
                                             goto check_again;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             $session->setFlash('error', l_('CONN_CONNECTION_NOT_FOUND'));
             return false;
         }
     } else {
         $session->setFlash('error', l_('CONN_NO_CONNECTION_NAME'));
         return false;
     }
     //check permissions
     if (!empty($this->connection['Connection']['extras'][$this->area]['permissions'][$this->action])) {
         $gcb = $this->Request->data('gcb', null);
         $owner_id = null;
         if ($this->connection['Connection']['extras'][$this->area]['permissions'][$this->action]['owner'] == 1) {
             if (!empty($gcb) and !empty($this->connection['Connection']['extras'][$this->area]['permissions_conf']['owner_id_column'])) {
                 $user_id_column = $this->connection['Connection']['extras'][$this->area]['permissions_conf']['owner_id_column'];
                 $records = $this->pmodel->find('all', array('recursive' => -1, 'fields' => array($this->pmodel->pkey, $user_id_column), 'conditions' => array($this->pmodel->pkey => $gcb)));
                 if (strpos($user_id_column, '.') === false) {
                     $user_id_column = $this->pmodel->alias . '.' . $user_id_column;
                 }
                 $owners = \GCore\Libs\Arr::getVal($records, explode('.', '[n].' . $user_id_column));
                 $owners = array_values(array_unique($owners));
                 if (count($owners) > 1) {
                     goto check_all;
                 } else {
                     $owner_id = $owners[0];
                 }
             }
         }
         check_all:
         if (!\GCore\Libs\Authorize::check_rules($this->connection['Connection']['extras'][$this->area]['permissions'][$this->action], \GCore\Libs\Authenticate::get_user_groups(), $owner_id)) {
             $session->setFlash('error', l_('CONNECTIVITY_ACCESS_DENIED'));
             return false;
         }
     }
     //load plugins
     if (!empty($this->pmodel)) {
         if (!empty($this->connection['Connection']['extras'][$this->area]['display']['page_limit']) and is_numeric($this->connection['Connection']['extras'][$this->area]['display']['page_limit'])) {
             $this->pmodel->page_limit = (int) $this->connection['Connection']['extras'][$this->area]['display']['page_limit'];
         }
         //run plugins
         if (!empty($this->connection['Connection']['extras']['plugins'])) {
             //sort plugins execution
             $plugins_priority = array();
             foreach ($this->connection['Connection']['extras']['plugins'] as $plg => $plg_data) {
                 $plugins_priority[$plg] = \GCore\Libs\Arr::getVal($plg_data, array('priority'), 0);
             }
             array_multisort($plugins_priority, SORT_DESC, $this->connection['Connection']['extras']['plugins']);
             foreach ($this->connection['Connection']['extras']['plugins'] as $plugin => $plugin_data) {
                 if (!empty($plugin_data['enabled'])) {
                     $this->plugins[] = $plugin;
                     if (class_exists('\\GCore\\Admin\\Extensions\\Chronoconnectivity\\Plugins\\' . \GCore\Libs\Str::camilize($plugin) . '\\' . \GCore\Libs\Str::camilize($plugin . '_helper'))) {
                         $plugin_helper = '\\GCore\\Admin\\Extensions\\Chronoconnectivity\\Plugins\\' . \GCore\Libs\Str::camilize($plugin) . '\\' . \GCore\Libs\Str::camilize($plugin . '_helper');
                         $this->helpers[$plugin_helper]['connection'] = $this->connection;
                         $this->helpers[$plugin_helper]['model'] = $this->pmodel;
                     }
                 }
             }
         }
         $this->helpers['\\GCore\\Admin\\Extensions\\Chronoconnectivity\\Helpers\\Plugin']['plugins'] = $this->plugins;
         $this->_process_plugins('on_initialize', $this);
     }
 }