@@ -69,6 +69,17 @@ public function beforeFilter()
69
69
// set default language for now
70
70
Configure::write ('Config.language ' , 'eng ' );
71
71
72
+ // if we have a session transfered to us
73
+ if ($ this ->_hasSessionTransferData ()) {
74
+ if ($ this ->_authenticateWithSessionTransferData ()) {
75
+ if (method_exists ($ this , '_afterLogin ' )) {
76
+ $ this ->_afterLogin (false );
77
+ }
78
+ } else {
79
+ $ this ->Session ->setFlash ($ this ->Auth ->loginError , $ this ->Auth ->flashElement , array (), 'auth ' );
80
+ }
81
+ }
82
+
72
83
// store user in the singleton for global access
73
84
User::store ($ this ->Auth ->user ());
74
85
@@ -144,58 +155,55 @@ public function _extractModel($model,$array,$field)
144
155
*/
145
156
protected function _sendEmail ($ content , $ subject , $ from , $ toAddress , $ templateName = 'default ' , $ ccAddress = array (), $ bcc = array ())
146
157
{
147
- $ this ->SysParameter ->reload ();
148
- $ smtp ['port ' ] = $ this ->SysParameter ->get ('email.port ' );
149
- $ smtp ['host ' ] = $ this ->SysParameter ->get ('email.host ' );
150
- $ smtp ['username ' ] = $ this ->SysParameter ->get ('email.username ' );
151
- $ smtp ['password ' ] = $ this ->SysParameter ->get ('email.password ' );
152
- $ smtp ['timeout ' ] = 30 ;
153
158
$ this ->Email ->reset ();
154
159
155
- $ this ->Email ->smtpOptions = $ smtp ;
156
- $ this ->Email ->delivery = 'smtp ' ;
160
+ $ smtpHost = $ this ->SysParameter ->get ('email.host ' );
161
+ if (!empty ($ smtpHost )) {
162
+ $ smtp ['port ' ] = $ this ->SysParameter ->get ('email.port ' );
163
+ $ smtp ['host ' ] = $ this ->SysParameter ->get ('email.host ' );
164
+ $ smtp ['username ' ] = $ this ->SysParameter ->get ('email.username ' );
165
+ $ smtp ['password ' ] = $ this ->SysParameter ->get ('email.password ' );
166
+ $ smtp ['timeout ' ] = 30 ;
167
+ $ this ->Email ->delivery = 'smtp ' ;
168
+ $ this ->Email ->smtpOptions = $ smtp ;
169
+ } else {
170
+ $ this ->Email ->delivery = 'mail ' ;
171
+ }
172
+
157
173
$ this ->Email ->to = $ toAddress ;
158
174
$ this ->Email ->cc = $ ccAddress ;
159
175
$ this ->Email ->bcc = $ bcc ;
160
176
$ this ->Email ->subject = $ subject ;
161
- $ this ->Email ->from = $ from ;
177
+ $ this ->Email ->from = ( $ from == null ? $ this -> SysParameter -> get ( ' display.contact_info ' ) : $ from ) ;
162
178
$ this ->Email ->template = $ templateName ;
163
179
$ this ->Email ->sendAs = 'both ' ;
180
+ //$this->Email->delivery = 'debug';
164
181
165
182
return $ this ->Email ->send ($ content );
166
183
}
167
184
168
185
/**
169
- * beforeLogin callback, called every time in auth compoment
186
+ * beforeLogin callback, called every time in auth compoment if user is not
187
+ * logged in yet
170
188
*
171
189
* @access public
172
190
* @return void
173
191
*/
174
192
public function _beforeLogin ()
175
193
{
176
- // if we have a session transfered to us
177
- if ($ this ->_hasSessionTransferData ()) {
178
- if ($ this ->_authenticateWithSessionTransferData ()) {
179
- if (method_exists ($ this , '_afterLogin ' )) {
180
- $ this ->_afterLogin ();
181
- }
182
- return true ;
183
- } else {
184
- $ this ->Session ->setFlash ($ this ->Auth ->loginError , $ this ->Auth ->flashElement , array (), 'auth ' );
185
- return false ;
186
- }
187
- }
188
194
$ this ->set ('loginHeader ' , $ this ->SysParameter ->get ('display.login.header ' ));
189
195
$ this ->set ('loginFooter ' , $ this ->SysParameter ->get ('display.login.footer ' ));
190
196
}
191
197
192
198
/**
193
199
* afterLogin callback, called when logging in successfully
194
200
*
201
+ * @param bool $isRedirect whether redirecting
202
+ *
195
203
* @access public
196
204
* @return void
197
205
*/
198
- public function _afterLogin ()
206
+ public function _afterLogin ($ isRedirect = true )
199
207
{
200
208
if ($ this ->Auth ->isAuthorized ()) {
201
209
User::getInstance ($ this ->Auth ->user ());
@@ -205,11 +213,16 @@ public function _afterLogin()
205
213
//TODO logging!
206
214
}
207
215
216
+ if (!$ isRedirect ) {
217
+ return ;
218
+ }
219
+
208
220
$ redirect = $ this ->Auth ->redirect ();
209
221
if (isset ($ this ->params ['url ' ]['redirect ' ])) {
210
222
$ redirect = $ this ->params ['url ' ]['redirect ' ];
211
223
}
212
224
225
+ $ this ->log ('redirecting to ' .$ redirect , 'debug ' );
213
226
$ this ->redirect ($ redirect );
214
227
}
215
228
@@ -251,16 +264,25 @@ function _hasSessionTransferData()
251
264
*/
252
265
function _authenticateWithSessionTransferData ()
253
266
{
267
+ // valid signature first
254
268
$ message = $ this ->sessionTransferData ['username ' ].$ this ->sessionTransferData ['timestamp ' ].$ this ->sessionTransferData ['token ' ];
255
269
$ secret = $ this ->OauthToken ->getTokenSecret ($ this ->sessionTransferData ['token ' ]);
256
270
$ signature = base64_encode (hash_hmac ('sha1 ' , $ message , $ secret , true ));
257
- if ($ signature = = $ this ->sessionTransferData ['signature ' ]) {
258
- $ user = $ this ->User -> findByUsername ( $ this ->sessionTransferData ['username ' ]);
259
- $ this -> Session -> write ( $ this -> Auth -> sessionKey , $ user [ ' User ' ]);
260
- return true ;
271
+ if ($ signature ! = $ this ->sessionTransferData ['signature ' ]) {
272
+ $ this ->log ( ' Invalid signature! Expect ' . $ signature . ' , Got ' . $ this ->sessionTransferData ['signature ' ]);
273
+
274
+ return false ;
261
275
}
262
276
263
- $ this ->log ('Invalid signature! Expect ' .$ signature .', Got ' .$ this ->sessionTransferData ['signature ' ]);
264
- return false ;
277
+ // find the userId by username and use it to login
278
+ $ userId = $ this ->User ->field ('id ' , array ('username ' => $ this ->sessionTransferData ['username ' ]));
279
+ if (!$ this ->Auth ->login ($ userId )) {
280
+ $ this ->log ('Invalid username ' .$ this ->sessionTransferData ['username ' ].' from session transfer. ' , 'debug ' );
281
+ return false ;
282
+ }
283
+
284
+ $ this ->log ('User ' .$ this ->sessionTransferData ['username ' ].' is logged in with session transfer. ' , 'debug ' );
285
+
286
+ return true ;
265
287
}
266
288
}
0 commit comments