pieceofcake2/authenticate 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pieceofcake2/authenticate

最新稳定版本:v1.0.3

Composer 安装命令:

composer require pieceofcake2/authenticate

包简介

CakePHP plugin with authentication classes for AuthComponent.

README 文档

README

GitHub License Packagist Version PHP CakePHP CI Codecov

This is forked for CakePHP2.

Plugin containing some authenticate classes for AuthComponent.

Current classes:

  • MultiColumnAuthenticate, allow login with multiple db columns in single username field For example username or email
  • CookieAuthenticate, login with a cookie
  • TokenAuthenticate, login with a token as url parameter or header

GoogleAuthenticate is moved to separate repo: https://github.com/ceeram/GoogleAuthenticate

Requirements

  • PHP 8.0+
  • CakePHP 2.10+

Installation

run: composer require pieceofcake2/authenticate

Usage

In app/Config/bootstrap.php add: CakePlugin::load('Authenticate');

Configuration:

Setup the authentication class settings

MultiColumnAuthenticate:

    //in $components
    public $components = [
        'Auth' => [
            'authenticate' => [
                'Authenticate.MultiColumn' => [
                    'fields' => [
                        'username' => 'login',
                        'password' => 'password'
                    ],
                    'columns' => ['username', 'email'],
                    'userModel' => 'User',
                    'scope' => ['User.active' => 1],
                ]
            ]
        ]
    ];

    //Or in beforeFilter()
    $this->Auth->authenticate = [
        'Authenticate.MultiColumn' => [
            'fields' => [
                'username' => 'login',
                'password' => 'password'
            ],
            'columns' => ['username', 'email'],
            'userModel' => 'User',
            'scope' => ['User.active' => 1],
        ]
    ];

CookieAuthenticate:

    //in $components
    public $components = [
        'Auth' => [
            'authenticate' => [
                'Authenticate.Cookie' => [
                    'fields' => [
                        'username' => 'login',
                        'password' => 'password'
                    ],
                    'userModel' => 'SomePlugin.User',
                    'scope' => ['User.active' => 1],
                ]
            ]
        ]
    ];

    //Or in beforeFilter()
    $this->Auth->authenticate = [
        'Authenticate.Cookie' => [
            'fields' => [
                'username' => 'login',
                'password' => 'password'
            ],
            'userModel' => 'SomePlugin.User',
            'scope' => ['User.active' => 1],
        ]
    ];

Setup both:

It will first try to read the cookie, if that fails will try with form data:

    //in $components
    public $components = [
        'Auth' => [
            'authenticate' => [
                'Authenticate.Cookie' => [
                    'fields' => [
                        'username' => 'login',
                        'password' => 'password'
                    ],
                    'userModel' => 'SomePlugin.User',
                    'scope' => ['User.active' => 1],
                ],
                'Authenticate.MultiColumn' => [
                    'fields' => [
                        'username' => 'login',
                        'password' => 'password'
                    ],
                    'columns' => ['username', 'email'],
                    'userModel' => 'User',
                    'scope' => ['User.active' => 1],
                ]
            ]
        ]
    ];

Security

For enhanced security, make sure you add this code to your AppController::beforeFilter() if you intend to use Cookie authentication:

public function beforeFilter() {
  $this->Cookie->type('rijndael'); //Enable AES symetric encryption of cookie
}

Setting the cookie

Example for setting the cookie:

<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController
{
    public $components = ['Cookie'];

    public function beforeFilter() {
        $this->Cookie->type('rijndael');
    }

    public function login() {
        if ($this->Auth->loggedIn() || $this->Auth->login()) {
            $this->_setCookie();
            $this->redirect($this->Auth->redirect());
        }
    }

    protected function _setCookie() {
        if (!$this->request->data('User.remember_me')) {
            return false;
        }
        $data = [
            'username' => $this->request->data('User.username'),
            'password' => $this->request->data('User.password')
        ];
        $this->Cookie->write('User', $data, true, '+1 week');
        return true;
    }

    public function logout() {
        $this->Auth->logout();
        $this->Session->setFlash('Logged out');
        $this->redirect($this->Auth->redirect('/'));
    }
}

TokenAuthenticate

    //in $components
    public $components = [
        'Auth' => [
            'authenticate' => [
                'Authenticate.Token' => [
                    'parameter' => '_token',
                    'header' => 'X-MyApiTokenHeader',
                    'userModel' => 'User',
                    'scope' => ['User.active' => 1],
                    'fields' => [
                        'username' => 'username',
                        'password' => 'password',
                        'token' => 'public_key',
                    ],
                    'continue' => true,
                ]
            ]
        ]
    ];

    //Or in beforeFilter()
    $this->Auth->authenticate = [
        'Authenticate.Token' => [
            'parameter' => '_token',
            'header' => 'X-MyApiTokenHeader',
            'userModel' => 'User',
            'scope' => ['User.active' => 1],
            'fields' => [
                'username' => 'username',
                'password' => 'password',
                'token' => 'public_key',
            ],
            'continue' => true,
        ]
    ];

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 40
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-07