shovv / phalcon-user-plugin
Phalcon PHP 框架的用户插件
7.0.1
2024-01-30 18:33 UTC
Requires
- php: >=8.2.0
- facebook/php-sdk-v4: 4.0.*
- google/apiclient: 2.15.*
- imagine/imagine: 1.3.*
- paragonie/random_compat: 2.0.*
- symfony/mailer: 6.3.0
Requires (Dev)
- phalcon/devtools: >=5.0
- phalcon/incubator: dev-php8-phalcon5
- phpunit/phpunit: 10.4.2
- dev-master
- 7.0.1
- 7.0.0
- 6.1.0
- 6.0.0
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- v3.0.0.x-dev
- 3.0.0
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- dev-feature/5-refactor-mail-class
- dev-fix/4-fixes-after-user-tests
- dev-fix/3-add-session-service
- dev-fix/provide-stable-symfony-mailer-version
- dev-feature/2-fix-failing-tests
- dev-feature/1-upgrade-to-phalcon-v5
- dev-2.0-dev
This package is not auto-updated.
Last update: 2024-09-24 21:26:03 UTC
README
关于
这是一个基于 Vokuro ACL 概念的插件。
功能
- 使用领英账户登录/注册
- 使用 Twitter 账户登录/注册
- 使用 Google 账户登录/注册
- 更改密码
- 通过电子邮件恢复密码
- 保护网站的不同区域,用户必须登录才能访问
- 根据每个用户的 ACL 列表保护不同操作
- 用户资料:出生日期、出生地、当前位置、个人资料照片
- 位置 - 使用 Google API 保存位置 - 请参阅 Wiki 中的示例
- 简单的通知系统
安装
推荐通过 Composer 安装。
对于小版本/错误版本(基于 语义版本化)
$ composer require crada/phalcon-user-plugin:^3.0.0
对于所有提交和最新版本(不稳定)
$ composer require --dev crada/phalcon-user-plugin:v3.0.0
仅针对错误版本(最大稳定性)
$ composer require crada/phalcon-user-plugin:~3.0.0
或者手动添加以下内容到您的 composer.json
{ "require": { "crada/phalcon-user-plugin": "^3.0.0" } }
然后更新 composer
$ composer update
插入插件
将以下行添加到事件管理器中
$security = new \Phalcon\UserPlugin\Plugin\Security($di); $eventsManager->attach('dispatch', $security);
完整示例代码
use Phalcon\UserPlugin\Plugin\Security as SecurityPlugin; use Phalcon\Mvc\Dispatcher; $di->setShared( 'dispatcher', function() use ($di) { $eventsManager = $di->getShared('eventsManager'); $security = new SecurityPlugin($di); $eventsManager->attach('dispatch', $security); $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($eventsManager); return $dispatcher; } );
注册 Auth、Mail 和 Acl 服务
use Phalcon\UserPlugin\Auth\Auth; use Phalcon\UserPlugin\Acl\Acl; use Phalcon\UserPlugin\Mail\Mail; $di->setShared( 'auth', function() { return new Auth(); } ); $di->setShared( 'acl', function() { return new Acl(); } ); $di->setShared( 'mail', function() { return new Mail(); } );
配置
您必须将配置键添加到您的 config.php 文件中。如果您正在使用多模块应用程序,我建议您为每个模块设置单独的配置。
配置示例
在下面的示例中,您将网站视为公共网站,除了 USER 控制器的 ACCOUNT 和 PROFILE 操作
'pup' => [ 'redirect' => [ 'success' => 'user/profile', 'failure' => 'user/login' ], 'resources' => [ 'type' => 'public', 'resources' => [ '*' => [ // All except 'user' => ['account', 'profile'] ] ] ] ];
在下面的示例中,唯一的公共资源是 USER 控制器的 LOGIN 和 REGISTER 操作
'pup' => [ 'redirect' => [ 'success' => 'user/profile', 'failure' => 'user/login' ], 'resources' => [ 'type' => 'public', 'resources' => [ 'user' => [ 'user' => ['login', 'register'] ] ] ] ];
在下面的示例中,您将网站视为私有网站,除了 USER 控制器的 LOGIN 和 REGISTER 操作
'pup' => [ 'redirect' => [ 'success' => 'user/profile', 'failure' => 'user/login' ], 'resources' => [ 'type' => 'private', 'resources' => [ '*' => [ // All except 'user' => ['login', 'register'] ] ] ] ];
在下面的示例中,唯一的私有资源是 USER 控制器的 ACCOUNT 和 PROFILE 操作
'pup' => [ 'redirect' => [ 'success' => 'user/profile', 'failure' => 'user/login' ], 'resources' => [ 'type' => 'private', 'resources' => [ 'user' => [ 'user' => ['account', 'profile'] ] ] ] ];
带有连接器的配置示例
// phalcon-user-plugin 'pup' => [ 'redirect' => [ 'success' => 'user/profile', 'failure' => 'user/login' ], 'resources' => [ 'type' => 'public', 'resources' => [ '*' => [ // All except 'user' => ['account', 'profile'] ] ] ], 'connectors' => [ 'linkedIn' => [ 'api_key' => 'YOUR_LINKED_IN_APP_ID', 'api_secret' => 'YOUR_LINKED_IN_APP_SECRET', 'callback_url' => 'CALLBACK_URL' ], 'twitter' => [ 'consumer_key' => 'TWITTER_CONSUMER_KEY', 'consumer_secret' => 'TWITTER_CONSUMER_SECRET', // Leave empty if you don't want to set it 'user_agent' => 'YOUR_APPLICATION_NAME' ], 'google' => [ 'application_name' => 'YOUR_APPLICATION_NAME', 'client_id' => 'YOUR_CLIENT_ID', 'client_secret' => 'YOUR_CLIENT_SECRET', 'developer_key' => 'YOUR_DEVELOPER_KEY', 'redirect_uri' => 'YOUR_REDIRECT_URI' ] ] ];
示例控制器
- 要获取完整的控制器示例,请阅读 Wiki 页面:https://github.com/calinrada/PhalconUserPlugin/wiki/Controller
class UserController extends Controller { /** * Login user * @return \Phalcon\Http\ResponseInterface */ public function loginAction() { if (true === $this->auth->isUserSignedIn()) { $this->response->redirect(['action' => 'profile']); } $form = new LoginForm(); try { $this->auth->login($form); } catch (AuthException $e) { $this->flash->error($e->getMessage()); } $this->view->form = $form; } /** * Login with LinkedIn account */ public function loginWithLinkedInAction() { try { $this->view->disable(); $this->auth->loginWithLinkedIn(); } catch(AuthException $e) { $this->flash->error('There was an error connectiong to LinkedIn.'); } } /** * Login with Twitter account */ public function loginWithTwitterAction() { try { $this->view->disable(); $this->auth->loginWithTwitter(); } catch(AuthException $e) { $this->flash->error('There was an error connectiong to Twitter.'); } } /** * Login with Google account */ public function loginWithGoogleAction() { try { $this->view->disable(); $this->auth->loginWithGoogle(); } catch(AuthException $e) { $this->flash->error('There was an error connectiong to Google.'); } } /** * Logout user and clear the data from session * * @return \Phalcon\Http\ResponseInterface */ public function signoutAction() { $this->auth->remove(); return $this->response->redirect('/', true); } }
已知问题
- Twitter 不提供电子邮件。我们为用户生成随机的电子邮件。您可以选择如何处理这个问题
- Facebook 库不支持 php8,因此已被删除,Facebook 登录已被删除
- Phalcon/incubator 不支持 phalcon5,因此将存在解决方案
示例
待办事项
- 实现 ACl、UserManagement 等的 CRUD 模板
- 更新 Swift Mailer 到 Symfony Mailer
- 修复测试失败 testBeforeDispatchLoopRedirect 和 testBeforeDispatchLoopFail
- 检查代码中的@TODOs