Index: branches/5.2.x/core/units/users/users_event_handler.php =================================================================== diff -u -N -r14447 -r14468 --- branches/5.2.x/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 14447) +++ branches/5.2.x/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 14468) @@ -1,6 +1,6 @@ Array('self' => true), 'OnUpdatePassword' => Array('self' => true), 'OnSaveSelected' => Array ('self' => 'view'), + 'OnGeneratePassword' => Array ('self' => 'view'), // front 'OnRefreshForm' => Array('self' => true), @@ -297,7 +298,7 @@ */ function OnAfterItemCreate(&$event) { - $this->saveUserImages($event); + $this->afterItemChanged($event); $object =& $event->getObject(); /* @var $object kDBItem */ @@ -1285,7 +1286,7 @@ */ function OnAfterItemUpdate(&$event) { - $this->saveUserImages($event); + $this->afterItemChanged($event); $object =& $event->getObject(); /* @var $object UsersItem */ @@ -1298,6 +1299,27 @@ } /** + * Occurs, after item is changed + * + * @param kEvent $event + */ + function afterItemChanged(&$event) + { + $this->saveUserImages($event); + + $object =& $event->getObject(); + /* @var $object kDBItem */ + + if ( $object->GetDBField('EmailPassword') && $object->GetDBField('Password_plain') ) { + $email_passwords = $this->Application->RecallVar('email_passwords'); + $email_passwords = $email_passwords ? unserialize($email_passwords) : Array (); + + $email_passwords[ $object->GetID() ] = $object->GetDBField('Password_plain'); + $this->Application->StoreVar('email_passwords', serialize($email_passwords)); + } + } + + /** * Stores user's original Status before overwriting with data from temp table * * @param kEvent $event @@ -1324,15 +1346,34 @@ function OnAfterCopyToLive(&$event) { $temp_id = $event->getEventParam('temp_id'); - if ($temp_id == 0) { - // this is new user create, don't send email events - return ; + $email_passwords = $this->Application->RecallVar('email_passwords'); + + if ( $email_passwords ) { + $email_passwords = unserialize($email_passwords); + + if ( isset($email_passwords[$temp_id]) ) { + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $object->SwitchToLive(); + $object->Load( $event->getEventParam('id') ); + $object->SetField('Password', $email_passwords[$temp_id]); + $object->SetField('VerifyPassword', $email_passwords[$temp_id]); + + $this->Application->EmailEventUser($temp_id > 0 ? 'USER.NEW.PASSWORD': 'USER.ADD.BYADMIN', $object->GetID()); + + unset($email_passwords[$temp_id]); + $this->Application->StoreVar('email_passwords', serialize($email_passwords)); + } } - $new_status = $this->getUserStatus($temp_id); - $user_status = $this->Application->GetVar('user_status'); + if ( $temp_id > 0 ) { + // only send status change e-mail on user update + $new_status = $this->getUserStatus($temp_id); + $user_status = $this->Application->GetVar('user_status'); - $this->sendStatusChangeEvent($temp_id, $user_status[$temp_id], $new_status); + $this->sendStatusChangeEvent($temp_id, $user_status[$temp_id], $new_status); + } } /** @@ -1601,22 +1642,28 @@ { parent::OnPreCreate($event); - if ($event->status == kEvent::erSUCCESS) { - $user_type = $this->Application->GetVar('user_type'); + if ($event->status != kEvent::erSUCCESS) { + return ; + } - if ($user_type) { - $object =& $event->getObject(); - /* @var $object kDBItem */ + $object =& $event->getObject(); + /* @var $object kDBItem */ - $object->SetDBField('UserType', $user_type); + $user_type = $this->Application->GetVar('user_type'); - if ( $user_type == UserType::ADMIN ) { - $object->SetDBField('PrimaryGroupId', $this->Application->ConfigValue('User_AdminGroup')); - } + if ($user_type) { + $object->SetDBField('UserType', $user_type); + + if ( $user_type == UserType::ADMIN ) { + $object->SetDBField('PrimaryGroupId', $this->Application->ConfigValue('User_AdminGroup')); } + } - $this->_makePasswordRequired($event); + if ( $this->Application->ConfigValue('User_Password_Auto') ) { + $object->SetDBField('EmailPassword', 1); } + + $this->_makePasswordRequired($event); } /** @@ -1670,4 +1717,18 @@ parent::LoadItem($event); } + + /** + * Generates password + * + * @param kEvent $event + */ + function OnGeneratePassword(&$event) + { + $event->status = kEvent::erSTOP; + + if ( $this->Application->isAdminUser ) { + echo kUtil::generatePassword(); + } + } }