1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\Modules\Google; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | class Module extends \Aurora\System\Module\AbstractModule |
20: | { |
21: | protected $sService = 'google'; |
22: | |
23: | protected $aRequireModules = array( |
24: | 'OAuthIntegratorWebclient' |
25: | ); |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | public function init() |
34: | { |
35: | $this->subscribeEvent('GetServicesSettings', array($this, 'onGetServicesSettings')); |
36: | $this->subscribeEvent('UpdateServicesSettings', array($this, 'onUpdateServicesSettings')); |
37: | } |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | public function onGetServicesSettings(&$aServices) |
46: | { |
47: | $aSettings = $this->GetSettings(); |
48: | if (!empty($aSettings)) { |
49: | $aServices[] = $aSettings; |
50: | } |
51: | } |
52: | |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
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'], $aSettings['Key']); |
67: | } |
68: | } |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | |
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: | 'Key' => $this->getConfig('Key', '') |
91: | ); |
92: | } |
93: | |
94: | if (!empty($oUser) && $oUser->isNormalOrTenant()) { |
95: | $oAccount = null; |
96: | $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator(); |
97: | if ($oOAuthIntegratorWebclientDecorator) { |
98: | $oAccount = $oOAuthIntegratorWebclientDecorator->GetAccount($this->sService); |
99: | } |
100: | $aResult = array( |
101: | 'EnableModule' => $this->getConfig('EnableModule', false), |
102: | 'Connected' => $oAccount ? true : false, |
103: | 'AccountId' => $oAccount instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount ? $oAccount->Id : null |
104: | ); |
105: | $aArgs = array( |
106: | 'OAuthAccount' => $oAccount |
107: | ); |
108: | } |
109: | $this->broadcastEvent('GetSettings', $aArgs, $aResult); |
110: | |
111: | return $aResult; |
112: | } |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | public function UpdateSettings($EnableModule, $Id, $Secret, $Key) |
125: | { |
126: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin); |
127: | |
128: | try { |
129: | $this->setConfig('EnableModule', $EnableModule); |
130: | $this->setConfig('Id', $Id); |
131: | $this->setConfig('Secret', $Secret); |
132: | $this->setConfig('Key', $Key); |
133: | $this->saveModuleConfig(); |
134: | } catch (\Exception $ex) { |
135: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::CanNotSaveSettings); |
136: | } |
137: | |
138: | return true; |
139: | } |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | public function DeleteAccount() |
147: | { |
148: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
149: | |
150: | $bResult = false; |
151: | $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator(); |
152: | if ($oOAuthIntegratorWebclientDecorator) { |
153: | $bResult = $oOAuthIntegratorWebclientDecorator->DeleteAccount($this->sService); |
154: | } |
155: | |
156: | return $bResult; |
157: | } |
158: | |
159: | } |
160: | |