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: * @property Settings $oModuleSettings
18: *
19: * @package Modules
20: */
21: class Module extends \Aurora\System\Module\AbstractWebclientModule
22: {
23: protected $sService = 'dropbox';
24:
25: protected $aRequireModules = array(
26: 'OAuthIntegratorWebclient',
27: 'Dropbox'
28: );
29:
30: /**
31: * @return Module
32: */
33: public static function getInstance()
34: {
35: return parent::getInstance();
36: }
37:
38: /**
39: * @return Module
40: */
41: public static function Decorator()
42: {
43: return parent::Decorator();
44: }
45:
46: /**
47: * @return Settings
48: */
49: public function getModuleSettings()
50: {
51: return $this->oModuleSettings;
52: }
53:
54: /***** private functions *****/
55: protected function issetScope($sScope)
56: {
57: return in_array($sScope, explode(' ', $this->oModuleSettings->Scopes));
58: }
59:
60: /**
61: * Initializes DropBoxAuthWebclient Module.
62: *
63: * @ignore
64: */
65: public function init()
66: {
67: $this->subscribeEvent('OAuthIntegratorWebclient::GetServices::after', array($this, 'onAfterGetServices'));
68: $this->subscribeEvent('OAuthIntegratorAction', array($this, 'onOAuthIntegratorAction'));
69: $this->subscribeEvent('Dropbox::GetSettings', array($this, 'onGetSettings'));
70: $this->subscribeEvent('Dropbox::UpdateSettings::after', array($this, 'onAfterUpdateSettings'));
71: }
72:
73: /**
74: * Adds service name to array passed by reference.
75: *
76: * @ignore
77: * @param array $aArgs
78: * @param array $aServices Array with services names passed by reference.
79: */
80: public function onAfterGetServices($aArgs, &$aServices)
81: {
82: $oModule = \Aurora\Modules\Dropbox\Module::getInstance();
83: if ($oModule) {
84: $sId = $oModule->oModuleSettings->Id;
85: $sSecret = $oModule->oModuleSettings->Secret;
86:
87: if ($oModule->oModuleSettings->EnableModule && $this->issetScope('auth') && !empty($sId) && !empty($sSecret)) {
88: $aServices[] = $this->sService;
89: }
90: }
91: }
92:
93: /**
94: * Passes data to connect to service.
95: *
96: * @ignore
97: * @param string $aArgs Service type to verify if data should be passed.
98: * @param boolean|array $mResult variable passed by reference to take the result.
99: */
100: public function onOAuthIntegratorAction($aArgs, &$mResult)
101: {
102: if ($aArgs['Service'] === $this->sService) {
103: $sScopes = isset($_COOKIE['oauth-scopes']) ? $_COOKIE['oauth-scopes'] : '';
104: $mResult = false;
105: $oConnector = new Classes\Connector($this);
106: $oDropboxModule = \Aurora\Modules\Dropbox\Module::getInstance();
107: if ($oConnector) {
108: $mResult = $oConnector->Init(
109: $oDropboxModule->oModuleSettings->Id,
110: $oDropboxModule->oModuleSettings->Secret,
111: $sScopes
112: );
113: }
114: return true;
115: }
116: }
117:
118: /**
119: * Passes data to connect to service.
120: *
121: * @ignore
122: * @param string $aArgs Service type to verify if data should be passed.
123: * @param boolean|array $mResult variable passed by reference to take the result.
124: */
125: public function onGetSettings($aArgs, &$mResult)
126: {
127: $oUser = \Aurora\System\Api::getAuthenticatedUser();
128:
129: if ($oUser) {
130: $aScope = array(
131: 'Name' => 'auth',
132: 'Description' => $this->i18N('SCOPE_AUTH'),
133: 'Value' => false
134: );
135: if ($oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) {
136: $aScope['Value'] = $this->issetScope('auth');
137: $mResult['Scopes'][] = $aScope;
138: }
139: if ($oUser->isNormalOrTenant()) {
140: if ($aArgs['OAuthAccount'] instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount) {
141: $aScope['Value'] = $aArgs['OAuthAccount']->issetScope('auth');
142: }
143: if ($this->issetScope('auth')) {
144: $mResult['Scopes'][] = $aScope;
145: }
146: }
147: }
148: }
149:
150: public function onAfterUpdateSettings($aArgs, &$mResult)
151: {
152: $sScope = '';
153: if (isset($aArgs['Scopes']) && is_array($aArgs['Scopes'])) {
154: foreach ($aArgs['Scopes'] as $aScope) {
155: if ($aScope['Name'] === 'auth') {
156: if ($aScope['Value']) {
157: $sScope = 'auth';
158: break;
159: }
160: }
161: }
162: }
163: $this->setConfig('Scopes', $sScope);
164: $this->saveModuleConfig();
165: }
166: }
167: