public function getLogout() { $c = Collection::get('auth'); $c->logout(); $this->flash()->success('@string/user_logout_success'); return $this->response()->redirect(''); }
public function getShow($object_id) { $collection_name = $this->collectionName(); $object_name = strtolower($this->objectName()); $c = Collection::get($collection_name); $c->{$object_name}->sync($object_id); }
public function beforeRoute() { /** * Check authentication. */ $auth = Collection::get('auth'); if (!$auth->isAuthenticated()) { $this->flash()->error('@string/user_not_authenticated'); $this->response()->redirect(''); return FALSE; } /** * Check route rights. */ try { $this->checkAuthorizations(); } catch (ForbiddenException $e) { $this->flash()->error('@string/user_forbidden'); $this->response()->redirect(''); return FALSE; } return TRUE; }
protected static function declareServices() { /* Registering default services. */ /** * Autoload */ Autoload::register(); /** * Biome default logger service. */ if (!Biome::hasService('logger')) { Biome::registerService('logger', function () { return new \Psr\Log\NullLogger(); }); } /** * Biome default request. */ if (!Biome::hasService('request')) { Biome::registerService('request', function () { return Request::createFromGlobals(); }); } /** * Biome default lang service. */ if (!Biome::hasService('lang')) { Biome::registerService('lang', function () { $languages = Biome::getService('request')->getLanguages(); $lang = new \Biome\Core\Lang\XMLLang($languages); return $lang; }); } /** * Biome default view service. */ if (!Biome::hasService('view')) { Biome::registerService('view', function () { $view = new \Biome\Core\View(); $app_name = Biome::getService('lang')->get('app_name'); $view->setTitle($app_name); return $view; }); } /** * Biome default rights service. */ if (!Biome::hasService('rights')) { Biome::registerService('rights', function () { $auth = \Biome\Core\Collection::get('auth'); if ($auth->isAuthenticated()) { $admin_id = 1; // Default value of the Admin role id. if (Biome::hasService('config')) { $admin_id = Biome::getService('config')->get('ADMIN_ROLE_ID', 1); } $roles = $auth->user->roles; foreach ($roles as $role) { /* If Admin. */ if ($role->role_id == $admin_id) { return new \Biome\Core\Rights\FreeRights(); } $rights = AccessRights::loadFromJSON($role->role_rights); } return $rights; } $rights = AccessRights::loadFromArray(array()); $rights->setAttribute('User', 'firstname', TRUE, TRUE)->setAttribute('User', 'lastname', TRUE, TRUE)->setAttribute('User', 'mail', TRUE, TRUE)->setAttribute('User', 'password', TRUE, TRUE)->setRoute('GET', 'index', 'index')->setRoute('GET', 'auth', 'login')->setRoute('POST', 'auth', 'signin')->setRoute('POST', 'auth', 'signup'); return $rights; }); } /** * Biome default route service. */ if (!Biome::hasService('router')) { Biome::registerService('router', function () { $router = new Route(); $router->autoroute(); return $router; }); } /** * Biome default dispatch service. */ if (!Biome::hasService('dispatcher')) { Biome::registerService('dispatcher', function () { return Biome::getService('router')->getDispatcher(); }); } Logger::info('Services registered!'); }
protected function parameterInjection($type, $name, $required, $args) { $request = \Biome\Biome::getService('request'); $value = NULL; if (empty($type)) { return $args[$name]; } switch ($type) { // Default PHP type case 'string': case 'int': return $value; break; default: // Class injection } /** * Collection injection */ if (substr($type, -strlen('Collection')) == 'Collection') { // Instanciate the collection $collection_name = strtolower(substr($type, 0, -strlen('Collection'))); $value = Collection::get($collection_name); // Check if data are sent foreach ($request->request->keys() as $key) { if (strncmp($collection_name . '/', $key, strlen($collection_name . '/')) == 0) { $raw = explode('/', $key); $total = count($raw); $iter = $value; for ($i = 1; $i < $total - 1; $i++) { $iter = $iter->{$raw[$i]}; } $v = $request->request->get($key); $iter->{$raw[$i]} = $v; } } } else { $object_name = strtolower($type); $value = ObjectLoader::get($object_name); // Check if data are sent foreach ($request->request->keys() as $key) { if (strncmp($object_name . '/', $key, strlen($object_name . '/')) == 0) { $raw = explode('/', $key); $total = count($raw); $iter = $value; for ($i = 1; $i < $total - 1; $i++) { $iter = $iter->{$raw[$i]}; } $v = $request->request->get($key); $iter->{$raw[$i]} = $v; } } } return $value; }
/** * From the longest to the smallest. */ protected function rec_fetchValue($var, &$inner_context = 'global') { $ctx = NULL; $result = $this->getContext($var, $ctx); if ($result !== NULL) { return $result; } /* Remove one item, save the name of the last one. */ $raw = explode('.', $var); if (count($raw) > 1) { $end = array_pop($raw); $result = $this->rec_fetchValue(join('.', $raw), $ctx); /* We find the preceding item, fetch the next. */ if (method_exists($result, 'get' . $end)) { $end = 'get' . $end; $result = $result->{$end}(); } else { if (method_exists($result, $end)) { $result = $result->{$end}(); } else { if (is_array($result) && isset($result[$end])) { $result = $result[$end]; } else { $result = $result->{$end}; } } } $this->setContext($var, $result, $ctx); return $result; } /* No item found, check view. */ $view = \Biome\Biome::getService('view'); $result = $view->{$raw[0]}; if ($result !== NULL) { return $result; } /* No item found, check collections. */ $result = Collection::get($raw[0]); if ($result !== NULL) { return $result; } /* No item found, check objects. */ $result = ObjectLoader::get($raw[0]); if ($result !== NULL) { return $result; } throw new \Exception('Unable to find the variable ' . $var . ' in the context!'); }