CakePHP授權

WBOY
發布: 2024-08-29 12:58:47
原創
668 人瀏覽過

以下文章提供了 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();
?>
登入後複製

說明:

這裡我們建立一個模板來查看結果。執行上述程式碼後,我們將得到以下畫面。

CakePHP授權

在這裡我們可以提供用於登入的使用者憑證。

我們必須建立另一個用於登出的 PHP 檔案並編寫以下程式碼。

代碼:

<?php
echo $this->Html->link('logout',[
"controller" => "Auth","action" => "logout"
]);
?>
登入後複製

After executing the above code, we will get the following screen.

CakePHP授權

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
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!