Example #1
0
 /**
  * Rule allowed permission
  *
  * @param string $rule
  * @return bool
  */
 private function _isAllowed($rule)
 {
     if (strpos($rule, '|') && !empty($this->moduleName) && !empty($this->controllerName)) {
         return $this->security->isAllowed($this->moduleName . '|' . $this->controllerName . '|' . $rule);
     }
     return $this->security->isAllowed($rule);
 }
Example #2
0
 /**
  * Init Services
  *
  * @param mixed $config
  * @param \Phalcon\DiInterface $di
  */
 public function _initServices($di, $config)
 {
     /**
      * The URL component is used to generate all kind of urls in the application
      */
     $di->set('url', function () use($config) {
         $url = new UrlResolver();
         $url->setBaseUri($config->website->baseUri);
         return $url;
     }, true);
     /**
      * Start the session the first time some component request the session service
      */
     $di->set('session', function () use($config) {
         $session = new ZSession(['uniqueId' => $config->auth->salt]);
         $session->start();
         return $session;
     }, true);
     /**
      * Set view cache
      */
     $di->set('viewCache', function () use($config) {
         //Cache data for one day by default
         $frontCache = new FrontendOutput(['lifetime' => $config->viewCache->lifetime]);
         //File backend settings
         $cache = new CacheFile($frontCache, ['cacheDir' => ROOT_PATH . $config->viewCache->dir]);
         return $cache;
     });
     if ($config->modelMetadataCache->status) {
         /**
          * Set models metadata
          */
         $di->set('modelsMetadata', function () use($config) {
             if ($config->modelMetadataCache->type == 'apc') {
                 return new MetaDataApc(['lifetime' => $config->modelMetadataCache->lifetime, 'prefix' => $config->modelMetadataCache->prefix]);
             } else {
                 return new MetadataFiles(['metaDataDir' => ROOT_PATH . '/cache/metadata/', 'lifetime' => $config->modelMetadataCache->lifetime]);
             }
         });
     }
     /**
      * Crypt service
      */
     $di->set('crypt', function () use($config) {
         $crypt = ZCrypt::getInstance();
         $crypt->setKey($config->crypt->key);
         return $crypt;
     });
     /**
      * Set security
      */
     $di->set('security', function () {
         $security = new Security();
         $security->setWorkFactor(8);
         return $security;
     });
     /**
      * Set up database connection
      */
     $di->set('db', function () use($config) {
         $adapter = 'Phalcon\\Db\\Adapter\\Pdo\\' . $config->database->adapter;
         /**
          * @var \Phalcon\Db\Adapter\Pdo\Postgresql $db
          */
         if ($config->database->adapter == 'Mysql') {
             $db = new $adapter($config->database->toArray());
         } else {
             $db = new $adapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname));
         }
         if ($config->database->log) {
             $eventsManager = new EventsManager();
             if (!file_exists(ROOT_PATH . '/cache/logs/db.log')) {
                 file_put_contents(ROOT_PATH . '/cache/logs/db.log', '');
             }
             $logger = new FileLogger(ROOT_PATH . '/cache/logs/db.log');
             //Listen all the database events
             $eventsManager->attach('db', function ($event, $db) use($logger) {
                 /**
                  * @var \Phalcon\Events\Event $event
                  */
                 if ($event->getType() == 'beforeQuery') {
                     /**
                      * @var \Phalcon\Db\Adapter\Pdo\Postgresql $db
                      */
                     $logger->log($db->getSQLStatement(), Logger::INFO);
                 }
             });
             //Assign the eventsManager to the db adapter instance
             $db->setEventsManager($eventsManager);
         }
         return $db;
     });
     /**
      * Set a models manager
      */
     $di->set('modelsManager', new ModelsManager());
     /**
      * Set up model cache for Phalcon model
      */
     $di->set('modelsCache', function () {
         return ZCache::getInstance('_ZCMS_MODEL');
     });
     /**
      * Set up asset add css, js
      */
     $di->set('assets', new ZAssets());
     /**
      * Loading routes from the routes.php file
      */
     $di->set('router', function () {
         return require APP_DIR . '/config/router.php';
     });
     $di->set('acl', ZAcl::getInstance());
     /**
      * Set up the flash service (custom with bootstrap)
      */
     $di->set('flashSession', function () {
         $flashSession = new FlashSession(['warning' => 'alert alert-warning', 'notice' => 'alert alert-info', 'success' => 'alert alert-success', 'error' => 'alert alert-danger']);
         return $flashSession;
     });
     /**
      * Set up cache
      */
     $di->set('cache', ZCache::getInstance('_ZCMS_GLOBAL'));
 }
Example #3
0
 /**
  * Edit role
  *
  * @param int $id
  * @return bool
  */
 public function editAction($id)
 {
     $id = intval($id);
     /**
      * @var UserRoles $edit_data
      */
     $edit_data = UserRoles::findFirst(['conditions' => 'role_id = ?0', 'bind' => [$id]]);
     //If id not exist
     if (!$edit_data) {
         $this->flashSession->error("Cant not find that item to edit!");
         return $this->response->redirect('/admin/system/role/');
     } elseif ($edit_data->is_super_admin == 1) {
         $this->flashSession->error("You can't not edit Super Admin!");
         return $this->response->redirect('/admin/system/role/');
     } else {
         $this->view->setVar('edit_data', $edit_data);
     }
     //Add toolbar button
     $this->_toolbar->addSaveButton();
     $this->_toolbar->addCancelButton("index");
     $this->_addCSSAndJS();
     //Get rules
     $this->getRules();
     //Get edit rules
     /**
      * @var UserRoleMapping[] $edit_user_role_mapping
      */
     $edit_user_role_mapping = UserRoleMapping::find(["conditions" => "role_id = ?0", "bind" => [0 => $edit_data->role_id]]);
     $edit_rules = [];
     foreach ($edit_user_role_mapping as $arm) {
         $edit_rules[] = $arm->rule_id;
     }
     $this->view->setVar('edit_rules_id', implode(",", $edit_rules));
     if ($this->request->isPost()) {
         //Begin transaction
         $this->db->begin();
         //Get current auth
         $auth = ZAcl::getInstance()->getAuth();
         //Save admin role
         $edit_data->name = $this->request->getPost("name", "striptags");
         $edit_data->updated_at = date("Y-m-d H:i:s");
         $edit_data->updated_by = $auth['id'];
         $edit_data->location = (int) $this->request->getPost('location');
         $edit_data->is_default = (int) $this->request->getPost('is_default');
         if ($edit_data->save() == false) {
             $this->db->rollback();
             return $this->flashSession->error("m_system_role_message_cannot_save_role");
         }
         //Save admin role mapping
         $userRulesPost = trim($this->request->getPost("admin_rules"), ' ');
         if ($userRulesPost == '') {
             $this->db->commit();
             $this->flashSession->success('m_system_role_message_new_role_was_created_successfully');
             $this->response->redirect('/admin/system/role/');
             return true;
         }
         $user_rules = explode(",", $userRulesPost);
         $number_new_rules = count($user_rules);
         $number_old_rules = count($edit_user_role_mapping);
         $sub = $number_new_rules - $number_old_rules;
         if ($sub < 0) {
             foreach ($edit_user_role_mapping as $key => $arm) {
                 if ($user_rules[$key]) {
                     $arm->rule_id = $user_rules[$key];
                     if ($arm->save() == false) {
                         $this->db->rollback();
                         return $this->flashSession->error('m_system_role_message_update_role_failed');
                     }
                 } else {
                     if ($arm->delete() == false) {
                         $this->db->rollback();
                         return $this->flashSession->error('m_system_role_message_update_role_failed');
                     }
                 }
             }
         } elseif ($sub == 0) {
             //echo '<pre>'; var_dump($edit_user_role_mapping->toArray());echo '</pre>'; die();
             foreach ($edit_user_role_mapping as $key => $arm) {
                 $arm->rule_id = $user_rules[$key];
                 if ($arm->save() == false) {
                     $this->db->rollback();
                     return $this->flashSession->error('m_system_role_message_update_role_failed');
                 }
             }
         } else {
             foreach ($edit_user_role_mapping as $key => $arm) {
                 $arm->rule_id = $user_rules[$key];
                 if ($arm->save() == false) {
                     $this->db->rollback();
                     return $this->flashSession->error('m_system_role_message_update_role_failed');
                 }
             }
             for ($i = $number_old_rules; $i < $number_new_rules; $i++) {
                 $new_user_role_mapping = new UserRoleMapping();
                 $new_user_role_mapping->role_id = $edit_data->role_id;
                 $new_user_role_mapping->rule_id = $user_rules[$i];
                 if ($new_user_role_mapping->save() == false) {
                     $this->db->rollback();
                     return $this->flashSession->error('m_system_role_message_update_role_failed');
                 }
             }
         }
         //After all success full, commit transaction
         $this->db->commit();
         $this->flashSession->success(__('m_system_role_message_new_role_was_updated_successfully', ['1' => $edit_data->name]));
         return $this->response->redirect('/admin/system/role/');
     }
     return true;
 }