Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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."
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* @package hubzero-cms
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/

use Hubzero\Content\Migration\Base;

// No direct access
defined('_HZEXEC_') or die();

/**
* Migration script for adding User - US plugin
**/
class Migration20260421000001PlgUserUs extends Base
{
/**
* Up
**/
public function up()
{
$this->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');
}
}
116 changes: 116 additions & 0 deletions core/plugins/user/us/us.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* @package hubzero-cms
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/

// No direct access
defined('_HZEXEC_') or die();

/**
* Plugin for automatically adding users to the location_us group based on IP country lookup
*/
class plgUserUs extends \Hubzero\Plugin\Plugin
{
public function onLoginUser($user, $options = array())
{
return $this->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();
}
}
}
16 changes: 16 additions & 0 deletions core/plugins/user/us/us.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="user">
<name>plg_user_us</name>
<author>HUBzero</author>
<authorUrl>hubzero.org</authorUrl>
<authorEmail>support@hubzero.org</authorEmail>
<copyright>Copyright (c) 2005-2020 The Regents of the University of California.</copyright>
<license>http://opensource.org/licenses/MIT MIT</license>
<description>PLG_USER_US_XML_DESCRIPTION</description>
<files>
<filename plugin="us">us.php</filename>
</files>
<languages>
<language tag="en-GB">language/en-GB/en-GB.plg_user_us.sys.ini</language>
</languages>
</extension>