1: <?php
2: /**
3: * This code is licensed under AGPLv3 license or Afterlogic Software License
4: * if commercial version of the product was purchased.
5: * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
6: */
7:
8: namespace Aurora\Modules\DropboxAuthWebclient;
9:
10: /**
11: * Adds ability to login using Dropbox account.
12: *
13: * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
14: * @license https://afterlogic.com/products/common-licensing Afterlogic Software License
15: * @copyright Copyright (c) 2023, Afterlogic Corp.
16: *
17: * @package Modules
18: */
19: class Module extends \Aurora\System\Module\AbstractWebclientModule
20: {
21: protected $sService = 'dropbox';
22:
23: protected $aRequireModules = array(
24: 'OAuthIntegratorWebclient',
25: 'Dropbox'
26: );
27:
28: /***** private functions *****/
29: protected function issetScope($sScope)
30: {
31: return in_array($sScope, explode(' ', $this->getConfig('Scopes')));
32: }
33:
34: /**
35: * Initializes DropBoxAuthWebclient Module.
36: *
37: * @ignore
38: */
39: public function init()
40: {
41: $this->subscribeEvent('OAuthIntegratorWebclient::GetServices::after', array($this, 'onAfterGetServices'));
42: $this->subscribeEvent('OAuthIntegratorAction', array($this, 'onOAuthIntegratorAction'));
43: $this->subscribeEvent('Dropbox::GetSettings', array($this, 'onGetSettings'));
44: $this->subscribeEvent('Dropbox::UpdateSettings::after', array($this, 'onAfterUpdateSettings'));
45: }
46:
47: /**
48: * Adds service name to array passed by reference.
49: *
50: * @ignore
51: * @param array $aArgs
52: * @param array $aServices Array with services names passed by reference.
53: */
54: public function onAfterGetServices($aArgs, &$aServices)
55: {
56: $oModule = \Aurora\System\Api::GetModule('Dropbox');
57: if ($oModule) {
58: $sId = $oModule->getConfig('Id', '');
59: $sSecret = $oModule->getConfig('Secret', '');
60:
61: if ($oModule->getConfig('EnableModule', false) && $this->issetScope('auth') && !empty($sId) && !empty($sSecret)) {
62: $aServices[] = $this->sService;
63: }
64: }
65: }
66:
67: /**
68: * Passes data to connect to service.
69: *
70: * @ignore
71: * @param string $aArgs Service type to verify if data should be passed.
72: * @param boolean|array $mResult variable passed by reference to take the result.
73: */
74: public function onOAuthIntegratorAction($aArgs, &$mResult)
75: {
76: if ($aArgs['Service'] === $this->sService) {
77: $sScopes = isset($_COOKIE['oauth-scopes']) ? $_COOKIE['oauth-scopes'] : '';
78: $mResult = false;
79: $oConnector = new Classes\Connector($this);
80: if ($oConnector) {
81: $mResult = $oConnector->Init(
82: \Aurora\System\Api::GetModule('Dropbox')->getConfig('Id'),
83: \Aurora\System\Api::GetModule('Dropbox')->getConfig('Secret'),
84: $sScopes
85: );
86: }
87: return true;
88: }
89: }
90:
91: /**
92: * Passes data to connect to service.
93: *
94: * @ignore
95: * @param string $aArgs Service type to verify if data should be passed.
96: * @param boolean|array $mResult variable passed by reference to take the result.
97: */
98: public function onGetSettings($aArgs, &$mResult)
99: {
100: $oUser = \Aurora\System\Api::getAuthenticatedUser();
101:
102: if (!empty($oUser)) {
103: $aScope = array(
104: 'Name' => 'auth',
105: 'Description' => $this->i18N('SCOPE_AUTH'),
106: 'Value' => false
107: );
108: if ($oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) {
109: $aScope['Value'] = $this->issetScope('auth');
110: $mResult['Scopes'][] = $aScope;
111: }
112: if ($oUser->isNormalOrTenant()) {
113: if ($aArgs['OAuthAccount'] instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount) {
114: $aScope['Value'] = $aArgs['OAuthAccount']->issetScope('auth');
115: }
116: if ($this->issetScope('auth')) {
117: $mResult['Scopes'][] = $aScope;
118: }
119: }
120: }
121: }
122:
123: public function onAfterUpdateSettings($aArgs, &$mResult)
124: {
125: $sScope = '';
126: if (isset($aArgs['Scopes']) && is_array($aArgs['Scopes'])) {
127: foreach ($aArgs['Scopes'] as $aScope) {
128: if ($aScope['Name'] === 'auth') {
129: if ($aScope['Value']) {
130: $sScope = 'auth';
131: break;
132: }
133: }
134: }
135: }
136: $this->setConfig('Scopes', $sScope);
137: $this->saveModuleConfig();
138: }
139: }
140: