[英]How to restrict saving of data in db for user when the admin deactivated it in cakephp3.x?
我需要一種解決方案,可以限制用戶在管理員取消激活后保存任何數據。 假設用戶在要保存表單的頁面上處於活動狀態,但同時管理員已停用了該用戶。 因此,現在,當他嘗試保存表單時,應該將他重定向到登錄頁面,其中顯示“您的帳戶已被停用,請聯系支持人員”,而不保存數據。 我在cakephp 3.x中工作。 我嘗試使用beforeFilter。 但這不僅使用戶停用,而且用戶也能夠保存數據。
我也有類似的情況。 我在auth組件中添加了一個自定義查找器,以限制停用的用戶在停用時發出請求,但它僅停止停用的用戶登錄,而沒有立即限制它們發出任何請求。 這意味着停用的用戶仍然可以在會話的剩余時間內訪問該應用程序。 (例如,在10個小時之內,不滿的失職員工可能會造成很多破壞。)
我的解決方案是告訴auth組件使用控制器掛鈎方法進行授權。 食譜信息在這里
App Controller-初始化操作-身份驗證組件
$this->loadComponent('Auth', [
'authorize' => 'Controller', // ADDED THIS LINE
'authenticate' => [
'Form' => [
'finder' => 'active' // Custom finder to retrieve details for login of active users - for login only.
]
],
// Other auth component actions here
]);
這就是立即將用戶注銷的原因
應用控制器-isAuthorized
public function isAuthorized()
{
if ($this->checkActiveAndRole() === true) {
return true;
}
return false;
}
應用程序控制器-checkActiveAndRole-(簡化此職位)
private function checkActiveAndRole()
{
// Initialise and validate the id from auth component.
$id = $this->Auth->user('id');
// Select the users status and role.
$Users = TableRegistry::getTableLocator()->get('Users');
$query = $Users->find('statusRole', [
'id' => $id
]);
if ($query->isEmpty()) {
return false;
}
$status = 0;
foreach ($query as $row):
$status = $row->status;
endforeach;
// Check if the user is active.
if ($status === 0) {
return false;
}
return true;
}
這對我有用。 即:使用應用程序控制器中的isAuthorized()函數,檢查用戶是否對每個請求都處於活動狀態
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.