如果管理员在cakephp3.x中将其停用,如何限制db中的数据保存?

问题描述 投票:0回答:1

我需要一个解决方案,我可以限制用户在管理员停用后保存任何数据。假设用户在他要保存表单的页面上处于活动状态,但在同一实例中,admin已将其停用。因此,现在当他尝试保存表单时,应该将其重定向到登录页面,说“您的帐户已被停用,请联系支持人员”,而不保存数据。我在cakephp 3.x工作。我尝试使用beforeFilter。但它正在停用用户,但用户也可以保存数据。

authorization cakephp-3.x before-filter
1个回答
0
投票

我有类似的情况。我向auth组件添加了一个自定义查找程序,以限制停用的用户在停用时发出请求,但它只停止停用的用户登录而不是立即限制他们发出任何请求。这意味着已停用的用户仍可以在其余会话中访问该应用程序。 (例如10小时内,一名心怀不满的员工可能造成很大的破坏。)

我的解决方案是告诉auth组件使用控制器钩子方法进行授权。 Cookbook info here

App Controller - 初始化操作 - Auth组件

$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
]);

这就是立即记录用户的原因

App Controller - isAuthorized

public function isAuthorized()
{
    if ($this->checkActiveAndRole() === true) {
        return true;
    } 
    return false;    
}

App Controller - 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;               
}

这对我有用。即:使用app控制器中的isAuthorized()函数检查用户是否在每个请求上都处于活动状态

© www.soinside.com 2019 - 2024. All rights reserved.