@@ -43,6 +43,11 @@ class User extends UFModel {
4343 */
4444 protected $ _primary_group ;
4545
46+ /**
47+ * @var UserEvent[] An array of events to be inserted for this User when save is called.
48+ */
49+ protected $ new_events = [];
50+
4651 /**
4752 * @var bool Enable timestamps for Users.
4853 */
@@ -149,6 +154,7 @@ public function newCollection(array $models = Array()) {
149154
150155 /**
151156 * Get all events for this user.
157+ * @todo save events in $new_events as well?
152158 */
153159 public function events (){
154160 return $ this ->hasMany ('UserFrosting\UserEvent ' );
@@ -329,20 +335,6 @@ private function fetchPrimaryGroup() {
329335 }
330336 return $ this ->belongsTo ('UserFrosting\Group ' , 'primary_group_id ' )->getEager ()->first ();
331337 }
332-
333- /**
334- * Store the User to the database, along with any group associations, updating as necessary.
335- *
336- */
337- public function save (array $ options = []){
338- // Update the user record itself
339- $ result = parent ::save ($ options );
340-
341- // Synchronize model's group relations with database
342- $ this ->syncCachedGroups ();
343-
344- return $ result ;
345- }
346338
347339 /**
348340 * Create an event saying that this user registered their account, or an account was created for them.
@@ -356,10 +348,10 @@ public function newEventSignUp($creator = null){
356348 else
357349 $ description = "User {$ this ->user_name } successfully registered on " . date ("Y-m-d H:i:s " ) . ". " ;
358350 $ event = new UserEvent ([
359- "user_id " => $ this ->id ,
360351 "event_type " => "sign_up " ,
361352 "description " => $ description
362353 ]);
354+ $ this ->new_events [] = $ event ;
363355 return $ event ;
364356 }
365357
@@ -369,11 +361,12 @@ public function newEventSignUp($creator = null){
369361 * @return UserEvent
370362 */
371363 public function newEventSignIn (){
372- return new UserEvent ([
373- "user_id " => $ this ->id ,
364+ $ event = new UserEvent ([
374365 "event_type " => "sign_in " ,
375366 "description " => "User {$ this ->user_name } signed in at " . date ("Y-m-d H:i:s " ) . ". "
376367 ]);
368+ $ this ->new_events [] = $ event ;
369+ return $ event ;
377370 }
378371
379372 /**
@@ -384,10 +377,10 @@ public function newEventSignIn(){
384377 public function newEventVerificationRequest (){
385378 $ this ->secret_token = User::generateActivationToken ();
386379 $ event = new UserEvent ([
387- "user_id " => $ this ->id ,
388380 "event_type " => "verification_request " ,
389381 "description " => "User {$ this ->user_name } requested verification on " . date ("Y-m-d H:i:s " ) . ". "
390382 ]);
383+ $ this ->new_events [] = $ event ;
391384 return $ event ;
392385 }
393386
@@ -400,13 +393,32 @@ public function newEventPasswordReset(){
400393 $ this ->secret_token = User::generateActivationToken ();
401394 $ this ->flag_password_reset = "1 " ;
402395 $ event = new UserEvent ([
403- "user_id " => $ this ->id ,
404396 "event_type " => "password_reset_request " ,
405397 "description " => "User {$ this ->user_name } requested a password reset on " . date ("Y-m-d H:i:s " ) . ". "
406398 ]);
399+ $ this ->new_events [] = $ event ;
407400 return $ event ;
408401 }
409402
403+ /**
404+ * Store the User to the database, along with any group associations and new events, updating as necessary.
405+ *
406+ */
407+ public function save (array $ options = []){
408+ // Update the user record itself
409+ $ result = parent ::save ($ options );
410+
411+ // Synchronize model's group relations with database
412+ $ this ->syncCachedGroups ();
413+
414+ // Save any new events for this user
415+ foreach ($ this ->new_events as $ event ){
416+ $ this ->events ()->save ($ event );
417+ }
418+
419+ return $ result ;
420+ }
421+
410422 /**
411423 * Delete this user from the database, along with any linked groups and authorization rules
412424 *
@@ -513,8 +525,7 @@ public function verifyPassword($password){
513525 */
514526 public function login (){
515527 // Add a sign in event (time is automatically set by database)
516- $ event = $ this ->newEventSignIn ();
517- $ event ->save ();
528+ $ this ->newEventSignIn ();
518529
519530 // Update password if we had encountered an outdated hash
520531 if (Authentication::getPasswordHashType ($ this ->password ) != "modern " ){
@@ -528,12 +539,18 @@ public function login(){
528539 }
529540 }
530541
531- // Store changes
532- $ this ->store ();
542+ // Save changes
543+ $ this ->save ();
533544
534545 return $ this ;
535546 }
536547
548+ /**
549+ * Log this user out.
550+ *
551+ * Destroys the PHP session as well.
552+ * @param bool $complete If set to true, will also clear out any persistent sessions.
553+ */
537554 public function logout ($ complete = false ) {
538555 if ($ complete ){
539556 $ storage = new \Birke \Rememberme \Storage \PDO (static ::$ app ->remember_me_table );
0 commit comments