Index: branches/5.2.x/core/units/users/users_event_handler.php =================================================================== diff -u -N -r14434 -r14437 --- branches/5.2.x/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 14434) +++ branches/5.2.x/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 14437) @@ -1,6 +1,6 @@ Array('self' => 'view'), // because setting to logged in user only 'OnUpdateRootPassword' => Array('self' => true), - 'OnUpdatePassword' => Array('self' => true), + 'OnUpdatePassword' => Array('self' => true), + 'OnSaveSelected' => Array ('self' => 'view'), // front 'OnRefreshForm' => Array('self' => true), @@ -59,12 +60,12 @@ $object =& $event->getObject(); /* @var $object kDBList */ - if ($event->Special == 'admins') { - $object->addFilter('primary_filter', 'ug.GroupId = 11'); + if ($event->Special == 'regular') { + $object->addFilter('primary_filter', '%1$s.UserType = ' . UserType::USER); } - if ($event->Special == 'regular') { - $object->addFilter('primary_filter', 'ug.GroupId <> 11 OR ug.GroupId IS NULL'); + if ($event->Special == 'admins') { + $object->addFilter('primary_filter', '%1$s.UserType = ' . UserType::ADMIN); } if (!$this->Application->isAdminUser) { @@ -77,16 +78,16 @@ if ($event->Special == 'group') { $group_id = $this->Application->GetVar('g_id'); + if ($group_id !== false) { // show only users, that user doesn't belong to current group - $table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:g'); $sql = 'SELECT PortalUserId - FROM ' . $table_name . ' + FROM ' . $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:g') . ' WHERE GroupId = ' . (int)$group_id; $user_ids = $this->Conn->GetCol($sql); -// array_push($user_ids); // Guest & Everyone groups are set dynamically + if ($user_ids) { - $object->addFilter('already_member_filter', '%1$s.PortalUserId NOT IN ('.implode(',', $user_ids).')'); + $object->addFilter('already_member_filter', '%1$s.PortalUserId NOT IN (' . implode(',', $user_ids) . ')'); } } } @@ -298,46 +299,24 @@ { $this->saveUserImages($event); - if ($this->Application->GetVar('skip_set_primary')) return; - $is_subscriber = $this->Application->GetVar('IsSubscriber'); - if(!$is_subscriber) - { - $object =& $event->getObject(); + $object =& $event->getObject(); + /* @var $object kDBItem */ - $ug_table = TABLE_PREFIX.'UserGroup'; - if ($object->IsTempTable()) { - $ug_table = $this->Application->GetTempName($ug_table, 'prefix:'.$event->Prefix); - } + $primary_group_id = $object->GetDBField('PrimaryGroupId'); - $sql = 'UPDATE '.$ug_table.' - SET PrimaryGroup = 0 - WHERE PortalUserId = '.$object->GetDBField('PortalUserId'); - $this->Conn->Query($sql); + if ($primary_group_id) { + $ug_table = TABLE_PREFIX . 'UserGroup'; - // set primary group to user - if ($this->Application->isAdminUser && $this->Application->GetVar('user_group')) { - // while in admin you can set any group for new users - $group_id = $this->Application->GetVar('user_group'); + if ( $object->IsTempTable() ) { + $ug_table = $this->Application->GetTempName($ug_table, 'prefix:' . $event->Prefix); } - else { - $group_id = $object->GetDBField('UserGroup'); - if ($group_id) { - // check, that group is allowed for Front-End - $sql = 'SELECT GroupId - FROM ' . TABLE_PREFIX . 'PortalGroup - WHERE GroupId = ' . (int)$group_id . ' AND FrontRegistration = 1'; - $group_id = $this->Conn->GetOne($sql); - } + $fields_hash = Array ( + 'PortalUserId' => $object->GetID(), + 'GroupId' => $primary_group_id, + ); - if (!$group_id) { - // when group not selected -> use default group - $group_id = $this->Application->ConfigValue('User_NewGroup'); - } - } - - $sql = 'REPLACE INTO '.$ug_table.'(PortalUserId,GroupId,PrimaryGroup) VALUES (%s,%s,1)'; - $this->Conn->Query( sprintf($sql, $object->GetID(), $group_id) ); + $this->Conn->doInsert($fields_hash, $ug_table, 'REPLACE'); } } @@ -369,31 +348,30 @@ { $ret = false; $object =& $event->getObject( Array('skip_autoload' => true) ); + $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if($items_info) - { + + if ($items_info) { list($id, $field_values) = each($items_info); $user_email = isset($field_values['Email']) ? $field_values['Email'] : false; - if($user_email) - { + + if ($user_email) { // check if is subscriber $verify_user =& $this->Application->recallObject('u.verify', null, Array('skip_autoload' => true) ); $verify_user->Load($user_email, 'Email'); - if( $verify_user->isLoaded() && $verify_user->isSubscriberOnly() ) - { + + if ( $verify_user->isLoaded() && $verify_user->isSubscriberOnly() ) { $items_info = Array( $verify_user->GetDBField('PortalUserId') => $field_values ); $this->Application->SetVar($event->getPrefixSpecial(true), $items_info); $ret = true; } } } - if( isset($event->MasterEvent) ) - { + if ( isset($event->MasterEvent) ) { $event->MasterEvent->setEventParam('is_subscriber_only', $ret); } - else - { + else { $event->setEventParam('is_subscriber_only', $ret); } } @@ -443,18 +421,20 @@ $this->setUserStatus($event); } - if (!$this->isSubscriberOnly($event)) { + if ( !$this->isSubscriberOnly($event) ) { $object =& $event->getObject( Array('skip_autoload' => true) ); /* @var $object kDBItem */ - if ($this->Application->ConfigValue('User_Password_Auto')) { - $pass = kUtil::generatePassword(rand(5,8)); + + if ( $this->Application->ConfigValue('User_Password_Auto') ) { + $pass = kUtil::generatePassword( rand(5, 8) ); $object->SetField('Password', $pass); $object->SetField('VerifyPassword', $pass); - $this->Application->SetVar('user_password',$pass); + $this->Application->SetVar('user_password', $pass); } + parent::OnCreate($event); - $this->Application->SetVar('u.current_id', $object->getID() ); // for affil:OnRegisterAffiliate after hook + $this->Application->SetVar('u.current_id', $object->getID()); // for affil:OnRegisterAffiliate after hook $this->setNextTemplate($event); @@ -512,7 +492,7 @@ $cs_helper =& $this->Application->recallObject('CountryStatesHelper'); /* @var $cs_helper kCountryStatesHelper */ - if (!$this->isSubscriberOnly($event, true)) { + if ( !$this->isSubscriberOnly($event, true) ) { $cs_helper->CheckStateField($event, 'State', 'Country'); } else { @@ -531,6 +511,7 @@ } $object->setLogin(); + $this->setUserGroup($object); $user_helper =& $this->Application->recallObject('UserHelper'); /* @var $user_helper UserHelper */ @@ -542,17 +523,51 @@ } /** + * Sets primary group of the user + * + * @param kDBItem $object + */ + protected function setUserGroup(&$object) + { + if ($object->Special == 'subscriber') { + $object->SetDBField('PrimaryGroupId', $this->Application->ConfigValue('User_SubscriberGroup')); + + return ; + } + + // set primary group to user + if ( !$this->Application->isAdminUser ) { + $group_id = $object->GetDBField('PrimaryGroupId'); + + if ($group_id) { + // check, that group is allowed for Front-End + $sql = 'SELECT GroupId + FROM ' . TABLE_PREFIX . 'PortalGroup + WHERE GroupId = ' . (int)$group_id . ' AND FrontRegistration = 1'; + $group_id = $this->Conn->GetOne($sql); + } + + if (!$group_id) { + // when group not selected OR not allowed -> use default group + $object->SetDBField('PrimaryGroupId', $this->Application->ConfigValue('User_NewGroup')); + } + } + } + + /** * Set's new unique resource id to user * * @param kEvent $event */ function OnAfterItemValidate(&$event) { $object =& $event->getObject(); + /* @var $object kDBItem */ + $resource_id = $object->GetDBField('ResourceId'); - if (!$resource_id) - { - $object->SetDBField('ResourceId', $this->Application->NextResourceId() ); + + if (!$resource_id) { + $object->SetDBField('ResourceId', $this->Application->NextResourceId()); } } @@ -654,21 +669,24 @@ function OnSubscribeQuery(&$event) { $user_email = $this->Application->GetVar('subscriber_email'); - if (preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $user_email)) { - $object =& $this->Application->recallObject($this->Prefix.'.subscriber', null, Array('skip_autoload' => true)); + if ( preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $user_email) ) { + $object =& $this->Application->recallObject($this->Prefix . '.subscriber', null, Array('skip_autoload' => true)); /* @var $object UsersItem */ - $this->Application->StoreVar('SubscriberEmail', $user_email); - $object->Load($user_email, 'Email'); - if ($object->isLoaded()) { - $group_info = $this->GetGroupInfo($object->GetID()); - $event->redirect = $this->Application->GetVar($group_info ? 'unsubscribe_template' : 'subscribe_template'); + $event->SetRedirectParam('subscriber_email', $user_email); + + if ( $object->isLoaded() ) { + if ( $this->isSubscribed($object) ) { + $event->redirect = $this->Application->GetVar('unsubscribe_template'); + } + else { + $event->redirect = $this->Application->GetVar('subscribe_template'); + } } else { $event->redirect = $this->Application->GetVar('subscribe_template'); - $this->Application->StoreVar('SubscriberEmail', $user_email); } } else { @@ -688,93 +706,104 @@ */ function OnSubscribeUser(&$event) { - $object = &$this->Application->recallObject($this->Prefix.'.subscriber', null, Array('skip_autoload' => true)); + $object = &$this->Application->recallObject($this->Prefix . '.subscriber', null, Array('skip_autoload' => true)); /* @var $object UsersItem */ - $user_email = $this->Application->RecallVar('SubscriberEmail'); - if (preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $user_email)) { + $user_email = $this->Application->GetVar('subscriber_email'); + + if ( preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $user_email) ) { $this->RemoveRequiredFields($object); $object->Load($user_email, 'Email'); - if ($object->isLoaded()) { - $group_info = $this->GetGroupInfo($object->GetID()); + if ( $object->isLoaded() ) { + if ( $this->isSubscribed($object) ) { + if ( $event->getEventParam('no_unsubscribe') ) { + // for customization code from FormsEventHandler + return ; + } - if ($group_info){ - if ($event->getEventParam('no_unsubscribe')) return; - - if ($group_info['PrimaryGroup']){ + if ( $object->isSubscriberOnly() ) { $this->Application->SetVar($object->getPrefixSpecial(true) . '_id', $object->GetID()); $delete_event = new kEvent($object->getPrefixSpecial() . ':OnDelete'); $this->Application->HandleEvent($delete_event); } else { - $this->RemoveSubscriberGroup($object->GetID()); + $this->RemoveSubscriberGroup( $object->GetID() ); } $event->redirect = $this->Application->GetVar('unsubscribe_ok_template'); } else { - $this->AddSubscriberGroup($object->GetID(), 0); + $this->AddSubscriberGroup($object); $event->redirect = $this->Application->GetVar('subscribe_ok_template'); } } else { - $object->SetField('Email', $user_email); - $object->SetField('Login', $user_email); - $object->SetDBField('dob', 1); - $object->SetDBField('dob_date', 1); - $object->SetDBField('dob_time', 1); + $object->SetDBField('Email', $user_email); + $object->SetDBField('Login', $user_email); $object->SetDBField('Status', STATUS_ACTIVE); // make user subscriber Active by default - $ip = getenv('HTTP_X_FORWARDED_FOR')?getenv('HTTP_X_FORWARDED_FOR'):getenv('REMOTE_ADDR'); - $object->SetDBField('ip', $ip); + $object->SetDBField('ip', $_SERVER['REMOTE_ADDR']); - $this->Application->SetVar('IsSubscriber', 1); - - if ($object->Create()) { - $this->AddSubscriberGroup($object->GetID(), 1); + if ( $object->Create() ) { + $this->AddSubscriberGroup($object); $event->redirect = $this->Application->GetVar('subscribe_ok_template'); } - - $this->Application->SetVar('IsSubscriber', 0); } } } - function AddSubscriberGroup($user_id, $is_primary) + /** + * Adding user to subscribers group + * + * @param UsersItem $object + */ + function AddSubscriberGroup(&$object) { - $group_id = $this->Application->ConfigValue('User_SubscriberGroup'); - $sql = 'INSERT INTO ' . TABLE_PREFIX . 'UserGroup - (PortalUserId, GroupId, PrimaryGroup) VALUES (%s, %s, ' . $is_primary . ')'; - $this->Conn->Query( sprintf($sql, $user_id, $group_id) ); + if ( !$object->isSubscriberOnly() ) { + $fields_hash = Array ( + 'PortalUserId' => $object->GetID(), + 'GroupId' => $this->Application->ConfigValue('User_SubscriberGroup'), + ); + $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'UserGroup'); + } + $this->Application->EmailEventAdmin('USER.SUBSCRIBE'); - $this->Application->EmailEventUser('USER.SUBSCRIBE', $user_id); + $this->Application->EmailEventUser('USER.SUBSCRIBE', $object->GetID()); } + /** + * Removing user from subscribers group + * + * @param int $user_id + */ function RemoveSubscriberGroup($user_id) { $group_id = $this->Application->ConfigValue('User_SubscriberGroup'); + $sql = 'DELETE FROM ' . TABLE_PREFIX . 'UserGroup - WHERE PortalUserId = ' . $user_id . ' - AND GroupId = ' . $this->Application->ConfigValue('User_SubscriberGroup'); + WHERE PortalUserId = ' . $user_id . ' AND GroupId = ' . $group_id; $this->Conn->Query($sql); $this->Application->EmailEventAdmin('USER.UNSUBSCRIBE'); $this->Application->EmailEventUser('USER.UNSUBSCRIBE', $user_id); } /** - * Allows to detect user subscription status (subscribed or not) + * Checks user subscription status * - * @param int $user_id + * @param kDBItem $object * @return bool */ - function GetGroupInfo($user_id) + function isSubscribed(&$object) { - $sql = 'SELECT * FROM ' . TABLE_PREFIX . 'UserGroup - WHERE (PortalUserId = ' . $user_id . ') - AND (GroupId = ' . $this->Application->ConfigValue('User_SubscriberGroup') . ')'; - return $this->Conn->GetRow($sql); + $group_id = $this->Application->ConfigValue('User_SubscriberGroup'); + + $sql = 'SELECT GroupId + FROM ' . TABLE_PREFIX . 'UserGroup + WHERE (PortalUserId = ' . $object->GetID() . ') AND (GroupId = ' . $group_id . ')'; + + return $this->Conn->GetOne($sql); } function OnForgotPassword(&$event) @@ -1384,17 +1413,16 @@ $first_country = $this->Application->ConfigValue('User_Default_Registration_Country'); } + $fields = $this->Application->getUnitOption($event->Prefix, 'Fields'); + if ($first_country) { // update user country dropdown sql - $fields = $this->Application->getUnitOption($event->Prefix, 'Fields'); $fields['Country']['options_sql'] = preg_replace('/ORDER BY (.*)/', 'ORDER BY IF (CountryStateId = '.$first_country.', 1, 0) DESC, \\1', $fields['Country']['options_sql']); - $this->Application->setUnitOption($event->Prefix, 'Fields', $fields); } // 2. set default user registration group - $virtual_fields = $this->Application->getUnitOption($event->Prefix, 'VirtualFields'); - $virtual_fields['UserGroup']['default'] = $this->Application->ConfigValue('User_NewGroup'); - $this->Application->setUnitOption($event->Prefix, 'VirtualFields', $virtual_fields); + $fields['PrimaryGroupId']['default'] = $this->Application->ConfigValue('User_NewGroup'); + $this->Application->setUnitOption($event->Prefix, 'Fields', $fields); // 3. allow avatar upload on Front-End $file_helper =& $this->Application->recallObject('FileHelper'); @@ -1441,13 +1469,11 @@ return; } - $event->status=kEvent::erSUCCESS; - $ids = $this->StoreSelectedIDs($event); - - $this->Application->SetVar('skip_set_primary', 1); // otherwise it will default primary group, search for skip_set_primary above $temp_handler =& $this->Application->recallObject($event->Prefix.'_TempHandler', 'kTempTablesHandler'); /* @var $temp_handler kTempTablesHandler */ - $cloned_users = $temp_handler->CloneItems($event->Prefix, '', $ids); + + $ids = $this->StoreSelectedIDs($event); + $temp_handler->CloneItems($event->Prefix, '', $ids); $this->clearSelectedIDs($event); } @@ -1460,30 +1486,15 @@ { $object =& $event->getObject(); /* @var $object kDBItem */ - $object->setRequired('Password', 0); - $object->setRequired('VerifyPassword', 0); - $object->SetDBField('Password', rand(100000000, 999999999)); - $object->SetDBField('CreatedOn', adodb_mktime()); - $object->SetDBField('ResourceId', false); // this will reset it - // change email cause it should be unique - $object->NameCopy(array(), $object->GetID(), 'Email', 'copy%1$s.%2$s'); + $password = kUtil::generatePassword(); + $object->SetField('Password', $password); + $object->SetField('VerifyPassword', $password); - $object->UpdateFormattersSubFields(); - } + $object->SetDBField('ResourceId', 0); // this will reset it - /** - * Copy user groups after copying user - * - * @param kEvent $event - */ - function OnAfterClone(&$event) - { - $id = $event->getEventParam('id'); - $original_id = $event->getEventParam('original_id'); - - $sql = 'INSERT '.TABLE_PREFIX."UserGroup SELECT $id, GroupId, MembershipExpires, PrimaryGroup, 0 FROM ".TABLE_PREFIX."UserGroup WHERE PortalUserId = $original_id"; - $this->Conn->Query($sql); + // change email because it should be unique + $object->NameCopy(Array (), $object->GetID(), 'Email', 'copy%1$s.%2$s'); } /** @@ -1496,11 +1507,11 @@ $this->StoreSelectedIDs($event); // remove current ID, otherwise group selector will use it in filters - $this->Application->DeleteVar($event->getPrefixSpecial(true).'_id'); + $this->Application->DeleteVar($event->getPrefixSpecial(true) . '_id'); } /** - * Adds selected link to listing + * Sets primary group of selected users * * @param kEvent $event */ @@ -1524,34 +1535,27 @@ $table_name = $this->Application->getUnitOption('ug', 'TableName'); + // 1. mark group as primary + $sql = 'UPDATE ' . TABLE_PREFIX . 'PortalUser + SET PrimaryGroupId = ' . $primary_group_id . ' + WHERE PortalUserId IN (' . implode(',', $user_ids) . ')'; + $this->Conn->Query($sql); + $sql = 'SELECT PortalUserId - FROM '.$table_name.' - WHERE (GroupId = '.$primary_group_id.') AND (PortalUserId IN ('.implode(',', $user_ids).'))'; + FROM ' . $table_name . ' + WHERE (GroupId = ' . $primary_group_id . ') AND (PortalUserId IN (' . implode(',', $user_ids) . '))'; $existing_members = $this->Conn->GetCol($sql); - // 1. reset primary group mark - $sql = 'UPDATE '.$table_name.' - SET PrimaryGroup = 0 - WHERE PortalUserId IN ('.implode(',', $user_ids).')'; - $this->Conn->Query($sql); + // 2. add new members to a group + $new_members = array_diff($user_ids, $existing_members); - foreach ($user_ids as $user_id) { - if (in_array($user_id, $existing_members)) { - // 2. already member of that group -> just make primary - $sql = 'UPDATE '.$table_name.' - SET PrimaryGroup = 1 - WHERE (PortalUserId = '.$user_id.') AND (GroupId = '.$primary_group_id.')'; - $this->Conn->Query($sql); - } - else { - // 3. not member of that group -> make member & make primary - $fields_hash = Array ( - 'GroupId' => $primary_group_id, - 'PortalUserId' => $user_id, - 'PrimaryGroup' => 1, - ); - $this->Conn->doInsert($fields_hash, $table_name); - } + foreach ($new_members as $user_id) { + $fields_hash = Array ( + 'GroupId' => $primary_group_id, + 'PortalUserId' => $user_id, + ); + + $this->Conn->doInsert($fields_hash, $table_name); } } @@ -1608,6 +1612,19 @@ parent::OnPreCreate($event); if ($event->status == kEvent::erSUCCESS) { + $user_type = $this->Application->GetVar('user_type'); + + if ($user_type) { + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $object->SetDBField('UserType', $user_type); + + if ( $user_type == UserType::ADMIN ) { + $object->SetDBField('PrimaryGroupId', $this->Application->ConfigValue('User_AdminGroup')); + } + } + $this->_makePasswordRequired($event); } }