Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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