Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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