public function setCurrentUser() { if ($this->isLogged()) { $userId = (string) HttpContext::getInstance()->getSession()->userId; $this->currentUser = UserManager::getInstance()->getUserInfo($userId); } }
private function beforeActionExecute() { $userId = (string) HttpContext::getInstance()->getSession()->userId; if ($userId !== "") { Helpers::redirect(''); } }
private function beforeActionExecute() { $userId = (string) HttpContext::getInstance()->getSession()->userId; if ($userId === "" || !UserManager::getInstance()->isInRoleById($userId, AppConfig::DEFAULT_ADMIN_ROLE)) { throw new ApplicationException("Not enough permissions to see this page!"); } }
/** * @return array */ public function getAllUsers() { $userId = HttpContext::getInstance()->getIdentity()->getCurrentUser()->getId(); $query = "SELECT\n u.id,\n u.username,\n u.fullname,\n r.name AS roleName\n FROM users AS u\n JOIN user_roles AS ur\n ON ur.user_id = u.id\n JOIN roles AS r\n ON r.id = ur.role_id\n WHERE u.id != ?\n ORDER BY u.username"; $result = $this->db->prepare($query); $result->execute([$userId]); return $result->fetchAll(); }
private function initController() { $controllerName = $this->controllerName; if (!Helpers::startsWith($controllerName, AppConfig::CONTROLLERS_NAMESPACE)) { $controllerName = AppConfig::CONTROLLERS_NAMESPACE . ucfirst($this->controllerName) . AppConfig::CONTROLLERS_SUFFIX; } class_exists($controllerName, false); $annotationsParser = new AnnotationsParser($controllerName, $this->actionName); $annotationsParser->checkAnnotations(); $this->controller = new $controllerName(HttpContext::getInstance()); }
private function beforeActionExecute() { $userId = (string) HttpContext::getInstance()->getSession()->userId; if ($userId == "") { Helpers::redirect("users/login"); } $userRole = UserManager::getInstance()->getUserRole(intval($userId)); if (!in_array($userRole->getName(), $this->roles)) { throw new ApplicationException("Not enough permissions to see this page!"); } }
public function start() { try { Database::createNonExistingDatabase(DatabaseConfig::DB_NAME); Database::setInstance(DatabaseConfig::DB_INSTANCE, DatabaseConfig::DB_DRIVER, DatabaseConfig::DB_USER, DatabaseConfig::DB_PASS, DatabaseConfig::DB_NAME, DatabaseConfig::DB_HOST); } catch (\Exception $e) { require_once "error.php"; exit; } Manager::getInstance()->start(); HttpContext::getInstance()->getIdentity()->setCurrentUser(); $this->frontController->dispatch(); }
<h3>Welcome to admin page, <?php echo \Framework\HttpContext\HttpContext::getInstance()->getIdentity()->getCurrentUser()->getFullName(); ?> </h3>
private function checkBindingModel() { $errors = []; $controller = AppConfig::CONTROLLERS_NAMESPACE . ucfirst($this->getControllerName()) . AppConfig::CONTROLLERS_SUFFIX; $reflector = new \ReflectionClass($controller); $method = $reflector->getMethod($this->action); if (!$method->getParameters()) { return; } $params = $method->getParameters(); $count = 0; foreach ($params as $param) { if ($param->getClass() !== null && class_exists($param->getClass()->getName(), false)) { $className = $param->getClass()->getName(); if (Helpers::endsWith($className, "BindingModel")) { $paramReflectorClass = new \ReflectionClass($param->getClass()->getName()); $bindingModelName = $paramReflectorClass->getName(); $bindingModel = new $bindingModelName(); $paramClassFields = $paramReflectorClass->getProperties(); foreach ($paramClassFields as $field) { $doc = $field->getDocComment(); $annotations = self::getBindingModelAnnotations($doc); $fieldName = $field->getName(); $setter = 'set' . $field->getName(); $displayName = array_key_exists("Display", $annotations) ? $annotations["Display"] : $fieldName; if (array_key_exists("Required", $annotations) && !isset($_POST[$fieldName]) || strlen($_POST[$fieldName]) === 0) { $errors[] = $displayName . " is required."; } else { if (array_key_exists("MinLength", $annotations) && isset($_POST[$fieldName]) && strlen($_POST[$fieldName]) < intval($annotations["MinLength"])) { $errors[] = "Min length for " . $displayName . " is " . $annotations["MinLength"]; } else { if (array_key_exists("MaxLength", $annotations) && isset($_POST[$fieldName]) && strlen($_POST[$fieldName]) > intval($annotations["MaxLength"])) { $errors[] = "Max length for " . $displayName . " is " . $annotations["MaxLength"]; } else { $bindingModel->{$setter}($_POST[$fieldName]); } } } } $this->params[] = $bindingModel; } } else { if (count($this->params) < $count + 1) { throw new \Exception("Different parameters count!"); } else { if (preg_match('/@param ([^\\s]+) \\$' . $param->getName() . "/", $method->getDocComment(), $parameterType)) { if ($parameterType[1] === "int") { $this->params[$count] = intval($this->params[$count]); } } } } $count++; } if (count($errors) > 0) { $redirect = $this->requestStr; if (HttpContext::getInstance()->getRequest()->getForm()->redirect !== "") { $redirect = HttpContext::getInstance()->getRequest()->getForm()->redirect; } $_SESSION["binding-errors"] = $errors; throw new ApplicationException("", $redirect); } }
" class="hvr-underline-reveal">Venues</a></li> <li><a href="<?php echo \Framework\Helpers\Helpers::url() . 'admin/halls'; ?> " class="hvr-underline-reveal">Halls</a></li> <li><a href="<?php echo \Framework\Helpers\Helpers::url() . 'admin/api'; ?> " class="hvr-underline-reveal">Api</a></li> </ul> </li> <?php } ?> <?php if (\Framework\HttpContext\HttpContext::getInstance()->getIdentity()->isLogged()) { ?> <li><a href="<?php echo \Framework\Helpers\Helpers::url() . 'users/profile'; ?> " class="hvr-underline-reveal"><span class="glyphicon glyphicon-user"></span></a></li> <li><a href="<?php echo \Framework\Helpers\Helpers::url() . 'users/password'; ?> "><span class="glyphicon glyphicon-lock"></span></a></li> <li><a href="<?php echo \Framework\Helpers\Helpers::url() . 'users/logout'; ?> " class="hvr-underline-reveal"><span class="glyphicon glyphicon-log-out"></span></a></li> <?php } else {
<?php use Framework\HttpContext\HttpContext; date_default_timezone_set('Europe/Sofia'); ini_set('display_errors', 1); session_start(); require_once 'Autoloader.php'; \Framework\Autoloader::init(); HttpContext::setInstance(new Framework\HttpContext\HttpRequest(), new Framework\HttpContext\HttpCookie(), new Framework\HttpContext\HttpSession(), new \Framework\HttpContext\HttpUser()); //\Framework\HttpContext\HttpContext::getInstance()->getCookies()->time->delete(); // //$date = new DateTime('now', new DateTimeZone('Europe/Sofia')); //\Framework\HttpContext\HttpContext::getInstance()->getCookies()->time = $date->format("Y-m-d H:i:s"); // //echo "Time cookie " . \Framework\HttpContext\HttpContext::getInstance()->getCookies()->time . " END"; //var_dump($_COOKIE); // //\Framework\HttpContext\HttpContext::getInstance()->getCookies()->time->delete(); // //var_dump($_COOKIE); // //exit; //\Framework\HttpContext\HttpContext::getInstance()->getSession()->time->delete(); // //var_dump($_SESSION); // //$date = new DateTime('now', new DateTimeZone('Europe/Sofia')); //\Framework\HttpContext\HttpContext::getInstance()->getSession()->time = $date->format("Y-m-d H:i:s"); // //echo "Time session " . \Framework\HttpContext\HttpContext::getInstance()->getSession()->time . " END"; //var_dump($_SESSION);
/** * @@Authorize * @POST * @param CreateConferenceBindingModel $model */ public function createPst(CreateConferenceBindingModel $model) { try { if (!Helpers::validateDate($model->getStartTime())) { throw new ApplicationException("Start time is not a valid date!"); } if (!Helpers::validateDate($model->getEndTime())) { throw new ApplicationException("End time is not a valid date!"); } $conference = new Conference($model->getTitle(), $model->getDescription(), $model->getStartTime(), $model->getEndTime(), intval(HttpContext::getInstance()->getIdentity()->getCurrentUser()->getId())); $conferenceId = ConferencesRepository::getInstance()->create($conference); $this->redirect("conferences/edit/" . $conferenceId); } catch (ApplicationException $e) { $_SESSION["binding-errors"] = [$e->getMessage()]; $this->redirect("conferences/create"); } }