以下文章提供了 CakePHP 授权的概述。 CakePHP 是一个开源工具,它以可插拔的方式提供 Auth 组件来执行我们的任务。 Auth组件用于提供身份验证和授权对象。换句话说,我们可以说它是两者的组合,用于根据我们的要求确定用户的授权和身份验证。身份验证意味着确定用户凭据并验证这些凭据,例如用户名和密码。另一方面,授权意味着根据用户凭据和用户提供的其他信息对用户进行验证。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
什么是CakePHP授权?
如您所知,添加了两个“最近”(不是最近)的新模块来管理 CakePHP 应用程序中的身份验证和授权的思想。从长远来看,身份验证和授权是在控制器层使用 AuthComponent 进行监督的。随着任务的发展,这两件事通常会变得错综复杂,使 AuthComponent 成为同时管理许多元素的令人困惑的类。
这些新模块背后的第一个想法是重构 AuthComponent 并创建显式层来处理:
确认:你是谁?
批准:你说你被允许了吗?
我们将利用特定模型研究本文中的授权想法:我们应该设想一些游戏应用程序,用户将在其中监督锦标赛。用户希望举办新的锦标赛并通过具有众多附属关系的锦标赛会员资格加入锦标赛。除非欢迎客户参加比赛,否则客户不会参加锦标赛。锦标赛的玩家可以欢迎不同的用户来玩。
如何检查CakePHP授权?
现在让我们看看如何检查 CakePHP 授权,如下所示:
在我们各自的应用程序中实现授权中间件后,我们可以检查授权。这是因为中间件包装了每个请求的身份。
现在让我们看看如何检查单个资源的授权,如下所示:
他们可以帮助您真正查看对单个资产的批准。通常这是一个 ORM 物质或应用领域对象。
您的政策给出了决定批准选择的理由:
代码:
// Fetch identity from each and every request $user = $this->request->getAttribute('identity'); // Checking authorization on $sample if ($user->can('delete', $sample)) { // Do delete operation }
现在让我们看看如何应用范围条件,如下所示:
每当您需要对各种项目进行批准检查(例如分页查询)时,您通常需要获取当前客户接触的记录。这个模块将这个想法作为“范围”来实现。
范围方法允许您“范围”查询或结果集并返回刷新的纲要或问题对象:
代码:
// Fetch the identity from each and every request $specified user = $this->request->getAttribute('identity'); $Sql_query = $specified fuser->ApplyScopeTo('index', $Sql_query);
授权组件可用于监管活动中以顺利批准,从而提高失望的豁免率。
创建 CakePHP 授权
现在让我们看看如何在 CakePHP 中创建授权,示例如下:
首先,我们需要了解需要考虑哪些参数,如下:
确认是区分合适客户的最常见方式。 CakePHP 支持三种验证。
- FormAuthenticate:它允许您确认给定结构化 POST 信息的客户端。通常,这是客户端输入数据的登录结构。这是默认的验证策略。
- BasicAuthenticate:它允许您确认客户端正在使用基本 HTTP 验证。
- DigestAuthenticate:它允许您确认客户端正在使用摘要 HTTP 验证。
首先,我们需要配置routes.php文件,如下:
代码:
<?php use Cake\Core\Plugin; use Cake\Routing\RouteBuilder; use Cake\Routing\Router; Router::defaultRouteClass('DRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/auth',['controller'=>'Auth','action'=>'index']); $routes->connect('/login',['controller'=>'Auth','action'=>'login']); $routes->connect('/logout',['controller'=>'Auth','action'=>'logout']); $routes->fallbacks('DRoute'); }); Plugin::routes();
之后,我们需要创建一个controller.php文件,并编写如下代码:
代码:
<?php namespace App\Controller; use Cake\Controller\Controller; use Cake\Event\Event; use Cake\Controller\Component\AuthComponent; class DemoController extends Controller { public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('Auth', [ 'authenticate' => [ 'Form' => [ 'fields' => [ 'username' => 'userid', 'password' => 'userpass' ] ] ], 'loginAction' => [ 'controller' => 'Authexs', 'action' => 'login' ], 'loginRedirect' => [ 'controller' => 'Authexs', 'action' => 'index' ], 'logoutRedirect' => [ 'controller' => 'Authexs', 'action' => 'login' ] ]); } public function BFilter(Event $eventt) { $this->Auth->allow(['index','view']); $this->set('loggedIn', $this->Auth->specified user()); } }
现在创建 authcontrollr.php 文件并编写以下代码:
代码:
<?php namespace App\Controller; use App\Controller\AppController; use Cake\ORM\TableRegistry; use Cake\Datasource\ConnectionManager; use Cake\Event\Eventt; use Cake\Auth\DefaultPasswordHasher; class AuthController extends AppController { var $component = array('Auth'); public function index(){ } public function login(){ if($this->request->is('post')) { $specified_user = $this->Auth->identify(); if($user){ $this->Auth->setUser($specified_user); return $this->redirect($this->Auth->redirectUrl()); } else $this->Flash->errormsg('Entered username and password is wrong'); } } public function logout(){ return $this->redirect($this->Auth->logout()); } }
最后,我们需要创建一个登录模板来查看结果,如下。
<?php echo $this->Form->create(); echo $this->Form->control('UserID'); echo $this->Form->control('Userpass'); echo $this->Form->button('Submit'); echo $this->Form->end(); ?>
说明:
这里我们创建一个模板来查看结果。执行上述代码后,我们将得到以下屏幕。
在这里我们可以提供用于登录的用户凭据。
我们必须创建另一个用于注销的 PHP 文件并编写以下代码。
代码:
<?php echo $this->Html->link('logout',[ "controller" => "Auth","action" => "logout" ]); ?>
After executing the above code, we will get the following screen.
CakePHP Authorization Installing
Now let’s see how we can install authorization in CakePHP as follows:
First, we need to load the plugin by using the following statement as follows:
Code:
$this-> addPlugin('Authorization');
After that, we need to enable all authorization plugins by importing the following class as follows:
Code:
use Authorization\AuthorizationService; use Authorization\AuthorizationServiceInterface; use Authorization\AuthorizationServiceProviderInterface; use Authorization\Middleware\AuthorizationMiddleware; use Authorization\Policy\OrmResolver;
After creating a policy as per our requirement, we also need to fix add and edit action as per our requirement. The requirement mentioned above we can achieve through coding.
Conclusion
From the above article, we have taken in the essential idea of the CakePHP authorization and see the representation and example of the CakePHP authorization. Finally, we saw how and when we use the CakePHP authorization from this article.
以上是CakePHP授权的详细内容。更多信息请关注PHP中文网其他相关文章!

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

Dreamweaver CS6
视觉化网页开发工具