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