PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/baser/models/user.php

https://github.com/hashing/basercms
PHP | 322 lines | 181 code | 20 blank | 121 comment | 14 complexity | 4792e107b40c52e29b7a7aeaa032df8f 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.models
  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. * Include files
  22. */
  23. /**
  24. * ユーザーモデル
  25. *
  26. * @package baser.models
  27. */
  28. class User extends AppModel {
  29. /**
  30. * クラス名
  31. *
  32. * @var string
  33. * @access public
  34. */
  35. var $name = 'User';
  36. /**
  37. * ビヘイビア
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. var $actsAs = array('BcCache');
  43. /**
  44. * データベース接続
  45. *
  46. * @var string
  47. * @access public
  48. */
  49. var $useDbConfig = 'baser';
  50. /**
  51. * belongsTo
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. var $belongsTo = array('UserGroup' => array( 'className'=>'UserGroup',
  57. 'foreignKey'=>'user_group_id'));
  58. /**
  59. * hasMany
  60. *
  61. * @var array
  62. * @access public
  63. */
  64. var $hasMany = array('Favorite' => array(
  65. 'className' => 'Favorite',
  66. 'order' => 'Favorite.sort',
  67. 'foreignKey' => 'user_id',
  68. 'dependent' => false,
  69. 'exclusive' => false,
  70. 'finderQuery' => ''
  71. ));
  72. /**
  73. * validate
  74. *
  75. * @var array
  76. * @access public
  77. */
  78. var $validate = array(
  79. 'name'=>array(
  80. 'notEmpty' => array(
  81. 'rule' => array('notEmpty'),
  82. 'message' => 'アカウント名を入力してください。'
  83. ),
  84. 'alphaNumericPlus' => array(
  85. 'rule' => 'alphaNumericPlus',
  86. 'message' => 'アカウント名は半角英数字とハイフン、アンダースコアのみで入力してください。'
  87. ),
  88. 'duplicate' => array(
  89. 'rule' => array('duplicate','name'),
  90. 'message' => '既に登録のあるアカウント名です。'
  91. ),
  92. 'maxLength' => array(
  93. 'rule' => array('maxLength', 255),
  94. 'message' => 'アカウント名は255文字以内で入力してください。'
  95. )
  96. ),
  97. 'real_name_1' => array(
  98. 'notEmpty' => array(
  99. 'rule' => array('notEmpty'),
  100. 'message' => '名前[姓]を入力してください。'),
  101. 'maxLength' => array(
  102. 'rule' => array('maxLength', 50),
  103. 'message' => 'アカウント名は50文字以内で入力してください。'
  104. )
  105. ),
  106. 'real_name_2' => array(
  107. 'maxLength' => array(
  108. 'rule' => array('maxLength', 50),
  109. 'message' => '名前[名]は50文字以内で入力してください。'
  110. )
  111. ),
  112. 'password' => array(
  113. 'minLength' => array(
  114. 'rule' => array('minLength',6),
  115. 'allowEmpty'=> false,
  116. 'message' => 'パスワードは6文字以上で入力してください。'
  117. ),
  118. 'maxLength' => array(
  119. 'rule' => array('maxLength', 255),
  120. 'message' => 'パスワードは255文字以内で入力してください。'
  121. ),
  122. 'alphaNumeric' => array(
  123. 'rule' => 'alphaNumericPlus',
  124. 'message' => 'パスワードは半角英数字とハイフン、アンダースコアのみで入力してください。'
  125. ),
  126. 'confirm' => array(
  127. 'rule' => array('confirm', array('password_1', 'password_2')),
  128. 'message' => 'パスワードが同じものではありません。'
  129. )
  130. ),
  131. 'email' => array(
  132. 'email' => array(
  133. 'rule' => array('email'),
  134. 'message' => 'Eメールの形式が不正です。',
  135. 'allowEmpty'=> true),
  136. 'maxLength' => array(
  137. 'rule' => array('maxLength', 255),
  138. 'message' => 'Eメールは255文字以内で入力してください。')
  139. ),
  140. 'user_group_id'=>array(
  141. 'rule' => array('notEmpty'),
  142. 'message' => 'グループを選択してください。'
  143. )
  144. );
  145. /**
  146. * validates
  147. *
  148. * @param string $options An optional array of custom options to be made available in the beforeValidate callback
  149. * @return boolean True if there are no errors
  150. * @access public
  151. */
  152. function validates($options = array()) {
  153. $result = parent::validates($options);
  154. if(isset($this->validationErrors['password'])) {
  155. $this->invalidate('password_1');
  156. $this->invalidate('password_2');
  157. }
  158. return $result;
  159. }
  160. /**
  161. * コントロールソースを取得する
  162. *
  163. * @param string フィールド名
  164. * @return array コントロールソース
  165. * @access public
  166. */
  167. function getControlSource($field) {
  168. switch($field) {
  169. case 'user_group_id':
  170. $controlSources['user_group_id'] = $this->UserGroup->find('list');
  171. break;
  172. }
  173. if(isset($controlSources[$field])) {
  174. return $controlSources[$field];
  175. }else {
  176. return false;
  177. }
  178. }
  179. /**
  180. * ユーザーリストを取得する
  181. * 条件を指定する場合は引数を指定する
  182. *
  183. * @param array $authUser
  184. * @return array
  185. * @access public
  186. */
  187. function getUserList($conditions = array()) {
  188. $users = $this->find("all",array('fields'=>array('id','real_name_1','real_name_2'), 'conditions'=>$conditions));
  189. $list = array();
  190. if ($users) {
  191. // 苗字が同じ場合にわかりにくいので、foreachで生成
  192. //$this->set('users',Set::combine($users, '{n}.User.id', '{n}.User.real_name_1'));
  193. foreach($users as $key => $user) {
  194. if($user[$this->alias]['real_name_2']) {
  195. $name = $user[$this->alias]['real_name_1']." ".$user[$this->alias]['real_name_2'];
  196. }else {
  197. $name = $user[$this->alias]['real_name_1'];
  198. }
  199. $list[$user[$this->alias]['id']] = $name;
  200. }
  201. }
  202. return $list;
  203. }
  204. /**
  205. * フォームの初期値を設定する
  206. *
  207. * @return array 初期値データ
  208. * @access public
  209. */
  210. function getDefaultValue() {
  211. $data['User']['user_group_id'] = 1;
  212. return $data;
  213. }
  214. /**
  215. * afterFind
  216. *
  217. * @param array 結果セット
  218. * @param array $primary
  219. * @return array 結果セット
  220. * @access public
  221. */
  222. function afterFind($results, $primary = false) {
  223. if(isset($results[0]['User'][0])) {
  224. $results[0]['User'] = $this->convertResults($results[0]['User']);
  225. }else {
  226. $results = $this->convertResults($results);
  227. }
  228. return parent::afterFind($results,$primary);
  229. }
  230. /**
  231. * 取得結果を変換する
  232. * HABTM対応
  233. *
  234. * @param array 結果セット
  235. * @return array 結果セット
  236. * @access public
  237. */
  238. function convertResults($results) {
  239. if($results) {
  240. if(isset($result['User'])||isset($results[0]['User'])) {
  241. foreach($results as $key => $result) {
  242. if(isset($result['User'])) {
  243. if($result['User']) {
  244. $results[$key]['User'] = $this->convertToView($result['User']);
  245. }
  246. }elseif(!empty($result)) {
  247. $results[$key] = $this->convertToView($result);
  248. }
  249. }
  250. }else {
  251. $results = $this->convertToView($results);
  252. }
  253. }
  254. return $results;
  255. }
  256. /**
  257. * View用のデータを取得する
  258. *
  259. * @param array 結果セット
  260. * @return array 結果セット
  261. * @access public
  262. */
  263. function convertToView($data) {
  264. return $data;
  265. }
  266. /**
  267. * ユーザーが許可されている認証プレフィックスを取得する
  268. *
  269. * @param string $userName
  270. * @return string
  271. */
  272. function getAuthPrefix($userName) {
  273. $user = $this->find('first', array(
  274. 'conditions' => array('User.name'=>$userName),
  275. 'recursive' => 1
  276. ));
  277. if(isset($user['UserGroup']['auth_prefix'])) {
  278. return $user['UserGroup']['auth_prefix'];
  279. } else {
  280. return '';
  281. }
  282. }
  283. function afterSave($created) {
  284. parent::afterSave($created);
  285. if($created) {
  286. $defaultFavorites = $this->UserGroup->field('default_favorites', array('UserGroup.id' => $this->data['User']['user_group_id']));
  287. if($defaultFavorites) {
  288. $defaultFavorites = unserialize($defaultFavorites);
  289. if($defaultFavorites) {
  290. $userId = $this->getLastInsertID();
  291. foreach($defaultFavorites as $favorites) {
  292. $favorites['user_id'] = $userId;
  293. $favorites['sort'] = $this->Favorite->getMax('sort')+1;
  294. $this->Favorite->create($favorites);
  295. $this->Favorite->save();
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. ?>