/** * Check if the current route is under the admin path * * @return bool */ public function isAdminPath() { if ($this->uri->route() == $this->base || substr($this->uri->route(), 0, strlen($this->base) + 1) == $this->base . '/') { return true; } return false; }
/** * Initialize the admin. * * @throws \RuntimeException */ protected function initializeAdmin() { $this->enable(['onPagesInitialized' => ['onPagesInitialized', 1000], 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000], 'onTwigSiteVariables' => ['onTwigSiteVariables', 1000], 'onTask.GPM' => ['onTaskGPM', 0]]); // Check for required plugins if (!$this->grav['config']->get('plugins.login.enabled') || !$this->grav['config']->get('plugins.form.enabled') || !$this->grav['config']->get('plugins.email.enabled')) { throw new \RuntimeException('One of the required plugins is missing or not enabled'); } // Double check we have system.yaml and site.yaml $config_files[] = $this->grav['locator']->findResource('user://config') . '/system.yaml'; $config_files[] = $this->grav['locator']->findResource('user://config') . '/site.yaml'; foreach ($config_files as $config_file) { if (!file_exists($config_file)) { touch($config_file); } } // Decide admin template and route. $path = trim(substr($this->uri->route(), strlen($this->base)), '/'); $this->template = 'dashboard'; if ($path) { $array = explode('/', $path, 2); $this->template = array_shift($array); $this->route = array_shift($array); } // Initialize admin class. require_once __DIR__ . '/classes/admin.php'; $this->admin = new Admin($this->grav, $this->base, $this->template, $this->route); // And store the class into DI container. $this->grav['admin'] = $this->admin; // Get theme for admin $this->theme = $this->config->get('plugins.admin.theme', 'grav'); }
/** * Initialize the admin. * * @throws \RuntimeException */ protected function initializeAdmin() { $this->enable(['onTwigExtensions' => ['onTwigExtensions', 1000], 'onPagesInitialized' => ['onPagesInitialized', 1000], 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000], 'onTwigSiteVariables' => ['onTwigSiteVariables', 1000], 'onTask.GPM' => ['onTaskGPM', 0]]); // Check for required plugins if (!$this->grav['config']->get('plugins.login.enabled') || !$this->grav['config']->get('plugins.form.enabled') || !$this->grav['config']->get('plugins.email.enabled')) { throw new \RuntimeException('One of the required plugins is missing or not enabled'); } // Double check we have system.yaml and site.yaml $config_files[] = $this->grav['locator']->findResource('user://config') . '/system.yaml'; $config_files[] = $this->grav['locator']->findResource('user://config') . '/site.yaml'; foreach ($config_files as $config_file) { if (!file_exists($config_file)) { touch($config_file); } } // Initialize Admin Language if needed /** @var Language $language */ $language = $this->grav['language']; if ($language->enabled() && empty($this->grav['session']->admin_lang)) { $this->grav['session']->admin_lang = $language->getLanguage(); } // Decide admin template and route. $path = trim(substr($this->uri->route(), strlen($this->base)), '/'); $this->template = 'dashboard'; if ($path) { $array = explode('/', $path, 2); $this->template = array_shift($array); $this->route = array_shift($array); } // Initialize admin class. require_once __DIR__ . '/classes/admin.php'; $this->admin = new Admin($this->grav, $this->base, $this->template, $this->route); // And store the class into DI container. $this->grav['admin'] = $this->admin; // Get theme for admin $this->theme = $this->config->get('plugins.admin.theme', 'grav'); $assets = $this->grav['assets']; $translations = 'if (!window.translations) window.translations = {}; ' . PHP_EOL . 'window.translations.PLUGIN_ADMIN = {};' . PHP_EOL; // Enable language translations $translations_actual_state = $this->config->get('system.languages.translations'); $this->config->set('system.languages.translations', true); $strings = ['EVERYTHING_UP_TO_DATE', 'UPDATES_ARE_AVAILABLE', 'IS_AVAILABLE_FOR_UPDATE', 'AND', 'IS_NOW_AVAILABLE', 'CURRENT', 'UPDATE_GRAV_NOW', 'TASK_COMPLETED', 'UPDATE', 'UPDATING_PLEASE_WAIT', 'GRAV_SYMBOLICALLY_LINKED', 'OF_YOUR', 'OF_THIS', 'HAVE_AN_UPDATE_AVAILABLE', 'UPDATE_AVAILABLE', 'UPDATES_AVAILABLE', 'FULLY_UPDATED', 'DAYS', 'PAGE_MODES', 'PAGE_TYPES', 'ACCESS_LEVELS']; foreach ($strings as $string) { $translations .= 'translations.PLUGIN_ADMIN.' . $string . ' = "' . $this->admin->translate('PLUGIN_ADMIN.' . $string) . '"; ' . PHP_EOL; } // set the actual translations state back $this->config->set('system.languages.translations', $translations_actual_state); $assets->addInlineJs($translations); }
public function testRoute() { $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper')->init(); $this->assertSame('/grav/it/ueper', $this->uri->route()); $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper:xxx')->init(); $this->assertSame('/grav/it', $this->uri->route()); $this->uri->initializeWithURL('http://localhost:8080/grav/it/ueper?test=x')->init(); $this->assertSame('/grav/it/ueper', $this->uri->route()); $this->uri->initializeWithURL('http://localhost:8080/a/b/c/d')->init(); $this->assertSame('/a/b/c/d', $this->uri->route()); $this->uri->initializeWithURL('http://localhost:8080/a/b/c/d/e/f/a/b/c/d/e/f/a/b/c/d/e/f')->init(); $this->assertSame('/a/b/c/d/e/f/a/b/c/d/e/f/a/b/c/d/e/f', $this->uri->route()); }
/** * Authenticate user. * * @param array $form Form fields. * @return bool */ public function authenticate($form) { if (!$this->user->authenticated && isset($form['username']) && isset($form['password'])) { $user = User::load($form['username']); if ($user->exists()) { $user->authenticated = true; // Authenticate user. $result = $user->authenticate($form['password']); if ($result) { $this->user = $this->session->user = $user; /** @var Grav $grav */ $grav = $this->grav; $l = $this->grav['language']; $this->setMessage($l->translate('LOGIN_LOGGED_IN'), 'info'); // $redirect_route =$this->getLoginRedirect() ?: $this->uri->route(); $redirect_route = $this->uri->route(); $grav->redirect($redirect_route); } } } return $this->authorise(); }
/** * Authenticate user. * * @param array $form Form fields. * * @return bool */ public function authenticate($form) { if (!$this->user->authenticated && isset($form['username']) && isset($form['password'])) { $user = User::load($form['username']); //default to english if language not set if (empty($user->language)) { $user->set('language', 'en'); } if ($user->exists()) { $user->authenticated = true; // Authenticate user. $result = $user->authenticate($form['password']); if ($result) { $this->user = $this->session->user = $user; /** @var Grav $grav */ $grav = $this->grav; $this->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN', [$this->user->language]), 'info'); $redirect_route = $this->uri->route(); $grav->redirect($redirect_route); } } } return $this->authorize(); }