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\Facebook;
9:
10: /**
11: * Adds ability to work with Facebook.
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 = 'facebook';
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 Facebook 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']);
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: );
117: }
118:
119: if ($oUser && $oUser->isNormalOrTenant()) {
120: $oAccount = null;
121: $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator();
122: if ($oOAuthIntegratorWebclientDecorator) {
123: $oAccount = $oOAuthIntegratorWebclientDecorator->GetAccount($this->sService);
124: }
125: $aResult = array(
126: 'EnableModule' => $this->oModuleSettings->EnableModule,
127: 'Connected' => $oAccount ? true : false
128: );
129: $aArgs = array(
130: 'OAuthAccount' => $oAccount
131: );
132: }
133: $this->broadcastEvent('GetSettings', $aArgs, $aResult);
134:
135: return $aResult;
136: }
137:
138: /**
139: * Updates service settings.
140: *
141: * @param boolean $EnableModule **true** if module should be enabled.
142: * @param string $Id Service app identifier.
143: * @param string $Secret Service app secret.
144: *
145: * @throws \Aurora\System\Exceptions\ApiException
146: */
147: public function UpdateSettings($EnableModule, $Id, $Secret)
148: {
149: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin);
150:
151: try {
152: $this->setConfig('EnableModule', $EnableModule);
153: $this->setConfig('Id', $Id);
154: $this->setConfig('Secret', $Secret);
155: $this->saveModuleConfig();
156: } catch (\Exception $ex) {
157: throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::CanNotSaveSettings);
158: }
159:
160: return true;
161: }
162:
163: /**
164: * Deletes DropBox account.
165: *
166: * @return boolean
167: */
168: public function DeleteAccount()
169: {
170: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
171:
172: $bResult = false;
173: $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator();
174: if ($oOAuthIntegratorWebclientDecorator) {
175: $bResult = $oOAuthIntegratorWebclientDecorator->DeleteAccount($this->sService);
176: }
177:
178: return $bResult;
179: }
180: /***** public functions might be called with web API *****/
181: }
182: