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\Google;
9:
10: /**
11: * Adds ability to work with Google.
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\AbstractModule
22: {
23: protected $sService = 'google';
24:
25: protected $aRequireModules = array(
26: 'OAuthIntegratorWebclient'
27: );
28:
29: /**
30: * @return Module
31: */
32: public static function getInstance()
33: {
34: return parent::getInstance();
35: }
36:
37: /**
38: * @return Module
39: */
40: public static function Decorator()
41: {
42: return parent::Decorator();
43: }
44:
45: /**
46: * @return Settings
47: */
48: public function getModuleSettings()
49: {
50: return $this->oModuleSettings;
51: }
52:
53: /***** private functions *****/
54: /**
55: * Initializes Google Module.
56: *
57: * @ignore
58: */
59: public function init()
60: {
61: $this->subscribeEvent('GetServicesSettings', array($this, 'onGetServicesSettings'));
62: $this->subscribeEvent('UpdateServicesSettings', array($this, 'onUpdateServicesSettings'));
63: }
64:
65: /**
66: * Adds service settings to array passed by reference.
67: *
68: * @ignore
69: * @param array $aServices Array with services settings passed by reference.
70: */
71: public function onGetServicesSettings(&$aServices)
72: {
73: $aSettings = $this->GetSettings();
74: if (!empty($aSettings)) {
75: $aServices[] = $aSettings;
76: }
77: }
78:
79: /**
80: * Updates service settings.
81: *
82: * @ignore
83: * @param array $aServices Array with new values for service settings.
84: *
85: * @throws \Aurora\System\Exceptions\ApiException
86: */
87: public function onUpdateServicesSettings($aServices)
88: {
89: $aSettings = $aServices[$this->sService];
90:
91: if (\is_array($aSettings)) {
92: $this->UpdateSettings($aSettings['EnableModule'], $aSettings['Id'], $aSettings['Secret'], $aSettings['Key']);
93: }
94: }
95: /***** private functions *****/
96:
97: /***** public functions might be called with web API *****/
98: /**
99: * Obtains list of module settings for authenticated user.
100: *
101: * @return array
102: */
103: public function GetSettings()
104: {
105: $aResult = array();
106: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous);
107:
108: $oUser = \Aurora\System\Api::getAuthenticatedUser();
109: if ($oUser && $oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) {
110: $aResult = array(
111: 'Name' => $this->sService,
112: 'DisplayName' => self::GetName(),
113: 'EnableModule' => $this->oModuleSettings->EnableModule,
114: 'Id' => $this->oModuleSettings->Id,
115: 'Secret' => $this->oModuleSettings->Secret,
116: 'Key' => $this->oModuleSettings->Key
117: );
118: }
119:
120: if ($oUser && $oUser->isNormalOrTenant()) {
121: $oAccount = null;
122: $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator();
123: if ($oOAuthIntegratorWebclientDecorator) {
124: $oAccount = $oOAuthIntegratorWebclientDecorator->GetAccount($this->sService);
125: }
126: $aResult = array(
127: 'EnableModule' => $this->oModuleSettings->EnableModule,
128: 'Connected' => $oAccount ? true : false,
129: 'AccountId' => $oAccount instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount ? $oAccount->Id : null
130: );
131: $aArgs = array(
132: 'OAuthAccount' => $oAccount
133: );
134: }
135: $this->broadcastEvent('GetSettings', $aArgs, $aResult);
136:
137: return $aResult;
138: }
139:
140: /**
141: * Updates service settings.
142: *
143: * @param boolean $EnableModule **true** if module should be enabled.
144: * @param string $Id Service app identifier.
145: * @param string $Secret Service app secret.
146: * @param string $Key Service app key.
147: *
148: * @throws \Aurora\System\Exceptions\ApiException
149: */
150: public function UpdateSettings($EnableModule, $Id, $Secret, $Key)
151: {
152: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin);
153:
154: try {
155: $this->setConfig('EnableModule', $EnableModule);
156: $this->setConfig('Id', $Id);
157: $this->setConfig('Secret', $Secret);
158: $this->setConfig('Key', $Key);
159: $this->saveModuleConfig();
160: } catch (\Exception $ex) {
161: throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::CanNotSaveSettings);
162: }
163:
164: return true;
165: }
166:
167: /**
168: * Deletes DropBox account.
169: *
170: * @return boolean
171: */
172: public function DeleteAccount()
173: {
174: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
175:
176: $bResult = false;
177: $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator();
178: if ($oOAuthIntegratorWebclientDecorator) {
179: $bResult = $oOAuthIntegratorWebclientDecorator->DeleteAccount($this->sService);
180: }
181:
182: return $bResult;
183: }
184: /***** public functions might be called with web API *****/
185: }
186: