1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\FacebookAuthWebclient; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
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: | |
29: | protected function issetScope($sScope) |
30: | { |
31: | return in_array($sScope, explode(' ', $this->getConfig('Scopes'))); |
32: | } |
33: | |
34: | |
35: | |
36: | |
37: | |
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: | |
49: | |
50: | |
51: | |
52: | |
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: | |
67: | |
68: | |
69: | |
70: | |
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: | |
91: | |
92: | |
93: | |
94: | |
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: | |