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
114 changes: 114 additions & 0 deletions core/plugins/user/d1/d1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?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 d1_nation group based on IP country group lookup
*/
class plgUserD1 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())
{
$approved = \Hubzero\User\Group::getInstance('d1_override');

if (is_object($approved) && $approved->isMember(User::get('id')))
{
Log::debug('plgUserD1: [' . User::get('username') . '] is in d1_override, removing from d1_nation and skipping geo check.');

$nation = \Hubzero\User\Group::getInstance('d1_nation');

if (is_object($nation))
{
$nation->remove('members', array(User::get('id')));
$nation->update();
}

return;
}

$ip = $_SERVER['REMOTE_ADDR'];

$gdb = \Hubzero\Geocode\Geocode::getGeoDBO();

if (!$gdb)
{
Log::debug('plgUserD1: geo database unavailable, skipping group update for [' . User::get('username') . '].');
return;
}

$gdb->setQuery(
"SELECT cg.countrygroup FROM ipcountry ic" .
" JOIN countrygroup cg ON ic.countrySHORT = cg.countrycode" .
" WHERE ic.ipfrom <= INET_ATON(" . $gdb->quote($ip) . ")" .
" AND ic.ipto >= INET_ATON(" . $gdb->quote($ip) . ")"
);
$countrygroup = $gdb->loadResult();

if (!$countrygroup)
{
return;
}

if ($countrygroup == 'D1')
{
Log::debug($ip . ' is in a D1 nation, adding [' . User::get('username') . '] to group [d1_nation].');

$group = \Hubzero\User\Group::getInstance('d1_nation');

if (is_object($group))
{
$group->add('members', array(User::get('id')));
$group->update();
}
else
{
Log::debug('group [d1_nation] does not exist, member addition failed.');
}
}
else
{
Log::debug($ip . ' has countrygroup [' . $countrygroup . '], leaving [' . User::get('username') . '] membership to group [d1_nation] unchanged.');
}
}

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('d1_nation');

if (is_object($group))
{
$group->remove('members', array($user['id']));
$group->update();
}
}
}
16 changes: 16 additions & 0 deletions core/plugins/user/d1/d1.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_d1</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_D1_XML_DESCRIPTION</description>
<files>
<filename plugin="d1">d1.php</filename>
</files>
<languages>
<language tag="en-GB">language/en-GB/en-GB.plg_user_d1.sys.ini</language>
</languages>
</extension>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PLG_USER_D1="User - D1"
PLG_USER_D1_XML_DESCRIPTION="Adds users to the d1_nation group on login when their IP resolves to a D1 country group via the ipcountry and countrygroup tables."
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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 - D1 plugin
**/
class Migration20260421000000PlgUserD1 extends Base
{
/**
* Up
**/
public function up()
{
$this->addPluginEntry('user', 'd1');

if ($this->db->tableExists('#__xgroups'))
{
foreach (array('d1_nation', 'd1_override') as $cn)
{
$this->db->setQuery(
"INSERT IGNORE INTO `#__xgroups` (cn, description, published, approved, type, join_policy, discoverability, created)" .
" VALUES (" . $this->db->quote($cn) . ", " . $this->db->quote($cn) . ", 1, 1, 1, 3, 1, NOW())"
);
$this->db->query();
}
}
}

/**
* Down
**/
public function down()
{
$this->deletePluginEntry('user', 'd1');

if ($this->db->tableExists('#__xgroups'))
{
$this->db->setQuery(
"DELETE FROM `#__xgroups` WHERE cn IN ('d1_nation', 'd1_override')"
);
$this->db->query();
}
}
}