-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserManagerAvatarController.php
More file actions
195 lines (171 loc) · 4.74 KB
/
UserManagerAvatarController.php
File metadata and controls
195 lines (171 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* UsersAvatarController
*
* @copyright Copyright 2014, NetCommons Project
* @author Kohei Teraguchi <kteraguchi@commonsnet.org>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('Controller', 'Controller');
App::uses('AuthComponent', 'Controller/Component');
App::uses('AppModel', 'Model');
/**
* UsersAvatarController
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\UserManager\Controller
*/
class UserManagerAvatarController extends Controller {
/**
* use component
*
* @var array
*/
public $components = array(
'NetCommons.NetCommons',
'Files.Download',
);
/**
* beforeRender
*
* @return void
*/
public function beforeRender() {
// WysiwygImageControllerDownloadTest::testDownloadGet 用の処理
// @see https://github.com/NetCommons3/NetCommons/blob/3.1.2/Controller/NetCommonsAppController.php#L241
// @see https://github.com/NetCommons3/NetCommons/blob/3.1.2/Controller/Component/NetCommonsComponent.php#L58
App::uses('NetCommonsAppController', 'NetCommons.Controller');
$this->NetCommons->renderJson();
}
/**
* download method
*
* @return void
* @throws NotFoundException
*/
public function download() {
/* @var $User AppModel */
/* @var $UserAttributeSetting AppModel */
// シンプルにしたかったためAppModelを利用。インスタンス生成時少し速かった。
$User = $this->_getSimpleModel('User');
$User->Behaviors->load('Users.Avatar');
// @see https://github.com/NetCommons3/Users/blob/3.1.2/Model/Behavior/AvatarBehavior.php#L42
$User->plugin = 'Users';
ClassRegistry::removeObject('User');
ClassRegistry::removeObject('AvatarBehavior');
$params = $this->_getBindParamsForUser();
$User->bindModel($params);
$query = $this->_getQueryForUser();
$user = $User->find('first', $query);
ClassRegistry::removeObject('UploadFile');
if (!$user ||
!$user['UploadFile']['id']) {
return $this->_downloadNoImage($User, $user);
}
//会員管理が使えない場合、NoImageを出力する
$PluginsRole = $this->_getSimpleModel('PluginsRole');
$query = $this->_getQueryForPluginsRole();
if (! $PluginsRole->find('count', $query)) {
return $this->_downloadNoImage($User, $user);
}
$options = [
'size' => $this->params['size'],
];
return $this->Download->doDownloadByUploadFileId($user['UploadFile']['id'], $options);
}
/**
* download method
*
* @param Model $User User model(AppModel)
* @param array $user User data
* @return void
*/
protected function _downloadNoImage($User, $user) {
$fieldName = $this->request->params['field_name'];
$fieldSize = $this->request->params['size'];
// @see https://github.com/NetCommons3/Users/blob/3.1.2/Model/Behavior/AvatarBehavior.php#L123-L125
App::uses('User', 'Users.Model');
$this->response->file(
$User->temporaryAvatar($user, $fieldName, $fieldSize),
array('name' => 'No Image')
);
return $this->response;
}
/**
* download method
*
* @param string $modelName Model name
* @return void
*/
protected function _getSimpleModel($modelName) {
// TestでAvatarBehavior::temporaryAvatar をMock にしているため、removeObjectしない。
// ClassRegistry::removeObject($modelName);
$Model = ClassRegistry::init($modelName);
$params = [
'belongsTo' => [
'TrackableCreator',
'TrackableUpdater',
]
];
$Model->unbindModel($params);
$Model->Behaviors->unload('Trackable');
return $Model;
}
/**
* get bind params for User
*
* @return void
*/
protected function _getBindParamsForUser() {
$params = [
'hasOne' => [
'UploadFile' => [
'className' => 'UploadFile',
'foreignKey' => false,
'conditions' => [
'UploadFile.plugin_key' => $this->plugin,
'UploadFile.content_key = User.id',
'UploadFile.field_name' => $this->request->params['field_name'],
],
'fields' => ['id']
]
],
];
return $params;
}
/**
* get query for User
*
* @return void
*/
protected function _getQueryForUser() {
$query = [
'conditions' => [
'User.id' => $this->request->params['user_id'],
//@see https://github.com/NetCommons3/Users/blob/3.1.2/Controller/UsersController.php#L105-L111
//@see https://github.com/NetCommons3/Users/blob/3.1.2/Model/Behavior/UserPermissionBehavior.php#L31-L33
'User.is_deleted' => '0',
],
'recursive' => 0,
'callbacks' => false,
];
return $query;
}
/**
* get query for PluginsRole
*
* @return void
*/
protected function _getQueryForPluginsRole() {
$query = [
'conditions' => [
'PluginsRole.role_key' => AuthComponent::user('role_key'),
'PluginsRole.plugin_key' => $this->plugin,
],
'recursive' => -1,
'callbacks' => false,
];
return $query;
}
}