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: * @package Modules
18: */
19: class Module extends \Aurora\System\Module\AbstractModule
20: {
21: protected $sService = 'facebook';
22:
23: protected $aRequireModules = array(
24: 'OAuthIntegratorWebclient'
25: );
26:
27: /***** private functions *****/
28: /**
29: * Initializes Facebook 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']);
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: );
91: }
92:
93: if (!empty($oUser) && $oUser->isNormalOrTenant()) {
94: $oAccount = null;
95: $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator();
96: if ($oOAuthIntegratorWebclientDecorator) {
97: $oAccount = $oOAuthIntegratorWebclientDecorator->GetAccount($this->sService);
98: }
99: $aResult = array(
100: 'EnableModule' => $this->getConfig('EnableModule', false),
101: 'Connected' => $oAccount ? true : false
102: );
103: $aArgs = array(
104: 'OAuthAccount' => $oAccount
105: );
106: }
107: $this->broadcastEvent('GetSettings', $aArgs, $aResult);
108:
109: return $aResult;
110: }
111:
112: /**
113: * Updates service settings.
114: *
115: * @param boolean $EnableModule **true** if module should be enabled.
116: * @param string $Id Service app identifier.
117: * @param string $Secret Service app secret.
118: *
119: * @throws \Aurora\System\Exceptions\ApiException
120: */
121: public function UpdateSettings($EnableModule, $Id, $Secret)
122: {
123: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin);
124:
125: try {
126: $this->setConfig('EnableModule', $EnableModule);
127: $this->setConfig('Id', $Id);
128: $this->setConfig('Secret', $Secret);
129: $this->saveModuleConfig();
130: } catch (\Exception $ex) {
131: throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::CanNotSaveSettings);
132: }
133:
134: return true;
135: }
136:
137: /**
138: * Deletes DropBox account.
139: *
140: * @return boolean
141: */
142: public function DeleteAccount()
143: {
144: \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser);
145:
146: $bResult = false;
147: $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator();
148: if ($oOAuthIntegratorWebclientDecorator) {
149: $bResult = $oOAuthIntegratorWebclientDecorator->DeleteAccount($this->sService);
150: }
151:
152: return $bResult;
153: }
154: /***** public functions might be called with web API *****/
155: }
156: