Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

3 Apr 2017

[CakePHP2] Using ACL Behaviar with cakedc/users

As of April, 2017, with cake 2.9.7 and the newest plugins,
You will need 
     'userModel' => 'AppUser',
at your settings for Auth Component Section in AppController.php.
whole block would be:
 public $components = array(
  'Session',
  'Flash',
  'Acl',
  'Auth' => array(
   'loginAction' => array(
    'controller' => 'app_users',
    'action' => 'login',
    'plugin' => false
   ),
   'loginRedirect' => array(
    'controller' => 'posts',
    'action' => 'index'
   ),
   'logoutRedirect' => array(
    'controller' => 'pages',
    'action' => 'display',
    'home'
   ),
   'flash' => array(
    'element' => 'alert',
    'key' => 'auth',
    'params' => array(
     'plugin' => 'BoostCake',
     'class' => 'alert-error'
    )
   ),
   'authorize' => array(
    'Actions' => array(
     'actionPath' => 'controllers',
     'userModel' => 'AppUser',
    ),
   ),
   //'authError' => 'Did you really think you are allowed to see that?',
   'authenticate' => array(
    'Form' => array(
     'passwordHasher' => 'Blowfish',
     'userModel' => 'AppUser',
     'fields' => array(
      'username' => 'username', //Default is 'username' in the userModel
      'password' => 'password'  //Default is 'password' in the userModel
     ),
    ),
   ),
  ),
  'DebugKit.Toolbar'
 );

3 Jan 2015

[CakePHP2] データベースにログをとる

Logging(CakePHP official)
http://book.cakephp.org/2.0/en/core-libraries/logging.html
Database logging in CakePHP
http://blog.jandorsman.com/blog/database-logging-in-cakephp
一つ目の記事に書いてあることはCake2.*でもそのままつかえます。ファイルの場所が 'app/libs/log/database_logger.php'(for CakePHP 1.*) から 'app/Lib/Log/Engine/DatabaseLog.php' (for CakePHP 2.*)になるぐらいです。素晴しい。

たとえばログにセッション情報(ユーザIDとかね)を記録したい、みたいな事になったら、ユーザがログインしてるのか確かめないと、ログインしてない時に上手くログに入りません。例えばログイン失敗しました、みたいなログがきちんと取られないことになります。
public function write($type, $message) {
 $log['type'] = ucfirst($type);
 $log['created'] = date('Y-m-d H:i:s');
 $log['content'] = $message;
 if ($login = CakeSession::read('Auth.User')) {
  $log['user_id'] = $login['id'];
 }


 return $this->Log->save($log);
}

[CakePHP2]Database logging (with some Cakesession Object)

Logging(CakePHP official)
http://book.cakephp.org/2.0/en/core-libraries/logging.html
Database logging in CakePHP
http://blog.jandorsman.com/blog/database-logging-in-cakephp
Well, Jan's article is totally usable even in CakePHP 2.4 except one location 'app/libs/log/database_logger.php'(for CakePHP 1.*) to 'app/Lib/Log/Engine/DatabaseLog.php' (for CakePHP 2.*).

When you want log some information via Cakesession Object (i.e. user ID), you should check the object first in case the user not logging in.

public function write($type, $message) {
 $log['type'] = ucfirst($type);
 $log['created'] = date('Y-m-d H:i:s');
 $log['content'] = $message;
 if ($login = CakeSession::read('Auth.User')) {
  $log['user_id'] = $login['id'];
 }


 return $this->Log->save($log);
}

19 Nov 2014

[PHP]時間を15分刻みに変換する

覚え書きの殴り書き。 時間を15分刻みに変換する。切り上げ、切り捨てありです。
/*
 * dateBy15Minutes
 * @assert ("2001-03-10 17:16:18") == "2001-03-10 17:15:00"
 * @assert ("2001-03-10 17:00:00") == "2001-03-10 17:00:00"
 * @assert ("2001-03-10 17:59:59") == "2001-03-10 17:45:00"
 * @assert ("2001-03-10 17:40:18") == "2001-03-10 17:30:00"
 */

 protected function _dateBy15Minutes($mode = 'ceil',$time = NULL) {
  if (\is_null($time)) {
   $time = time();
  } else {
   $time = \strtotime($time);
  }
  if ($mode == 'ceil') {
   $minute = \sprintf("%02s", \ceil(\date('i') / 15) * 15);
  } else {
   $minute = \sprintf("%02s", \floor(\date('i') / 15) * 15);
  }
  $time_string = \date("Y-m-d H:i:s", \mktime(\date('H'), $minute));
  return $time_string;
 }