Proyectos de Subversion LeadersLinked - Services

Rev

Rev 280 | Rev 291 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 280 Rev 283
Línea 10... Línea 10...
10
use Laminas\Authentication\AuthenticationService;
10
use Laminas\Authentication\AuthenticationService;
11
use Laminas\Authentication\Result as AuthResult;
11
use Laminas\Authentication\Result as AuthResult;
12
use Laminas\Mvc\Controller\AbstractActionController;
12
use Laminas\Mvc\Controller\AbstractActionController;
13
use Laminas\View\Model\JsonModel;
13
use Laminas\View\Model\JsonModel;
Línea -... Línea 14...
-
 
14
 
14
 
15
 
15
use LeadersLinked\Form\Auth\SigninForm;
16
use LeadersLinked\Form\Auth\SigninForm;
16
use LeadersLinked\Form\Auth\ResetPasswordForm;
17
use LeadersLinked\Form\Auth\ResetPasswordForm;
17
use LeadersLinked\Form\Auth\ForgotPasswordForm;
18
use LeadersLinked\Form\Auth\ForgotPasswordForm;
Línea 48... Línea 49...
48
use LeadersLinked\Mapper\JwtTokenMapper;
49
use LeadersLinked\Mapper\JwtTokenMapper;
49
use Firebase\JWT\JWT;
50
use Firebase\JWT\JWT;
50
use Firebase\JWT\Key;
51
use Firebase\JWT\Key;
51
use LeadersLinked\Form\Auth\SigninDebugForm;
52
use LeadersLinked\Form\Auth\SigninDebugForm;
52
use LeadersLinked\Library\ExternalCredentials;
53
use LeadersLinked\Library\ExternalCredentials;
53
//use LeadersLinked\Library\Storage;
54
use LeadersLinked\Library\Storage;
Línea 54... Línea 55...
54
 
55
 
55
 
56
 
56
 
-
 
57
class AuthController extends AbstractActionController
57
 
Línea 58... Línea 58...
58
{
58
class AuthController extends AbstractActionController
59
    const _USE_S3 = false;
59
{
60
    
60
 
61
    
61
    
Línea 588... Línea 588...
588
                $replace_main = false;
588
                $replace_main = false;
589
            } else {
589
            } else {
590
                $replace_main = true;
590
                $replace_main = true;
591
            }
591
            }
Línea 592... Línea -...
592
            
-
 
593
            if(self::_USE_S3) {
-
 
594
            /*
592
            
595
                
593
 
596
                $storage = Storage::getInstance($this->config);
594
                $storage = Storage::getInstance($this->config);
Línea 597... Línea 595...
597
                $path = $storage->getPathNetwork();
595
                $path = $storage->getPathNetwork();
598
                
596
                
Línea 610... Línea 608...
610
                
608
                
611
                if($currentNetwork->favico) {
609
                if($currentNetwork->favico) {
612
                    $favico_url = $storage->getGenericImage($path, $currentNetwork->uuid, $currentNetwork->favico);
610
                    $favico_url = $storage->getGenericImage($path, $currentNetwork->uuid, $currentNetwork->favico);
613
                } else {
611
                } else {
614
                    $favico_url = '';
-
 
615
                }*/
-
 
616
            } else {
-
 
617
                $logo_url = $this->url()->fromRoute('storage-network', ['type' => 'logo'],['force_canonical' => true]);
-
 
618
                if($replace_main) {
-
 
619
                    $logo_url = str_replace($currentNetwork->main_hostname, $currentNetwork->service_hostname, $logo_url);
612
                    $favico_url = '';
620
                }
-
 
621
                
613
                }
622
                
-
 
623
                
-
 
624
                
-
 
625
                if($currentNetwork->alternative_hostname) {
-
 
626
                    $logo_url = str_replace($currentNetwork->alternative_hostname, $currentNetwork->service_hostname, $logo_url);
-
 
627
                    
-
 
628
                }
-
 
629
                
-
 
630
                
-
 
631
                $navbar_url = $this->url()->fromRoute('storage-network', ['type' => 'navbar'],['force_canonical' => true]);
-
 
632
                if($replace_main) {
-
 
633
                    $navbar_url = str_replace($currentNetwork->main_hostname, $currentNetwork->service_hostname, $navbar_url);
-
 
634
                }
-
 
635
                if($currentNetwork->alternative_hostname) {
-
 
636
                    $navbar_url = str_replace($currentNetwork->alternative_hostname, $currentNetwork->service_hostname, $navbar_url);
-
 
637
                    
-
 
638
                }
-
 
639
                
-
 
640
                
-
 
641
                $favico_url= $this->url()->fromRoute('storage-network', ['type' => 'favico'],['force_canonical' => true]);
-
 
642
                if($replace_main) {
-
 
643
                    $favico_url = str_replace($currentNetwork->main_hostname, $currentNetwork->service_hostname, $favico_url);
-
 
644
                }
-
 
645
                if($currentNetwork->alternative_hostname) {
-
 
646
                    $favico_url = str_replace($currentNetwork->alternative_hostname, $currentNetwork->service_hostname, $favico_url);
-
 
647
                    
-
 
648
                }
-
 
649
            }
-
 
Línea 650... Línea 614...
650
            
614
           
651
 
615
 
Línea 1659... Línea 1623...
1659
            ];
1623
            ];
1660
        }
1624
        }
Línea 1661... Línea 1625...
1661
 
1625
 
1662
        return new JsonModel($response);
1626
        return new JsonModel($response);
1663
    }
-
 
1664
 
1627
    }
1665
 
-
 
1666
 
1628
    
1667
    public function onroomAction()
1629
    public function onroomAction()
1668
    {
1630
    {
1669
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
1631
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
1670
        $currentNetwork  = $currentNetworkPlugin->getNetwork();
1632
        $currentNetwork  = $currentNetworkPlugin->getNetwork();
1671
 
1633
        
1672
 
1634
        
1673
 
1635
        
1674
        $request = $this->getRequest();
1636
        $request = $this->getRequest();
1675
 
1637
        
1676
        if ($request->isPost()) {
1638
        if ($request->isPost()) {
1677
 
1639
            
1678
            $dataPost = $request->getPost()->toArray();
1640
            $dataPost = $request->getPost()->toArray();
1679
 
1641
            
1680
 
1642
            
1681
            $form = new  MoodleForm();
1643
            $form = new  MoodleForm();
1682
            $form->setData($dataPost);
1644
            $form->setData($dataPost);
1683
            if ($form->isValid()) {
1645
            if ($form->isValid()) {
1684
 
1646
                
1685
                $dataPost   = (array) $form->getData();
1647
                $dataPost   = (array) $form->getData();
1686
                $username   = $dataPost['username'];
1648
                $username   = $dataPost['username'];
1687
                $password   = $dataPost['password'];
1649
                $password   = $dataPost['password'];
1688
                $timestamp  = $dataPost['timestamp'];
1650
                $timestamp  = $dataPost['timestamp'];
1689
                $rand       = $dataPost['rand'];
1651
                $rand       = $dataPost['rand'];
1690
                $data       = $dataPost['data'];
1652
                $data       = $dataPost['data'];
1691
 
1653
                
1692
                $config_username    = $this->config['leaderslinked.moodle.username'];
1654
                $config_username    = $this->config['leaderslinked.moodle.username'];
1693
                $config_password    = $this->config['leaderslinked.moodle.password'];
1655
                $config_password    = $this->config['leaderslinked.moodle.password'];
1694
                $config_rsa_n       = $this->config['leaderslinked.moodle.rsa_n'];
1656
                $config_rsa_n       = $this->config['leaderslinked.moodle.rsa_n'];
1695
                $config_rsa_d       = $this->config['leaderslinked.moodle.rsa_d'];
1657
                $config_rsa_d       = $this->config['leaderslinked.moodle.rsa_d'];
1696
                $config_rsa_e       = $this->config['leaderslinked.moodle.rsa_e'];
1658
                $config_rsa_e       = $this->config['leaderslinked.moodle.rsa_e'];
1697
 
1659
                
1698
 
1660
                
1699
 
1661
                
1700
 
1662
                
1701
                if (empty($username) || empty($password) || empty($timestamp) || empty($rand) || !is_integer($rand)) {
1663
                if (empty($username) || empty($password) || empty($timestamp) || empty($rand) || !is_integer($rand)) {
1702
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY1']);
1664
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY1']);
1703
                    exit;
1665
                    exit;
1704
                }
1666
                }
1705
 
1667
                
1706
                if ($username != $config_username) {
1668
                if ($username != $config_username) {
1707
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY2']);
1669
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY2']);
1708
                    exit;
1670
                    exit;
1709
                }
1671
                }
1710
 
1672
                
1711
                $dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $timestamp);
1673
                $dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $timestamp);
1712
                if (!$dt) {
1674
                if (!$dt) {
1713
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY3']);
1675
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY3']);
1714
                    exit;
1676
                    exit;
1715
                }
1677
                }
1716
 
1678
                
1717
                $t0 = $dt->getTimestamp();
1679
                $t0 = $dt->getTimestamp();
1718
                $t1 = strtotime('-5 minutes');
1680
                $t1 = strtotime('-5 minutes');
1719
                $t2 = strtotime('+5 minutes');
1681
                $t2 = strtotime('+5 minutes');
1720
 
1682
                
1721
                if ($t0 < $t1 || $t0 > $t2) {
1683
                if ($t0 < $t1 || $t0 > $t2) {
1722
                    //echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY4']) ;
1684
                    //echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY4']) ;
1723
                    //exit;
1685
                    //exit;
1724
                }
1686
                }
1725
 
1687
                
1726
                if (!password_verify($username . '-' . $config_password . '-' . $rand . '-' . $timestamp, $password)) {
1688
                if (!password_verify($username . '-' . $config_password . '-' . $rand . '-' . $timestamp, $password)) {
1727
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY5']);
1689
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY5']);
1728
                    exit;
1690
                    exit;
1729
                }
1691
                }
1730
 
1692
                
1731
                if (empty($data)) {
1693
                if (empty($data)) {
1732
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS1']);
1694
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS1']);
1733
                    exit;
1695
                    exit;
1734
                }
1696
                }
1735
 
1697
                
1736
                $data = base64_decode($data);
1698
                $data = base64_decode($data);
1737
                if (empty($data)) {
1699
                if (empty($data)) {
1738
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS2']);
1700
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS2']);
1739
                    exit;
1701
                    exit;
1740
                }
1702
                }
1741
 
1703
                
1742
 
1704
                
1743
                try {
1705
                try {
1744
                    $rsa = Rsa::getInstance();
1706
                    $rsa = Rsa::getInstance();
1745
                    $data = $rsa->decrypt($data,  $config_rsa_d,  $config_rsa_n);
1707
                    $data = $rsa->decrypt($data,  $config_rsa_d,  $config_rsa_n);
1746
                } catch (\Throwable $e) {
1708
                } catch (\Throwable $e) {
1747
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS3']);
1709
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS3']);
1748
                    exit;
1710
                    exit;
1749
                }
1711
                }
1750
 
1712
                
1751
                $data = (array) json_decode($data);
1713
                $data = (array) json_decode($data);
1752
                if (empty($data)) {
1714
                if (empty($data)) {
1753
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS4']);
1715
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS4']);
1754
                    exit;
1716
                    exit;
1755
                }
1717
                }
1756
 
1718
                
1757
                $email      = isset($data['email']) ? Functions::sanitizeFilterString($data['email']) : '';
1719
                $email      = isset($data['email']) ? Functions::sanitizeFilterString($data['email']) : '';
1758
                $first_name = isset($data['first_name']) ? Functions::sanitizeFilterString($data['first_name']) : '';
1720
                $first_name = isset($data['first_name']) ? Functions::sanitizeFilterString($data['first_name']) : '';
1759
                $last_name  = isset($data['last_name']) ? Functions::sanitizeFilterString($data['last_name']) : '';
1721
                $last_name  = isset($data['last_name']) ? Functions::sanitizeFilterString($data['last_name']) : '';
1760
 
1722
                
1761
                if (!filter_var($email, FILTER_VALIDATE_EMAIL) || empty($first_name) || empty($last_name)) {
1723
                if (!filter_var($email, FILTER_VALIDATE_EMAIL) || empty($first_name) || empty($last_name)) {
1762
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS5']);
1724
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS5']);
1763
                    exit;
1725
                    exit;
1764
                }
1726
                }
1765
 
1727
                
1766
                $userMapper = UserMapper::getInstance($this->adapter);
1728
                $userMapper = UserMapper::getInstance($this->adapter);
1767
                $user = $userMapper->fetchOneByEmail($email);
1729
                $user = $userMapper->fetchOneByEmail($email);
1768
                if (!$user) {
1730
                if (!$user) {
1769
 
1731
                    
1770
 
1732
                    
1771
                    $user = new User();
1733
                    $user = new User();
1772
                    $user->network_id = $currentNetwork->id;
1734
                    $user->network_id = $currentNetwork->id;
1773
                    $user->blocked = User::BLOCKED_NO;
1735
                    $user->blocked = User::BLOCKED_NO;
1774
                    $user->email = $email;
1736
                    $user->email = $email;
Línea 1778... Línea 1740...
1778
                    $user->login_attempt = 0;
1740
                    $user->login_attempt = 0;
1779
                    $user->password = '-NO-PASSWORD-';
1741
                    $user->password = '-NO-PASSWORD-';
1780
                    $user->usertype_id = UserType::USER;
1742
                    $user->usertype_id = UserType::USER;
1781
                    $user->status = User::STATUS_ACTIVE;
1743
                    $user->status = User::STATUS_ACTIVE;
1782
                    $user->show_in_search = User::SHOW_IN_SEARCH_YES;
1744
                    $user->show_in_search = User::SHOW_IN_SEARCH_YES;
1783
 
1745
                    
1784
                    if ($userMapper->insert($user)) {
1746
                    if ($userMapper->insert($user)) {
1785
                        echo json_encode(['success' => false, 'data' => $userMapper->getError()]);
1747
                        echo json_encode(['success' => false, 'data' => $userMapper->getError()]);
1786
                        exit;
1748
                        exit;
1787
                    }
1749
                    }
Línea 1788... Línea 1750...
1788
                    
1750
                    
1789
                    $user = $userMapper->fetchOne($user->id);
1751
                    $user = $userMapper->fetchOne($user->id);
1790
 
1752
                    
1791
 
1753
                    
1792
 
1754
                    
1793
 
1755
                    
1794
                    $filename   = trim(isset($data['avatar_filename']) ? filter_var($data['avatar_filename'], FILTER_SANITIZE_EMAIL) : '');
1756
                    $filename   = trim(isset($data['avatar_filename']) ? filter_var($data['avatar_filename'], FILTER_SANITIZE_EMAIL) : '');
1795
                    $content    = isset($data['avatar_content']) ? Functions::sanitizeFilterString($data['avatar_content']) : '';
1757
                    $content    = isset($data['avatar_content']) ? Functions::sanitizeFilterString($data['avatar_content']) : '';
1796
 
1758
                    
1797
                    if ($filename && $content) {
1759
                    if ($filename && $content) {
1798
                        $source = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename;
1760
                        $source = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename;
Línea 1799... Línea 1761...
1799
                        try {
1761
                        try {
1800
                            
1762
                            
1801
                            
1763
                            
1802
                            file_put_contents($source, base64_decode($content));
1764
                            file_put_contents($source, base64_decode($content));
1803
                            if (file_exists($source)) {
1765
                            if (file_exists($source)) {
1804
                                list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
1766
                                list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
1805
 
1767
                                
1806
                                $target_filename    = 'user-' . uniqid() . '.png';
1768
                                $target_filename    = 'user-' . uniqid() . '.png';
1807
                                $crop_to_dimensions = true;
1769
                                $crop_to_dimensions = true;
1808
 
1770
                                
1809
                                $image = Image::getInstance($this->config);
1771
                                $image = Image::getInstance($this->config);
Línea 1810... Línea 1772...
1810
                                $target_path    = $image->getStorage()->getPathUser();
1772
                                $target_path    = $image->getStorage()->getPathUser();
1811
                                $unlink_source  = true;
1773
                                $unlink_source  = true;
1812
                                 
1774
                                
1813
                                
1775
                                
1814
                                if (!$image->uploadImageChangeSize($source, $target_path, $user->uuid, $target_filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
1776
                                if (!$image->uploadImageChangeSize($source, $target_path, $user->uuid, $target_filename, $target_width, $target_height, $crop_to_dimensions, $unlink_source)) {
1815
                                    return new JsonModel([
1777
                                    return new JsonModel([
1816
                                        'success'   => false,
1778
                                        'success'   => false,
1817
                                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1779
                                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1818
                                    ]);
1780
                                    ]);
1819
                                }
1781
                                }
1820
 
1782
                                
1821
                                $user->image = $target_filename;
1783
                                $user->image = $target_filename;
Línea 1827... Línea 1789...
1827
                                unlink($source);
1789
                                unlink($source);
1828
                            }
1790
                            }
1829
                        }
1791
                        }
1830
                    }
1792
                    }
1831
                }
1793
                }
1832
 
1794
                
1833
                $auth = new AuthEmailAdapter($this->adapter);
1795
                $auth = new AuthEmailAdapter($this->adapter);
1834
                $auth->setData($email);
1796
                $auth->setData($email);
1835
 
1797
                
1836
                $result = $auth->authenticate();
1798
                $result = $auth->authenticate();
1837
                if ($result->getCode() == AuthResult::SUCCESS) {
1799
                if ($result->getCode() == AuthResult::SUCCESS) {
1838
                    return $this->redirect()->toRoute('dashboard');
1800
                    return $this->redirect()->toRoute('dashboard');
1839
                } else {
1801
                } else {
1840
                    $message = $result->getMessages()[0];
1802
                    $message = $result->getMessages()[0];
Línea 1842... Línea 1804...
1842
                        'ERROR_USER_NOT_FOUND', 'ERROR_USER_EMAIL_HASNT_BEEN_VARIFIED', 'ERROR_USER_IS_BLOCKED',
1804
                        'ERROR_USER_NOT_FOUND', 'ERROR_USER_EMAIL_HASNT_BEEN_VARIFIED', 'ERROR_USER_IS_BLOCKED',
1843
                        'ERROR_USER_IS_INACTIVE', 'ERROR_ENTERED_PASS_INCORRECT_USER_IS_BLOCKED', 'ERROR_ENTERED_PASS_INCORRECT_2',
1805
                        'ERROR_USER_IS_INACTIVE', 'ERROR_ENTERED_PASS_INCORRECT_USER_IS_BLOCKED', 'ERROR_ENTERED_PASS_INCORRECT_2',
1844
                        'ERROR_ENTERED_PASS_INCORRECT_1'
1806
                        'ERROR_ENTERED_PASS_INCORRECT_1'
1845
                    ])) {
1807
                    ])) {
1846
                    }
1808
                    }
1847
 
1809
                    
1848
                    switch ($message) {
1810
                    switch ($message) {
1849
                        case 'ERROR_USER_NOT_FOUND':
1811
                        case 'ERROR_USER_NOT_FOUND':
1850
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Email no existe', ['ip' => Functions::getUserIP()]);
1812
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Email no existe', ['ip' => Functions::getUserIP()]);
1851
                            break;
1813
                            break;
1852
 
1814
                            
1853
                        case 'ERROR_USER_EMAIL_HASNT_BEEN_VARIFIED':
1815
                        case 'ERROR_USER_EMAIL_HASNT_BEEN_VARIFIED':
1854
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Email no verificado', ['ip' => Functions::getUserIP()]);
1816
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Email no verificado', ['ip' => Functions::getUserIP()]);
1855
                            break;
1817
                            break;
1856
 
1818
                            
1857
                        case 'ERROR_USER_IS_BLOCKED':
1819
                        case 'ERROR_USER_IS_BLOCKED':
1858
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Usuario bloqueado', ['ip' => Functions::getUserIP()]);
1820
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Usuario bloqueado', ['ip' => Functions::getUserIP()]);
1859
                            break;
1821
                            break;
1860
 
1822
                            
1861
                        case 'ERROR_USER_IS_INACTIVE':
1823
                        case 'ERROR_USER_IS_INACTIVE':
1862
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Usuario inactivo', ['ip' => Functions::getUserIP()]);
1824
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Usuario inactivo', ['ip' => Functions::getUserIP()]);
1863
                            break;
1825
                            break;
1864
 
1826
                            
1865
 
1827
                            
1866
                        case 'ERROR_ENTERED_PASS_INCORRECT_USER_IS_BLOCKED':
1828
                        case 'ERROR_ENTERED_PASS_INCORRECT_USER_IS_BLOCKED':
1867
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - 3er Intento Usuario bloqueado', ['ip' => Functions::getUserIP()]);
1829
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - 3er Intento Usuario bloqueado', ['ip' => Functions::getUserIP()]);
1868
                            break;
1830
                            break;
1869
 
1831
                            
1870
 
1832
                            
1871
                        case 'ERROR_ENTERED_PASS_INCORRECT_2':
1833
                        case 'ERROR_ENTERED_PASS_INCORRECT_2':
1872
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - 1er Intento', ['ip' => Functions::getUserIP()]);
1834
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - 1er Intento', ['ip' => Functions::getUserIP()]);
1873
                            break;
1835
                            break;
1874
 
1836
                            
1875
 
1837
                            
1876
                        case 'ERROR_ENTERED_PASS_INCORRECT_1':
1838
                        case 'ERROR_ENTERED_PASS_INCORRECT_1':
1877
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - 2do Intento', ['ip' => Functions::getUserIP()]);
1839
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - 2do Intento', ['ip' => Functions::getUserIP()]);
1878
                            break;
1840
                            break;
1879
 
1841
                            
1880
 
1842
                            
1881
                        default:
1843
                        default:
1882
                            $message = 'ERROR_UNKNOWN';
1844
                            $message = 'ERROR_UNKNOWN';
1883
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Error desconocido', ['ip' => Functions::getUserIP()]);
1845
                            $this->logger->err('Error de ingreso a LeadersLinked de ' . $email . ' - Error desconocido', ['ip' => Functions::getUserIP()]);
1884
                            break;
1846
                            break;
1885
                    }
1847
                    }
1886
 
1848
                    
1887
 
1849
                    
1888
 
1850
                    
1889
 
1851
                    
1890
                    return new JsonModel([
1852
                    return new JsonModel([
1891
                        'success'   => false,
1853
                        'success'   => false,
1892
                        'data'   => $message
1854
                        'data'   => $message
1893
                    ]);
1855
                    ]);
1894
                }
1856
                }
1895
            } else {
1857
            } else {
1896
                $messages = [];
1858
                $messages = [];
-
 
1859
                
-
 
1860
                
-
 
1861
                
-
 
1862
                $form_messages = (array) $form->getMessages();
-
 
1863
                foreach ($form_messages  as $fieldname => $field_messages) {
-
 
1864
                    
-
 
1865
                    $messages[$fieldname] = array_values($field_messages);
-
 
1866
                }
-
 
1867
                
-
 
1868
                return new JsonModel([
-
 
1869
                    'success'   => false,
-
 
1870
                    'data'   => $messages
-
 
1871
                ]);
-
 
1872
            }
-
 
1873
        } else {
-
 
1874
            $data = [
-
 
1875
                'success' => false,
-
 
1876
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
-
 
1877
            ];
-
 
1878
            
-
 
1879
            return new JsonModel($data);
-
 
1880
        }
-
 
1881
        
-
 
1882
        return new JsonModel($data);
-
 
1883
    }
-
 
1884
 
-
 
1885
 
-
 
1886
 
-
 
1887
    public function cesamsAction()
-
 
1888
    {
-
 
1889
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
-
 
1890
        $currentNetwork  = $currentNetworkPlugin->getNetwork();
-
 
1891
 
-
 
1892
        $request = $this->getRequest();
-
 
1893
 
-
 
1894
        if ($request->isPost()) {
-
 
1895
 
-
 
1896
            $dataPost = $request->getPost()->toArray();
-
 
1897
 
-
 
1898
 
-
 
1899
            $form = new  MoodleForm();
-
 
1900
            $form->setData($dataPost);
-
 
1901
            if ($form->isValid()) {
-
 
1902
 
-
 
1903
                $dataPost   = (array) $form->getData();
-
 
1904
                $username   = $dataPost['username'];
-
 
1905
                $password   = $dataPost['password'];
-
 
1906
                $timestamp  = $dataPost['timestamp'];
-
 
1907
                $rand       = $dataPost['rand'];
-
 
1908
                $data       = $dataPost['data'];
-
 
1909
 
-
 
1910
                $config_username    = $this->config['leaderslinked.moodle.username'];
-
 
1911
                $config_password    = $this->config['leaderslinked.moodle.password'];
-
 
1912
                $config_rsa_n       = $this->config['leaderslinked.moodle.rsa_n'];
-
 
1913
                $config_rsa_d       = $this->config['leaderslinked.moodle.rsa_d'];
-
 
1914
                $config_rsa_e       = $this->config['leaderslinked.moodle.rsa_e'];
-
 
1915
 
-
 
1916
 
-
 
1917
 
-
 
1918
 
-
 
1919
                if (empty($username) || empty($password) || empty($timestamp) || empty($rand) || !is_integer($rand)) {
-
 
1920
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY1']);
-
 
1921
                    exit;
-
 
1922
                }
-
 
1923
 
-
 
1924
                if ($username != $config_username) {
-
 
1925
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY2']);
-
 
1926
                    exit;
-
 
1927
                }
-
 
1928
 
-
 
1929
                $dt = \DateTime::createFromFormat('Y-m-d\TH:i:s', $timestamp);
-
 
1930
                if (!$dt) {
-
 
1931
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY3']);
-
 
1932
                    exit;
-
 
1933
                }
-
 
1934
 
-
 
1935
                $dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s',  gmdate('Y-m-d\TH:i:s'));
-
 
1936
                $dtMax = $dt->add(\DateInterval::createFromDateString('5 minutes'));
-
 
1937
                $dtMin = $dt->sub(\DateInterval::createFromDateString('5 minutes'));
-
 
1938
                
-
 
1939
                
-
 
1940
                $t0 = $dt->getTimestamp();
-
 
1941
                $t1 = $dtMin->getTimestamp();
-
 
1942
                $t2 = $dtMax->getTimestamp();
-
 
1943
                if ($t0 < $t1 || $t0 > $t2) {
-
 
1944
                    //echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY4']) ;
-
 
1945
                    //exit;
-
 
1946
                }
-
 
1947
 
-
 
1948
                if (!password_verify($username . '-' . $config_password . '-' . $rand . '-' . $timestamp, $password)) {
-
 
1949
                    echo json_encode(['success' => false, 'data' => 'ERROR_SECURITY5']);
-
 
1950
                    exit;
-
 
1951
                }
-
 
1952
 
-
 
1953
                if (empty($data)) {
-
 
1954
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS1']);
-
 
1955
                    exit;
-
 
1956
                }
-
 
1957
 
-
 
1958
                $data = base64_decode($data);
-
 
1959
                if (empty($data)) {
-
 
1960
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS2']);
-
 
1961
                    exit;
-
 
1962
                }
-
 
1963
 
-
 
1964
 
-
 
1965
                try {
-
 
1966
                    $rsa = Rsa::getInstance();
-
 
1967
                    $data = $rsa->decrypt($data,  $config_rsa_d,  $config_rsa_n);
-
 
1968
                } catch (\Throwable $e) {
-
 
1969
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS3']);
-
 
1970
                    exit;
-
 
1971
                }
-
 
1972
 
-
 
1973
                $data = (array) json_decode($data);
-
 
1974
                if (empty($data)) {
-
 
1975
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS4']);
-
 
1976
                    exit;
-
 
1977
                }
-
 
1978
 
-
 
1979
                $email      = isset($data['email']) ? Functions::sanitizeFilterString($data['email']) : '';
-
 
1980
                $first_name = isset($data['first_name']) ? Functions::sanitizeFilterString($data['first_name']) : '';
-
 
1981
                $last_name  = isset($data['last_name']) ? Functions::sanitizeFilterString($data['last_name']) : '';
-
 
1982
                $password   = isset($data['password']) ? Functions::sanitizeFilterString($data['password']) : '';
-
 
1983
 
-
 
1984
                if (!filter_var($email, FILTER_VALIDATE_EMAIL) || empty($first_name) || empty($last_name) || empty($password)) {
-
 
1985
                    echo json_encode(['success' => false, 'data' => 'ERROR_PARAMETERS5']);
-
 
1986
                    exit;
-
 
1987
                }
-
 
1988
 
-
 
1989
                $userMapper = UserMapper::getInstance($this->adapter);
-
 
1990
                $user = $userMapper->fetchOneByEmail($email);
-
 
1991
                if (!$user) {
-
 
1992
 
-
 
1993
                    $user = new User();
-
 
1994
                    $user->network_id = $currentNetwork->id;
-
 
1995
                    $user->blocked = User::BLOCKED_NO;
-
 
1996
                    $user->email = $email;
-
 
1997
                    $user->email_verified = User::EMAIL_VERIFIED_YES;
-
 
1998
                    $user->first_name = $first_name;
-
 
1999
                    $user->last_name = $last_name;
-
 
2000
                    $user->login_attempt = 0;
-
 
2001
                    $user->password = password_hash($password, PASSWORD_DEFAULT);
-
 
2002
                    $user->usertype_id = UserType::USER;
-
 
2003
                    $user->status = User::STATUS_ACTIVE;
-
 
2004
                    $user->show_in_search = User::SHOW_IN_SEARCH_YES;
-
 
2005
                    
-
 
2006
 
-
 
2007
                    if ($userMapper->insert($user)) {
-
 
2008
                        echo json_encode(['success' => false, 'data' => $userMapper->getError()]);
-
 
2009
                        exit;
-
 
2010
                    }
-
 
2011
                    
-
 
2012
                    $user = $userMapper->fetchOne($user->id);
-
 
2013
                    
-
 
2014
                    $userPassword = new UserPassword();
-
 
2015
                    $userPassword->user_id = $user->id;
-
 
2016
                    $userPassword->password = password_hash($password, PASSWORD_DEFAULT);
-
 
2017
                    
-
 
2018
                    $userPasswordMapper = UserPasswordMapper::getInstance($this->adapter);
-
 
2019
                    $userPasswordMapper->insert($userPassword);
-
 
2020
                    
-
 
2021
                    $userDefaultForConnection = $userMapper->fetchOneDefaultForConnection();
-
 
2022
                    if($userDefaultForConnection) {
-
 
2023
                    
-
 
2024
                        $connection = new Connection();
-
 
2025
                        $connection->request_from = $userDefaultForConnection->id; 
-
 
2026
                        $connection->request_to = $user->id;
-
 
2027
                        $connection->status = Connection::STATUS_ACCEPTED;
-
 
2028
                        
-
 
2029
                        $connectionMapper = ConnectionMapper::getInstance($this->adapter);
-
 
2030
                        $connectionMapper->insert($connection);
-
 
2031
                    }
-
 
2032
                }
-
 
2033
 
-
 
2034
                return new JsonModel([
-
 
2035
                    'success'   => true,
-
 
2036
                    'data'   => $user->uuid
-
 
2037
                ]);
-
 
2038
        
-
 
2039
            } else {
-
 
2040
                $messages = [];
Línea 1897... Línea 2041...
1897
 
2041
 
1898
 
2042