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\FacebookAuthWebclient;
9:
10: /**
11: * Adds ability to login using Facebook 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 = 'facebook';
24:
25: protected $aRequireModules = array(
26: 'OAuthIntegratorWebclient',
27: 'Facebook'
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 FacebookAuthWebclient 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('Facebook::GetSettings', array($this, 'onGetSettings'));
70: $this->subscribeEvent('Facebook::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\Facebook\Module::getInstance();
83: $sId = $oModule->oModuleSettings->Id;
84: $sSecret = $oModule->oModuleSettings->Secret;
85:
86: if ($oModule->oModuleSettings->EnableModule && $this->issetScope('auth') && !empty($sId) && !empty($sSecret)) {
87: $aServices[] = $this->sService;
88: }
89: }
90:
91: /**
92: * Passes data to connect to service.
93: *
94: * @ignore
95: * @param string $aArgs Service type to verify if data should be passed.
96: * @param boolean|array $mResult variable passed by reference to take the result.
97: */
98: public function onOAuthIntegratorAction($aArgs, &$mResult)
99: {
100: if ($aArgs['Service'] === $this->sService) {
101: $sScopes = isset($_COOKIE['oauth-scopes']) ? $_COOKIE['oauth-scopes'] : '';
102: $mResult = false;
103: $oConnector = new Classes\Connector($this);
104: $oFacebookModule = \Aurora\Modules\Facebook\Module::getInstance();
105: if ($oConnector) {
106: $mResult = $oConnector->Init(
107: $oFacebookModule->oModuleSettings->Id,
108: $oFacebookModule->oModuleSettings->Secret,
109: $sScopes
110: );
111: }
112: return true;
113: }
114: }
115:
116: /**
117: * Passes data to connect to service.
118: *
119: * @ignore
120: * @param string $aArgs Service type to verify if data should be passed.
121: * @param boolean|array $mResult variable passed by reference to take the result.
122: */
123: public function onGetSettings($aArgs, &$mResult)
124: {
125: $oUser = \Aurora\System\Api::getAuthenticatedUser();
126:
127: if ($oUser) {
128: $aScope = array(
129: 'Name' => 'auth',
130: 'Description' => $this->i18N('SCOPE_AUTH'),
131: 'Value' => false
132: );
133: if ($oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) {
134: $aScope['Value'] = $this->issetScope('auth');
135: $mResult['Scopes'][] = $aScope;
136: }
137: if ($oUser->isNormalOrTenant()) {
138: if ($aArgs['OAuthAccount'] instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount) {
139: $aScope['Value'] = $aArgs['OAuthAccount']->issetScope('auth');
140: }
141: if ($this->issetScope('auth')) {
142: $mResult['Scopes'][] = $aScope;
143: }
144: }
145: }
146: }
147:
148: public function onAfterUpdateSettings($aArgs, &$mResult)
149: {
150: $sScope = '';
151: if (isset($aArgs['Scopes']) && is_array($aArgs['Scopes'])) {
152: foreach ($aArgs['Scopes'] as $aScope) {
153: if ($aScope['Name'] === 'auth') {
154: if ($aScope['Value']) {
155: $sScope = 'auth';
156: break;
157: }
158: }
159: }
160: }
161: $this->setConfig('Scopes', $sScope);
162: $this->saveModuleConfig();
163: }
164: }
165: