Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Model/Behavior/SaveUserBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

App::uses('ModelBehavior', 'Model');
App::uses('CurrentSystem', 'NetCommons.Utility');

/**
* SaveUser Behavior
Expand Down Expand Up @@ -220,6 +221,90 @@ private function __setValidates(Model $model, $userAttribute) {
}
}

/**
* beforeSave is called before a model is saved. Returning false from a beforeSave callback
* will abort the save operation.
*
* @param Model $model Model using this behavior
* @param array $options Options passed from Model::save().
* @return mixed False if the operation should abort. Any other result will continue.
* @see Model::save()
*/
public function beforeSave(Model $model, $options = array()) {
//インストール時は、言語のCurrentデータをセットする
if (! Configure::read('NetCommons.installed')) {
(new CurrentSystem())->setLanguage();
}

return true;
}

/**
* afterSave is called after a model is saved.
*
* @param Model $model Model using this behavior
* @param bool $created True if this save created a new record
* @param array $options Options passed from Model::save().
* @return bool
* @see Model::save()
* @throws InternalErrorException
*/
public function afterSave(Model $model, $created, $options = array()) {
//UsersLanguage登録
$usersLanguages = Hash::get($model->data, 'UsersLanguage', array());
if ($created) {
$usersLanguages = Hash::insert($usersLanguages, '{n}.user_id', $model->data['User']['id']);
}
foreach ($usersLanguages as $index => $usersLanguage) {
if (! $ret = $model->UsersLanguage->save($usersLanguage, false, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$model->data['UsersLanguage'][$index] = Hash::extract($ret, 'UsersLanguage');
}

if ($created) {
//プライベートルームの登録
$model->loadModels([
'PrivateSpace' => 'PrivateSpace.PrivateSpace',
'Room' => 'Rooms.Room',
]);
$room = $model->PrivateSpace->createRoom();
$room['RolesRoomsUser']['user_id'] = $model->data['User']['id'];
$room = $model->Room->saveRoom($room);
if (! $room) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

//プライベートルームのデフォルトでプラグイン設置
$result = $model->PrivateSpace->saveDefaultFrames($room);
if (! $result) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

//デフォルト参加ルームの登録
$rooms = $model->Room->find('all', array(
'recursive' => -1,
'conditions' => array(
'OR' => array(
'default_participation' => true,
'root_id' => null
)
),
));

if (! Configure::read('NetCommons.installed')) {
$rooms = Hash::insert(
$rooms, '{n}.Room.default_role_key', Role::ROOM_ROLE_KEY_ROOM_ADMINISTRATOR
);
}
foreach ($rooms as $room) {
$room['RolesRoomsUser']['user_id'] = $model->data['User']['id'];
$model->Room->saveDefaultRolesRoomsUser($room, false);
}
}
return true;
}

/**
* ユーザの登録処理
*
Expand Down
62 changes: 0 additions & 62 deletions Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\Users\Model
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class User extends UsersAppModel {

Expand Down Expand Up @@ -376,66 +374,6 @@ public function afterValidate() {
}
}

/**
* Called after each successful save operation.
*
* @param bool $created True if this save created a new record
* @param array $options Options passed from Model::save().
* @return void
* @link http://book.cakephp.org/2.0/en/models/callback-methods.html#aftersave
* @see Model::save()
* @throws InternalErrorException
*/
public function afterSave($created, $options = array()) {
//UsersLanguage登録
$usersLanguages = Hash::get($this->data, 'UsersLanguage', array());
if ($created) {
$usersLanguages = Hash::insert($usersLanguages, '{n}.user_id', $this->data['User']['id']);
}
foreach ($usersLanguages as $index => $usersLanguage) {
if (! $ret = $this->UsersLanguage->save($usersLanguage, false, false)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}
$this->data['UsersLanguage'][$index] = Hash::extract($ret, 'UsersLanguage');
}

//インストール時は、言語のCurrentデータをセットする
if (! Configure::read('NetCommons.installed')) {
(new CurrentSystem())->setLanguage();
}

if ($created) {
//プライベートルームの登録
$this->loadModels([
'PrivateSpace' => 'PrivateSpace.PrivateSpace',
'Room' => 'Rooms.Room',
]);
$room = $this->PrivateSpace->createRoom();
$room['RolesRoomsUser']['user_id'] = $this->data['User']['id'];
if (! $this->Room->saveRoom($room)) {
throw new InternalErrorException(__d('net_commons', 'Internal Server Error'));
}

//デフォルト参加ルームの登録
$rooms = $this->Room->find('all', array(
'recursive' => -1,
'conditions' => array(
'OR' => array(
'default_participation' => true,
'root_id' => null
)
),
));
foreach ($rooms as $room) {
$room['RolesRoomsUser']['user_id'] = $this->data['User']['id'];
if (! Configure::read('NetCommons.installed')) {
$room['Room']['default_role_key'] = Role::ROOM_ROLE_KEY_ROOM_ADMINISTRATOR;
}
$this->Room->saveDefaultRolesRoomsUser($room, false);
}
}
}

/**
* Userの生成
*
Expand Down