访问我的应用程序(按顺序)
>将IP地址列入白名单
>在无效的IP上重定向到404
>检查上一个活动是否为> 2小时前
>重定向到登录页面并终止会话
>通过查看$_SESSION中的用户数据来检查用户是否已登录
>重定向到登录页面(如果无效)
Index.php
(注意,它与this问题非常相似):
/**
* Set Timezone
*/
date_default_timezone_set('Zulu');
/**
* Include globals and config files
*/
require_once('env.php');
/*
* Closure for providing lazy initialization of DB connection
*/
$db = new Database();
/*
* Creates basic structures, which will be
* used for interaction with model layer.
*/
$serviceFactory = new ServiceFactory(new RepositoryFactory($db), new EntityFactory);
$serviceFactory->setDefaultNamespace('\\MyApp\\Service');
$request = new Request();
$session = new Session();
$session->start();
$router = new Router($request, $session);
/*
* Whitelist IP addresses
*/
if (!$session->isValidIp()) {
$router->import($whitelist_config);
/*
* Check if Session is expired or invalid
*/
} elseif (!$session->isValid()) {
$router->import($session_config);
/*
* Check if user is logged in
*/
} elseif (!$session->loggedIn()) {
$router->import($login_config);
} else {
$router->import($routes_config);
}
/*
* Find matched route, or throw 400 header.
*
* If matched route, add resource name
* and action to the request object.
*/
$router->route();
/*
* Initialization of View
*/
$class = '\\MyApp\\View\\' . $request->getResourceName();
$view = new $class($serviceFactory);
/*
* Initialization of Controller
*/
$class = '\\MyApp\\Controller\\' . $request->getResourceName();
$controller = new $class($serviceFactory, $view);
/*
* Execute the necessary command on the controller
*/
$command = $request->getCommand();
$controller->{$command}($request);
/*
* Produces the response
*/
echo $view->render();
$router-> import()函数获取具有路由配置的json文件并创建路由(尚未决定是否要保留该路由).我的路由器是Klein的修改版.
我的问题
这是如何检查会话数据的正确实现吗?
我希望检查会话中的用户数据是否可以在数据库中找到,但是我需要为此使用Service,并且Services应该仅由controller(?)访问.我不知道将用户发送到哪个控制器,因为如果用户登录,则路由配置会更改.
例如,如果有人试图访问www.myapp.com/orders/123,则如果有人登录,我会将其发送到Orders控制器,如果不是,则将其发送到Session控制器(以呈现登录页面). .
我已经阅读了从this开始的ACL实施问题.但是,除非我弄错了,否则这是为了控制已登录用户的访问权限,而不是未登录用户的访问权限.如果不是这种情况,请有人说明我将如何实现我的ACL检查这个?
我非常感谢您的帮助,因为对这个答案的搜索给了我真正混合的解决方案,并且其中大多数我都不喜欢或者看起来不干净.就像会话管理器一样,这基本上是我正在做的事情,但假装不这样做. = /
更新index.php(我的解决方案)
/**
* Set Timezone
*/
date_default_timezone_set('Zulu');
/**
* Include globals and config files
*/
require_once('env.php');
/*
* Closure for providing lazy initialization of DB connection
*/
$db = new Database();
/*
* Creates basic structures, which will be
* used for interaction with model layer.
*/
$serviceFactory = new ServiceFactory(new MapperFactory($db), new DomainFactory);
$serviceFactory->setDefaultNamespace('\\MyApp\\Service');
include CONFIG_PATH.'routes.php';
$request = new Request();
$router = new Router($routes,$request);
/*
* Find matched route.
*
* If matched route, add resource name
* and command to the request object.
*/
$router->route();
$session = $serviceFactory->create('Session');
/*
* Whitelist Ip address, check if user is
* logged in and session hasn't expired.
*/
$session->authenticate();
/*
* Access Control List
*/
include CONFIG_PATH.'acl_settings.php';
$aclFactory = new AclFactory($roles,$resources,$rules);
$acl = $aclFactory->build();
$user = $session->currentUser();
$role = $user->role();
$resource = $request->getResourceName();
$command = $request->getCommand();
// User trying to access unauthorized page
if (!$acl->isAllowed($role, $resource, $command) {
$request->setResourceName('Session');
$request->setCommand('index');
if ($role === 'blocked') {
$request->setResourceName('Error');
}
}
/*
* Initialization of View
*/
$class = '\\MyApp\\View\\' . $request->getResourceName();
$view = new $class($serviceFactory, $acl);
/*
* Initialization of Controller
*/
$class = '\\MyApp\\Controller\\' . $request->getResourceName();
$controller = new $class($serviceFactory, $view, $acl);
/*
* Execute the necessary command on the controller
*/
$command = $request->getCommand();
$controller->{$command}($request);
/*
* Produces the response
*/
$view->{$command}
$view->render();
我开始会话并在会话模型中授权用户.如果未登录,会话的currentUser将具有“来宾”的角色,如果其IP地址不在白名单中,则将具有“阻止”的角色.我想按照Teresko先前的ACL帖子中的建议实现Controller包装器,但是我需要一些可以重定向用户的包装器.如果他们尝试访问不允许访问的页面,我会将其发送到其主页(Session#index),如果被阻止,则将其发送到Error#index. Session#index将使View决定是否显示已登录用户的主页,如果未登录则显示登录页面(通过检查用户角色).也许不是最好的解决方案,但似乎并不可怕.
解决方法:
单一责任
您的会话对象正在做太多事情.会话或多或少只是为了保持请求之间的持久性.会话不应执行任何身份验证逻辑.您在会话中存储了登录用户的标识符,但是实际的验证,登录/注销应在身份验证服务中完成.
路线管理
根据用户身份验证状态导入不同的路由将无法很好地扩展,并且当您有更多的路由时,以后进行调试将很麻烦.最好在一个地方定义所有路由,并使用身份验证服务进行重定向(如果未授权).我对那个路由器不是很熟悉,但是通过查看文档,您应该可以
$router->respond(function ($request, $response, $service, $app) {
$app->register('auth', function() {
return new AuthService();
}
});
然后在您需要登录的路线上,您可以执行以下操作
$router->respond('GET', '/resource', function ($request, $response, $service, $app) {
if( ! $app->auth->authenticate() )
return $response->redirect('/login', 401);
// ...
});
标签:model-view-controller,architecture,session,php
来源: https://codeday.me/bug/20191120/2043983.html