Bladeren bron

Allow Route to handle auth

Piotr Labudda 11 jaren geleden
bovenliggende
commit
8f26b1b456
5 gewijzigde bestanden met toevoegingen van 56 en 10 verwijderingen
  1. 6 0
      SE/se-lib/Route/FixProjectPath.php
  2. 6 0
      SE/se-lib/Route/FixZasobPath.php
  3. 4 0
      SE/se-lib/RouteBase.php
  4. 31 10
      SE/se-lib/Router.php
  5. 9 0
      SE/se-lib/User.php

+ 6 - 0
SE/se-lib/Route/FixProjectPath.php

@@ -4,6 +4,12 @@ Lib::loadClass('RouteBase');
 
 class Route_FixProjectPath extends RouteBase {
 
+	public function handleAuth() {
+		if (!User::logged()) {
+			throw new HttpException('Unauthorized', 401);
+		}
+	}
+
 	public function defaultAction() {
 		//$sqlList['Check'] = "SHOW PROCEDURE STATUS where `Name`='update_project_path_idx_rec'";
 		SE_Layout::gora();

+ 6 - 0
SE/se-lib/Route/FixZasobPath.php

@@ -4,6 +4,12 @@ Lib::loadClass('RouteBase');
 
 class Route_FixZasobPath extends RouteBase {
 
+	public function handleAuth() {
+		if (!User::logged()) {
+			throw new HttpException('Unauthorized', 401);
+		}
+	}
+
 	public function defaultAction() {
 		//$sqlList['Check'] = "SHOW PROCEDURE STATUS where `Name`='update_zasob_path_idx_rec'";
 		SE_Layout::gora();

+ 4 - 0
SE/se-lib/RouteBase.php

@@ -47,4 +47,8 @@ class RouteBase {
 		return $msg;
 	}
 
+	public function handleAuth() {
+		User::authByRequest();
+	}
+
 }

+ 31 - 10
SE/se-lib/Router.php

@@ -7,20 +7,41 @@ class Router {
 	public static function route($route) {
 		if (empty($route)) return;
 
+		try {
+			$route = self::getRoute($route);
+			$route->route();
+		} catch (HttpException $e) {
+			Http::sendHeaderByCode($e->getCode());
+			die($e->getMessage());
+		} catch (Exception $e) {
+			die($e->getMessage());
+		}
+		exit;
+	}
+
+	public static function handleAuth($route) {
+		if (empty($route)) return;
+
+		try {
+			$route = self::getRoute($route);
+			$route->handleAuth();
+		} catch (HttpException $e) {
+			Http::sendHeaderByCode($e->getCode());
+			die($e->getMessage());
+		} catch (Exception $e) {
+			die($e->getMessage());
+		}
+	}
+
+	public static function getRoute($route) {
+		if (empty($route)) return;
+
 		$routeClassName = "Route_{$route}";
 		if (Lib::tryLoadClass($routeClassName)) {
 			$route = new $routeClassName();
-			try {
-				$route->route();
-			} catch (HttpException $e) {
-				Http::sendHeaderByCode($e->getCode());
-				die($e->getMessage());
-			} catch (Exception $e) {
-				die($e->getMessage());
-			}
-			exit;
+			return $route;
 		} else {
-			die("Route '{$route}' not exists");
+			throw new HttpException("Not found", 404);
 		}
 	}
 

+ 9 - 0
SE/se-lib/User.php

@@ -174,6 +174,15 @@ class User {
 	}
 
 	public static function auth() {
+		$route = V::get('_route', '', $_REQUEST);
+		if (!empty($route)) {
+			Router::handleAuth($route);
+		} else {
+			self::authByRequest();
+		}
+	}
+
+	public static function authByRequest() {
 		$task = V::get('LOGIN', '', $_REQUEST);
 
 		$data = array();