Rev 1 | Rev 6849 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
/**
* Controlador para manejar todo lo relacionado con las empresas
*
*/
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Library\Rsa;
class MoodleController extends AbstractActionController
{
/**
*
* @var AdapterInterface
*/
private $adapter;
/**
*
* @var AbstractAdapter
*/
private $cache;
/**
*
* @var LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @param AdapterInterface $adapter
* @param AbstractAdapter $cache
* @param LoggerInterface $logger
* @param array $config
*/
public function __construct($adapter, $cache , $logger, $config)
{
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
}
public function indexAction()
{
$request = $this->getRequest();
if($request->isPost()) {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
$currentNetwork = $currentNetworkPlugin->getNetwork();
if($currentNetwork->moodle_url) {
$url = $currentNetwork->moodle_url;
} else {
$sandbox = $this->config['leaderslinked.runmode.sandbox'];
if($sandbox) {
$url = $this->config['leaderslinked.moodle.url_sandbox'];
} else {
$url = $this->config['leaderslinked.moodle.url_production'];
}
}
$username = $this->config['leaderslinked.moodle.username'];
$password = $this->config['leaderslinked.moodle.password'];
$rsa_n = $this->config['leaderslinked.moodle.rsa_n'];
//$rsa_d = $this->config['leaderslinked.moodle.rsa_d'];
$rsa_e = $this->config['leaderslinked.moodle.rsa_e'];
$userMapper = UserMapper::getInstance($this->adapter);
$user = $userMapper->fetchOne($currentUser->id);
if($user->image) {
$image = file_get_contents($this->config['leaderslinked.fullpath.user'] .$user->uuid. DIRECTORY_SEPARATOR . '/' . $user->image);
} else {
$image = '';
}
$data = new \stdClass();
$data->first_name = $user->first_name;
$data->last_name = $user->last_name;
$data->email = $user->email;
$data->image_filename = $user->image;
$data->image_content = $image ? base64_encode($image) : '';
$data = json_encode($data);
list($usec, $sec) = explode(' ', microtime());
$seed = intval($sec + ((float) $usec * 100000));
$timestamp = date('Y-m-d\TH:i:s');
mt_srand($seed, MT_RAND_MT19937);
$rand = mt_rand();
$password = password_hash($username.'-'. $password. '-' . $rand. '-' . $timestamp, PASSWORD_DEFAULT);
$rsa = Rsa::getInstance();
$data = $rsa->encrypt($data, $rsa_e, $rsa_n);
$response = [
'success' => true,
'data' => [
'url' => $url,
'username' => $username,
'password' => $password,
'rand' => $rand,
'timestamp' => $timestamp,
'data' => base64_encode($data),
]
];
return new JsonModel($response);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
}