diff --git a/core/plugins/user/us/language/en-GB/en-GB.plg_user_us.sys.ini b/core/plugins/user/us/language/en-GB/en-GB.plg_user_us.sys.ini new file mode 100644 index 00000000000..a9caf13907c --- /dev/null +++ b/core/plugins/user/us/language/en-GB/en-GB.plg_user_us.sys.ini @@ -0,0 +1,2 @@ +PLG_USER_US="User - US" +PLG_USER_US_XML_DESCRIPTION="Adds users to the location_us group on login when their IP resolves to a US address via the ipcountry table." diff --git a/core/plugins/user/us/migrations/Migration20260421000001PlgUserUs.php b/core/plugins/user/us/migrations/Migration20260421000001PlgUserUs.php new file mode 100644 index 00000000000..546cedf2a8d --- /dev/null +++ b/core/plugins/user/us/migrations/Migration20260421000001PlgUserUs.php @@ -0,0 +1,48 @@ +addPluginEntry('user', 'us'); + + if ($this->db->tableExists('#__xgroups')) + { + $this->db->setQuery( + "INSERT IGNORE INTO `#__xgroups` (`cn`, `published`, `approved`, `type`, `join_policy`, `discoverability`) + VALUES ('location_us', 1, 1, 1, 3, 1)" + ); + $this->db->query(); + } + } + + /** + * Down + **/ + public function down() + { + if ($this->db->tableExists('#__xgroups')) + { + $this->db->setQuery("DELETE FROM `#__xgroups` WHERE `cn` = 'location_us'"); + $this->db->query(); + } + + $this->deletePluginEntry('user', 'us'); + } +} diff --git a/core/plugins/user/us/us.php b/core/plugins/user/us/us.php new file mode 100644 index 00000000000..c18f9840c63 --- /dev/null +++ b/core/plugins/user/us/us.php @@ -0,0 +1,116 @@ +onUserLogin($user, $options); + } + + /** + * This method should handle any login logic and report back to the subject + * + * @param array $user holds the user data + * @param array $options holding options (remember, autoregister, group) + * @return bool + */ + public function onUserLogin($user, $options = array()) + { + $ip = $_SERVER['REMOTE_ADDR']; + + $d1 = \Hubzero\User\Group::getInstance('d1_nation'); + + if (is_object($d1) && in_array(User::get('id'), $d1->get('members'))) + { + Log::debug('[' . User::get('username') . '] is a member of [d1_nation], removing from group [location_us].'); + + $group = \Hubzero\User\Group::getInstance('location_us'); + + if (is_object($group)) + { + $group->remove('members', array(User::get('id'))); + $group->update(); + } + + return; + } + + $gdb = \Hubzero\Geocode\Geocode::getGeoDBO(); + + if (!$gdb) + { + Log::debug('plgUserUs: geo database unavailable, skipping group update for [' . User::get('username') . '].'); + return; + } + + $gdb->setQuery( + "SELECT countrySHORT FROM ipcountry" . + " WHERE ipfrom <= INET_ATON(" . $gdb->quote($ip) . ")" . + " AND ipto >= INET_ATON(" . $gdb->quote($ip) . ")" + ); + $country = $gdb->loadResult(); + + if ($country == 'US') + { + Log::debug($ip . ' is a US address, adding [' . User::get('username') . '] to group [location_us].'); + + $group = \Hubzero\User\Group::getInstance('location_us'); + + if (is_object($group)) + { + $group->add('members', array(User::get('id'))); + $group->update(); + } + else + { + Log::debug('group [location_us] does not exist, member addition failed.'); + } + } + else + { + Log::debug($ip . ' is not a US address, removing [' . User::get('username') . '] from group [location_us].'); + + $group = \Hubzero\User\Group::getInstance('location_us'); + + if (is_object($group)) + { + $group->remove('members', array(User::get('id'))); + $group->update(); + } + } + } + + public function onAfterDeleteUser($user, $success, $msg) + { + return $this->onUserAfterDelete($user, $success, $msg); + } + + /** + * Method is called after user data is deleted from the database + * + * @param array $user holds the user data + * @param bool $success true if user was successfully stored in the database + * @param string $msg message + */ + public function onUserAfterDelete($user, $success, $msg) + { + $group = \Hubzero\User\Group::getInstance('location_us'); + + if (is_object($group)) + { + $group->remove('members', array($user['id'])); + $group->update(); + } + } +} diff --git a/core/plugins/user/us/us.xml b/core/plugins/user/us/us.xml new file mode 100644 index 00000000000..c56cb2486fe --- /dev/null +++ b/core/plugins/user/us/us.xml @@ -0,0 +1,16 @@ + + + plg_user_us + HUBzero + hubzero.org + support@hubzero.org + Copyright (c) 2005-2020 The Regents of the University of California. + http://opensource.org/licenses/MIT MIT + PLG_USER_US_XML_DESCRIPTION + + us.php + + + language/en-GB/en-GB.plg_user_us.sys.ini + +