q<?php
/*----------------------------------------------------------------------------------|  www.vdm.io  |----/
				Lmskaran 
/-------------------------------------------------------------------------------------------------------/

	@version		1.0.77
	@build			24th July, 2022
	@created		22nd July, 2020
	@package		Moojla
	@subpackage		module_permission.php
	@author			Lmskaran <http://Lmskaran.com>	
	@copyright		Copyright (C) 2015. All Rights Reserved
	@license		GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
  ____  _____  _____  __  __  __      __       ___  _____  __  __  ____  _____  _  _  ____  _  _  ____ 
 (_  _)(  _  )(  _  )(  \/  )(  )    /__\     / __)(  _  )(  \/  )(  _ \(  _  )( \( )( ___)( \( )(_  _)
.-_)(   )(_)(  )(_)(  )    (  )(__  /(__)\   ( (__  )(_)(  )    (  )___/ )(_)(  )  (  )__)  )  (   )(  
\____) (_____)(_____)(_/\/\_)(____)(__)(__)   \___)(_____)(_/\/\_)(__)  (_____)(_)\_)(____)(_)\_) (__) 

/------------------------------------------------------------------------------------------------------*/

// No direct access to this file
defined('_JEXEC') or die('Restricted access');


/***[JCBGUI.class_extends.head.1.$$$$]***/

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Plugin\CMSPlugin;
/***[/JCBGUI$$$$]***/


/***[JCBGUI.class_extends.comment.1.$$$$]***/
/**
 * System - Module_permission plugin.
 *
 * @package   Module_permission
 * @since     1.0.0
 *//***[/JCBGUI$$$$]***/

class PlgSystemModule_permission extends CMSPlugin
{

/***[JCBGUI.joomla_plugin.main_class_code.61.$$$$]***/
    public function __construct(&$subject, $config = array())
    {
        $this->gApp = JFactory::getApplication();
        parent::__construct($subject, $config);
    }

    public function onContentPrepareData($context, $data)
    {

        if ($context != 'com_modules.module')
            return true;

        $result= $this->authorize_user($data->id);

        if (!$result)
        {
            $app = JFactory::getApplication();
            $message = JText::_('JGLOBAL_AUTH_ACCESS_DENIED');
            $app->redirect(JRoute::_('index.php?option=com_modules&view=modules', false), $message, 'error');
        }

        return true;
    }

    public function authorize_user($module_id)
    {
        $module_id;
        $user= JFactory::getUser();
        $userid= $user->get('id', 0);

        if ($user->gid== 8)
            return true;

        foreach ((array)$this->params->get('user_module') as $item) {
            if ($item->user== $userid && $item->module_id== $module_id)
                return true;
        }
        
        return false;
    }
    /***[/JCBGUI$$$$]***/

}
