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: * @package Modules
18: */
19: class Module extends \Aurora\System\Module\AbstractWebclientModule
20: {
21: protected $sService = 'facebook';
22:
23: protected $aRequireModules = array(
24: 'OAuthIntegratorWebclient',
25: 'Facebook'
26: );
27:
28: /***** private functions *****/
29: protected function issetScope($sScope)
30: {
31: return in_array($sScope, explode(' ', $this->getConfig('Scopes')));
32: }
33:
34: /**
35: * Initializes FacebookAuthWebclient Module.
36: *
37: * @ignore
38: */
39: public function init()
40: {
41: $this->subscribeEvent('OAuthIntegratorWebclient::GetServices::after', array($this, 'onAfterGetServices'));
42: $this->subscribeEvent('OAuthIntegratorAction', array($this, 'onOAuthIntegratorAction'));
43: $this->subscribeEvent('Facebook::GetSettings', array($this, 'onGetSettings'));
44: $this->subscribeEvent('Facebook::UpdateSettings::after', array($this, 'onAfterUpdateSettings'));
45: }
46:
47: /**
48: * Adds service name to array passed by reference.
49: *
50: * @ignore
51: * @param array $aArgs
52: * @param array $aServices Array with services names passed by reference.
53: */
54: public function onAfterGetServices($aArgs, &$aServices)
55: {
56: $oModule = \Aurora\System\Api::GetModule('Facebook');
57: $sId = $oModule->getConfig('Id', '');
58: $sSecret = $oModule->getConfig('Secret', '');
59:
60: if ($oModule->getConfig('EnableModule', false) && $this->issetScope('auth') && !empty($sId) && !empty($sSecret)) {
61: $aServices[] = $this->sService;
62: }
63: }
64:
65: /**
66: * Passes data to connect to service.
67: *
68: * @ignore
69: * @param string $aArgs Service type to verify if data should be passed.
70: * @param boolean|array $mResult variable passed by reference to take the result.
71: */
72: public function onOAuthIntegratorAction($aArgs, &$mResult)
73: {
74: if ($aArgs['Service'] === $this->sService) {
75: $sScopes = isset($_COOKIE['oauth-scopes']) ? $_COOKIE['oauth-scopes'] : '';
76: $mResult = false;
77: $oConnector = new Classes\Connector($this);
78: if ($oConnector) {
79: $mResult = $oConnector->Init(
80: \Aurora\System\Api::GetModule('Facebook')->getConfig('Id'),
81: \Aurora\System\Api::GetModule('Facebook')->getConfig('Secret'),
82: $sScopes
83: );
84: }
85: return true;
86: }
87: }
88:
89: /**
90: * Passes data to connect to service.
91: *
92: * @ignore
93: * @param string $aArgs Service type to verify if data should be passed.
94: * @param boolean|array $mResult variable passed by reference to take the result.
95: */
96: public function onGetSettings($aArgs, &$mResult)
97: {
98: $oUser = \Aurora\System\Api::getAuthenticatedUser();
99:
100: if (!empty($oUser)) {
101: $aScope = array(
102: 'Name' => 'auth',
103: 'Description' => $this->i18N('SCOPE_AUTH'),
104: 'Value' => false
105: );
106: if ($oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) {
107: $aScope['Value'] = $this->issetScope('auth');
108: $mResult['Scopes'][] = $aScope;
109: }
110: if ($oUser->isNormalOrTenant()) {
111: if ($aArgs['OAuthAccount'] instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount) {
112: $aScope['Value'] = $aArgs['OAuthAccount']->issetScope('auth');
113: }
114: if ($this->issetScope('auth')) {
115: $mResult['Scopes'][] = $aScope;
116: }
117: }
118: }
119: }
120:
121: public function onAfterUpdateSettings($aArgs, &$mResult)
122: {
123: $sScope = '';
124: if (isset($aArgs['Scopes']) && is_array($aArgs['Scopes'])) {
125: foreach ($aArgs['Scopes'] as $aScope) {
126: if ($aScope['Name'] === 'auth') {
127: if ($aScope['Value']) {
128: $sScope = 'auth';
129: break;
130: }
131: }
132: }
133: }
134: $this->setConfig('Scopes', $sScope);
135: $this->saveModuleConfig();
136: }
137: }
138: