国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
什麼是CakePHP授權?
如何檢查CakePHP授權?
建立 CakePHP 授權
CakePHP Authorization Installing
Conclusion

CakePHP授權

Aug 29, 2024 pm 12:58 PM
php

以下文章提供了 CakePHP 授權的概述。 CakePHP 是一個開源工具,它以可插拔的方式提供 Auth 元件來執(zhí)行我們的任務。 Auth元件用於提供身份驗證和授權物件。換句話說,我們可以說它是兩者的組合,用於根據(jù)我們的要求確定使用者的授權和身份驗證。身份驗證意味著確定使用者憑證並驗證這些憑證,例如使用者名稱和密碼。另一方面,授權意味著根據(jù)使用者憑證和使用者提供的其他資訊對使用者進行驗證。

開始您的免費軟體開發(fā)課程

網(wǎng)頁開發(fā)、程式語言、軟體測試及其他

什麼是CakePHP授權?

正如您可能知道的,「最近」(不是最近)添加了兩個新模組來管理 CakePHP 應用程式中的身份驗證和授權的想法。從長遠來看,身份驗證和授權是在控制器層使用 AuthComponent 進行監(jiān)督的。隨著任務的發(fā)展,這兩件事通常會變得錯綜複雜,使 AuthComponent 成為同時管理許多元素的令人困惑的類別。

這些新模組背後的第一個想法是重構 AuthComponent 並創(chuàng)建明確層來處理:

確認:你是誰?

批準:你說你被允許了嗎?

我們將利用特定模型研究本文中的授權想法:我們應該設想一些遊戲應用程序,用戶將在其中監(jiān)督錦標賽。用戶希望舉辦新的錦標賽並透過具有眾多附屬關係的錦標賽會員資格加入錦標賽。除非歡迎客戶參加比賽,否則客戶不會參加錦標賽。錦標賽的玩家可以歡迎不同的使用者來玩。

如何檢查CakePHP授權?

現(xiàn)在讓我們看看如何檢查 CakePHP 授權,如下所示:

在我們各自的應用程式中實作授權中間件後,我們可以檢查授權。這是因為中間件包裝了每個請求的身份。

現(xiàn)在讓我們看看如何檢查單一資源的授權,如下所示:

他們可以幫助您真正查看單一資產(chǎn)的批準。通常這是一個 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
}

現(xiàn)在讓我們來看看如何應用範圍條件,如下:

每當您需要對各種項目進行批準檢查(例如分頁查詢)時,您通常需要取得目前客戶接觸的記錄。這個模組將這個想法作為「範圍」來實現(xiàn)。

範圍方法可讓您「範圍」查詢或結果集並傳回刷新的綱要或問題物件:

代碼:

// Fetch the identity from each and every request
$specified user = $this->request->getAttribute('identity');
$Sql_query = $specified fuser->ApplyScopeTo('index', $Sql_query);

授權組件可用於監(jiān)管活動中以??順利批準,從而提高失望的豁免率。

建立 CakePHP 授權

現(xiàn)在讓我們看看如何在 CakePHP 中建立授權,範例如下:

首先,我們需要了解需要考慮哪些參數(shù),如下:

確認是區(qū)分合適客戶最常見的方式。 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());
}
}

現(xiàn)在建立 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();
?>

說明:

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

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授權的詳細內(nèi)容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內(nèi)容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

我如何了解最新的PHP開發(fā)和最佳實踐? 我如何了解最新的PHP開發(fā)和最佳實踐? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

什麼是PHP,為什麼它用於Web開發(fā)? 什麼是PHP,為什麼它用於Web開發(fā)? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

如何設置PHP時區(qū)? 如何設置PHP時區(qū)? Jun 25, 2025 am 01:00 AM

tosetTherightTimeZoneInphp,restate_default_timezone_set()functionAtthestArtofyourscriptWithavalIdidentIdentifiersuchas'america/new_york'.1.usedate_default_default_timezone_set_set()

我如何驗證PHP中的用戶輸入以確保其符合某些標準? 我如何驗證PHP中的用戶輸入以確保其符合某些標準? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

什麼是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? 什麼是php(serialize(),Unserialize())中的數(shù)據(jù)序列化? Jun 22, 2025 am 01:03 AM

thephpfunctionserize()andunSerialize()redustoconvertComplexdatStructDestoresToroStoroStoroSandaBackagagain.1.Serialize()

如何將PHP代碼嵌入HTML文件中? 如何將PHP代碼嵌入HTML文件中? Jun 22, 2025 am 01:00 AM

可以將PHP代碼嵌入HTML文件中,但需確保文件以.php為擴展名,以便服務器能正確解析。使用標準的標籤包裹PHP代碼,可在HTML中任意位置插入動態(tài)內(nèi)容。此外,可在同一文件中多次切換PHP與HTML,實現(xiàn)條件渲染等動態(tài)功能。務必注意服務器配置及語法正確性,避免因短標籤、引號錯誤或遺漏結束標籤導致問題。

編寫清潔和可維護的PHP代碼的最佳實踐是什麼? 編寫清潔和可維護的PHP代碼的最佳實踐是什麼? Jun 24, 2025 am 12:53 AM

寫乾淨、易維護的PHP代碼關鍵在於清晰命名、遵循標準、合理結構、善用註釋和可測試性。 1.使用明確的變量、函數(shù)和類名,如$userData和calculateTotalPrice();2.遵循PSR-12標準統(tǒng)一代碼風格;3.按職責拆分代碼結構,使用MVC或Laravel式目錄組織;4.避免麵條式代碼,將邏輯拆分為單一職責的小函數(shù);5.在關鍵處添加註釋並撰寫接口文檔,明確參數(shù)、返回值和異常;6.提高可測試性,採用依賴注入、減少全局狀態(tài)和靜態(tài)方法。這些做法提升代碼質量、協(xié)作效率和後期維護便利性。

如何使用PHP執(zhí)行SQL查詢? 如何使用PHP執(zhí)行SQL查詢? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles