rootwork / phalcon-extras
Phalcon 扩展
0.3.0
2019-05-20 10:14 UTC
Requires
- php: >=7.2
Requires (Dev)
- firebase/php-jwt: ^3.0
- maknz/slack: dev-master
- phpunit/phpunit: ^5.3
- rootwork/phpunit-helpers: dev-master
Suggests
- firebase/php-jwt: Required for JWT sessions
- maknz/slack: Required to use the Slack log adapter
Replaces
- rootwork/phalcon-toolbox: 0.1.*
README
Phalcon 有用的类集合
安装
composer require rootwork/phalcon-extras:dev-master
授权组件
使用 Phalcon\Acl,见 acl.example.php。
<?php
cp acl.example.php app/config/acl.php
AuthComponent 使用事件来允许或拒绝会话角色后执行自定义逻辑。
<?php /** * Setup the auth component */ $di->setShared('auth', function () { /** @var \Phalcon\Config $aclConfig */ $aclConfig = include APP_PATH . '/config/acl.php'; $auth = new AuthComponent($aclConfig->toArray()); $eventsManager = new EventsManager(); $eventsManager->attach('auth:afterAllowed', function (Event $event, AuthComponent $auth) { if ($auth->persistent->role == 'Guest') { return true; // If the ACL allows a Guest at this route, no additional steps } // Load authorized user from the DB if ($user = User::findFirstById($auth->persistent->userId)) { $auth->di->setShared('user', $user); return true; } return false; }); $eventsManager->attach('auth:afterDenied', function (Event $event, AuthComponent $auth) { // Redirect unathorized users $auth->response->redirect('/login'); $auth->response->send(); return false; }); $auth->setEventsManager($eventsManager); return $auth; }); /** * Register a dispatcher */ $di->setShared('dispatcher', function () use ($config, $di) { $eventsManager = new EventsManager(); $eventsManager->attach('dispatch:beforeExecuteRoute', $di->get('auth')); $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('App\Controller'); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); /** * Make the current user available to the app */ $di->setShared('user', function () { return null; });
创建授权会话
在你的登录代码中,你必须设置用户角色。你可以选择性地持久化其他认证数据。
<?php // In a login action... $auth = $this->getDI()->get('auth'); $auth->setRole($user->role); // Optional persisted auth data $auth->persistent->userId = $user->id; $auth->persistent->name = $user->name;
待办事项
- 使用 Micro 应用程序记录使用说明