PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/controllers/users_controller.php

https://github.com/hashing/basercms
PHP | 573 lines | 328 code | 67 blank | 178 comment | 73 complexity | 51e5c160cfa34b2ee217b3a0047deadc MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * ユーザーコントローラー
  5. *
  6. * PHP versions 5
  7. *
  8. * baserCMS : Based Website Development Project <http://basercms.net>
  9. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  10. *
  11. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  12. * @link http://basercms.net baserCMS Project
  13. * @package baser.controllers
  14. * @since baserCMS v 0.1.0
  15. * @version $Revision$
  16. * @modifiedby $LastChangedBy$
  17. * @lastmodified $Date$
  18. * @license http://basercms.net/license/index.html
  19. */
  20. /**
  21. * ユーザーコントローラー
  22. *
  23. * ユーザーを管理するコントローラー。ログイン処理を担当する。
  24. *
  25. * @package baser.controllers
  26. */
  27. class UsersController extends AppController {
  28. /**
  29. * クラス名
  30. *
  31. * @var string
  32. * @access public
  33. */
  34. var $name = 'Users';
  35. /**
  36. * モデル
  37. *
  38. * @var array
  39. * @access public
  40. */
  41. var $uses = array('User','GlobalMenu','UserGroup');
  42. /**
  43. * ヘルパー
  44. *
  45. * @var array
  46. * @access public
  47. */
  48. var $helpers = array(BC_HTML_HELPER, BC_TIME_HELPER, BC_FORM_HELPER);
  49. /**
  50. * コンポーネント
  51. *
  52. * @var array
  53. * @access public
  54. */
  55. var $components = array('BcReplacePrefix', 'BcAuth','Cookie','BcAuthConfigure', 'BcEmail');
  56. /**
  57. * サブメニューエレメント
  58. *
  59. * @var array
  60. * @access public
  61. */
  62. var $subMenuElements = array();
  63. /**
  64. * ぱんくずナビ
  65. *
  66. * @var array
  67. * @access public
  68. */
  69. var $crumbs = array(
  70. array('name' => 'システム設定', 'url' => array('controller' => 'site_configs', 'action' => 'form')),
  71. array('name' => 'ユーザー管理', 'url' => array('controller' => 'users', 'action' => 'index'))
  72. );
  73. /**
  74. * beforeFilter
  75. *
  76. * @return void
  77. * @access public
  78. */
  79. function beforeFilter() {
  80. /* 認証設定 */
  81. // parent::beforeFilterの前に記述する必要あり
  82. if(isset($this->params['prefix'])) {
  83. $this->BcAuth->allow(
  84. 'admin_login',
  85. 'admin_logout',
  86. 'admin_login_exec',
  87. 'admin_reset_password',
  88. 'admin_ajax_login',
  89. 'admin_login_exec',
  90. 'admin_reset_password'
  91. );
  92. $this->set('usePermission',$this->UserGroup->checkOtherAdmins());
  93. }
  94. parent::beforeFilter();
  95. $this->BcReplacePrefix->allow('login', 'logout', 'login_exec', 'reset_password', 'ajax_login');
  96. }
  97. /**
  98. * ログイン処理を行う
  99. * ・リダイレクトは行わない
  100. * ・requestActionから呼び出す
  101. *
  102. * @return boolean
  103. * @access public
  104. */
  105. function admin_login_exec() {
  106. if(!$this->data) {
  107. return false;
  108. }
  109. if($this->BcAuth->login($this->data)) {
  110. return true;
  111. }
  112. return false;
  113. }
  114. /**
  115. * [ADMIN] 管理者ログイン画面
  116. *
  117. * @return void
  118. * @access public
  119. */
  120. function admin_login() {
  121. if($this->BcAuth->loginAction != ('/'.$this->params['url']['url'])) {
  122. $this->notFound();
  123. }
  124. $user = $this->BcAuth->user();
  125. $userModel = $this->BcAuth->userModel;
  126. if($this->data) {
  127. if ($user) {
  128. if (!empty($this->data[$userModel]['saved'])) {
  129. if(Configure::read('BcRequest.agentAlias') != 'mobile') {
  130. $this->setAuthCookie($this->data);
  131. } else {
  132. $this->BcAuth->saveSerial();
  133. }
  134. unset($this->data[$userModel]['save']);
  135. }else {
  136. $this->Cookie->destroy();
  137. }
  138. $this->Session->setFlash("ようこそ、".$user[$userModel]['real_name_1']." ".$user[$userModel]['real_name_2']." さん。");
  139. }
  140. }
  141. if ($user) {
  142. $this->redirect($this->BcAuth->redirect());
  143. } else {
  144. if($this->data) {
  145. $this->redirect($this->referer());
  146. }
  147. }
  148. $pageTitle = 'ログイン';
  149. if(isset($this->params['prefix'])) {
  150. $prefixAuth = Configure::read('BcAuthPrefix.'.$this->params['prefix']);
  151. if($prefixAuth && isset($prefixAuth['loginTitle'])) {
  152. $pageTitle = $prefixAuth['loginTitle'];
  153. }
  154. }
  155. /* 表示設定 */
  156. $this->crumbs = array();
  157. $this->subMenuElements = '';
  158. $this->pageTitle = $pageTitle;
  159. }
  160. /**
  161. * [ADMIN] 代理ログイン
  162. *
  163. * @param int $id
  164. * @return ダッシュボードへのURL
  165. * @access public
  166. */
  167. function admin_ajax_agent_login($id) {
  168. if(!$this->Session->check('AuthAgent')) {
  169. $user = $this->BcAuth->user();
  170. $this->Session->write('AuthAgent', $user);
  171. }
  172. $this->data = $this->User->find('first', array('conditions' => array('User.id' => $id), 'recursive' => -1));
  173. Configure::write('debug', 0);
  174. $this->setAction('admin_ajax_login');
  175. exit();
  176. }
  177. /**
  178. * 代理ログインをしている場合、元のユーザーに戻る
  179. *
  180. * @return void
  181. * @access public
  182. */
  183. function admin_back_agent() {
  184. if($this->Session->check('AuthAgent')) {
  185. $this->Session->write($this->BcAuth->sessionKey, $this->Session->read('AuthAgent.'.$this->BcAuth->userModel));
  186. $this->Session->delete('AuthAgent');
  187. $this->Session->setFlash('元のユーザーに戻りました。');
  188. } else {
  189. $this->Session->setFlash('不正な操作です。');
  190. }
  191. $this->redirect('/admin');
  192. }
  193. /**
  194. * [ADMIN] 管理者ログイン画面(Ajax)
  195. *
  196. * @return void
  197. * @access public
  198. */
  199. function admin_ajax_login() {
  200. if(!$this->BcAuth->login($this->data)) {
  201. $this->ajaxError(500, 'アカウント名、パスワードが間違っています。');
  202. }
  203. $user = $this->BcAuth->user();
  204. $userModel = $this->BcAuth->userModel;
  205. if($this->data) {
  206. if ($user) {
  207. if (!empty($this->data[$userModel]['saved'])) {
  208. if(Configure::read('BcRequest.agentAlias') != 'mobile') {
  209. $this->setAuthCookie($this->data);
  210. } else {
  211. $this->BcAuth->saveSerial();
  212. }
  213. unset($this->data[$userModel]['save']);
  214. }else {
  215. $this->Cookie->destroy();
  216. }
  217. $this->Session->setFlash("ようこそ、".$user[$userModel]['real_name_1']." ".$user[$userModel]['real_name_2']." さん。");
  218. }
  219. }
  220. Configure::write('debug', 0);
  221. if ($user) {
  222. exit(Router::url($this->BcAuth->redirect()));
  223. }
  224. exit();
  225. }
  226. /**
  227. * 認証クッキーをセットする
  228. *
  229. * @param array $data
  230. * @return void
  231. * @access public
  232. */
  233. function setAuthCookie($data) {
  234. $userModel = $this->BcAuth->userModel;
  235. $cookie = array();
  236. $cookie['name'] = $data[$userModel]['name'];
  237. $cookie['password'] = $data[$userModel]['password']; // ハッシュ化されている
  238. $this->Cookie->write('Auth.'.$userModel, $cookie, true, '+2 weeks'); // 3つめの'true'で暗号化
  239. }
  240. /**
  241. * [ADMIN] 管理者ログアウト
  242. *
  243. * @return void
  244. * @access public
  245. */
  246. function admin_logout() {
  247. $userModel = $this->BcAuth->userModel;
  248. $this->BcAuth->logout();
  249. $this->Cookie->del('Auth.'.$userModel);
  250. $this->Session->setFlash('ログアウトしました');
  251. $this->redirect(array($this->params['prefix'] => true, 'action' => 'login'));
  252. }
  253. /**
  254. * [ADMIN] ユーザーリスト
  255. *
  256. * @return void
  257. * @access public
  258. */
  259. function admin_index() {
  260. /* データ取得 */
  261. $default = array('named' => array('num' => $this->siteConfigs['admin_list_num']));
  262. $this->setViewConditions('User', array('default' => $default));
  263. $conditions = $this->_createAdminIndexConditions($this->data);
  264. $this->paginate = array(
  265. 'conditions'=>$conditions,
  266. 'fields'=>array(),
  267. 'order'=>'User.user_group_id,User.id',
  268. 'limit'=>$this->passedArgs['num']
  269. );
  270. $dbDatas = $this->paginate();
  271. if($dbDatas) {
  272. $this->set('users',$dbDatas);
  273. }
  274. if($this->RequestHandler->isAjax() || !empty($this->params['url']['ajax'])) {
  275. $this->render('ajax_index');
  276. return;
  277. }
  278. $this->subMenuElements = array('site_configs', 'users', 'user_groups');
  279. $this->pageTitle = 'ユーザー一覧';
  280. $this->search = 'users_index';
  281. $this->help = 'users_index';
  282. }
  283. /**
  284. * ページ一覧用の検索条件を生成する
  285. *
  286. * @param array $data
  287. * @return array $conditions
  288. * @access protected
  289. */
  290. function _createAdminIndexConditions($data) {
  291. unset($data['_Token']);
  292. if(isset($data['User']['user_group_id']) && $data['User']['user_group_id'] === '') {
  293. unset($data['User']['user_group_id']);
  294. }
  295. $conditions = $this->postConditions($data);
  296. if($conditions) {
  297. return $conditions;
  298. } else {
  299. return array();
  300. }
  301. }
  302. /**
  303. * [ADMIN] ユーザー情報登録
  304. *
  305. * @return void
  306. * @access public
  307. */
  308. function admin_add() {
  309. if(empty($this->data)) {
  310. $this->data = $this->User->getDefaultValue();
  311. }else {
  312. /* 登録処理 */
  313. $this->data['User']['password'] = $this->data['User']['password_1'];
  314. $this->User->create($this->data);
  315. if($this->User->validates()) {
  316. unset($this->data['User']['password_1']);
  317. unset($this->data['User']['password_2']);
  318. if(isset($this->data['User']['password'])) {
  319. $this->data['User']['password'] = $this->BcAuth->password($this->data['User']['password']);
  320. }
  321. $this->User->save($this->data,false);
  322. $this->Session->setFlash('ユーザー「'.$this->data['User']['name'].'」を追加しました。');
  323. $this->User->saveDbLog('ユーザー「'.$this->data['User']['name'].'」を追加しました。');
  324. $this->redirect(array('action' => 'edit', $this->User->getInsertID()));
  325. }else {
  326. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  327. }
  328. }
  329. /* 表示設定 */
  330. $userGroups = $this->User->getControlSource('user_group_id');
  331. $editable = true;
  332. $user = $this->BcAuth->user();
  333. $userModel = $this->getUserModel();
  334. if($user[$userModel]['user_group_id'] != 1) {
  335. unset($userGroups[1]);
  336. }
  337. $this->set('userGroups', $userGroups);
  338. $this->set('editable', true);
  339. $this->subMenuElements = array('site_configs', 'users', 'user_groups');
  340. $this->pageTitle = '新規ユーザー登録';
  341. $this->help = 'users_form';
  342. $this->render('form');
  343. }
  344. /**
  345. * [ADMIN] ユーザー情報編集
  346. *
  347. * @param int user_id
  348. * @return void
  349. * @access public
  350. */
  351. function admin_edit($id) {
  352. /* 除外処理 */
  353. if(!$id && empty($this->data)) {
  354. $this->Session->setFlash('無効なIDです。');
  355. $this->redirect(array('action' => 'index'));
  356. }
  357. $selfUpdate = false;
  358. $user = $this->BcAuth->user();
  359. $userModel = $this->getUserModel();
  360. if(empty($this->data)) {
  361. $this->data = $this->User->read(null, $id);
  362. if($user[$userModel]['id'] == $this->data['User']['id']) {
  363. $selfUpdate = true;
  364. }
  365. }else {
  366. if($user[$userModel]['id'] == $this->data['User']['id']) {
  367. $selfUpdate = true;
  368. }
  369. /* 更新処理 */
  370. // パスワードがない場合は更新しない
  371. if($this->data['User']['password_1'] || $this->data['User']['password_2']) {
  372. $this->data['User']['password'] = $this->data['User']['password_1'];
  373. }
  374. $this->User->set($this->data);
  375. if($this->User->validates()) {
  376. unset($this->data['User']['password_1']);
  377. unset($this->data['User']['password_2']);
  378. if(isset($this->data['User']['password'])) {
  379. $this->data['User']['password'] = $this->BcAuth->password($this->data['User']['password']);
  380. }
  381. $this->User->save($this->data,false);
  382. if($selfUpdate) {
  383. $this->admin_logout();
  384. }
  385. $this->Session->setFlash('ユーザー「'.$this->data['User']['name'].'」を更新しました。');
  386. $this->User->saveDbLog('ユーザー「'.$this->data['User']['name'].'」を更新しました。');
  387. $this->redirect(array('action' => 'edit', $id));
  388. }else {
  389. $this->Session->setFlash('入力エラーです。内容を修正してください。');
  390. }
  391. }
  392. /* 表示設定 */
  393. $userGroups = $this->User->getControlSource('user_group_id');
  394. $editable = true;
  395. $user = $this->BcAuth->user();
  396. $userModel = $this->getUserModel();
  397. if($user[$userModel]['user_group_id'] != 1 && Configure::read('debug') !== -1) {
  398. if($this->data['User']['user_group_id'] == 1) {
  399. $editable = false;
  400. } else {
  401. unset($userGroups[1]);
  402. }
  403. }
  404. $this->set('userGroups', $userGroups);
  405. $this->set('editable', $editable);
  406. $this->set('selfUpdate', $selfUpdate);
  407. $this->subMenuElements = array('site_configs', 'users', 'user_groups');
  408. $this->pageTitle = 'ユーザー情報編集';
  409. $this->help = 'users_form';
  410. $this->render('form');
  411. }
  412. /**
  413. * [ADMIN] ユーザー情報削除 (ajax)
  414. *
  415. * @param int user_id
  416. * @return void
  417. * @access public
  418. */
  419. function admin_ajax_delete($id = null) {
  420. /* 除外処理 */
  421. if(!$id) {
  422. $this->ajaxError(500, '無効な処理です。');
  423. }
  424. // 最後のユーザーの場合は削除はできない
  425. if($this->User->field('user_group_id',array('User.id'=>$id)) == 1 &&
  426. $this->User->find('count',array('conditions'=>array('User.user_group_id'=>1))) == 1) {
  427. $this->ajaxError(500, 'このユーザーは削除できません。');
  428. }
  429. // メッセージ用にデータを取得
  430. $user = $this->User->read(null, $id);
  431. /* 削除処理 */
  432. if($this->User->del($id)) {
  433. $this->User->saveDbLog('ユーザー「'.$user['User']['name'].'」を削除しました。');
  434. exit(true);
  435. }
  436. exit();
  437. }
  438. /**
  439. * [ADMIN] ユーザー情報削除
  440. *
  441. * @param int user_id
  442. * @return void
  443. * @access public
  444. */
  445. function admin_delete($id = null) {
  446. /* 除外処理 */
  447. if(!$id) {
  448. $this->Session->setFlash('無効なIDです。');
  449. $this->redirect(array('action' => 'index'));
  450. }
  451. // 最後のユーザーの場合は削除はできない
  452. if($this->User->field('user_group_id',array('User.id'=>$id)) == 1 &&
  453. $this->User->find('count',array('conditions'=>array('User.user_group_id'=>1))) == 1) {
  454. $this->Session->setFlash('最後の管理者ユーザーは削除する事はできません。');
  455. $this->redirect(array('action' => 'index'));
  456. }
  457. // メッセージ用にデータを取得
  458. $user = $this->User->read(null, $id);
  459. /* 削除処理 */
  460. if($this->User->del($id)) {
  461. $this->Session->setFlash('ユーザー: '.$user['User']['name'].' を削除しました。');
  462. $this->User->saveDbLog('ユーザー「'.$user['User']['name'].'」を削除しました。');
  463. }else {
  464. $this->Session->setFlash('データベース処理中にエラーが発生しました。');
  465. }
  466. $this->redirect(array('action' => 'index'));
  467. }
  468. /**
  469. * ログインパスワードをリセットする
  470. * 新しいパスワードを生成し、指定したメールアドレス宛に送信する
  471. *
  472. * @return void
  473. * @access public
  474. */
  475. function admin_reset_password () {
  476. $this->pageTitle = 'パスワードのリセット';
  477. $userModel = $this->BcAuth->userModel;
  478. if($this->data) {
  479. if(empty($this->data[$userModel]['email'])) {
  480. $this->Session->setFlash('メールアドレスを入力してください。');
  481. return;
  482. }
  483. $email = $this->data[$userModel]['email'];
  484. $user = $this->{$userModel}->findByEmail($email);
  485. if(!$user) {
  486. $this->Session->setFlash('送信されたメールアドレスは登録されていません。');
  487. return;
  488. }
  489. $password = $this->generatePassword();
  490. $user['User']['password'] = $this->BcAuth->password($password);
  491. $this->{$userModel}->set($user);
  492. if(!$this->{$userModel}->save()) {
  493. $this->Session->setFlash('新しいパスワードをデータベースに保存できませんでした。');
  494. return;
  495. }
  496. $body = $email.' の新しいパスワードは、 '.$password.' です。';
  497. if(!$this->sendMail($email, 'パスワードを変更しました', $body)) {
  498. $this->Session->setFlash('メール送信時にエラーが発生しました。');
  499. return;
  500. }
  501. $this->Session->setFlash($email.' 宛に新しいパスワードを送信しました。');
  502. $this->data = array();
  503. }
  504. }
  505. }
  506. ?>