| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | namespace Aurora\Modules\Google; |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | class Module extends \Aurora\System\Module\AbstractModule |
| 22: | { |
| 23: | protected $sService = 'google'; |
| 24: | |
| 25: | protected $aRequireModules = array( |
| 26: | 'OAuthIntegratorWebclient' |
| 27: | ); |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | public static function getInstance() |
| 33: | { |
| 34: | return parent::getInstance(); |
| 35: | } |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | public static function Decorator() |
| 41: | { |
| 42: | return parent::Decorator(); |
| 43: | } |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | public function getModuleSettings() |
| 49: | { |
| 50: | return $this->oModuleSettings; |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | public function init() |
| 60: | { |
| 61: | $this->subscribeEvent('GetServicesSettings', array($this, 'onGetServicesSettings')); |
| 62: | $this->subscribeEvent('UpdateServicesSettings', array($this, 'onUpdateServicesSettings')); |
| 63: | } |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | public function onGetServicesSettings(&$aServices) |
| 72: | { |
| 73: | $aSettings = $this->GetSettings(); |
| 74: | if (!empty($aSettings)) { |
| 75: | $aServices[] = $aSettings; |
| 76: | } |
| 77: | } |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | public function onUpdateServicesSettings($aServices) |
| 88: | { |
| 89: | $aSettings = $aServices[$this->sService]; |
| 90: | |
| 91: | if (\is_array($aSettings)) { |
| 92: | $this->UpdateSettings($aSettings['EnableModule'], $aSettings['Id'], $aSettings['Secret'], $aSettings['Key']); |
| 93: | } |
| 94: | } |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | public function GetSettings() |
| 104: | { |
| 105: | $aResult = array(); |
| 106: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::Anonymous); |
| 107: | |
| 108: | $oUser = \Aurora\System\Api::getAuthenticatedUser(); |
| 109: | if ($oUser && $oUser->Role === \Aurora\System\Enums\UserRole::SuperAdmin) { |
| 110: | $aResult = array( |
| 111: | 'Name' => $this->sService, |
| 112: | 'DisplayName' => self::GetName(), |
| 113: | 'EnableModule' => $this->oModuleSettings->EnableModule, |
| 114: | 'Id' => $this->oModuleSettings->Id, |
| 115: | 'Secret' => $this->oModuleSettings->Secret, |
| 116: | 'Key' => $this->oModuleSettings->Key |
| 117: | ); |
| 118: | } |
| 119: | |
| 120: | if ($oUser && $oUser->isNormalOrTenant()) { |
| 121: | $oAccount = null; |
| 122: | $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator(); |
| 123: | if ($oOAuthIntegratorWebclientDecorator) { |
| 124: | $oAccount = $oOAuthIntegratorWebclientDecorator->GetAccount($this->sService); |
| 125: | } |
| 126: | $aResult = array( |
| 127: | 'EnableModule' => $this->oModuleSettings->EnableModule, |
| 128: | 'Connected' => $oAccount ? true : false, |
| 129: | 'AccountId' => $oAccount instanceof \Aurora\Modules\OAuthIntegratorWebclient\Models\OauthAccount ? $oAccount->Id : null |
| 130: | ); |
| 131: | $aArgs = array( |
| 132: | 'OAuthAccount' => $oAccount |
| 133: | ); |
| 134: | } |
| 135: | $this->broadcastEvent('GetSettings', $aArgs, $aResult); |
| 136: | |
| 137: | return $aResult; |
| 138: | } |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | public function UpdateSettings($EnableModule, $Id, $Secret, $Key) |
| 151: | { |
| 152: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin); |
| 153: | |
| 154: | try { |
| 155: | $this->setConfig('EnableModule', $EnableModule); |
| 156: | $this->setConfig('Id', $Id); |
| 157: | $this->setConfig('Secret', $Secret); |
| 158: | $this->setConfig('Key', $Key); |
| 159: | $this->saveModuleConfig(); |
| 160: | } catch (\Exception $ex) { |
| 161: | throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::CanNotSaveSettings); |
| 162: | } |
| 163: | |
| 164: | return true; |
| 165: | } |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | public function DeleteAccount() |
| 173: | { |
| 174: | \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); |
| 175: | |
| 176: | $bResult = false; |
| 177: | $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator(); |
| 178: | if ($oOAuthIntegratorWebclientDecorator) { |
| 179: | $bResult = $oOAuthIntegratorWebclientDecorator->DeleteAccount($this->sService); |
| 180: | } |
| 181: | |
| 182: | return $bResult; |
| 183: | } |
| 184: | |
| 185: | } |
| 186: | |