Index: admin/system_presets/simple/user_groups_g-ug.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- admin/system_presets/simple/user_groups_g-ug.php (revision 5818) +++ admin/system_presets/simple/user_groups_g-ug.php (revision ) @@ -18,7 +18,7 @@ // fields to hide $hidden_fields = Array ( - /* 'PortalUserId', 'GroupId', 'MembershipExpires', 'PrimaryGroup', 'ExpirationReminderSent', */ + /* 'PortalUserId', 'GroupId', 'MembershipExpires', 'ExpirationReminderSent', */ ); // virtual fields to hide @@ -28,7 +28,7 @@ // fields to make required $required_fields = Array ( -/* 'PortalUserId', 'GroupId', 'MembershipExpires', 'PrimaryGroup', 'ExpirationReminderSent', */ +/* 'PortalUserId', 'GroupId', 'MembershipExpires', 'ExpirationReminderSent', */ ); // virtual fields to make required Index: core/units/users/users_config.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/users/users_config.php (revision 5818) +++ core/units/users/users_config.php (revision ) @@ -135,7 +135,7 @@ 'users_list' => Array ( 'prefixes' => Array ('u_List'), 'format' => "!la_title_Users!", - 'toolbar_buttons' => Array ('new_item', 'edit', 'delete', 'approve', 'decline', 'e-mail', 'export', 'view', 'dbl-click'), + 'toolbar_buttons' => Array ('new_item', 'edit', 'delete', 'setprimary', 'approve', 'decline', 'e-mail', 'export', 'view', 'dbl-click'), ), 'users_edit' => Array ( @@ -287,22 +287,19 @@ 'TableName' => TABLE_PREFIX.'PortalUser', 'ListSQLs' => Array( '' => ' SELECT %1$s.* %2$s FROM %1$s - LEFT JOIN '.TABLE_PREFIX.'UserGroup ug ON %1$s.PortalUserId = ug.PortalUserId AND ug.PrimaryGroup = 1 - LEFT JOIN '.TABLE_PREFIX.'PortalGroup g ON ug.GroupId = g.GroupId + LEFT JOIN '.TABLE_PREFIX.'PortalGroup g ON %1$s.PrimaryGroupId = g.GroupId LEFT JOIN '.TABLE_PREFIX.'%3$sPortalUserCustomData cust ON %1$s.ResourceId = cust.ResourceId LEFT JOIN '.TABLE_PREFIX.'%3$sImages img ON img.ResourceId = %1$s.ResourceId AND img.DefaultImg = 1', 'online' => ' SELECT %1$s.* %2$s FROM %1$s LEFT JOIN '.TABLE_PREFIX.'UserSession s ON s.PortalUserId = %1$s.PortalUserId - LEFT JOIN '.TABLE_PREFIX.'UserGroup ug ON %1$s.PortalUserId = ug.PortalUserId AND ug.PrimaryGroup = 1 - LEFT JOIN '.TABLE_PREFIX.'PortalGroup g ON ug.GroupId = g.GroupId + LEFT JOIN '.TABLE_PREFIX.'PortalGroup g ON %1$s.PrimaryGroupId = g.GroupId LEFT JOIN '.TABLE_PREFIX.'%3$sPortalUserCustomData cust ON %1$s.ResourceId = cust.ResourceId LEFT JOIN '.TABLE_PREFIX.'%3$sImages img ON img.ResourceId = %1$s.ResourceId AND img.DefaultImg = 1', ), 'ItemSQLs' => Array( '' => ' SELECT %1$s.* %2$s FROM %1$s - LEFT JOIN '.TABLE_PREFIX.'UserGroup ug ON %1$s.PortalUserId = ug.PortalUserId AND ug.PrimaryGroup = 1 - LEFT JOIN '.TABLE_PREFIX.'PortalGroup g ON ug.GroupId = g.GroupId + LEFT JOIN '.TABLE_PREFIX.'PortalGroup g ON %1$s.PrimaryGroupId = g.GroupId LEFT JOIN '.TABLE_PREFIX.'%3$sPortalUserCustomData cust ON %1$s.ResourceId = cust.ResourceId LEFT JOIN '.TABLE_PREFIX.'%3$sImages img ON img.ResourceId = %1$s.ResourceId AND img.DefaultImg = 1', ), @@ -374,7 +371,7 @@ 'type' => 'int', 'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_Yes', 0 => 'la_No'), 'use_phrases' => 1, 'not_null' => 1, 'default' => 0 - ), + ), 'Fax' => Array('type' => 'string', 'not_null' => 1, 'default' => ''), 'Street' => Array('type' => 'string', 'not_null' => 1, 'default' => ''), 'Street2' => Array('type' => 'string', 'not_null' => 1, 'default' => ''), @@ -422,7 +419,17 @@ ), 'use_phrases' => 1, 'multiple' => 1, 'default' => NULL ), + 'UserType' => Array ( + 'type' => 'int', + 'formatter' => 'kOptionsFormatter', 'options' => Array (0 => 'la_opt_UserTypeUser', 1 => 'la_opt_UserTypeAdmin'), 'use_phrases' => 1, + 'not_null' => 1, 'default' => 0 - ), + ), + 'PrimaryGroupId' => Array ( + 'type' => 'int', + 'formatter' => 'kOptionsFormatter', 'options_sql' => 'SELECT %1$s FROM ' . TABLE_PREFIX . 'PortalGroup WHERE Enabled = 1 AND FrontRegistration = 1', 'option_key_field' => 'GroupId', 'option_title_field' => 'Name', + 'default' => NULL + ), + ), 'VirtualFields' => Array( 'ValidateLogin' => Array('type'=>'string','default'=>''), @@ -431,11 +438,6 @@ 'RootPassword' => Array('type' => 'string', 'formatter' => 'kPasswordFormatter', 'encryption_method' => 'md5', 'verify_field' => 'VerifyRootPassword', 'skip_empty' => 1, 'default' => md5('') ), 'FullName' => Array ('type' => 'string', 'default' => ''), - 'UserGroup' => Array ( - 'type' => 'int', - 'formatter' => 'kOptionsFormatter', 'options_sql' => 'SELECT %1$s FROM ' . TABLE_PREFIX . 'PortalGroup WHERE Enabled = 1 AND FrontRegistration = 1', 'option_key_field' => 'GroupId', 'option_title_field' => 'Name', - 'default' => 0, - ), 'AltName' => Array ('type' => 'string', 'default' => ''), 'SameImages' => Array ('type' => 'string', 'default' => ''), 'LocalThumb' => Array ('type' => 'string', 'default' => ''), @@ -521,4 +523,4 @@ ), ), - ); \ No newline at end of file + ); Index: core/units/user_groups/user_groups_eh.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/user_groups/user_groups_eh.php (revision 5818) +++ core/units/user_groups/user_groups_eh.php (revision ) @@ -25,35 +25,37 @@ function OnProcessSelected(&$event) { if ($event->Prefix == 'u-ug') { - $new_groups = $this->Application->GetVar('g'); - if (!$new_groups) { - return ; - } + $new_groups = $this->Application->GetVar('g'); + if (!$new_groups) { + return ; + } - $new_groups = array_keys($new_groups); + $new_groups = array_keys($new_groups); - // don't insert duplicate group membership record - $user_id = $this->Application->GetVar('u_id'); - $table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:u'); + // don't insert duplicate group membership record + $user_id = $this->Application->GetVar('u_id'); + $table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:u'); + - $sql = 'SELECT GroupId - FROM '.$table_name.' - WHERE PortalUserId = '.(int)$user_id; - $old_groups = $this->Conn->GetCol($sql); + $sql = 'SELECT GroupId + FROM '.$table_name.' + WHERE PortalUserId = '.(int)$user_id; + $old_groups = $this->Conn->GetCol($sql); - $new_groups = array_diff($new_groups, $old_groups); + $new_groups = array_diff($new_groups, $old_groups); + - if ($new_groups) { - foreach ($new_groups as $new_group) { - $fields_hash = Array ( - 'GroupId' => $new_group, - 'PortalUserId' => $user_id, + if ($new_groups) { + foreach ($new_groups as $new_group) { + $fields_hash = Array ( + 'GroupId' => $new_group, + 'PortalUserId' => $user_id, - 'PrimaryGroup' => 0, - ); - $this->Conn->doInsert($fields_hash, $table_name); - } - } + ); + $this->Conn->doInsert($fields_hash, $table_name); + } + } } - if ($event->Prefix == 'g-ug') { + elseif ($event->Prefix == 'g-ug') { $new_users = $this->Application->GetVar('u'); + if (!$new_users) { return ; } @@ -63,18 +65,19 @@ // don't insert duplicate group membership record $group_id = $this->Application->GetVar('g_id'); $table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:g'); + $sql = 'SELECT PortalUserId FROM ' . $table_name . ' WHERE GroupId = ' . (int)$group_id; $old_users = $this->Conn->GetCol($sql); $new_users = array_diff($new_users, $old_users); + if ($new_users) { foreach ($new_users as $new_user) { $fields_hash = Array ( 'GroupId' => $group_id, 'PortalUserId' => $new_user, - 'PrimaryGroup' => 0, ); $this->Conn->doInsert($fields_hash, $table_name); } @@ -93,20 +96,13 @@ function OnSetPrimary(&$event) { $ids = $this->StoreSelectedIDs($event); + if ($ids) { - $id = array_shift($ids); - $object =& $event->getObject( Array('skip_autoload' => true) ); - $user_id = $this->Application->GetVar('u_id'); + $user =& $this->Application->recallObject('u'); + /* @var $user kDBItem */ - $sql = 'UPDATE ' . $object->TableName . ' - SET PrimaryGroup = 0 - WHERE PortalUserId = ' . (int)$user_id; - $this->Conn->Query($sql); - - $sql = 'UPDATE ' . $object->TableName . ' - SET PrimaryGroup = 1 - WHERE ' . $object->IDField . ' = ' . $id . ' AND PortalUserId = ' . (int)$user_id; - $this->Conn->Query($sql); + $user->SetDBField('PrimaryGroupId', array_shift($ids)); + $user->Update(); } $this->clearSelectedIDs($event); @@ -121,26 +117,36 @@ { if ($event->Name == 'OnMassDelete' && $type == 'before') { $ids = $event->getEventParam('ids'); + if ($ids) { $object =& $event->getObject( Array('skip_autoload' => true) ); if ($event->Prefix == 'u-ug') { - $user_id = $this->Application->GetVar('u_id'); - $sql = 'SELECT '.$object->IDField.' - FROM '.$object->TableName.' - WHERE '.$object->IDField.' IN ('.implode(',', $ids).') AND PortalUserId = '.(int)$user_id.' AND PrimaryGroup = 0'; - $ids = $this->Conn->GetCol($sql); + // allow deleting non-primary group of current user ($ids - groups) + $sql = 'SELECT PrimaryGroupId + FROM ' . $this->Application->GetTempName(TABLE_PREFIX . 'PortalUser', 'prefix:u') . ' + WHERE PortalUserId = ' . (int)$this->Application->GetVar('u_id'); + $primary_group_id = (int)$this->Conn->GetOne($sql); + + $index = array_search($primary_group_id, $ids); + + if ($index !== false) { + unset($ids[$index]); + $event->setEventParam('ids', $ids); - } + } + } elseif ($event->Prefix == 'g-ug') { - $group_id = $this->Application->GetVar('g_id'); - $sql = 'SELECT '.$object->IDField.' - FROM '.$object->TableName.' - WHERE '.$object->IDField.' IN ('.implode(',', $ids).') AND GroupId = '.(int)$group_id.' AND PrimaryGroup = 0'; - $ids = $this->Conn->GetCol($sql); + // allow deleting users from group record, then it's not their primary group ($ids - users) + $group_id = (int)$this->Application->GetVar('g_id'); + + $sql = 'SELECT PortalUserId + FROM ' . TABLE_PREFIX . 'PortalUser' . ' + WHERE PortalUserId IN (' . implode(',', $ids) . ') AND PrimaryGroupId = ' . $group_id; + $exclude_users = $this->Conn->GetCol($sql); + + $event->setEventParam('ids', array_diff($ids, $exclude_users)); } - $event->setEventParam('ids', $ids); } } } - } \ No newline at end of file Index: core/admin_templates/users/users_list.tpl IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/admin_templates/users/users_list.tpl (revision 5818) +++ core/admin_templates/users/users_list.tpl (revision ) @@ -30,7 +30,7 @@ /*a_toolbar.AddButton ( new ToolBarButton( - 'primary_user_group', + 'setprimary', '::', function() { openSelector('u.regular', '', 'PrimaryGroupId', '800x600', 'OnSaveSelected'); @@ -93,6 +93,6 @@ \ No newline at end of file Index: core/units/users/users_item.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/users/users_item.php (revision 5818) +++ core/units/users/users_item.php (revision ) @@ -32,7 +32,7 @@ $sql = 'SELECT GroupId FROM ' . TABLE_PREFIX . 'UserGroup WHERE (PortalUserId = ' . $this->GetID() . ') AND ( (MembershipExpires IS NULL) OR ( MembershipExpires >= UNIX_TIMESTAMP() ) ) - ORDER BY PrimaryGroup DESC'; + ORDER BY IF(GroupId = ' . $this->GetDBField('PrimaryGroupId') . ', 1, 0) DESC'; return $this->Conn->GetCol($sql); } @@ -75,15 +75,14 @@ return $password; } + /** + * Checks that user is subscriber only + * + * @return bool + */ function isSubscriberOnly() { - $subscribers_group_id = $this->Application->ConfigValue('User_SubscriberGroup'); - $sql = 'SELECT PortalUserId - FROM '.TABLE_PREFIX.'UserGroup - WHERE GroupId = '.$subscribers_group_id.' AND - PortalUserId = '.$this->GetDBField('PortalUserId').' AND - PrimaryGroup = 1'; - return $this->Conn->GetOne($sql) == $this->GetDBField('PortalUserId'); + return $this->GetDBField('PrimaryGroupId') == $this->Application->ConfigValue('User_SubscriberGroup'); } function Create($force_id=false, $system_create=false) @@ -142,4 +141,4 @@ $this->SetDBField('LastName', $last_name); } - } \ No newline at end of file + } Index: core/units/users/users_event_handler.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/users/users_event_handler.php (revision 5818) +++ core/units/users/users_event_handler.php (revision ) @@ -28,7 +28,8 @@ // admin 'OnSetPersistantVariable' => 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), @@ -60,12 +61,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) { @@ -78,16 +79,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) . ')'); } } } @@ -299,49 +300,27 @@ { $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 */ + $primary_group_id = $object->GetDBField('PrimaryGroupId'); + + if ($primary_group_id) { - $ug_table = TABLE_PREFIX.'UserGroup'; + $ug_table = TABLE_PREFIX . 'UserGroup'; - if ($object->mode == 't') { + + if ( $object->IsTempTable() ) { - $ug_table = $this->Application->GetTempName($ug_table, 'prefix:'.$event->Prefix); + $ug_table = $this->Application->GetTempName($ug_table, 'prefix:' . $event->Prefix); } - $sql = 'UPDATE '.$ug_table.' - SET PrimaryGroup = 0 - WHERE PortalUserId = '.$object->GetDBField('PortalUserId'); - $this->Conn->Query($sql); + $fields_hash = Array ( + 'PortalUserId' => $object->GetID(), + 'GroupId' => $primary_group_id, + ); - // 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'); + $this->Conn->doInsert($fields_hash, $ug_table, 'REPLACE'); - } + } - 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); - } + } - 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) ); - } - } - /** * Login user if possible, if not then redirect to corresponding template * @@ -369,18 +348,19 @@ { $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; @@ -388,12 +368,10 @@ } } - if( isset($event->MasterEvent) ) - { + if ( isset($event->MasterEvent) ) { $event->MasterEvent->setEventParam('is_subscriber_only', $ret); } - else - { + else { $event->setEventParam('is_subscriber_only', $ret); } } @@ -442,16 +420,18 @@ $this->setUserStatus($event); } - if (!$this->isSubscriberOnly($event)) { + if ( !$this->isSubscriberOnly($event) ) { $object =& $event->getObject( Array('skip_autoload' => true) ); /* @var $object UsersItem */ + - if ($this->Application->ConfigValue('User_Password_Auto')) { + if ( $this->Application->ConfigValue('User_Password_Auto') ) { $password = $object->generatePassword( rand(5, 8) ); $this->Application->SetVar('user_password', $password); } + 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); @@ -509,10 +489,10 @@ $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'); } - + $this->_makePasswordRequired($event); $cs_helper->PopulateStates($event, 'State', 'Country'); @@ -529,12 +509,46 @@ $user_helper =& $this->Application->recallObject('UserHelper'); /* @var $user_helper UserHelper */ + $this->setUserGroup($object); + if (!$user_helper->checkBanRules($object)) { $object->SetError('Login', 'banned'); } } /** + * 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 @@ -542,10 +556,12 @@ function OnAfterItemValidate(&$event) { $object =& $event->getObject(); + /* @var $object kDBItem */ + $resource_id = $object->GetDBField('ResourceId'); - if (!$resource_id) - { + + if (!$resource_id) { - $object->SetDBField('ResourceId', $this->Application->NextResourceId() ); + $object->SetDBField('ResourceId', $this->Application->NextResourceId()); } } @@ -646,24 +662,27 @@ function OnSubscribeQuery(&$event) { $user_email = $this->Application->GetVar('subscriber_email'); - if (preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $user_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)); + $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'); + $event->SetRedirectParam('subscriber_email', $user_email); + - if ($object->isLoaded()) { + if ( $object->isLoaded() ) { - $group_info = $this->GetGroupInfo($object->GetID()); - $event->redirect = $this->Application->GetVar($group_info ? 'unsubscribe_template' : 'subscribe_template'); + 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 { + } + } + else { + $event->redirect = $this->Application->GetVar('subscribe_template'); + } + } + else { // used for error reporting only -> rewrite code + theme (by Alex) $object =& $this->Application->recallObject('u', null, Array('skip_autoload' => true)); // TODO: change theme too /* @var $object UsersItem */ @@ -680,73 +699,83 @@ */ 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'); + $user_email = $this->Application->GetVar('subscriber_email'); + - if (preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $user_email)) { + if ( preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $user_email) ) { $this->RemoveRequiredFields($object); $object->Load($user_email, 'Email'); - if ($object->isLoaded()) { + if ( $object->isLoaded() ) { - $group_info = $this->GetGroupInfo($object->GetID()); + 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() ) { // delete user $object->Delete(); } 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 { - $password = makepassword4(); - $object->SetField('Password', $password); - $object->SetField('VerifyPassword', $password); + $object->generatePassword( rand(5, 8) ); $object->SetDBField('Email', $user_email); $object->SetDBField('Login', $user_email); $object->SetDBField('Status', STATUS_ACTIVE); // make user subscriber Active by default $object->SetDBField('ip', $_SERVER['REMOTE_ADDR']); - $this->Application->SetVar('IsSubscriber', 1); - - if ($object->Create()) { + if ( $object->Create() ) { - $this->AddSubscriberGroup($object->GetID(), 1); + $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'); @@ -754,17 +783,20 @@ } /** - * 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) @@ -896,12 +928,9 @@ if ($exp_time > adodb_mktime()) { $newpw = $user_object->generatePassword(); - + $this->Application->StoreVar('password', $newpw); - $user_object->SetField('Password', $newpw); - $user_object->SetField('VerifyPassword', $newpw); - $user_object->SetDBField('PassResetTime', adodb_mktime()); $user_object->SetDBField('PwResetConfirm', ''); $user_object->SetDBField('PwRequestTime', 0); @@ -1369,17 +1398,15 @@ $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'); // 3. allow avatar upload on Front-End $file_helper =& $this->Application->recallObject('FileHelper'); @@ -1410,12 +1437,13 @@ $this->Application->setUnitOption($event->Prefix, 'EditTabPresets', $edit_tab_presets); } } - + if ( !$this->Application->ConfigValue('Email_As_Login') ) { // Login becomes required only, when it's used in registration process $fields['Login']['required'] = 1; } + - $this->Application->setUnitOption($event->Prefix, 'Fields', $fields); + $this->Application->setUnitOption($event->Prefix, 'Fields', $fields); } /** @@ -1431,12 +1459,13 @@ } $event->status=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); } @@ -1454,27 +1483,11 @@ $object->SetDBField('CreatedOn', adodb_mktime()); $object->SetDBField('ResourceId', 0); // this will reset it - // change email cause it should be unique - $object->NameCopy(array(), $object->GetID(), 'Email', 'copy%1$s.%2$s'); - - $object->UpdateFormattersSubFields(); + // change email because it should be unique + $object->NameCopy(Array (), $object->GetID(), 'Email', 'copy%1$s.%2$s'); } /** - * 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); - } - - /** * Saves selected ids to session * * @param kEvent $event @@ -1484,11 +1497,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 */ @@ -1512,36 +1525,29 @@ $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.' + FROM ' . $table_name . ' - WHERE (GroupId = '.$primary_group_id.') AND (PortalUserId IN ('.implode(',', $user_ids).'))'; + 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 + foreach ($new_members as $user_id) { - $fields_hash = Array ( + $fields_hash = Array ( - 'GroupId' => $primary_group_id, + 'GroupId' => $primary_group_id, - 'PortalUserId' => $user_id, + 'PortalUserId' => $user_id, - 'PrimaryGroup' => 1, - ); + ); + - $this->Conn->doInsert($fields_hash, $table_name); - } - } + $this->Conn->doInsert($fields_hash, $table_name); + } + } - } /** * Loads user images @@ -1595,9 +1601,24 @@ { parent::OnPreCreate($event); - if ($event->status == erSUCCESS) { - $this->_makePasswordRequired($event); + if ( $event->status != erSUCCESS ) { + return; } + + $object = $event->getObject(); + /* @var $object kDBItem */ + + $user_type = $this->Application->GetVar('user_type'); + + if ( $user_type ) { + $object->SetDBField('UserType', $user_type); + + if ( $user_type == UserType::ADMIN ) { + $object->SetDBField('PrimaryGroupId', $this->Application->ConfigValue('User_AdminGroup')); + } + } + + $this->_makePasswordRequired($event); } /** @@ -1671,4 +1692,4 @@ { } - } + } \ No newline at end of file Index: core/kernel/constants.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/kernel/constants.php (revision 5818) +++ core/kernel/constants.php (revision ) @@ -176,4 +176,9 @@ define('RECIPIENT_TYPE_BCC', 3); define('PAGE_TYPE_VIRTUAL', 1); - define('PAGE_TYPE_TEMPLATE', 2); \ No newline at end of file + define('PAGE_TYPE_TEMPLATE', 2); + + class UserType { + const USER = 0; + const ADMIN = 1; + } Index: core/admin_templates/users/admins_list.tpl IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/admin_templates/users/admins_list.tpl (revision 5818) +++ core/admin_templates/users/admins_list.tpl (revision ) @@ -10,21 +10,22 @@ //do not rename - this function is used in default grid for double click! function edit() { - set_hidden_field('remove_specials[u.admins]', 1); + Application.SetVar('remove_specials[u.admins]', 1); std_edit_item('u.admins', 'users/admins_edit'); } var a_toolbar = new ToolBar(); a_toolbar.AddButton( new ToolBarButton('new_item', '', function() { - set_hidden_field('remove_specials[u.admins]', 1); + Application.SetVar('user_type', 1); + Application.SetVar('remove_specials[u.admins]', 1); std_precreate_item('u.admins', 'users/admins_edit') } ) ); a_toolbar.AddButton( new ToolBarButton('edit', '::', edit) ); a_toolbar.AddButton( new ToolBarButton('delete', '', function() { - set_hidden_field('remove_specials[u.admins]', 1); + Application.SetVar('remove_specials[u.admins]', 1); std_delete_items('u.admins') } ) ); @@ -33,7 +34,7 @@ a_toolbar.AddButton( new ToolBarButton('clone', '::', function() { - set_hidden_field('remove_specials[u.admins]', 1); + Application.SetVar('remove_specials[u.admins]', 1); submit_event('u.admins', 'OnMassCloneUsers'); } ) ); @@ -42,7 +43,7 @@ a_toolbar.AddButton( new ToolBarButton('refresh', '::', function() { - set_hidden_field('remove_specials[u.admins]', 1); + Application.SetVar('remove_specials[u.admins]', 1); submit_event('u.admins', 'OnMassResetSettings'); } ) ); @@ -65,4 +66,4 @@ - \ No newline at end of file + Index: core/units/user_groups/user_groups_config.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/user_groups/user_groups_config.php (revision 5818) +++ core/units/user_groups/user_groups_config.php (revision ) @@ -39,6 +39,7 @@ '' => Array( 'UserName' => 'CONCAT(u.LastName, \' \', u.FirstName)', 'UserLogin' => 'u.Login', + 'PrimaryGroup' => 'IF(u.PrimaryGroupId = %1$s.GroupId, 1, 0)', ), ), 'VirtualFields' => Array ( @@ -84,12 +85,11 @@ 'TitleField' => 'GroupName', 'TableName' => TABLE_PREFIX.'UserGroup', - 'ListSQLs' => Array( ''=>' SELECT %1$s.* %2$s FROM %1$s - LEFT JOIN '.TABLE_PREFIX.'PortalGroup g ON %1$s.GroupId = g.GroupId'), + 'ListSQLs' => Array( ''=>' SELECT %1$s.* %2$s + FROM %1$s + LEFT JOIN '.TABLE_PREFIX.'PortalGroup g ON %1$s.GroupId = g.GroupId + LEFT JOIN '.TABLE_PREFIX.'%3$sPortalUser u ON %1$s.PortalUserId = u.PortalUserId'), - 'ItemSQLs' => Array( ''=>' SELECT %1$s.* %2$s FROM %1$s - LEFT JOIN '.TABLE_PREFIX.'PortalGroup g ON %1$s.GroupId = g.GroupId'), - 'AutoDelete' => true, 'AutoClone' => false, @@ -97,6 +97,7 @@ '' => Array( 'GroupName' => 'g.Name', 'GroupDescription' => 'g.Description', + 'PrimaryGroup' => 'IF(u.PrimaryGroupId = %1$s.GroupId, 1, 0)', ), ), @@ -104,13 +105,17 @@ 'PortalUserId' => Array('type' => 'int', 'not_null' => 1, 'default' => 0), 'GroupId' => Array('type' => 'int', 'not_null' => 1, 'default' => 0), 'MembershipExpires' => Array('type' => 'int', 'formatter' => 'kDateFormatter', 'default' => null), - 'PrimaryGroup' => Array('type' => 'int', 'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_Yes', 0 => 'la_No'), 'use_phrases' => 1, 'not_null' => 1, 'default' => 1), 'ExpirationReminderSent' => Array('type' => 'int', 'not_null' => 1, 'default' => 0), ), 'VirtualFields' => Array ( 'GroupName' => Array('type' => 'string', 'default' => ''), 'GroupDescription' => Array('type' => 'string', 'default' => ''), + 'PrimaryGroup' => Array( + 'type' => 'int', + 'formatter' => 'kOptionsFormatter', 'options' => Array (1 => 'la_Yes', 0 => 'la_No'), 'use_phrases' => 1, + 'not_null' => 1, 'default' => 1 + ), ), 'Grids' => Array( @@ -129,4 +134,4 @@ ), ), - ); \ No newline at end of file + ); Index: core/units/helpers/user_helper.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/units/helpers/user_helper.php (revision 5818) +++ core/units/helpers/user_helper.php (revision ) @@ -189,6 +189,13 @@ $groups = Array(); } + if ( $object->GetDBField('UserType') == UserType::USER ) { + array_push($groups, $this->Application->ConfigValue('User_NewGroup') ); + } + elseif ( $object->GetDBField('UserType') == UserType::ADMIN ) { + array_push($groups, $this->Application->ConfigValue('User_AdminGroup') ); + } + // store groups, because kApplication::CheckPermission will use them! array_push($groups, $this->Application->ConfigValue('User_LoggedInGroup') ); $this->Application->StoreVar( 'UserGroups', implode(',', $groups), true ); // true for optional Index: core/admin_templates/users/admins_edit.tpl IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/admin_templates/users/admins_edit.tpl (revision 5818) +++ core/admin_templates/users/admins_edit.tpl (revision ) @@ -60,7 +60,9 @@ - + + +
@@ -74,4 +76,4 @@
- \ No newline at end of file + Index: admin/system_presets/simple/user_groups_u-ug.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- admin/system_presets/simple/user_groups_u-ug.php (revision 5818) +++ admin/system_presets/simple/user_groups_u-ug.php (revision ) @@ -18,7 +18,7 @@ // fields to hide $hidden_fields = Array ( - /* 'PortalUserId', 'GroupId', 'MembershipExpires', 'PrimaryGroup', 'ExpirationReminderSent', */ + /* 'PortalUserId', 'GroupId', 'MembershipExpires', 'ExpirationReminderSent', */ ); // virtual fields to hide @@ -28,7 +28,7 @@ // fields to make required $required_fields = Array ( - /* 'PortalUserId', 'GroupId', 'MembershipExpires', 'PrimaryGroup', 'ExpirationReminderSent', */ + /* 'PortalUserId', 'GroupId', 'MembershipExpires', 'ExpirationReminderSent', */ ); // virtual fields to make required Index: core/install/english.lang IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- core/install/english.lang (revision 5818) +++ core/install/english.lang (revision ) @@ -1525,6 +1525,7 @@ VXBkYXRpbmcgQ29uZmlndXJhdGlvbg== VXBsb2Fk VXNlIENyb24gdG8gcnVuIEFnZW50cw== + QXNzaWduIGFkbWluaXN0cmF0b3JzIHRvIGdyb3Vw QWxsb3cgbmV3IHVzZXIgcmVnaXN0cmF0aW9u QXNzaWduIEFsbCBVc2VycyBUbyBHcm91cA== QXNzaWduIHVzZXJzIG5vdCBsb2dnZWQgaW4gdG8gZ3JvdXA=