Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1 | Rev 6749 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
/**
3
 * Controlador para manejar todo lo relacionado con las empresas
4
 *
5
 */
6
declare(strict_types=1);
7
 
8
namespace LeadersLinked\Controller;
9
 
10
use Laminas\Db\Adapter\AdapterInterface;
11
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
12
use Laminas\Mvc\Controller\AbstractActionController;
13
use Laminas\Log\LoggerInterface;
14
use Laminas\View\Model\JsonModel;
15
use LeadersLinked\Mapper\UserMapper;
16
use LeadersLinked\Library\Rsa;
17
 
18
class MoodleController extends AbstractActionController
19
{
20
    /**
21
     *
22
     * @var AdapterInterface
23
     */
24
    private $adapter;
25
 
26
 
27
    /**
28
     *
29
     * @var AbstractAdapter
30
     */
31
    private $cache;
32
 
33
    /**
34
     *
35
     * @var  LoggerInterface
36
     */
37
    private $logger;
38
 
39
 
40
    /**
41
     *
42
     * @var array
43
     */
44
    private $config;
45
 
46
 
47
 
48
    /**
49
     *
50
     * @param AdapterInterface $adapter
51
     * @param AbstractAdapter $cache
52
     * @param LoggerInterface $logger
53
     * @param array $config
54
     */
55
    public function __construct($adapter, $cache , $logger, $config)
56
    {
57
        $this->adapter      = $adapter;
58
        $this->cache        = $cache;
59
        $this->logger       = $logger;
60
        $this->config       = $config;
61
    }
62
 
63
    public function indexAction()
64
    {
65
        $request = $this->getRequest();
66
        if($request->isPost()) {
67
 
68
            $currentUserPlugin = $this->plugin('currentUserPlugin');
69
            $currentUser = $currentUserPlugin->getUser();
70
 
4842 efrain 71
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
72
            $currentNetwork = $currentNetworkPlugin->getNetwork();
1 www 73
 
4842 efrain 74
 
75
            if($currentNetwork->moodle_url) {
76
                $url = $currentNetwork->moodle_url;
1 www 77
            } else {
4842 efrain 78
 
79
 
80
                $sandbox = $this->config['leaderslinked.runmode.sandbox'];
81
 
82
                if($sandbox) {
83
                    $url = $this->config['leaderslinked.moodle.url_sandbox'];
84
                } else {
85
                    $url = $this->config['leaderslinked.moodle.url_production'];
86
                }
1 www 87
            }
88
 
89
            $username = $this->config['leaderslinked.moodle.username'];
90
            $password = $this->config['leaderslinked.moodle.password'];
91
            $rsa_n = $this->config['leaderslinked.moodle.rsa_n'];
92
            //$rsa_d = $this->config['leaderslinked.moodle.rsa_d'];
93
            $rsa_e = $this->config['leaderslinked.moodle.rsa_e'];
94
 
95
            $userMapper = UserMapper::getInstance($this->adapter);
96
            $user = $userMapper->fetchOne($currentUser->id);
97
 
98
            if($user->image) {
99
 
100
                $image = file_get_contents($this->config['leaderslinked.fullpath.user'] .$user->uuid.  DIRECTORY_SEPARATOR . '/' . $user->image);
101
 
102
            } else {
103
                $image = '';
104
            }
105
 
106
 
107
            $data = new \stdClass();
108
            $data->first_name = $user->first_name;
109
            $data->last_name = $user->last_name;
110
            $data->email = $user->email;
111
            $data->image_filename =  $user->image;
112
            $data->image_content = $image ? base64_encode($image) : '';
113
 
114
            $data = json_encode($data);
115
 
116
            list($usec, $sec) = explode(' ', microtime());
117
            $seed = intval($sec + ((float) $usec * 100000));
118
 
119
            $timestamp  = date('Y-m-d\TH:i:s');
120
            mt_srand($seed, MT_RAND_MT19937);
121
            $rand =  mt_rand();
122
            $password  = password_hash($username.'-'. $password. '-' . $rand. '-' . $timestamp, PASSWORD_DEFAULT);
123
 
124
            $rsa = Rsa::getInstance();
125
            $data = $rsa->encrypt($data, $rsa_e, $rsa_n);
126
 
127
            $response = [
128
                'success' => true,
129
                'data' => [
130
                    'url' => $url,
131
                    'username' => $username,
132
                    'password' => $password,
133
                    'rand' => $rand,
134
                    'timestamp' => $timestamp,
135
                    'data' => base64_encode($data),
136
 
137
                ]
138
            ];
139
 
140
 
141
            return new JsonModel($response);
142
 
143
        } else {
144
            $data = [
145
                'success' => false,
146
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
147
            ];
148
 
149
            return new JsonModel($data);
150
        }
151
 
152
        return new JsonModel($data);
153
    }
154
 
155
 
156
 
157
}
158