|   1:  | <?php | 
|   2:  |  | 
|   3:  |  | 
|   4:  |  | 
|   5:  |  | 
|   6:  |  | 
|   7:  |  | 
|   8:  | namespace Aurora\Modules\Dropbox; | 
|   9:  |  | 
|  10:  |  | 
|  11:  |  | 
|  12:  |  | 
|  13:  |  | 
|  14:  |  | 
|  15:  |  | 
|  16:  |  | 
|  17:  |  | 
|  18:  |  | 
|  19:  |  | 
|  20:  |  | 
|  21:  | class Module extends \Aurora\System\Module\AbstractModule | 
|  22:  | { | 
|  23:  |     protected $sService = 'dropbox'; | 
|  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']); | 
|  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:  |             ); | 
| 117:  |         } | 
| 118:  |  | 
| 119:  |         if ($oUser && $oUser->isNormalOrTenant()) { | 
| 120:  |             $oAccount = null; | 
| 121:  |             $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator(); | 
| 122:  |             if ($oOAuthIntegratorWebclientDecorator) { | 
| 123:  |                 $oAccount = $oOAuthIntegratorWebclientDecorator->GetAccount($this->sService); | 
| 124:  |             } | 
| 125:  |             $aResult = array( | 
| 126:  |                 'EnableModule' => $this->oModuleSettings->EnableModule, | 
| 127:  |                 'Connected' => $oAccount ? true : false | 
| 128:  |             ); | 
| 129:  |             $aArgs = array( | 
| 130:  |                 'OAuthAccount' => $oAccount | 
| 131:  |             ); | 
| 132:  |         } | 
| 133:  |         $this->broadcastEvent('GetSettings', $aArgs, $aResult); | 
| 134:  |  | 
| 135:  |         return $aResult; | 
| 136:  |     } | 
| 137:  |  | 
| 138:  |      | 
| 139:  |  | 
| 140:  |  | 
| 141:  |  | 
| 142:  |  | 
| 143:  |  | 
| 144:  |  | 
| 145:  |  | 
| 146:  |  | 
| 147:  |     public function UpdateSettings($EnableModule, $Id, $Secret) | 
| 148:  |     { | 
| 149:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::TenantAdmin); | 
| 150:  |  | 
| 151:  |         try { | 
| 152:  |             $this->setConfig('EnableModule', $EnableModule); | 
| 153:  |             $this->setConfig('Id', $Id); | 
| 154:  |             $this->setConfig('Secret', $Secret); | 
| 155:  |             $this->saveModuleConfig(); | 
| 156:  |         } catch (\Exception $ex) { | 
| 157:  |             throw new \Aurora\System\Exceptions\ApiException(\Aurora\System\Notifications::CanNotSaveSettings); | 
| 158:  |         } | 
| 159:  |  | 
| 160:  |         return true; | 
| 161:  |     } | 
| 162:  |  | 
| 163:  |      | 
| 164:  |  | 
| 165:  |  | 
| 166:  |  | 
| 167:  |  | 
| 168:  |     public function DeleteAccount() | 
| 169:  |     { | 
| 170:  |         \Aurora\System\Api::checkUserRoleIsAtLeast(\Aurora\System\Enums\UserRole::NormalUser); | 
| 171:  |  | 
| 172:  |         $bResult = false; | 
| 173:  |         $oOAuthIntegratorWebclientDecorator = \Aurora\Modules\OAuthIntegratorWebclient\Module::Decorator(); | 
| 174:  |         if ($oOAuthIntegratorWebclientDecorator) { | 
| 175:  |             $bResult = $oOAuthIntegratorWebclientDecorator->DeleteAccount($this->sService); | 
| 176:  |         } | 
| 177:  |  | 
| 178:  |         return $bResult; | 
| 179:  |     } | 
| 180:  |      | 
| 181:  | } | 
| 182:  |  |