Skip to content

Commit

Permalink
Add privacy and action log plugins for joomla 3.9.x compatibility #29 (
Browse files Browse the repository at this point in the history
…#30)

* Task #136034 feat: Actionlog plugin for GDPR compliance

* Task #136034 feat: Actionlog plugin for GDPR compliance

* Task #136034 feat: Actionlog plugin for GDPR compliance
  • Loading branch information
ankush-maherwal authored and manojLondhe committed Oct 26, 2018
1 parent bd4d79a commit 5881ff1
Show file tree
Hide file tree
Showing 16 changed files with 712 additions and 43 deletions.
6 changes: 4 additions & 2 deletions administrator/controllers/hierarchy.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function save($key = null, $urlVar = null)
$data['created_by'] = $jinput->get('created_by', '', 'int');
$data['modified_by'] = $jinput->get('modified_by', '', 'int');

// To add new or remove manager from the selected managers list.
// Get the existing managers of the user
$hierarchysData = $model->getReportsTo($data['user_id']);

$userIDs = array();
Expand All @@ -91,16 +91,18 @@ public function save($key = null, $urlVar = null)
$userIDs[] = $hierarchy->reports_to;
}

// Get the list of removed managers for the user
$deleteUser = array_diff($userIDs, $data['reports_to']);

// Delete user from the existing list
// Delete records for removed managers for the user
foreach ($deleteUser as $key => $val)
{
$this->hierarchyTableObj->load(array('reports_to' => (int) $val, 'user_id' => (int) $data['user_id']));
$id = $this->hierarchyTableObj->id;
$return = $model->delete($id);
}

// Save the records for new managers of the user
foreach ($data['reports_to'] as $key => $val)
{
$data['reports_to'] = $val;
Expand Down
23 changes: 19 additions & 4 deletions administrator/controllers/hierarchys.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function csvImport()
* Get user id from user email
*
* @param string $email email.
*
*
* @return void
*
* @since 1.0
Expand All @@ -159,12 +159,27 @@ public function getUserId($email)
*/
public function remove()
{
$model = $this->getModel('hierarchys');
$model = $this->getModel();
$input = JFactory::getApplication()->input;
$post = $input->post;
$hierarchyID = $post->get('cid', '', 'ARRAY');
$userIds = $post->get('cid', '', 'ARRAY');

$records = array();

foreach ($userIds as $userId)
{
$hierarchyData = $model->getReportsTo($userId);
$records = array_merge($records, $hierarchyData);
}

$hierarchyIds = array();

foreach ($records as $record)
{
$hierarchyIds[] = $record->id;
}

if ($model->delete($hierarchyID))
if ($model->delete($hierarchyIds))
{
$msg = JText::_('COM_HIERARCHY_HIERARCHY_DELETED_SCUSS');
}
Expand Down
93 changes: 90 additions & 3 deletions administrator/models/hierarchy.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,24 @@ public function save($data)
$data['created_date'] = $date->toSql(true);
}

$isNew = empty($data['id']) ? false : true;

// On before assigning manager
$dispatcher = JDispatcher::getInstance();
JPluginHelper::importPlugin("system");
JPluginHelper::importPlugin("actionlog");
$dispatcher->trigger("hierarchyOnBeforeSaveHierarchy", array($data, $isNew));

if (parent::save($data))
{
$id = (int) $this->getState($this->getName() . '.id');

// On after assigning manager
$dispatcher = JDispatcher::getInstance();
JPluginHelper::importPlugin("system");
JPluginHelper::importPlugin("actionlog");
$dispatcher->trigger("hierarchyOnAfterSaveHierarchy", array($data, $isNew));

return $id;
}

Expand Down Expand Up @@ -313,9 +327,9 @@ public function getReportsTo($reportsTo)
return false;
}

$db = JFactory::getDBO();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('user_id','reports_to','name','username','email')));
$query->select($db->quoteName(array('hu.id', 'user_id','reports_to','name','username','email')));
$query->from($db->quoteName('#__hierarchy_users', 'hu'));
$query->join('INNER', $db->quoteName('#__users', 'u') . ' ON (' . $db->quoteName('u.id') . ' = ' . $db->quoteName('hu.reports_to') . ')');
$query->where($db->quoteName('hu.user_id') . " = " . $db->quote($reportsTo));
Expand All @@ -326,7 +340,7 @@ public function getReportsTo($reportsTo)
}

/**
* Method to get users to manage hierarchy.
* Method to get users to manager hierarchy.
*
* @param integer $userId userId
*
Expand Down Expand Up @@ -363,4 +377,77 @@ public function getAutoSuggestUsers($userId)

return $allUsers;
}

/**
* Method to delete a row from the database table by primary key value.
*
* @param array $items An array of primary key value to delete.
*
* @return int Returns count of success
*/
public function delete($items)
{
$user = JFactory::getUser();
$db = JFactory::getDbo();

JTable::addIncludePath(JPATH_ROOT . '/administrator/components/com_hierarchy/tables');

if (is_array($items))
{
foreach ($items as $id)
{
$hierarchyTable = JTable::getInstance('Hierarchy', 'HierarchyTable', array('dbo', $db));
$hierarchyTable->load(array('id' => $id));

$data = $hierarchyTable->getProperties();

// On before removing manager
JPluginHelper::importPlugin("system");
JPluginHelper::importPlugin("actionlog");
$dispatcher = JDispatcher::getInstance();
$dispatcher->trigger("hierarchyOnBeforeDeleteHierarchy", array($data));

if ($hierarchyTable->delete($data['id']))
{
// On after removing manager
JPluginHelper::importPlugin("system");
JPluginHelper::importPlugin("actionlog");
$dispatcher = JDispatcher::getInstance();
$dispatcher->trigger("hierarchyOnAfterDeleteHierarchy", array($data));
}
else
{
return false;
}
}
}
else
{
$hierarchyTable = JTable::getInstance('Hierarchy', 'HierarchyTable', array('dbo', $db));
$hierarchyTable->load(array('id' => $items));

$data = $hierarchyTable->getProperties();

// On before removing manager
$dispatcher = JDispatcher::getInstance();
JPluginHelper::importPlugin("system");
JPluginHelper::importPlugin("actionlog");
$dispatcher->trigger("hierarchyOnBeforeDeleteHierarchy", array($data));

if ($hierarchyTable->delete($data['id']))
{
// On after removing manager
$dispatcher = JDispatcher::getInstance();
JPluginHelper::importPlugin("system");
JPluginHelper::importPlugin("actionlog");
$dispatcher->trigger("hierarchyOnAfterDeleteHierarchy", array($data));
}
else
{
return false;
}
}

return true;
}
}
33 changes: 0 additions & 33 deletions administrator/models/hierarchys.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,37 +190,4 @@ public function getItems()

return $items;
}

/**
* Delete order
*
* @param integer $hierarchyID id of jticketing_order table to delete
*
* @return void
*
* @since 1.0
*/
public function delete($hierarchyID)
{
$id = implode(',', array_filter($hierarchyID));

if ($id)
{
// Delete the order item
$deleteHierarchy = $this->_db->getQuery(true);
$deleteHierarchy->delete($this->_db->quoteName('#__hierarchy_users'));
$deleteHierarchy->where('user_id IN (' . $id . ')');
$this->_db->setQuery($deleteHierarchy);
$confirm = $this->_db->execute();

if ($confirm)
{
return true;
}
else
{
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion administrator/views/hierarchys/tmpl/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
<?php
$clientUrl = '';

// Client and client_id is passed to the form URL
// Client and client_id is passed to the form URL
if ($this->client && $this->clientId)
{
$clientUrl = '&client=' . $this->client . '&client_id=' . $this->clientId;
Expand Down
152 changes: 152 additions & 0 deletions plugins/actionlog/hierarchy/hierarchy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php
/**
* @package Hierarchy
* @subpackage Plg_Actionlog_Hierarchy
*
* @author Techjoomla <[email protected]>
* @copyright Copyright (c) 2009-2018 Techjoomla. All rights reserved.
* @license GNU General Public License version 2 or later.
*/

// No direct access.
defined('_JEXEC') or die();

JLoader::register('ActionlogsHelper', JPATH_ADMINISTRATOR . '/components/com_actionlogs/helpers/actionlogs.php');

use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;

/**
* Hierarchy Actions Logging Plugin.
*
* @since __DEPLOY_VERSION__
*/
class PlgActionlogHierarchy extends CMSPlugin
{
/**
* Application object.
*
* @var JApplicationCms
* @since __DEPLOY_VERSION__
*/
protected $app;

/**
* Database object.
*
* @var JDatabaseDriver
* @since __DEPLOY_VERSION__
*/
protected $db;

/**
* Load plugin language file automatically so that it can be used inside component
*
* @var boolean
* @since __DEPLOY_VERSION__
*/
protected $autoloadLanguage = true;

/**
* Proxy for ActionlogsModelUserlog addLog method
*
* This method adds a record to #__action_logs contains (message_language_key, message, date, context, user)
*
* @param array $messages The contents of the messages to be logged
* @param string $messageLanguageKey The language key of the message
* @param string $context The context of the content passed to the plugin
* @param int $userId ID of user perform the action, usually ID of current logged in user
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function addLog($messages, $messageLanguageKey, $context, $userId = null)
{
JLoader::register('ActionlogsModelActionlog', JPATH_ADMINISTRATOR . '/components/com_actionlogs/models/actionlog.php');

/* @var ActionlogsModelActionlog $model */
$model = BaseDatabaseModel::getInstance('Actionlog', 'ActionlogsModel');
$model->addLog($messages, $messageLanguageKey, $context, $userId);
}

/**
* On after manager assigned to the user
*
* @param ARRAY $data hierarchy data
* @param BOOLEAN $isNew flag for new hierarchy
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function hierarchyOnAfterSaveHierarchy($data, $isNew)
{
if (!$this->params->get('logActionForAssigningManager', 1) || !$isNew)
{
return;
}

$option = $this->app->input->getCmd('option');
$actor = Factory::getUser();
$manager = Factory::getUser($data['reports_to']);
$user = Factory::getUser($data['user_id']);
$action = 'assignmanager';
$userId = $actor->id;
$userName = $actor->username;

$messageLanguageKey = 'PLG_ACTIONLOG_HIERARCHY_ASSIGN_MANAGER';

$message = array(
'action' => $action,
'actorAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $userId,
'actorName' => $userName,
'managerForUser' => $manager->username,
'managerForUserAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $manager->id,
'user' => $user->username,
'userAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id
);

$this->addLog(array($message), $messageLanguageKey, $option, $userId);
}

/**
* On after manager removed for the user
*
* @param ARRAY $data hierarchy data
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function hierarchyOnAfterDeleteHierarchy($data)
{
if (!$this->params->get('logActionForRemoveManager', 1) || empty($data['id']))
{
return;
}

$option = $this->app->input->getCmd('option');
$actor = Factory::getUser();
$manager = Factory::getUser($data['reports_to']);
$user = Factory::getUser($data['user_id']);
$action = 'assignmanager';
$userId = $actor->id;
$userName = $actor->username;

$messageLanguageKey = 'PLG_ACTIONLOG_HIERARCHY_REMOVE_MANAGER';

$message = array(
'action' => $action,
'actorAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $userId,
'actorName' => $userName,
'managerForUser' => $manager->username,
'managerForUserAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $manager->id,
'user' => $user->username,
'userAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id
);

$this->addLog(array($message), $messageLanguageKey, $option, $userId);
}
}
Loading

0 comments on commit 5881ff1

Please sign in to comment.