/**
  * @param string $name
  * @param waSystemConfig $config
  * @param bool $set_current
  * @throws waException
  * @return waSystem
  */
 public static function getInstance($name = null, waSystemConfig $config = null, $set_current = false)
 {
     if ($name === null) {
         if ($config && $config instanceof waAppConfig) {
             $name = $config->getName();
         } else {
             $name = self::$current;
         }
     }
     if (!isset(self::$instances[$name])) {
         if ($config === null && self::$current) {
             /**
              * @var $system waSystem
              */
             $system = self::$instances[self::$current];
             $locale = $set_current ? $system->getLocale() : null;
             $config = SystemConfig::getAppConfig($name, $system->getEnv(), $system->config->getRootPath(), $locale);
         }
         if ($config) {
             self::$instances[$name] = new self($config);
             if (!self::$instances[$name] instanceof waSystem) {
                 throw new waException(sprintf('Class "%s" is not of the type waSystem.', $config));
             }
         } else {
             throw new waException(sprintf('The "%s" system does not exist.', $name));
         }
     }
     if ($set_current) {
         self::setActive($name);
     } elseif (!self::$current || self::$current == 'wa-system') {
         self::$current = $name;
     }
     return self::$instances[$name];
 }
 public function execute()
 {
     // only allowed to global admin
     if (!wa()->getUser()->getRights('webasyst', 'backend')) {
         throw new waRightsException(_w('Access denied'));
     }
     $contact_id = waRequest::get('id');
     $group_ids = null;
     if ($contact_id > 0) {
         $user_groups_model = new waUserGroupsModel();
         $group_ids = $user_groups_model->getGroupIds($contact_id);
         $group_ids[] = 0;
     }
     $app_id = waRequest::get('app');
     $right_model = new waContactRightsModel();
     $rights = $right_model->get($contact_id, $app_id, null, false);
     $group_rights = null;
     if ($group_ids) {
         $group_rights = $right_model->get(array_map(wa_lambda('$a', 'return -$a;'), $group_ids), $app_id, null, false);
     }
     // Check custom rights items
     $app_config = SystemConfig::getAppConfig($app_id);
     $class_name = $app_config->getPrefix() . "RightConfig";
     $file_path = $app_config->getAppPath('lib/config/' . $class_name . ".class.php");
     if (file_exists($file_path)) {
         // Init app
         waSystem::getInstance($app_id, $app_config, true);
         include $file_path;
         /**
          * @var waRightConfig $right_config
          */
         $right_config = new $class_name();
         $rights += $right_config->getRights($contact_id);
         if ($group_ids) {
             $group_rights += $right_config->getRights(array_map(wa_lambda('$a', 'return -$a;'), $group_ids));
         }
         $this->view->assign('html', $right_config->getHTML($rights, $group_rights));
         waSystem::setActive('contacts');
     } else {
         $this->view->assign('html', '');
     }
     if ($contact_id > 0) {
         $this->view->assign('user', new waContact($contact_id));
     } else {
         $gm = new waGroupModel();
         $this->view->assign('group', $gm->getById(-$contact_id));
     }
     $app = wa()->getAppInfo($app_id);
     $app['id'] = $app_id;
     $this->view->assign('app', $app);
     $this->view->assign('rights', $rights);
     $this->view->assign('group_rights', $group_rights);
 }
 public static function revokeUser($id)
 {
     // wa_contact
     $user = new waContact($id);
     $user['is_user'] = 0;
     $user['login'] = null;
     $user['password'] = '';
     $user->save();
     // user groups
     $ugm = new waUserGroupsModel();
     $ugm->delete($id);
     // Access rigths
     $right_model = new waContactRightsModel();
     $right_model->deleteByField('group_id', -$id);
     // Custom application access rigths
     foreach (wa()->getApps() as $aid => $app) {
         if (isset($app['rights']) && $app['rights']) {
             $app_config = SystemConfig::getAppConfig($aid);
             $class_name = $app_config->getPrefix() . "RightConfig";
             $file_path = $app_config->getAppPath('lib/config/' . $class_name . ".class.php");
             $right_config = null;
             if (!file_exists($file_path)) {
                 continue;
             }
             waSystem::getInstance($aid, $app_config);
             include_once $file_path;
             /**
              * @var waRightConfig $right_config
              */
             $right_config = new $class_name();
             $right_config->clearRights($id);
         }
     }
 }
 public function execute()
 {
     // only allowed to global admin
     if (!wa()->getUser()->getRights('webasyst', 'backend')) {
         throw new waRightsException('Access denied.');
     }
     $app_id = waRequest::post('app_id');
     $name = waRequest::post('name');
     $value = (int) waRequest::post('value');
     $contact_id = waRequest::get('id');
     $has_backend_access_old = $this->hasBackendAccess($contact_id);
     if (!$name && !$value) {
         $values = waRequest::post('app');
         if (!is_array($values)) {
             throw new waException('Bad values for access rights.');
         }
     } else {
         $values = array($name => $value);
     }
     $right_model = new waContactRightsModel();
     $is_admin = $right_model->get($contact_id, 'webasyst', 'backend', false);
     if ($is_admin && $app_id != 'webasyst') {
         throw new waException('Cannot change application rights for global admin.');
     }
     // If $contact_id used to have limited access and we're changing global admin privileges,
     // then need to notify all applications to remove their custom access records.
     if (!$is_admin && $app_id == 'webasyst' && $name == 'backend') {
         foreach (wa()->getApps() as $aid => $app) {
             try {
                 if (isset($app['rights']) && $app['rights']) {
                     $app_config = SystemConfig::getAppConfig($aid);
                     $class_name = $app_config->getPrefix() . "RightConfig";
                     $file_path = $app_config->getAppPath('lib/config/' . $class_name . ".class.php");
                     $right_config = null;
                     if (!file_exists($file_path)) {
                         continue;
                     }
                     waSystem::getInstance($aid, $app_config);
                     include_once $file_path;
                     /**
                      * @var waRightConfig
                      */
                     $right_config = new $class_name();
                     $right_config->clearRights($contact_id);
                 }
             } catch (Exception $e) {
                 // silently ignore other applications errors
             }
         }
     }
     // Update $app_id access records
     $app_config = SystemConfig::getAppConfig($app_id);
     $class_name = $app_config->getPrefix() . "RightConfig";
     $file_path = $app_config->getAppPath('lib/config/' . $class_name . ".class.php");
     $right_config = null;
     if (file_exists($file_path)) {
         // Init app
         waSystem::getInstance($app_id, $app_config);
         include_once $file_path;
         /**
          * @var waRightConfig
          */
         $right_config = new $class_name();
     }
     foreach ($values as $name => $value) {
         if ($right_config && $right_config->setRights($contact_id, $name, $value)) {
             // If we've got response from custom rights config, then no need to update main rights table
             continue;
         }
         // Set default limited rights
         if ($right_config && $name == 'backend' && $value == 1) {
             /**
              * @var $right_config waRightConfig
              */
             foreach ($right_config->setDefaultRights($contact_id) as $n => $v) {
                 $right_model->save($contact_id, $app_id, $n, $v);
             }
         }
         $right_model->save($contact_id, $app_id, $name, $value);
     }
     waSystem::setActive('contacts');
     if ($contact_id) {
         // TODO: use waContact method for disabling
         $is_user = waRequest::post('is_user', null, 'int');
         if ($is_user === -1 || $is_user === 0 || $is_user === 1) {
             $contact = new waContact($contact_id);
             $contact->save(array('is_user' => $is_user));
             $this->response['access_disable_msg'] = contactsHelper::getAccessDisableMsg($contact);
         }
     }
     $has_backend_access_new = $this->hasBackendAccess($contact_id);
     if ($has_backend_access_new !== $has_backend_access_old) {
         if ($has_backend_access_new) {
             $this->logAction("grant_backend_access", null, $contact_id);
         } else {
             $this->logAction("revoke_backend_access", null, $contact_id);
         }
     }
 }
 private function deleteApp($app_id)
 {
     //remove db tables and etc
     $paths = array();
     /**
      * @var waAppConfig
      */
     $system = wa($app_id);
     $system->setActive($app_id);
     $app = SystemConfig::getAppConfig($app_id);
     $info = $app->getInfo();
     $name = _wd($app_id, $info['name']);
     /**
      * @var waAppConfig $config ;
      */
     $config = $system->getConfig();
     if (!empty($info['plugins'])) {
         $plugins = $config->getPlugins();
         foreach ($plugins as $plugin => $enabled) {
             try {
                 if ($enabled && ($plugin_instance = $system->getPlugin($plugin))) {
                     $plugin_instance->uninstall();
                 }
             } catch (Exception $ex) {
                 waLog::log($ex->getMessage(), 'installer.log');
             }
             $this->apps->updateAppPluginsConfig($app_id, $plugin, null);
             //wa-apps/$app_id/plugins/$slug
             $paths[] = wa()->getAppPath("plugins/" . $plugin, $app_id);
             while ($path = array_shift($paths)) {
                 waFiles::delete($path, true);
             }
             $paths = array();
         }
     }
     $config->uninstall();
     $this->apps->updateAppConfig($app_id, null);
     $paths[] = wa()->getTempPath(null, $app_id);
     //wa-cache/temp/$app_id/
     $paths[] = wa()->getAppCachePath(null, $app_id);
     //wa-cache/apps/$app_id/
     $paths[] = wa()->getDataPath(null, true, $app_id);
     //wa-data/public/$app_id/
     $paths[] = wa()->getDataPath(null, false, $app_id);
     //wa-data/protected/$app_id/
     if ($this->options['log']) {
         $paths[] = wa()->getConfig()->getPath('log') . '/' . $app_id;
         //wa-log/$app_id/
     }
     if ($this->options['config']) {
         $paths[] = wa()->getConfigPath($app_id);
         //wa-config/$app_id/
     }
     $paths[] = wa()->getAppPath(null, $app_id);
     //wa-apps/$app_id/
     $paths[] = wa()->getAppCachePath(null, 'webasyst');
     //wa-cache/apps/webasyst/
     foreach ($paths as $path) {
         try {
             waFiles::delete($path, true);
         } catch (waException $ex) {
         }
     }
     return $name;
 }