PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/controllers/components/bc_auth_configure.php

https://github.com/hashing/basercms
PHP | 140 lines | 66 code | 15 blank | 59 comment | 11 complexity | 2bab0a7f82bb319fb293641f5d56935a MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * 認証設定コンポーネント
  5. * (注)BaserのDB設計に依存している
  6. *
  7. * PHP versions 5
  8. *
  9. * baserCMS : Based Website Development Project <http://basercms.net>
  10. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  11. *
  12. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  13. * @link http://basercms.net baserCMS Project
  14. * @package baser.controllers.components
  15. * @since baserCMS v 0.1.0
  16. * @version $Revision$
  17. * @modifiedby $LastChangedBy$
  18. * @lastmodified $Date$
  19. * @license http://basercms.net/license/index.html
  20. * /
  21. /**
  22. * 認証設定コンポーネント
  23. *
  24. * @package baser.controllers.components
  25. */
  26. class BcAuthConfigureComponent extends Object {
  27. /**
  28. * コントローラー
  29. *
  30. * @var Controller
  31. * @access public
  32. */
  33. var $controller = null;
  34. /**
  35. * initialize
  36. *
  37. * @param object $controller
  38. * @return void
  39. * @access public
  40. */
  41. function initialize(&$controller) {
  42. $this->controller = $controller;
  43. }
  44. /**
  45. * 認証設定
  46. *
  47. * @param string $config
  48. * @return boolean
  49. * @access public
  50. */
  51. function setting($config) {
  52. if(empty($this->controller->BcAuth)) {
  53. return false;
  54. }
  55. $controller =& $this->controller;
  56. $auth =& $controller->BcAuth;
  57. $requestedPrefix = '';
  58. if(isset($controller->params['prefix'])) {
  59. $requestedPrefix = $controller->params['prefix'];
  60. }
  61. if(!$requestedPrefix) {
  62. return true;
  63. }
  64. $_config = array(
  65. 'loginRedirect' => '/'.$requestedPrefix,
  66. 'username' => 'name',
  67. 'password' => 'password',
  68. 'serial' => '',
  69. 'userScope' => '',
  70. 'loginAction' => ''
  71. );
  72. $config = array_merge($_config, $config);
  73. extract($config);
  74. if(empty($userModel)) {
  75. $userModel = 'User';
  76. }
  77. if(empty($loginAction)) {
  78. $loginAction = '/'.$requestedPrefix.'/users/login';
  79. }
  80. // オートリダイレクトをOFF
  81. $auth->autoRedirect = false;
  82. // エラーメッセージ
  83. $auth->loginError = '入力されたログイン情報を確認できませんでした。もう一度入力してください。';
  84. // 権限が無いactionを実行した際のエラーメッセージ
  85. $auth->authError = '指定されたページを開くにはログインする必要があります。';
  86. //ユーザIDとパスワードのフィールドを指定
  87. $auth->fields = array('username' => $username, 'password' => $password, 'serial' => $serial);
  88. $auth->authorize = 'controller';
  89. // ユーザIDとパスワードがあるmodelを指定('User'がデフォルト)
  90. $auth->userModel = $userModel;
  91. // AppController::isAuthorizeでチェックするのでコメントアウト
  92. /*if($requestedPrefix) {
  93. //$auth->userScope = array('UserGroup.auth_prefix'=>$requestedPrefix);
  94. }*/
  95. if($userScope) {
  96. $auth->userScope = $userScope;
  97. }
  98. // セッション識別
  99. $auth->sessionKey = 'Auth.'.$userModel;
  100. // ログインアクション
  101. $auth->loginAction = $loginAction;
  102. $redirect = $auth->Session->read('Auth.redirect');
  103. // 記録された過去のリダイレクト先が対象のプレフィックス以外の場合はリセット
  104. if($redirect && strpos($redirect, $requestedPrefix)===false) {
  105. $auth->Session->write('Auth.redirect',null);
  106. }
  107. // ログイン後にリダイレクトするURL
  108. $auth->loginRedirect = $loginRedirect;
  109. if(!$auth->user()) {
  110. // クッキーがある場合にはクッキーで認証
  111. $cookie = $controller->Cookie->read($auth->sessionKey);
  112. if(!empty($cookie)) {
  113. $auth->login($cookie);
  114. return true;
  115. }
  116. // インストールモードの場合は無条件に認証なし
  117. if(Configure::read('debug')==-1) {
  118. $controller->Session->delete('Message.auth');
  119. $auth->allow();
  120. }
  121. }
  122. return true;
  123. }
  124. }
  125. ?>