@@ -37,84 +37,84 @@ |
||
| 37 | 37 | |
| 38 | 38 | class Store implements IStore { |
| 39 | 39 | |
| 40 | - /** @var ISession */ |
|
| 41 | - private $session; |
|
| 42 | - |
|
| 43 | - /** @var ILogger */ |
|
| 44 | - private $logger; |
|
| 45 | - |
|
| 46 | - /** @var IProvider|null */ |
|
| 47 | - private $tokenProvider; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param ISession $session |
|
| 51 | - * @param ILogger $logger |
|
| 52 | - * @param IProvider $tokenProvider |
|
| 53 | - */ |
|
| 54 | - public function __construct(ISession $session, ILogger $logger, IProvider $tokenProvider = null) { |
|
| 55 | - $this->session = $session; |
|
| 56 | - $this->logger = $logger; |
|
| 57 | - $this->tokenProvider = $tokenProvider; |
|
| 58 | - |
|
| 59 | - Util::connectHook('OC_User', 'post_login', $this, 'authenticate'); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Hook listener on post login |
|
| 64 | - * |
|
| 65 | - * @param array $params |
|
| 66 | - */ |
|
| 67 | - public function authenticate(array $params) { |
|
| 68 | - $this->session->set('login_credentials', json_encode($params)); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Replace the session implementation |
|
| 73 | - * |
|
| 74 | - * @param ISession $session |
|
| 75 | - */ |
|
| 76 | - public function setSession(ISession $session) { |
|
| 77 | - $this->session = $session; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @since 12 |
|
| 82 | - * |
|
| 83 | - * @return ICredentials the login credentials of the current user |
|
| 84 | - * @throws CredentialsUnavailableException |
|
| 85 | - */ |
|
| 86 | - public function getLoginCredentials() { |
|
| 87 | - if (is_null($this->tokenProvider)) { |
|
| 88 | - throw new CredentialsUnavailableException(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $trySession = false; |
|
| 92 | - try { |
|
| 93 | - $sessionId = $this->session->getId(); |
|
| 94 | - $token = $this->tokenProvider->getToken($sessionId); |
|
| 95 | - |
|
| 96 | - $uid = $token->getUID(); |
|
| 97 | - $user = $token->getLoginName(); |
|
| 98 | - $password = $this->tokenProvider->getPassword($token, $sessionId); |
|
| 99 | - |
|
| 100 | - return new Credentials($uid, $user, $password); |
|
| 101 | - } catch (SessionNotAvailableException $ex) { |
|
| 102 | - $this->logger->debug('could not get login credentials because session is unavailable', ['app' => 'core']); |
|
| 103 | - } catch (InvalidTokenException $ex) { |
|
| 104 | - $this->logger->debug('could not get login credentials because the token is invalid', ['app' => 'core']); |
|
| 105 | - $trySession = true; |
|
| 106 | - } catch (PasswordlessTokenException $ex) { |
|
| 107 | - $this->logger->debug('could not get login credentials because the token has no password', ['app' => 'core']); |
|
| 108 | - $trySession = true; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - if ($trySession && $this->session->exists('login_credentials')) { |
|
| 112 | - $creds = json_decode($this->session->get('login_credentials')); |
|
| 113 | - return new Credentials($creds->uid, $creds->uid, $creds->password); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - // If we reach this line, an exception was thrown. |
|
| 117 | - throw new CredentialsUnavailableException(); |
|
| 118 | - } |
|
| 40 | + /** @var ISession */ |
|
| 41 | + private $session; |
|
| 42 | + |
|
| 43 | + /** @var ILogger */ |
|
| 44 | + private $logger; |
|
| 45 | + |
|
| 46 | + /** @var IProvider|null */ |
|
| 47 | + private $tokenProvider; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param ISession $session |
|
| 51 | + * @param ILogger $logger |
|
| 52 | + * @param IProvider $tokenProvider |
|
| 53 | + */ |
|
| 54 | + public function __construct(ISession $session, ILogger $logger, IProvider $tokenProvider = null) { |
|
| 55 | + $this->session = $session; |
|
| 56 | + $this->logger = $logger; |
|
| 57 | + $this->tokenProvider = $tokenProvider; |
|
| 58 | + |
|
| 59 | + Util::connectHook('OC_User', 'post_login', $this, 'authenticate'); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Hook listener on post login |
|
| 64 | + * |
|
| 65 | + * @param array $params |
|
| 66 | + */ |
|
| 67 | + public function authenticate(array $params) { |
|
| 68 | + $this->session->set('login_credentials', json_encode($params)); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Replace the session implementation |
|
| 73 | + * |
|
| 74 | + * @param ISession $session |
|
| 75 | + */ |
|
| 76 | + public function setSession(ISession $session) { |
|
| 77 | + $this->session = $session; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @since 12 |
|
| 82 | + * |
|
| 83 | + * @return ICredentials the login credentials of the current user |
|
| 84 | + * @throws CredentialsUnavailableException |
|
| 85 | + */ |
|
| 86 | + public function getLoginCredentials() { |
|
| 87 | + if (is_null($this->tokenProvider)) { |
|
| 88 | + throw new CredentialsUnavailableException(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $trySession = false; |
|
| 92 | + try { |
|
| 93 | + $sessionId = $this->session->getId(); |
|
| 94 | + $token = $this->tokenProvider->getToken($sessionId); |
|
| 95 | + |
|
| 96 | + $uid = $token->getUID(); |
|
| 97 | + $user = $token->getLoginName(); |
|
| 98 | + $password = $this->tokenProvider->getPassword($token, $sessionId); |
|
| 99 | + |
|
| 100 | + return new Credentials($uid, $user, $password); |
|
| 101 | + } catch (SessionNotAvailableException $ex) { |
|
| 102 | + $this->logger->debug('could not get login credentials because session is unavailable', ['app' => 'core']); |
|
| 103 | + } catch (InvalidTokenException $ex) { |
|
| 104 | + $this->logger->debug('could not get login credentials because the token is invalid', ['app' => 'core']); |
|
| 105 | + $trySession = true; |
|
| 106 | + } catch (PasswordlessTokenException $ex) { |
|
| 107 | + $this->logger->debug('could not get login credentials because the token has no password', ['app' => 'core']); |
|
| 108 | + $trySession = true; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + if ($trySession && $this->session->exists('login_credentials')) { |
|
| 112 | + $creds = json_decode($this->session->get('login_credentials')); |
|
| 113 | + return new Credentials($creds->uid, $creds->uid, $creds->password); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + // If we reach this line, an exception was thrown. |
|
| 117 | + throw new CredentialsUnavailableException(); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | 120 | } |
@@ -159,7 +159,7 @@ |
||
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - return array_filter($providers, function ($provider) use ($user) { |
|
| 162 | + return array_filter($providers, function($provider) use ($user) { |
|
| 163 | 163 | /* @var $provider IProvider */ |
| 164 | 164 | return $provider->isTwoFactorAuthEnabledForUser($user); |
| 165 | 165 | }); |
@@ -41,296 +41,296 @@ |
||
| 41 | 41 | |
| 42 | 42 | class Manager { |
| 43 | 43 | |
| 44 | - const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
| 45 | - const SESSION_UID_DONE = 'two_factor_auth_passed'; |
|
| 46 | - const BACKUP_CODES_APP_ID = 'twofactor_backupcodes'; |
|
| 47 | - const BACKUP_CODES_PROVIDER_ID = 'backup_codes'; |
|
| 48 | - const REMEMBER_LOGIN = 'two_factor_remember_login'; |
|
| 49 | - |
|
| 50 | - /** @var AppManager */ |
|
| 51 | - private $appManager; |
|
| 52 | - |
|
| 53 | - /** @var ISession */ |
|
| 54 | - private $session; |
|
| 55 | - |
|
| 56 | - /** @var IConfig */ |
|
| 57 | - private $config; |
|
| 58 | - |
|
| 59 | - /** @var IManager */ |
|
| 60 | - private $activityManager; |
|
| 61 | - |
|
| 62 | - /** @var ILogger */ |
|
| 63 | - private $logger; |
|
| 64 | - |
|
| 65 | - /** @var TokenProvider */ |
|
| 66 | - private $tokenProvider; |
|
| 67 | - |
|
| 68 | - /** @var ITimeFactory */ |
|
| 69 | - private $timeFactory; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @param AppManager $appManager |
|
| 73 | - * @param ISession $session |
|
| 74 | - * @param IConfig $config |
|
| 75 | - * @param IManager $activityManager |
|
| 76 | - * @param ILogger $logger |
|
| 77 | - * @param TokenProvider $tokenProvider |
|
| 78 | - * @param ITimeFactory $timeFactory |
|
| 79 | - */ |
|
| 80 | - public function __construct(AppManager $appManager, |
|
| 81 | - ISession $session, |
|
| 82 | - IConfig $config, |
|
| 83 | - IManager $activityManager, |
|
| 84 | - ILogger $logger, |
|
| 85 | - TokenProvider $tokenProvider, |
|
| 86 | - ITimeFactory $timeFactory) { |
|
| 87 | - $this->appManager = $appManager; |
|
| 88 | - $this->session = $session; |
|
| 89 | - $this->config = $config; |
|
| 90 | - $this->activityManager = $activityManager; |
|
| 91 | - $this->logger = $logger; |
|
| 92 | - $this->tokenProvider = $tokenProvider; |
|
| 93 | - $this->timeFactory = $timeFactory; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Determine whether the user must provide a second factor challenge |
|
| 98 | - * |
|
| 99 | - * @param IUser $user |
|
| 100 | - * @return boolean |
|
| 101 | - */ |
|
| 102 | - public function isTwoFactorAuthenticated(IUser $user) { |
|
| 103 | - $twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0; |
|
| 104 | - return $twoFactorEnabled && count($this->getProviders($user)) > 0; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Disable 2FA checks for the given user |
|
| 109 | - * |
|
| 110 | - * @param IUser $user |
|
| 111 | - */ |
|
| 112 | - public function disableTwoFactorAuthentication(IUser $user) { |
|
| 113 | - $this->config->setUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 1); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Enable all 2FA checks for the given user |
|
| 118 | - * |
|
| 119 | - * @param IUser $user |
|
| 120 | - */ |
|
| 121 | - public function enableTwoFactorAuthentication(IUser $user) { |
|
| 122 | - $this->config->deleteUserValue($user->getUID(), 'core', 'two_factor_auth_disabled'); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Get a 2FA provider by its ID |
|
| 127 | - * |
|
| 128 | - * @param IUser $user |
|
| 129 | - * @param string $challengeProviderId |
|
| 130 | - * @return IProvider|null |
|
| 131 | - */ |
|
| 132 | - public function getProvider(IUser $user, $challengeProviderId) { |
|
| 133 | - $providers = $this->getProviders($user, true); |
|
| 134 | - return isset($providers[$challengeProviderId]) ? $providers[$challengeProviderId] : null; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param IUser $user |
|
| 139 | - * @return IProvider|null the backup provider, if enabled for the given user |
|
| 140 | - */ |
|
| 141 | - public function getBackupProvider(IUser $user) { |
|
| 142 | - $providers = $this->getProviders($user, true); |
|
| 143 | - if (!isset($providers[self::BACKUP_CODES_PROVIDER_ID])) { |
|
| 144 | - return null; |
|
| 145 | - } |
|
| 146 | - return $providers[self::BACKUP_CODES_PROVIDER_ID]; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get the list of 2FA providers for the given user |
|
| 151 | - * |
|
| 152 | - * @param IUser $user |
|
| 153 | - * @param bool $includeBackupApp |
|
| 154 | - * @return IProvider[] |
|
| 155 | - * @throws Exception |
|
| 156 | - */ |
|
| 157 | - public function getProviders(IUser $user, $includeBackupApp = false) { |
|
| 158 | - $allApps = $this->appManager->getEnabledAppsForUser($user); |
|
| 159 | - $providers = []; |
|
| 160 | - |
|
| 161 | - foreach ($allApps as $appId) { |
|
| 162 | - if (!$includeBackupApp && $appId === self::BACKUP_CODES_APP_ID) { |
|
| 163 | - continue; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $info = $this->appManager->getAppInfo($appId); |
|
| 167 | - if (isset($info['two-factor-providers'])) { |
|
| 168 | - $providerClasses = $info['two-factor-providers']; |
|
| 169 | - foreach ($providerClasses as $class) { |
|
| 170 | - try { |
|
| 171 | - $this->loadTwoFactorApp($appId); |
|
| 172 | - $provider = OC::$server->query($class); |
|
| 173 | - $providers[$provider->getId()] = $provider; |
|
| 174 | - } catch (QueryException $exc) { |
|
| 175 | - // Provider class can not be resolved |
|
| 176 | - throw new Exception("Could not load two-factor auth provider $class"); |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return array_filter($providers, function ($provider) use ($user) { |
|
| 183 | - /* @var $provider IProvider */ |
|
| 184 | - return $provider->isTwoFactorAuthEnabledForUser($user); |
|
| 185 | - }); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Load an app by ID if it has not been loaded yet |
|
| 190 | - * |
|
| 191 | - * @param string $appId |
|
| 192 | - */ |
|
| 193 | - protected function loadTwoFactorApp($appId) { |
|
| 194 | - if (!OC_App::isAppLoaded($appId)) { |
|
| 195 | - OC_App::loadApp($appId); |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Verify the given challenge |
|
| 201 | - * |
|
| 202 | - * @param string $providerId |
|
| 203 | - * @param IUser $user |
|
| 204 | - * @param string $challenge |
|
| 205 | - * @return boolean |
|
| 206 | - */ |
|
| 207 | - public function verifyChallenge($providerId, IUser $user, $challenge) { |
|
| 208 | - $provider = $this->getProvider($user, $providerId); |
|
| 209 | - if (is_null($provider)) { |
|
| 210 | - return false; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $passed = $provider->verifyChallenge($user, $challenge); |
|
| 214 | - if ($passed) { |
|
| 215 | - if ($this->session->get(self::REMEMBER_LOGIN) === true) { |
|
| 216 | - // TODO: resolve cyclic dependency and use DI |
|
| 217 | - \OC::$server->getUserSession()->createRememberMeToken($user); |
|
| 218 | - } |
|
| 219 | - $this->session->remove(self::SESSION_UID_KEY); |
|
| 220 | - $this->session->remove(self::REMEMBER_LOGIN); |
|
| 221 | - $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
| 222 | - |
|
| 223 | - // Clear token from db |
|
| 224 | - $sessionId = $this->session->getId(); |
|
| 225 | - $token = $this->tokenProvider->getToken($sessionId); |
|
| 226 | - $tokenId = $token->getId(); |
|
| 227 | - $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $tokenId); |
|
| 228 | - |
|
| 229 | - $this->publishEvent($user, 'twofactor_success', [ |
|
| 230 | - 'provider' => $provider->getDisplayName(), |
|
| 231 | - ]); |
|
| 232 | - } else { |
|
| 233 | - $this->publishEvent($user, 'twofactor_failed', [ |
|
| 234 | - 'provider' => $provider->getDisplayName(), |
|
| 235 | - ]); |
|
| 236 | - } |
|
| 237 | - return $passed; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Push a 2fa event the user's activity stream |
|
| 242 | - * |
|
| 243 | - * @param IUser $user |
|
| 244 | - * @param string $event |
|
| 245 | - */ |
|
| 246 | - private function publishEvent(IUser $user, $event, array $params) { |
|
| 247 | - $activity = $this->activityManager->generateEvent(); |
|
| 248 | - $activity->setApp('core') |
|
| 249 | - ->setType('security') |
|
| 250 | - ->setAuthor($user->getUID()) |
|
| 251 | - ->setAffectedUser($user->getUID()) |
|
| 252 | - ->setSubject($event, $params); |
|
| 253 | - try { |
|
| 254 | - $this->activityManager->publish($activity); |
|
| 255 | - } catch (BadMethodCallException $e) { |
|
| 256 | - $this->logger->warning('could not publish backup code creation activity', ['app' => 'core']); |
|
| 257 | - $this->logger->logException($e, ['app' => 'core']); |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Check if the currently logged in user needs to pass 2FA |
|
| 263 | - * |
|
| 264 | - * @param IUser $user the currently logged in user |
|
| 265 | - * @return boolean |
|
| 266 | - */ |
|
| 267 | - public function needsSecondFactor(IUser $user = null) { |
|
| 268 | - if ($user === null) { |
|
| 269 | - return false; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - // If we are authenticated using an app password skip all this |
|
| 273 | - if ($this->session->exists('app_password')) { |
|
| 274 | - return false; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - // First check if the session tells us we should do 2FA (99% case) |
|
| 278 | - if (!$this->session->exists(self::SESSION_UID_KEY)) { |
|
| 279 | - |
|
| 280 | - // Check if the session tells us it is 2FA authenticated already |
|
| 281 | - if ($this->session->exists(self::SESSION_UID_DONE) && |
|
| 282 | - $this->session->get(self::SESSION_UID_DONE) === $user->getUID()) { |
|
| 283 | - return false; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /* |
|
| 44 | + const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
| 45 | + const SESSION_UID_DONE = 'two_factor_auth_passed'; |
|
| 46 | + const BACKUP_CODES_APP_ID = 'twofactor_backupcodes'; |
|
| 47 | + const BACKUP_CODES_PROVIDER_ID = 'backup_codes'; |
|
| 48 | + const REMEMBER_LOGIN = 'two_factor_remember_login'; |
|
| 49 | + |
|
| 50 | + /** @var AppManager */ |
|
| 51 | + private $appManager; |
|
| 52 | + |
|
| 53 | + /** @var ISession */ |
|
| 54 | + private $session; |
|
| 55 | + |
|
| 56 | + /** @var IConfig */ |
|
| 57 | + private $config; |
|
| 58 | + |
|
| 59 | + /** @var IManager */ |
|
| 60 | + private $activityManager; |
|
| 61 | + |
|
| 62 | + /** @var ILogger */ |
|
| 63 | + private $logger; |
|
| 64 | + |
|
| 65 | + /** @var TokenProvider */ |
|
| 66 | + private $tokenProvider; |
|
| 67 | + |
|
| 68 | + /** @var ITimeFactory */ |
|
| 69 | + private $timeFactory; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @param AppManager $appManager |
|
| 73 | + * @param ISession $session |
|
| 74 | + * @param IConfig $config |
|
| 75 | + * @param IManager $activityManager |
|
| 76 | + * @param ILogger $logger |
|
| 77 | + * @param TokenProvider $tokenProvider |
|
| 78 | + * @param ITimeFactory $timeFactory |
|
| 79 | + */ |
|
| 80 | + public function __construct(AppManager $appManager, |
|
| 81 | + ISession $session, |
|
| 82 | + IConfig $config, |
|
| 83 | + IManager $activityManager, |
|
| 84 | + ILogger $logger, |
|
| 85 | + TokenProvider $tokenProvider, |
|
| 86 | + ITimeFactory $timeFactory) { |
|
| 87 | + $this->appManager = $appManager; |
|
| 88 | + $this->session = $session; |
|
| 89 | + $this->config = $config; |
|
| 90 | + $this->activityManager = $activityManager; |
|
| 91 | + $this->logger = $logger; |
|
| 92 | + $this->tokenProvider = $tokenProvider; |
|
| 93 | + $this->timeFactory = $timeFactory; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Determine whether the user must provide a second factor challenge |
|
| 98 | + * |
|
| 99 | + * @param IUser $user |
|
| 100 | + * @return boolean |
|
| 101 | + */ |
|
| 102 | + public function isTwoFactorAuthenticated(IUser $user) { |
|
| 103 | + $twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0; |
|
| 104 | + return $twoFactorEnabled && count($this->getProviders($user)) > 0; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Disable 2FA checks for the given user |
|
| 109 | + * |
|
| 110 | + * @param IUser $user |
|
| 111 | + */ |
|
| 112 | + public function disableTwoFactorAuthentication(IUser $user) { |
|
| 113 | + $this->config->setUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 1); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Enable all 2FA checks for the given user |
|
| 118 | + * |
|
| 119 | + * @param IUser $user |
|
| 120 | + */ |
|
| 121 | + public function enableTwoFactorAuthentication(IUser $user) { |
|
| 122 | + $this->config->deleteUserValue($user->getUID(), 'core', 'two_factor_auth_disabled'); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Get a 2FA provider by its ID |
|
| 127 | + * |
|
| 128 | + * @param IUser $user |
|
| 129 | + * @param string $challengeProviderId |
|
| 130 | + * @return IProvider|null |
|
| 131 | + */ |
|
| 132 | + public function getProvider(IUser $user, $challengeProviderId) { |
|
| 133 | + $providers = $this->getProviders($user, true); |
|
| 134 | + return isset($providers[$challengeProviderId]) ? $providers[$challengeProviderId] : null; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param IUser $user |
|
| 139 | + * @return IProvider|null the backup provider, if enabled for the given user |
|
| 140 | + */ |
|
| 141 | + public function getBackupProvider(IUser $user) { |
|
| 142 | + $providers = $this->getProviders($user, true); |
|
| 143 | + if (!isset($providers[self::BACKUP_CODES_PROVIDER_ID])) { |
|
| 144 | + return null; |
|
| 145 | + } |
|
| 146 | + return $providers[self::BACKUP_CODES_PROVIDER_ID]; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get the list of 2FA providers for the given user |
|
| 151 | + * |
|
| 152 | + * @param IUser $user |
|
| 153 | + * @param bool $includeBackupApp |
|
| 154 | + * @return IProvider[] |
|
| 155 | + * @throws Exception |
|
| 156 | + */ |
|
| 157 | + public function getProviders(IUser $user, $includeBackupApp = false) { |
|
| 158 | + $allApps = $this->appManager->getEnabledAppsForUser($user); |
|
| 159 | + $providers = []; |
|
| 160 | + |
|
| 161 | + foreach ($allApps as $appId) { |
|
| 162 | + if (!$includeBackupApp && $appId === self::BACKUP_CODES_APP_ID) { |
|
| 163 | + continue; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $info = $this->appManager->getAppInfo($appId); |
|
| 167 | + if (isset($info['two-factor-providers'])) { |
|
| 168 | + $providerClasses = $info['two-factor-providers']; |
|
| 169 | + foreach ($providerClasses as $class) { |
|
| 170 | + try { |
|
| 171 | + $this->loadTwoFactorApp($appId); |
|
| 172 | + $provider = OC::$server->query($class); |
|
| 173 | + $providers[$provider->getId()] = $provider; |
|
| 174 | + } catch (QueryException $exc) { |
|
| 175 | + // Provider class can not be resolved |
|
| 176 | + throw new Exception("Could not load two-factor auth provider $class"); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return array_filter($providers, function ($provider) use ($user) { |
|
| 183 | + /* @var $provider IProvider */ |
|
| 184 | + return $provider->isTwoFactorAuthEnabledForUser($user); |
|
| 185 | + }); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Load an app by ID if it has not been loaded yet |
|
| 190 | + * |
|
| 191 | + * @param string $appId |
|
| 192 | + */ |
|
| 193 | + protected function loadTwoFactorApp($appId) { |
|
| 194 | + if (!OC_App::isAppLoaded($appId)) { |
|
| 195 | + OC_App::loadApp($appId); |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Verify the given challenge |
|
| 201 | + * |
|
| 202 | + * @param string $providerId |
|
| 203 | + * @param IUser $user |
|
| 204 | + * @param string $challenge |
|
| 205 | + * @return boolean |
|
| 206 | + */ |
|
| 207 | + public function verifyChallenge($providerId, IUser $user, $challenge) { |
|
| 208 | + $provider = $this->getProvider($user, $providerId); |
|
| 209 | + if (is_null($provider)) { |
|
| 210 | + return false; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $passed = $provider->verifyChallenge($user, $challenge); |
|
| 214 | + if ($passed) { |
|
| 215 | + if ($this->session->get(self::REMEMBER_LOGIN) === true) { |
|
| 216 | + // TODO: resolve cyclic dependency and use DI |
|
| 217 | + \OC::$server->getUserSession()->createRememberMeToken($user); |
|
| 218 | + } |
|
| 219 | + $this->session->remove(self::SESSION_UID_KEY); |
|
| 220 | + $this->session->remove(self::REMEMBER_LOGIN); |
|
| 221 | + $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
| 222 | + |
|
| 223 | + // Clear token from db |
|
| 224 | + $sessionId = $this->session->getId(); |
|
| 225 | + $token = $this->tokenProvider->getToken($sessionId); |
|
| 226 | + $tokenId = $token->getId(); |
|
| 227 | + $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $tokenId); |
|
| 228 | + |
|
| 229 | + $this->publishEvent($user, 'twofactor_success', [ |
|
| 230 | + 'provider' => $provider->getDisplayName(), |
|
| 231 | + ]); |
|
| 232 | + } else { |
|
| 233 | + $this->publishEvent($user, 'twofactor_failed', [ |
|
| 234 | + 'provider' => $provider->getDisplayName(), |
|
| 235 | + ]); |
|
| 236 | + } |
|
| 237 | + return $passed; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Push a 2fa event the user's activity stream |
|
| 242 | + * |
|
| 243 | + * @param IUser $user |
|
| 244 | + * @param string $event |
|
| 245 | + */ |
|
| 246 | + private function publishEvent(IUser $user, $event, array $params) { |
|
| 247 | + $activity = $this->activityManager->generateEvent(); |
|
| 248 | + $activity->setApp('core') |
|
| 249 | + ->setType('security') |
|
| 250 | + ->setAuthor($user->getUID()) |
|
| 251 | + ->setAffectedUser($user->getUID()) |
|
| 252 | + ->setSubject($event, $params); |
|
| 253 | + try { |
|
| 254 | + $this->activityManager->publish($activity); |
|
| 255 | + } catch (BadMethodCallException $e) { |
|
| 256 | + $this->logger->warning('could not publish backup code creation activity', ['app' => 'core']); |
|
| 257 | + $this->logger->logException($e, ['app' => 'core']); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Check if the currently logged in user needs to pass 2FA |
|
| 263 | + * |
|
| 264 | + * @param IUser $user the currently logged in user |
|
| 265 | + * @return boolean |
|
| 266 | + */ |
|
| 267 | + public function needsSecondFactor(IUser $user = null) { |
|
| 268 | + if ($user === null) { |
|
| 269 | + return false; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + // If we are authenticated using an app password skip all this |
|
| 273 | + if ($this->session->exists('app_password')) { |
|
| 274 | + return false; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // First check if the session tells us we should do 2FA (99% case) |
|
| 278 | + if (!$this->session->exists(self::SESSION_UID_KEY)) { |
|
| 279 | + |
|
| 280 | + // Check if the session tells us it is 2FA authenticated already |
|
| 281 | + if ($this->session->exists(self::SESSION_UID_DONE) && |
|
| 282 | + $this->session->get(self::SESSION_UID_DONE) === $user->getUID()) { |
|
| 283 | + return false; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /* |
|
| 287 | 287 | * If the session is expired check if we are not logged in by a token |
| 288 | 288 | * that still needs 2FA auth |
| 289 | 289 | */ |
| 290 | - try { |
|
| 291 | - $sessionId = $this->session->getId(); |
|
| 292 | - $token = $this->tokenProvider->getToken($sessionId); |
|
| 293 | - $tokenId = $token->getId(); |
|
| 294 | - $tokensNeeding2FA = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
| 295 | - |
|
| 296 | - if (!in_array($tokenId, $tokensNeeding2FA, true)) { |
|
| 297 | - $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
| 298 | - return false; |
|
| 299 | - } |
|
| 300 | - } catch (InvalidTokenException $e) { |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - if (!$this->isTwoFactorAuthenticated($user)) { |
|
| 305 | - // There is no second factor any more -> let the user pass |
|
| 306 | - // This prevents infinite redirect loops when a user is about |
|
| 307 | - // to solve the 2FA challenge, and the provider app is |
|
| 308 | - // disabled the same time |
|
| 309 | - $this->session->remove(self::SESSION_UID_KEY); |
|
| 310 | - |
|
| 311 | - $keys = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
| 312 | - foreach ($keys as $key) { |
|
| 313 | - $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $key); |
|
| 314 | - } |
|
| 315 | - return false; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - return true; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Prepare the 2FA login |
|
| 323 | - * |
|
| 324 | - * @param IUser $user |
|
| 325 | - * @param boolean $rememberMe |
|
| 326 | - */ |
|
| 327 | - public function prepareTwoFactorLogin(IUser $user, $rememberMe) { |
|
| 328 | - $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
| 329 | - $this->session->set(self::REMEMBER_LOGIN, $rememberMe); |
|
| 330 | - |
|
| 331 | - $id = $this->session->getId(); |
|
| 332 | - $token = $this->tokenProvider->getToken($id); |
|
| 333 | - $this->config->setUserValue($user->getUID(), 'login_token_2fa', $token->getId(), $this->timeFactory->getTime()); |
|
| 334 | - } |
|
| 290 | + try { |
|
| 291 | + $sessionId = $this->session->getId(); |
|
| 292 | + $token = $this->tokenProvider->getToken($sessionId); |
|
| 293 | + $tokenId = $token->getId(); |
|
| 294 | + $tokensNeeding2FA = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
| 295 | + |
|
| 296 | + if (!in_array($tokenId, $tokensNeeding2FA, true)) { |
|
| 297 | + $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
| 298 | + return false; |
|
| 299 | + } |
|
| 300 | + } catch (InvalidTokenException $e) { |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + if (!$this->isTwoFactorAuthenticated($user)) { |
|
| 305 | + // There is no second factor any more -> let the user pass |
|
| 306 | + // This prevents infinite redirect loops when a user is about |
|
| 307 | + // to solve the 2FA challenge, and the provider app is |
|
| 308 | + // disabled the same time |
|
| 309 | + $this->session->remove(self::SESSION_UID_KEY); |
|
| 310 | + |
|
| 311 | + $keys = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
| 312 | + foreach ($keys as $key) { |
|
| 313 | + $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $key); |
|
| 314 | + } |
|
| 315 | + return false; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + return true; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Prepare the 2FA login |
|
| 323 | + * |
|
| 324 | + * @param IUser $user |
|
| 325 | + * @param boolean $rememberMe |
|
| 326 | + */ |
|
| 327 | + public function prepareTwoFactorLogin(IUser $user, $rememberMe) { |
|
| 328 | + $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
| 329 | + $this->session->set(self::REMEMBER_LOGIN, $rememberMe); |
|
| 330 | + |
|
| 331 | + $id = $this->session->getId(); |
|
| 332 | + $token = $this->tokenProvider->getToken($id); |
|
| 333 | + $this->config->setUserValue($user->getUID(), 'login_token_2fa', $token->getId(), $this->timeFactory->getTime()); |
|
| 334 | + } |
|
| 335 | 335 | |
| 336 | 336 | } |
@@ -129,7 +129,7 @@ |
||
| 129 | 129 | $data = $result->fetchAll(); |
| 130 | 130 | $result->closeCursor(); |
| 131 | 131 | |
| 132 | - $entities = array_map(function ($row) { |
|
| 132 | + $entities = array_map(function($row) { |
|
| 133 | 133 | return DefaultToken::fromRow($row); |
| 134 | 134 | }, $data); |
| 135 | 135 | |
@@ -30,134 +30,134 @@ |
||
| 30 | 30 | |
| 31 | 31 | class DefaultTokenMapper extends Mapper { |
| 32 | 32 | |
| 33 | - public function __construct(IDBConnection $db) { |
|
| 34 | - parent::__construct($db, 'authtoken'); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Invalidate (delete) a given token |
|
| 39 | - * |
|
| 40 | - * @param string $token |
|
| 41 | - */ |
|
| 42 | - public function invalidate($token) { |
|
| 43 | - /* @var $qb IQueryBuilder */ |
|
| 44 | - $qb = $this->db->getQueryBuilder(); |
|
| 45 | - $qb->delete('authtoken') |
|
| 46 | - ->where($qb->expr()->eq('token', $qb->createParameter('token'))) |
|
| 47 | - ->setParameter('token', $token) |
|
| 48 | - ->execute(); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param int $olderThan |
|
| 53 | - * @param int $remember |
|
| 54 | - */ |
|
| 55 | - public function invalidateOld($olderThan, $remember = IToken::DO_NOT_REMEMBER) { |
|
| 56 | - /* @var $qb IQueryBuilder */ |
|
| 57 | - $qb = $this->db->getQueryBuilder(); |
|
| 58 | - $qb->delete('authtoken') |
|
| 59 | - ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
| 60 | - ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT))) |
|
| 61 | - ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))) |
|
| 62 | - ->execute(); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the user UID for the given token |
|
| 67 | - * |
|
| 68 | - * @param string $token |
|
| 69 | - * @throws DoesNotExistException |
|
| 70 | - * @return DefaultToken |
|
| 71 | - */ |
|
| 72 | - public function getToken($token) { |
|
| 73 | - /* @var $qb IQueryBuilder */ |
|
| 74 | - $qb = $this->db->getQueryBuilder(); |
|
| 75 | - $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope') |
|
| 76 | - ->from('authtoken') |
|
| 77 | - ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 78 | - ->execute(); |
|
| 79 | - |
|
| 80 | - $data = $result->fetch(); |
|
| 81 | - $result->closeCursor(); |
|
| 82 | - if ($data === false) { |
|
| 83 | - throw new DoesNotExistException('token does not exist'); |
|
| 84 | - } |
|
| 85 | - return DefaultToken::fromRow($data); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Get the token for $id |
|
| 90 | - * |
|
| 91 | - * @param string $id |
|
| 92 | - * @throws DoesNotExistException |
|
| 93 | - * @return DefaultToken |
|
| 94 | - */ |
|
| 95 | - public function getTokenById($id) { |
|
| 96 | - /* @var $qb IQueryBuilder */ |
|
| 97 | - $qb = $this->db->getQueryBuilder(); |
|
| 98 | - $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check', 'scope') |
|
| 99 | - ->from('authtoken') |
|
| 100 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 101 | - ->execute(); |
|
| 102 | - |
|
| 103 | - $data = $result->fetch(); |
|
| 104 | - $result->closeCursor(); |
|
| 105 | - if ($data === false) { |
|
| 106 | - throw new DoesNotExistException('token does not exist'); |
|
| 107 | - } |
|
| 108 | - return DefaultToken::fromRow($data); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Get all tokens of a user |
|
| 113 | - * |
|
| 114 | - * The provider may limit the number of result rows in case of an abuse |
|
| 115 | - * where a high number of (session) tokens is generated |
|
| 116 | - * |
|
| 117 | - * @param IUser $user |
|
| 118 | - * @return DefaultToken[] |
|
| 119 | - */ |
|
| 120 | - public function getTokenByUser(IUser $user) { |
|
| 121 | - /* @var $qb IQueryBuilder */ |
|
| 122 | - $qb = $this->db->getQueryBuilder(); |
|
| 123 | - $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope') |
|
| 124 | - ->from('authtoken') |
|
| 125 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 126 | - ->setMaxResults(1000); |
|
| 127 | - $result = $qb->execute(); |
|
| 128 | - $data = $result->fetchAll(); |
|
| 129 | - $result->closeCursor(); |
|
| 130 | - |
|
| 131 | - $entities = array_map(function ($row) { |
|
| 132 | - return DefaultToken::fromRow($row); |
|
| 133 | - }, $data); |
|
| 134 | - |
|
| 135 | - return $entities; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @param IUser $user |
|
| 140 | - * @param int $id |
|
| 141 | - */ |
|
| 142 | - public function deleteById(IUser $user, $id) { |
|
| 143 | - /* @var $qb IQueryBuilder */ |
|
| 144 | - $qb = $this->db->getQueryBuilder(); |
|
| 145 | - $qb->delete('authtoken') |
|
| 146 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 147 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))); |
|
| 148 | - $qb->execute(); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * delete all auth token which belong to a specific client if the client was deleted |
|
| 153 | - * |
|
| 154 | - * @param string $name |
|
| 155 | - */ |
|
| 156 | - public function deleteByName($name) { |
|
| 157 | - $qb = $this->db->getQueryBuilder(); |
|
| 158 | - $qb->delete('authtoken') |
|
| 159 | - ->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR)); |
|
| 160 | - $qb->execute(); |
|
| 161 | - } |
|
| 33 | + public function __construct(IDBConnection $db) { |
|
| 34 | + parent::__construct($db, 'authtoken'); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Invalidate (delete) a given token |
|
| 39 | + * |
|
| 40 | + * @param string $token |
|
| 41 | + */ |
|
| 42 | + public function invalidate($token) { |
|
| 43 | + /* @var $qb IQueryBuilder */ |
|
| 44 | + $qb = $this->db->getQueryBuilder(); |
|
| 45 | + $qb->delete('authtoken') |
|
| 46 | + ->where($qb->expr()->eq('token', $qb->createParameter('token'))) |
|
| 47 | + ->setParameter('token', $token) |
|
| 48 | + ->execute(); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param int $olderThan |
|
| 53 | + * @param int $remember |
|
| 54 | + */ |
|
| 55 | + public function invalidateOld($olderThan, $remember = IToken::DO_NOT_REMEMBER) { |
|
| 56 | + /* @var $qb IQueryBuilder */ |
|
| 57 | + $qb = $this->db->getQueryBuilder(); |
|
| 58 | + $qb->delete('authtoken') |
|
| 59 | + ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
| 60 | + ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT))) |
|
| 61 | + ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))) |
|
| 62 | + ->execute(); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the user UID for the given token |
|
| 67 | + * |
|
| 68 | + * @param string $token |
|
| 69 | + * @throws DoesNotExistException |
|
| 70 | + * @return DefaultToken |
|
| 71 | + */ |
|
| 72 | + public function getToken($token) { |
|
| 73 | + /* @var $qb IQueryBuilder */ |
|
| 74 | + $qb = $this->db->getQueryBuilder(); |
|
| 75 | + $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope') |
|
| 76 | + ->from('authtoken') |
|
| 77 | + ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 78 | + ->execute(); |
|
| 79 | + |
|
| 80 | + $data = $result->fetch(); |
|
| 81 | + $result->closeCursor(); |
|
| 82 | + if ($data === false) { |
|
| 83 | + throw new DoesNotExistException('token does not exist'); |
|
| 84 | + } |
|
| 85 | + return DefaultToken::fromRow($data); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Get the token for $id |
|
| 90 | + * |
|
| 91 | + * @param string $id |
|
| 92 | + * @throws DoesNotExistException |
|
| 93 | + * @return DefaultToken |
|
| 94 | + */ |
|
| 95 | + public function getTokenById($id) { |
|
| 96 | + /* @var $qb IQueryBuilder */ |
|
| 97 | + $qb = $this->db->getQueryBuilder(); |
|
| 98 | + $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check', 'scope') |
|
| 99 | + ->from('authtoken') |
|
| 100 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 101 | + ->execute(); |
|
| 102 | + |
|
| 103 | + $data = $result->fetch(); |
|
| 104 | + $result->closeCursor(); |
|
| 105 | + if ($data === false) { |
|
| 106 | + throw new DoesNotExistException('token does not exist'); |
|
| 107 | + } |
|
| 108 | + return DefaultToken::fromRow($data); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Get all tokens of a user |
|
| 113 | + * |
|
| 114 | + * The provider may limit the number of result rows in case of an abuse |
|
| 115 | + * where a high number of (session) tokens is generated |
|
| 116 | + * |
|
| 117 | + * @param IUser $user |
|
| 118 | + * @return DefaultToken[] |
|
| 119 | + */ |
|
| 120 | + public function getTokenByUser(IUser $user) { |
|
| 121 | + /* @var $qb IQueryBuilder */ |
|
| 122 | + $qb = $this->db->getQueryBuilder(); |
|
| 123 | + $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope') |
|
| 124 | + ->from('authtoken') |
|
| 125 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 126 | + ->setMaxResults(1000); |
|
| 127 | + $result = $qb->execute(); |
|
| 128 | + $data = $result->fetchAll(); |
|
| 129 | + $result->closeCursor(); |
|
| 130 | + |
|
| 131 | + $entities = array_map(function ($row) { |
|
| 132 | + return DefaultToken::fromRow($row); |
|
| 133 | + }, $data); |
|
| 134 | + |
|
| 135 | + return $entities; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @param IUser $user |
|
| 140 | + * @param int $id |
|
| 141 | + */ |
|
| 142 | + public function deleteById(IUser $user, $id) { |
|
| 143 | + /* @var $qb IQueryBuilder */ |
|
| 144 | + $qb = $this->db->getQueryBuilder(); |
|
| 145 | + $qb->delete('authtoken') |
|
| 146 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 147 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))); |
|
| 148 | + $qb->execute(); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * delete all auth token which belong to a specific client if the client was deleted |
|
| 153 | + * |
|
| 154 | + * @param string $name |
|
| 155 | + */ |
|
| 156 | + public function deleteByName($name) { |
|
| 157 | + $qb = $this->db->getQueryBuilder(); |
|
| 158 | + $qb->delete('authtoken') |
|
| 159 | + ->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR)); |
|
| 160 | + $qb->execute(); |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | 163 | } |
@@ -27,10 +27,10 @@ |
||
| 27 | 27 | |
| 28 | 28 | class DefaultTokenCleanupJob extends Job { |
| 29 | 29 | |
| 30 | - protected function run($argument) { |
|
| 31 | - /* @var $provider IProvider */ |
|
| 32 | - $provider = OC::$server->query('OC\Authentication\Token\IProvider'); |
|
| 33 | - $provider->invalidateOldTokens(); |
|
| 34 | - } |
|
| 30 | + protected function run($argument) { |
|
| 31 | + /* @var $provider IProvider */ |
|
| 32 | + $provider = OC::$server->query('OC\Authentication\Token\IProvider'); |
|
| 33 | + $provider->invalidateOldTokens(); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | } |
@@ -42,135 +42,135 @@ |
||
| 42 | 42 | */ |
| 43 | 43 | class DefaultToken extends Entity implements IToken { |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @var string user UID |
|
| 47 | - */ |
|
| 48 | - protected $uid; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var string login name used for generating the token |
|
| 52 | - */ |
|
| 53 | - protected $loginName; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var string encrypted user password |
|
| 57 | - */ |
|
| 58 | - protected $password; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @var string token name (e.g. browser/OS) |
|
| 62 | - */ |
|
| 63 | - protected $name; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @var string |
|
| 67 | - */ |
|
| 68 | - protected $token; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @var int |
|
| 72 | - */ |
|
| 73 | - protected $type; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @var int |
|
| 77 | - */ |
|
| 78 | - protected $remember; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @var int |
|
| 82 | - */ |
|
| 83 | - protected $lastActivity; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @var int |
|
| 87 | - */ |
|
| 88 | - protected $lastCheck; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @var string |
|
| 92 | - */ |
|
| 93 | - protected $scope; |
|
| 94 | - |
|
| 95 | - public function __construct() { |
|
| 96 | - $this->addType('type', 'int'); |
|
| 97 | - $this->addType('lastActivity', 'int'); |
|
| 98 | - $this->addType('lastCheck', 'int'); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public function getId() { |
|
| 102 | - return $this->id; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - public function getUID() { |
|
| 106 | - return $this->uid; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Get the login name used when generating the token |
|
| 111 | - * |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - public function getLoginName() { |
|
| 115 | - return parent::getLoginName(); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get the (encrypted) login password |
|
| 120 | - * |
|
| 121 | - * @return string |
|
| 122 | - */ |
|
| 123 | - public function getPassword() { |
|
| 124 | - return parent::getPassword(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - public function jsonSerialize() { |
|
| 128 | - return [ |
|
| 129 | - 'id' => $this->id, |
|
| 130 | - 'name' => $this->name, |
|
| 131 | - 'lastActivity' => $this->lastActivity, |
|
| 132 | - 'type' => $this->type, |
|
| 133 | - 'scope' => $this->getScopeAsArray() |
|
| 134 | - ]; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Get the timestamp of the last password check |
|
| 139 | - * |
|
| 140 | - * @return int |
|
| 141 | - */ |
|
| 142 | - public function getLastCheck() { |
|
| 143 | - return parent::getLastCheck(); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the timestamp of the last password check |
|
| 148 | - * |
|
| 149 | - * @param int $time |
|
| 150 | - */ |
|
| 151 | - public function setLastCheck($time) { |
|
| 152 | - return parent::setLastCheck($time); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - public function getScope() { |
|
| 156 | - return parent::getScope(); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - public function getScopeAsArray() { |
|
| 160 | - $scope = json_decode($this->getScope(), true); |
|
| 161 | - if (!$scope) { |
|
| 162 | - return [ |
|
| 163 | - 'filesystem'=> true |
|
| 164 | - ]; |
|
| 165 | - } |
|
| 166 | - return $scope; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - public function setScope($scope) { |
|
| 170 | - if (is_array($scope)) { |
|
| 171 | - parent::setScope(json_encode($scope)); |
|
| 172 | - } else { |
|
| 173 | - parent::setScope((string)$scope); |
|
| 174 | - } |
|
| 175 | - } |
|
| 45 | + /** |
|
| 46 | + * @var string user UID |
|
| 47 | + */ |
|
| 48 | + protected $uid; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var string login name used for generating the token |
|
| 52 | + */ |
|
| 53 | + protected $loginName; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var string encrypted user password |
|
| 57 | + */ |
|
| 58 | + protected $password; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @var string token name (e.g. browser/OS) |
|
| 62 | + */ |
|
| 63 | + protected $name; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @var string |
|
| 67 | + */ |
|
| 68 | + protected $token; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @var int |
|
| 72 | + */ |
|
| 73 | + protected $type; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @var int |
|
| 77 | + */ |
|
| 78 | + protected $remember; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @var int |
|
| 82 | + */ |
|
| 83 | + protected $lastActivity; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @var int |
|
| 87 | + */ |
|
| 88 | + protected $lastCheck; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @var string |
|
| 92 | + */ |
|
| 93 | + protected $scope; |
|
| 94 | + |
|
| 95 | + public function __construct() { |
|
| 96 | + $this->addType('type', 'int'); |
|
| 97 | + $this->addType('lastActivity', 'int'); |
|
| 98 | + $this->addType('lastCheck', 'int'); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public function getId() { |
|
| 102 | + return $this->id; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + public function getUID() { |
|
| 106 | + return $this->uid; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Get the login name used when generating the token |
|
| 111 | + * |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public function getLoginName() { |
|
| 115 | + return parent::getLoginName(); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get the (encrypted) login password |
|
| 120 | + * |
|
| 121 | + * @return string |
|
| 122 | + */ |
|
| 123 | + public function getPassword() { |
|
| 124 | + return parent::getPassword(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + public function jsonSerialize() { |
|
| 128 | + return [ |
|
| 129 | + 'id' => $this->id, |
|
| 130 | + 'name' => $this->name, |
|
| 131 | + 'lastActivity' => $this->lastActivity, |
|
| 132 | + 'type' => $this->type, |
|
| 133 | + 'scope' => $this->getScopeAsArray() |
|
| 134 | + ]; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Get the timestamp of the last password check |
|
| 139 | + * |
|
| 140 | + * @return int |
|
| 141 | + */ |
|
| 142 | + public function getLastCheck() { |
|
| 143 | + return parent::getLastCheck(); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the timestamp of the last password check |
|
| 148 | + * |
|
| 149 | + * @param int $time |
|
| 150 | + */ |
|
| 151 | + public function setLastCheck($time) { |
|
| 152 | + return parent::setLastCheck($time); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + public function getScope() { |
|
| 156 | + return parent::getScope(); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + public function getScopeAsArray() { |
|
| 160 | + $scope = json_decode($this->getScope(), true); |
|
| 161 | + if (!$scope) { |
|
| 162 | + return [ |
|
| 163 | + 'filesystem'=> true |
|
| 164 | + ]; |
|
| 165 | + } |
|
| 166 | + return $scope; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + public function setScope($scope) { |
|
| 170 | + if (is_array($scope)) { |
|
| 171 | + parent::setScope(json_encode($scope)); |
|
| 172 | + } else { |
|
| 173 | + parent::setScope((string)$scope); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -170,7 +170,7 @@ |
||
| 170 | 170 | if (is_array($scope)) { |
| 171 | 171 | parent::setScope(json_encode($scope)); |
| 172 | 172 | } else { |
| 173 | - parent::setScope((string)$scope); |
|
| 173 | + parent::setScope((string) $scope); |
|
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | } |
@@ -26,71 +26,71 @@ |
||
| 26 | 26 | |
| 27 | 27 | interface IToken extends JsonSerializable { |
| 28 | 28 | |
| 29 | - const TEMPORARY_TOKEN = 0; |
|
| 30 | - const PERMANENT_TOKEN = 1; |
|
| 31 | - const DO_NOT_REMEMBER = 0; |
|
| 32 | - const REMEMBER = 1; |
|
| 29 | + const TEMPORARY_TOKEN = 0; |
|
| 30 | + const PERMANENT_TOKEN = 1; |
|
| 31 | + const DO_NOT_REMEMBER = 0; |
|
| 32 | + const REMEMBER = 1; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get the token ID |
|
| 36 | - * |
|
| 37 | - * @return int |
|
| 38 | - */ |
|
| 39 | - public function getId(); |
|
| 34 | + /** |
|
| 35 | + * Get the token ID |
|
| 36 | + * |
|
| 37 | + * @return int |
|
| 38 | + */ |
|
| 39 | + public function getId(); |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get the user UID |
|
| 43 | - * |
|
| 44 | - * @return string |
|
| 45 | - */ |
|
| 46 | - public function getUID(); |
|
| 41 | + /** |
|
| 42 | + * Get the user UID |
|
| 43 | + * |
|
| 44 | + * @return string |
|
| 45 | + */ |
|
| 46 | + public function getUID(); |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Get the login name used when generating the token |
|
| 50 | - * |
|
| 51 | - * @return string |
|
| 52 | - */ |
|
| 53 | - public function getLoginName(); |
|
| 48 | + /** |
|
| 49 | + * Get the login name used when generating the token |
|
| 50 | + * |
|
| 51 | + * @return string |
|
| 52 | + */ |
|
| 53 | + public function getLoginName(); |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Get the (encrypted) login password |
|
| 57 | - * |
|
| 58 | - * @return string |
|
| 59 | - */ |
|
| 60 | - public function getPassword(); |
|
| 55 | + /** |
|
| 56 | + * Get the (encrypted) login password |
|
| 57 | + * |
|
| 58 | + * @return string |
|
| 59 | + */ |
|
| 60 | + public function getPassword(); |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Get the timestamp of the last password check |
|
| 64 | - * |
|
| 65 | - * @return int |
|
| 66 | - */ |
|
| 67 | - public function getLastCheck(); |
|
| 62 | + /** |
|
| 63 | + * Get the timestamp of the last password check |
|
| 64 | + * |
|
| 65 | + * @return int |
|
| 66 | + */ |
|
| 67 | + public function getLastCheck(); |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Set the timestamp of the last password check |
|
| 71 | - * |
|
| 72 | - * @param int $time |
|
| 73 | - */ |
|
| 74 | - public function setLastCheck($time); |
|
| 69 | + /** |
|
| 70 | + * Set the timestamp of the last password check |
|
| 71 | + * |
|
| 72 | + * @param int $time |
|
| 73 | + */ |
|
| 74 | + public function setLastCheck($time); |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Get the authentication scope for this token |
|
| 78 | - * |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - public function getScope(); |
|
| 76 | + /** |
|
| 77 | + * Get the authentication scope for this token |
|
| 78 | + * |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + public function getScope(); |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Get the authentication scope for this token |
|
| 85 | - * |
|
| 86 | - * @return array |
|
| 87 | - */ |
|
| 88 | - public function getScopeAsArray(); |
|
| 83 | + /** |
|
| 84 | + * Get the authentication scope for this token |
|
| 85 | + * |
|
| 86 | + * @return array |
|
| 87 | + */ |
|
| 88 | + public function getScopeAsArray(); |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Set the authentication scope for this token |
|
| 92 | - * |
|
| 93 | - * @param array $scope |
|
| 94 | - */ |
|
| 95 | - public function setScope($scope); |
|
| 90 | + /** |
|
| 91 | + * Set the authentication scope for this token |
|
| 92 | + * |
|
| 93 | + * @param array $scope |
|
| 94 | + */ |
|
| 95 | + public function setScope($scope); |
|
| 96 | 96 | } |
@@ -31,172 +31,172 @@ |
||
| 31 | 31 | * Helper class for large files on 32-bit platforms. |
| 32 | 32 | */ |
| 33 | 33 | class LargeFileHelper { |
| 34 | - /** |
|
| 35 | - * pow(2, 53) as a base-10 string. |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - const POW_2_53 = '9007199254740992'; |
|
| 34 | + /** |
|
| 35 | + * pow(2, 53) as a base-10 string. |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + const POW_2_53 = '9007199254740992'; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * pow(2, 53) - 1 as a base-10 string. |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - const POW_2_53_MINUS_1 = '9007199254740991'; |
|
| 40 | + /** |
|
| 41 | + * pow(2, 53) - 1 as a base-10 string. |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + const POW_2_53_MINUS_1 = '9007199254740991'; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @brief Checks whether our assumptions hold on the PHP platform we are on. |
|
| 48 | - * |
|
| 49 | - * @throws \RunTimeException if our assumptions do not hold on the current |
|
| 50 | - * PHP platform. |
|
| 51 | - */ |
|
| 52 | - public function __construct() { |
|
| 53 | - $pow_2_53 = floatval(self::POW_2_53_MINUS_1) + 1.0; |
|
| 54 | - if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) { |
|
| 55 | - throw new \RuntimeException( |
|
| 56 | - 'This class assumes floats to be double precision or "better".' |
|
| 57 | - ); |
|
| 58 | - } |
|
| 59 | - } |
|
| 46 | + /** |
|
| 47 | + * @brief Checks whether our assumptions hold on the PHP platform we are on. |
|
| 48 | + * |
|
| 49 | + * @throws \RunTimeException if our assumptions do not hold on the current |
|
| 50 | + * PHP platform. |
|
| 51 | + */ |
|
| 52 | + public function __construct() { |
|
| 53 | + $pow_2_53 = floatval(self::POW_2_53_MINUS_1) + 1.0; |
|
| 54 | + if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) { |
|
| 55 | + throw new \RuntimeException( |
|
| 56 | + 'This class assumes floats to be double precision or "better".' |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @brief Formats a signed integer or float as an unsigned integer base-10 |
|
| 63 | - * string. Passed strings will be checked for being base-10. |
|
| 64 | - * |
|
| 65 | - * @param int|float|string $number Number containing unsigned integer data |
|
| 66 | - * |
|
| 67 | - * @throws \UnexpectedValueException if $number is not a float, not an int |
|
| 68 | - * and not a base-10 string. |
|
| 69 | - * |
|
| 70 | - * @return string Unsigned integer base-10 string |
|
| 71 | - */ |
|
| 72 | - public function formatUnsignedInteger($number) { |
|
| 73 | - if (is_float($number)) { |
|
| 74 | - // Undo the effect of the php.ini setting 'precision'. |
|
| 75 | - return number_format($number, 0, '', ''); |
|
| 76 | - } else if (is_string($number) && ctype_digit($number)) { |
|
| 77 | - return $number; |
|
| 78 | - } else if (is_int($number)) { |
|
| 79 | - // Interpret signed integer as unsigned integer. |
|
| 80 | - return sprintf('%u', $number); |
|
| 81 | - } else { |
|
| 82 | - throw new \UnexpectedValueException( |
|
| 83 | - 'Expected int, float or base-10 string' |
|
| 84 | - ); |
|
| 85 | - } |
|
| 86 | - } |
|
| 61 | + /** |
|
| 62 | + * @brief Formats a signed integer or float as an unsigned integer base-10 |
|
| 63 | + * string. Passed strings will be checked for being base-10. |
|
| 64 | + * |
|
| 65 | + * @param int|float|string $number Number containing unsigned integer data |
|
| 66 | + * |
|
| 67 | + * @throws \UnexpectedValueException if $number is not a float, not an int |
|
| 68 | + * and not a base-10 string. |
|
| 69 | + * |
|
| 70 | + * @return string Unsigned integer base-10 string |
|
| 71 | + */ |
|
| 72 | + public function formatUnsignedInteger($number) { |
|
| 73 | + if (is_float($number)) { |
|
| 74 | + // Undo the effect of the php.ini setting 'precision'. |
|
| 75 | + return number_format($number, 0, '', ''); |
|
| 76 | + } else if (is_string($number) && ctype_digit($number)) { |
|
| 77 | + return $number; |
|
| 78 | + } else if (is_int($number)) { |
|
| 79 | + // Interpret signed integer as unsigned integer. |
|
| 80 | + return sprintf('%u', $number); |
|
| 81 | + } else { |
|
| 82 | + throw new \UnexpectedValueException( |
|
| 83 | + 'Expected int, float or base-10 string' |
|
| 84 | + ); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * @brief Tries to get the size of a file via various workarounds that |
|
| 90 | - * even work for large files on 32-bit platforms. |
|
| 91 | - * |
|
| 92 | - * @param string $filename Path to the file. |
|
| 93 | - * |
|
| 94 | - * @return null|int|float Number of bytes as number (float or int) or |
|
| 95 | - * null on failure. |
|
| 96 | - */ |
|
| 97 | - public function getFileSize($filename) { |
|
| 98 | - $fileSize = $this->getFileSizeViaCurl($filename); |
|
| 99 | - if (!is_null($fileSize)) { |
|
| 100 | - return $fileSize; |
|
| 101 | - } |
|
| 102 | - $fileSize = $this->getFileSizeViaExec($filename); |
|
| 103 | - if (!is_null($fileSize)) { |
|
| 104 | - return $fileSize; |
|
| 105 | - } |
|
| 106 | - return $this->getFileSizeNative($filename); |
|
| 107 | - } |
|
| 88 | + /** |
|
| 89 | + * @brief Tries to get the size of a file via various workarounds that |
|
| 90 | + * even work for large files on 32-bit platforms. |
|
| 91 | + * |
|
| 92 | + * @param string $filename Path to the file. |
|
| 93 | + * |
|
| 94 | + * @return null|int|float Number of bytes as number (float or int) or |
|
| 95 | + * null on failure. |
|
| 96 | + */ |
|
| 97 | + public function getFileSize($filename) { |
|
| 98 | + $fileSize = $this->getFileSizeViaCurl($filename); |
|
| 99 | + if (!is_null($fileSize)) { |
|
| 100 | + return $fileSize; |
|
| 101 | + } |
|
| 102 | + $fileSize = $this->getFileSizeViaExec($filename); |
|
| 103 | + if (!is_null($fileSize)) { |
|
| 104 | + return $fileSize; |
|
| 105 | + } |
|
| 106 | + return $this->getFileSizeNative($filename); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * @brief Tries to get the size of a file via a CURL HEAD request. |
|
| 111 | - * |
|
| 112 | - * @param string $fileName Path to the file. |
|
| 113 | - * |
|
| 114 | - * @return null|int|float Number of bytes as number (float or int) or |
|
| 115 | - * null on failure. |
|
| 116 | - */ |
|
| 117 | - public function getFileSizeViaCurl($fileName) { |
|
| 118 | - if (\OC::$server->getIniWrapper()->getString('open_basedir') === '') { |
|
| 119 | - $encodedFileName = rawurlencode($fileName); |
|
| 120 | - $ch = curl_init("file://$encodedFileName"); |
|
| 121 | - curl_setopt($ch, CURLOPT_NOBODY, true); |
|
| 122 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 123 | - curl_setopt($ch, CURLOPT_HEADER, true); |
|
| 124 | - $data = curl_exec($ch); |
|
| 125 | - curl_close($ch); |
|
| 126 | - if ($data !== false) { |
|
| 127 | - $matches = array(); |
|
| 128 | - preg_match('/Content-Length: (\d+)/', $data, $matches); |
|
| 129 | - if (isset($matches[1])) { |
|
| 130 | - return 0 + $matches[1]; |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - return null; |
|
| 135 | - } |
|
| 109 | + /** |
|
| 110 | + * @brief Tries to get the size of a file via a CURL HEAD request. |
|
| 111 | + * |
|
| 112 | + * @param string $fileName Path to the file. |
|
| 113 | + * |
|
| 114 | + * @return null|int|float Number of bytes as number (float or int) or |
|
| 115 | + * null on failure. |
|
| 116 | + */ |
|
| 117 | + public function getFileSizeViaCurl($fileName) { |
|
| 118 | + if (\OC::$server->getIniWrapper()->getString('open_basedir') === '') { |
|
| 119 | + $encodedFileName = rawurlencode($fileName); |
|
| 120 | + $ch = curl_init("file://$encodedFileName"); |
|
| 121 | + curl_setopt($ch, CURLOPT_NOBODY, true); |
|
| 122 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 123 | + curl_setopt($ch, CURLOPT_HEADER, true); |
|
| 124 | + $data = curl_exec($ch); |
|
| 125 | + curl_close($ch); |
|
| 126 | + if ($data !== false) { |
|
| 127 | + $matches = array(); |
|
| 128 | + preg_match('/Content-Length: (\d+)/', $data, $matches); |
|
| 129 | + if (isset($matches[1])) { |
|
| 130 | + return 0 + $matches[1]; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + return null; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * @brief Tries to get the size of a file via an exec() call. |
|
| 139 | - * |
|
| 140 | - * @param string $filename Path to the file. |
|
| 141 | - * |
|
| 142 | - * @return null|int|float Number of bytes as number (float or int) or |
|
| 143 | - * null on failure. |
|
| 144 | - */ |
|
| 145 | - public function getFileSizeViaExec($filename) { |
|
| 146 | - if (\OC_Helper::is_function_enabled('exec')) { |
|
| 147 | - $os = strtolower(php_uname('s')); |
|
| 148 | - $arg = escapeshellarg($filename); |
|
| 149 | - $result = null; |
|
| 150 | - if (strpos($os, 'linux') !== false) { |
|
| 151 | - $result = $this->exec("stat -c %s $arg"); |
|
| 152 | - } else if (strpos($os, 'bsd') !== false || strpos($os, 'darwin') !== false) { |
|
| 153 | - $result = $this->exec("stat -f %z $arg"); |
|
| 154 | - } |
|
| 155 | - return $result; |
|
| 156 | - } |
|
| 157 | - return null; |
|
| 158 | - } |
|
| 137 | + /** |
|
| 138 | + * @brief Tries to get the size of a file via an exec() call. |
|
| 139 | + * |
|
| 140 | + * @param string $filename Path to the file. |
|
| 141 | + * |
|
| 142 | + * @return null|int|float Number of bytes as number (float or int) or |
|
| 143 | + * null on failure. |
|
| 144 | + */ |
|
| 145 | + public function getFileSizeViaExec($filename) { |
|
| 146 | + if (\OC_Helper::is_function_enabled('exec')) { |
|
| 147 | + $os = strtolower(php_uname('s')); |
|
| 148 | + $arg = escapeshellarg($filename); |
|
| 149 | + $result = null; |
|
| 150 | + if (strpos($os, 'linux') !== false) { |
|
| 151 | + $result = $this->exec("stat -c %s $arg"); |
|
| 152 | + } else if (strpos($os, 'bsd') !== false || strpos($os, 'darwin') !== false) { |
|
| 153 | + $result = $this->exec("stat -f %z $arg"); |
|
| 154 | + } |
|
| 155 | + return $result; |
|
| 156 | + } |
|
| 157 | + return null; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * @brief Gets the size of a file via a filesize() call and converts |
|
| 162 | - * negative signed int to positive float. As the result of filesize() |
|
| 163 | - * will wrap around after a file size of 2^32 bytes = 4 GiB, this |
|
| 164 | - * should only be used as a last resort. |
|
| 165 | - * |
|
| 166 | - * @param string $filename Path to the file. |
|
| 167 | - * |
|
| 168 | - * @return int|float Number of bytes as number (float or int). |
|
| 169 | - */ |
|
| 170 | - public function getFileSizeNative($filename) { |
|
| 171 | - $result = filesize($filename); |
|
| 172 | - if ($result < 0) { |
|
| 173 | - // For file sizes between 2 GiB and 4 GiB, filesize() will return a |
|
| 174 | - // negative int, as the PHP data type int is signed. Interpret the |
|
| 175 | - // returned int as an unsigned integer and put it into a float. |
|
| 176 | - return (float) sprintf('%u', $result); |
|
| 177 | - } |
|
| 178 | - return $result; |
|
| 179 | - } |
|
| 160 | + /** |
|
| 161 | + * @brief Gets the size of a file via a filesize() call and converts |
|
| 162 | + * negative signed int to positive float. As the result of filesize() |
|
| 163 | + * will wrap around after a file size of 2^32 bytes = 4 GiB, this |
|
| 164 | + * should only be used as a last resort. |
|
| 165 | + * |
|
| 166 | + * @param string $filename Path to the file. |
|
| 167 | + * |
|
| 168 | + * @return int|float Number of bytes as number (float or int). |
|
| 169 | + */ |
|
| 170 | + public function getFileSizeNative($filename) { |
|
| 171 | + $result = filesize($filename); |
|
| 172 | + if ($result < 0) { |
|
| 173 | + // For file sizes between 2 GiB and 4 GiB, filesize() will return a |
|
| 174 | + // negative int, as the PHP data type int is signed. Interpret the |
|
| 175 | + // returned int as an unsigned integer and put it into a float. |
|
| 176 | + return (float) sprintf('%u', $result); |
|
| 177 | + } |
|
| 178 | + return $result; |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * Returns the current mtime for $fullPath |
|
| 183 | - * |
|
| 184 | - * @param string $fullPath |
|
| 185 | - * @return int |
|
| 186 | - */ |
|
| 187 | - public function getFileMtime($fullPath) { |
|
| 188 | - if (\OC_Helper::is_function_enabled('exec')) { |
|
| 189 | - $os = strtolower(php_uname('s')); |
|
| 190 | - if (strpos($os, 'linux') !== false) { |
|
| 191 | - return $this->exec('stat -c %Y ' . escapeshellarg($fullPath)); |
|
| 192 | - } |
|
| 193 | - } |
|
| 181 | + /** |
|
| 182 | + * Returns the current mtime for $fullPath |
|
| 183 | + * |
|
| 184 | + * @param string $fullPath |
|
| 185 | + * @return int |
|
| 186 | + */ |
|
| 187 | + public function getFileMtime($fullPath) { |
|
| 188 | + if (\OC_Helper::is_function_enabled('exec')) { |
|
| 189 | + $os = strtolower(php_uname('s')); |
|
| 190 | + if (strpos($os, 'linux') !== false) { |
|
| 191 | + return $this->exec('stat -c %Y ' . escapeshellarg($fullPath)); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - return filemtime($fullPath); |
|
| 196 | - } |
|
| 195 | + return filemtime($fullPath); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - protected function exec($cmd) { |
|
| 199 | - $result = trim(exec($cmd)); |
|
| 200 | - return ctype_digit($result) ? 0 + $result : null; |
|
| 201 | - } |
|
| 198 | + protected function exec($cmd) { |
|
| 199 | + $result = trim(exec($cmd)); |
|
| 200 | + return ctype_digit($result) ? 0 + $result : null; |
|
| 201 | + } |
|
| 202 | 202 | } |
@@ -188,7 +188,7 @@ |
||
| 188 | 188 | if (\OC_Helper::is_function_enabled('exec')) { |
| 189 | 189 | $os = strtolower(php_uname('s')); |
| 190 | 190 | if (strpos($os, 'linux') !== false) { |
| 191 | - return $this->exec('stat -c %Y ' . escapeshellarg($fullPath)); |
|
| 191 | + return $this->exec('stat -c %Y '.escapeshellarg($fullPath)); |
|
| 192 | 192 | } |
| 193 | 193 | } |
| 194 | 194 | |
@@ -28,81 +28,81 @@ |
||
| 28 | 28 | use Symfony\Component\Console\Formatter\OutputFormatterStyleInterface; |
| 29 | 29 | |
| 30 | 30 | class TimestampFormatter implements OutputFormatterInterface { |
| 31 | - /** @var IConfig */ |
|
| 32 | - protected $config; |
|
| 31 | + /** @var IConfig */ |
|
| 32 | + protected $config; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param IConfig $config |
|
| 36 | - * @param OutputFormatterInterface $formatter |
|
| 37 | - */ |
|
| 38 | - public function __construct(IConfig $config, OutputFormatterInterface $formatter) { |
|
| 39 | - $this->config = $config; |
|
| 40 | - $this->formatter = $formatter; |
|
| 41 | - } |
|
| 34 | + /** |
|
| 35 | + * @param IConfig $config |
|
| 36 | + * @param OutputFormatterInterface $formatter |
|
| 37 | + */ |
|
| 38 | + public function __construct(IConfig $config, OutputFormatterInterface $formatter) { |
|
| 39 | + $this->config = $config; |
|
| 40 | + $this->formatter = $formatter; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Sets the decorated flag. |
|
| 45 | - * |
|
| 46 | - * @param bool $decorated Whether to decorate the messages or not |
|
| 47 | - */ |
|
| 48 | - public function setDecorated($decorated) { |
|
| 49 | - $this->formatter->setDecorated($decorated); |
|
| 50 | - } |
|
| 43 | + /** |
|
| 44 | + * Sets the decorated flag. |
|
| 45 | + * |
|
| 46 | + * @param bool $decorated Whether to decorate the messages or not |
|
| 47 | + */ |
|
| 48 | + public function setDecorated($decorated) { |
|
| 49 | + $this->formatter->setDecorated($decorated); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Gets the decorated flag. |
|
| 54 | - * |
|
| 55 | - * @return bool true if the output will decorate messages, false otherwise |
|
| 56 | - */ |
|
| 57 | - public function isDecorated() { |
|
| 58 | - return $this->formatter->isDecorated(); |
|
| 59 | - } |
|
| 52 | + /** |
|
| 53 | + * Gets the decorated flag. |
|
| 54 | + * |
|
| 55 | + * @return bool true if the output will decorate messages, false otherwise |
|
| 56 | + */ |
|
| 57 | + public function isDecorated() { |
|
| 58 | + return $this->formatter->isDecorated(); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Sets a new style. |
|
| 63 | - * |
|
| 64 | - * @param string $name The style name |
|
| 65 | - * @param OutputFormatterStyleInterface $style The style instance |
|
| 66 | - */ |
|
| 67 | - public function setStyle($name, OutputFormatterStyleInterface $style) { |
|
| 68 | - $this->formatter->setStyle($name, $style); |
|
| 69 | - } |
|
| 61 | + /** |
|
| 62 | + * Sets a new style. |
|
| 63 | + * |
|
| 64 | + * @param string $name The style name |
|
| 65 | + * @param OutputFormatterStyleInterface $style The style instance |
|
| 66 | + */ |
|
| 67 | + public function setStyle($name, OutputFormatterStyleInterface $style) { |
|
| 68 | + $this->formatter->setStyle($name, $style); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Checks if output formatter has style with specified name. |
|
| 73 | - * |
|
| 74 | - * @param string $name |
|
| 75 | - * @return bool |
|
| 76 | - */ |
|
| 77 | - public function hasStyle($name) { |
|
| 78 | - $this->formatter->hasStyle($name); |
|
| 79 | - } |
|
| 71 | + /** |
|
| 72 | + * Checks if output formatter has style with specified name. |
|
| 73 | + * |
|
| 74 | + * @param string $name |
|
| 75 | + * @return bool |
|
| 76 | + */ |
|
| 77 | + public function hasStyle($name) { |
|
| 78 | + $this->formatter->hasStyle($name); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Gets style options from style with specified name. |
|
| 83 | - * |
|
| 84 | - * @param string $name |
|
| 85 | - * @return OutputFormatterStyleInterface |
|
| 86 | - */ |
|
| 87 | - public function getStyle($name) { |
|
| 88 | - return $this->formatter->getStyle($name); |
|
| 89 | - } |
|
| 81 | + /** |
|
| 82 | + * Gets style options from style with specified name. |
|
| 83 | + * |
|
| 84 | + * @param string $name |
|
| 85 | + * @return OutputFormatterStyleInterface |
|
| 86 | + */ |
|
| 87 | + public function getStyle($name) { |
|
| 88 | + return $this->formatter->getStyle($name); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Formats a message according to the given styles. |
|
| 93 | - * |
|
| 94 | - * @param string $message The message to style |
|
| 95 | - * @return string The styled message, prepended with a timestamp using the |
|
| 96 | - * log timezone and dateformat, e.g. "2015-06-23T17:24:37+02:00" |
|
| 97 | - */ |
|
| 98 | - public function format($message) { |
|
| 91 | + /** |
|
| 92 | + * Formats a message according to the given styles. |
|
| 93 | + * |
|
| 94 | + * @param string $message The message to style |
|
| 95 | + * @return string The styled message, prepended with a timestamp using the |
|
| 96 | + * log timezone and dateformat, e.g. "2015-06-23T17:24:37+02:00" |
|
| 97 | + */ |
|
| 98 | + public function format($message) { |
|
| 99 | 99 | |
| 100 | - $timeZone = $this->config->getSystemValue('logtimezone', 'UTC'); |
|
| 101 | - $timeZone = $timeZone !== null ? new \DateTimeZone($timeZone) : null; |
|
| 100 | + $timeZone = $this->config->getSystemValue('logtimezone', 'UTC'); |
|
| 101 | + $timeZone = $timeZone !== null ? new \DateTimeZone($timeZone) : null; |
|
| 102 | 102 | |
| 103 | - $time = new \DateTime('now', $timeZone); |
|
| 104 | - $timestampInfo = $time->format($this->config->getSystemValue('logdateformat', \DateTime::ATOM)); |
|
| 103 | + $time = new \DateTime('now', $timeZone); |
|
| 104 | + $timestampInfo = $time->format($this->config->getSystemValue('logdateformat', \DateTime::ATOM)); |
|
| 105 | 105 | |
| 106 | - return $timestampInfo . ' ' . $this->formatter->format($message); |
|
| 107 | - } |
|
| 106 | + return $timestampInfo . ' ' . $this->formatter->format($message); |
|
| 107 | + } |
|
| 108 | 108 | } |
@@ -103,6 +103,6 @@ |
||
| 103 | 103 | $time = new \DateTime('now', $timeZone); |
| 104 | 104 | $timestampInfo = $time->format($this->config->getSystemValue('logdateformat', \DateTime::ATOM)); |
| 105 | 105 | |
| 106 | - return $timestampInfo . ' ' . $this->formatter->format($message); |
|
| 106 | + return $timestampInfo.' '.$this->formatter->format($message); |
|
| 107 | 107 | } |
| 108 | 108 | } |
@@ -36,253 +36,253 @@ |
||
| 36 | 36 | |
| 37 | 37 | class SubAdmin extends PublicEmitter { |
| 38 | 38 | |
| 39 | - /** @var IUserManager */ |
|
| 40 | - private $userManager; |
|
| 41 | - |
|
| 42 | - /** @var IGroupManager */ |
|
| 43 | - private $groupManager; |
|
| 44 | - |
|
| 45 | - /** @var IDBConnection */ |
|
| 46 | - private $dbConn; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param IUserManager $userManager |
|
| 50 | - * @param IGroupManager $groupManager |
|
| 51 | - * @param IDBConnection $dbConn |
|
| 52 | - */ |
|
| 53 | - public function __construct(IUserManager $userManager, |
|
| 54 | - IGroupManager $groupManager, |
|
| 55 | - IDBConnection $dbConn) { |
|
| 56 | - $this->userManager = $userManager; |
|
| 57 | - $this->groupManager = $groupManager; |
|
| 58 | - $this->dbConn = $dbConn; |
|
| 59 | - |
|
| 60 | - $this->userManager->listen('\OC\User', 'postDelete', function($user) { |
|
| 61 | - $this->post_deleteUser($user); |
|
| 62 | - }); |
|
| 63 | - $this->groupManager->listen('\OC\Group', 'postDelete', function($group) { |
|
| 64 | - $this->post_deleteGroup($group); |
|
| 65 | - }); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * add a SubAdmin |
|
| 70 | - * @param IUser $user user to be SubAdmin |
|
| 71 | - * @param IGroup $group group $user becomes subadmin of |
|
| 72 | - * @return bool |
|
| 73 | - */ |
|
| 74 | - public function createSubAdmin(IUser $user, IGroup $group) { |
|
| 75 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 76 | - |
|
| 77 | - $qb->insert('group_admin') |
|
| 78 | - ->values([ |
|
| 79 | - 'gid' => $qb->createNamedParameter($group->getGID()), |
|
| 80 | - 'uid' => $qb->createNamedParameter($user->getUID()) |
|
| 81 | - ]) |
|
| 82 | - ->execute(); |
|
| 83 | - |
|
| 84 | - $this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]); |
|
| 85 | - \OC_Hook::emit("OC_SubAdmin", "post_createSubAdmin", ["gid" => $group->getGID()]); |
|
| 86 | - return true; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * delete a SubAdmin |
|
| 91 | - * @param IUser $user the user that is the SubAdmin |
|
| 92 | - * @param IGroup $group the group |
|
| 93 | - * @return bool |
|
| 94 | - */ |
|
| 95 | - public function deleteSubAdmin(IUser $user, IGroup $group) { |
|
| 96 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 97 | - |
|
| 98 | - $qb->delete('group_admin') |
|
| 99 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) |
|
| 100 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 101 | - ->execute(); |
|
| 102 | - |
|
| 103 | - $this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]); |
|
| 104 | - \OC_Hook::emit("OC_SubAdmin", "post_deleteSubAdmin", ["gid" => $group->getGID()]); |
|
| 105 | - return true; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * get groups of a SubAdmin |
|
| 110 | - * @param IUser $user the SubAdmin |
|
| 111 | - * @return IGroup[] |
|
| 112 | - */ |
|
| 113 | - public function getSubAdminsGroups(IUser $user) { |
|
| 114 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 115 | - |
|
| 116 | - $result = $qb->select('gid') |
|
| 117 | - ->from('group_admin') |
|
| 118 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 119 | - ->execute(); |
|
| 120 | - |
|
| 121 | - $groups = []; |
|
| 122 | - while($row = $result->fetch()) { |
|
| 123 | - $group = $this->groupManager->get($row['gid']); |
|
| 124 | - if(!is_null($group)) { |
|
| 125 | - $groups[] = $group; |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - $result->closeCursor(); |
|
| 129 | - |
|
| 130 | - return $groups; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * get SubAdmins of a group |
|
| 135 | - * @param IGroup $group the group |
|
| 136 | - * @return IUser[] |
|
| 137 | - */ |
|
| 138 | - public function getGroupsSubAdmins(IGroup $group) { |
|
| 139 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 140 | - |
|
| 141 | - $result = $qb->select('uid') |
|
| 142 | - ->from('group_admin') |
|
| 143 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) |
|
| 144 | - ->execute(); |
|
| 145 | - |
|
| 146 | - $users = []; |
|
| 147 | - while($row = $result->fetch()) { |
|
| 148 | - $user = $this->userManager->get($row['uid']); |
|
| 149 | - if(!is_null($user)) { |
|
| 150 | - $users[] = $user; |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - $result->closeCursor(); |
|
| 154 | - |
|
| 155 | - return $users; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * get all SubAdmins |
|
| 160 | - * @return array |
|
| 161 | - */ |
|
| 162 | - public function getAllSubAdmins() { |
|
| 163 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 164 | - |
|
| 165 | - $result = $qb->select('*') |
|
| 166 | - ->from('group_admin') |
|
| 167 | - ->execute(); |
|
| 168 | - |
|
| 169 | - $subadmins = []; |
|
| 170 | - while($row = $result->fetch()) { |
|
| 171 | - $user = $this->userManager->get($row['uid']); |
|
| 172 | - $group = $this->groupManager->get($row['gid']); |
|
| 173 | - if(!is_null($user) && !is_null($group)) { |
|
| 174 | - $subadmins[] = [ |
|
| 175 | - 'user' => $user, |
|
| 176 | - 'group' => $group |
|
| 177 | - ]; |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - $result->closeCursor(); |
|
| 181 | - |
|
| 182 | - return $subadmins; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * checks if a user is a SubAdmin of a group |
|
| 187 | - * @param IUser $user |
|
| 188 | - * @param IGroup $group |
|
| 189 | - * @return bool |
|
| 190 | - */ |
|
| 191 | - public function isSubAdminOfGroup(IUser $user, IGroup $group) { |
|
| 192 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 193 | - |
|
| 194 | - /* |
|
| 39 | + /** @var IUserManager */ |
|
| 40 | + private $userManager; |
|
| 41 | + |
|
| 42 | + /** @var IGroupManager */ |
|
| 43 | + private $groupManager; |
|
| 44 | + |
|
| 45 | + /** @var IDBConnection */ |
|
| 46 | + private $dbConn; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param IUserManager $userManager |
|
| 50 | + * @param IGroupManager $groupManager |
|
| 51 | + * @param IDBConnection $dbConn |
|
| 52 | + */ |
|
| 53 | + public function __construct(IUserManager $userManager, |
|
| 54 | + IGroupManager $groupManager, |
|
| 55 | + IDBConnection $dbConn) { |
|
| 56 | + $this->userManager = $userManager; |
|
| 57 | + $this->groupManager = $groupManager; |
|
| 58 | + $this->dbConn = $dbConn; |
|
| 59 | + |
|
| 60 | + $this->userManager->listen('\OC\User', 'postDelete', function($user) { |
|
| 61 | + $this->post_deleteUser($user); |
|
| 62 | + }); |
|
| 63 | + $this->groupManager->listen('\OC\Group', 'postDelete', function($group) { |
|
| 64 | + $this->post_deleteGroup($group); |
|
| 65 | + }); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * add a SubAdmin |
|
| 70 | + * @param IUser $user user to be SubAdmin |
|
| 71 | + * @param IGroup $group group $user becomes subadmin of |
|
| 72 | + * @return bool |
|
| 73 | + */ |
|
| 74 | + public function createSubAdmin(IUser $user, IGroup $group) { |
|
| 75 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 76 | + |
|
| 77 | + $qb->insert('group_admin') |
|
| 78 | + ->values([ |
|
| 79 | + 'gid' => $qb->createNamedParameter($group->getGID()), |
|
| 80 | + 'uid' => $qb->createNamedParameter($user->getUID()) |
|
| 81 | + ]) |
|
| 82 | + ->execute(); |
|
| 83 | + |
|
| 84 | + $this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]); |
|
| 85 | + \OC_Hook::emit("OC_SubAdmin", "post_createSubAdmin", ["gid" => $group->getGID()]); |
|
| 86 | + return true; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * delete a SubAdmin |
|
| 91 | + * @param IUser $user the user that is the SubAdmin |
|
| 92 | + * @param IGroup $group the group |
|
| 93 | + * @return bool |
|
| 94 | + */ |
|
| 95 | + public function deleteSubAdmin(IUser $user, IGroup $group) { |
|
| 96 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 97 | + |
|
| 98 | + $qb->delete('group_admin') |
|
| 99 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) |
|
| 100 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 101 | + ->execute(); |
|
| 102 | + |
|
| 103 | + $this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]); |
|
| 104 | + \OC_Hook::emit("OC_SubAdmin", "post_deleteSubAdmin", ["gid" => $group->getGID()]); |
|
| 105 | + return true; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * get groups of a SubAdmin |
|
| 110 | + * @param IUser $user the SubAdmin |
|
| 111 | + * @return IGroup[] |
|
| 112 | + */ |
|
| 113 | + public function getSubAdminsGroups(IUser $user) { |
|
| 114 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 115 | + |
|
| 116 | + $result = $qb->select('gid') |
|
| 117 | + ->from('group_admin') |
|
| 118 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 119 | + ->execute(); |
|
| 120 | + |
|
| 121 | + $groups = []; |
|
| 122 | + while($row = $result->fetch()) { |
|
| 123 | + $group = $this->groupManager->get($row['gid']); |
|
| 124 | + if(!is_null($group)) { |
|
| 125 | + $groups[] = $group; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + $result->closeCursor(); |
|
| 129 | + |
|
| 130 | + return $groups; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * get SubAdmins of a group |
|
| 135 | + * @param IGroup $group the group |
|
| 136 | + * @return IUser[] |
|
| 137 | + */ |
|
| 138 | + public function getGroupsSubAdmins(IGroup $group) { |
|
| 139 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 140 | + |
|
| 141 | + $result = $qb->select('uid') |
|
| 142 | + ->from('group_admin') |
|
| 143 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) |
|
| 144 | + ->execute(); |
|
| 145 | + |
|
| 146 | + $users = []; |
|
| 147 | + while($row = $result->fetch()) { |
|
| 148 | + $user = $this->userManager->get($row['uid']); |
|
| 149 | + if(!is_null($user)) { |
|
| 150 | + $users[] = $user; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + $result->closeCursor(); |
|
| 154 | + |
|
| 155 | + return $users; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * get all SubAdmins |
|
| 160 | + * @return array |
|
| 161 | + */ |
|
| 162 | + public function getAllSubAdmins() { |
|
| 163 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 164 | + |
|
| 165 | + $result = $qb->select('*') |
|
| 166 | + ->from('group_admin') |
|
| 167 | + ->execute(); |
|
| 168 | + |
|
| 169 | + $subadmins = []; |
|
| 170 | + while($row = $result->fetch()) { |
|
| 171 | + $user = $this->userManager->get($row['uid']); |
|
| 172 | + $group = $this->groupManager->get($row['gid']); |
|
| 173 | + if(!is_null($user) && !is_null($group)) { |
|
| 174 | + $subadmins[] = [ |
|
| 175 | + 'user' => $user, |
|
| 176 | + 'group' => $group |
|
| 177 | + ]; |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + $result->closeCursor(); |
|
| 181 | + |
|
| 182 | + return $subadmins; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * checks if a user is a SubAdmin of a group |
|
| 187 | + * @param IUser $user |
|
| 188 | + * @param IGroup $group |
|
| 189 | + * @return bool |
|
| 190 | + */ |
|
| 191 | + public function isSubAdminOfGroup(IUser $user, IGroup $group) { |
|
| 192 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 193 | + |
|
| 194 | + /* |
|
| 195 | 195 | * Primary key is ('gid', 'uid') so max 1 result possible here |
| 196 | 196 | */ |
| 197 | - $result = $qb->select('*') |
|
| 198 | - ->from('group_admin') |
|
| 199 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) |
|
| 200 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 201 | - ->execute(); |
|
| 202 | - |
|
| 203 | - $fetch = $result->fetch(); |
|
| 204 | - $result->closeCursor(); |
|
| 205 | - $result = !empty($fetch) ? true : false; |
|
| 206 | - |
|
| 207 | - return $result; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * checks if a user is a SubAdmin |
|
| 212 | - * @param IUser $user |
|
| 213 | - * @return bool |
|
| 214 | - */ |
|
| 215 | - public function isSubAdmin(IUser $user) { |
|
| 216 | - // Check if the user is already an admin |
|
| 217 | - if ($this->groupManager->isAdmin($user->getUID())) { |
|
| 218 | - return true; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 222 | - |
|
| 223 | - $result = $qb->select('gid') |
|
| 224 | - ->from('group_admin') |
|
| 225 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 226 | - ->setMaxResults(1) |
|
| 227 | - ->execute(); |
|
| 228 | - |
|
| 229 | - $isSubAdmin = $result->fetch(); |
|
| 230 | - $result->closeCursor(); |
|
| 231 | - |
|
| 232 | - $result = $isSubAdmin === false ? false : true; |
|
| 233 | - |
|
| 234 | - return $result; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * checks if a user is a accessible by a subadmin |
|
| 239 | - * @param IUser $subadmin |
|
| 240 | - * @param IUser $user |
|
| 241 | - * @return bool |
|
| 242 | - */ |
|
| 243 | - public function isUserAccessible($subadmin, $user) { |
|
| 244 | - if(!$this->isSubAdmin($subadmin)) { |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - if($this->groupManager->isAdmin($user->getUID())) { |
|
| 248 | - return false; |
|
| 249 | - } |
|
| 250 | - $accessibleGroups = $this->getSubAdminsGroups($subadmin); |
|
| 251 | - foreach($accessibleGroups as $accessibleGroup) { |
|
| 252 | - if($accessibleGroup->inGroup($user)) { |
|
| 253 | - return true; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - return false; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * delete all SubAdmins by $user |
|
| 261 | - * @param IUser $user |
|
| 262 | - * @return boolean |
|
| 263 | - */ |
|
| 264 | - private function post_deleteUser($user) { |
|
| 265 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 266 | - |
|
| 267 | - $qb->delete('group_admin') |
|
| 268 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 269 | - ->execute(); |
|
| 270 | - |
|
| 271 | - return true; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * delete all SubAdmins by $group |
|
| 276 | - * @param IGroup $group |
|
| 277 | - * @return boolean |
|
| 278 | - */ |
|
| 279 | - private function post_deleteGroup($group) { |
|
| 280 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 281 | - |
|
| 282 | - $qb->delete('group_admin') |
|
| 283 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) |
|
| 284 | - ->execute(); |
|
| 285 | - |
|
| 286 | - return true; |
|
| 287 | - } |
|
| 197 | + $result = $qb->select('*') |
|
| 198 | + ->from('group_admin') |
|
| 199 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) |
|
| 200 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 201 | + ->execute(); |
|
| 202 | + |
|
| 203 | + $fetch = $result->fetch(); |
|
| 204 | + $result->closeCursor(); |
|
| 205 | + $result = !empty($fetch) ? true : false; |
|
| 206 | + |
|
| 207 | + return $result; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * checks if a user is a SubAdmin |
|
| 212 | + * @param IUser $user |
|
| 213 | + * @return bool |
|
| 214 | + */ |
|
| 215 | + public function isSubAdmin(IUser $user) { |
|
| 216 | + // Check if the user is already an admin |
|
| 217 | + if ($this->groupManager->isAdmin($user->getUID())) { |
|
| 218 | + return true; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 222 | + |
|
| 223 | + $result = $qb->select('gid') |
|
| 224 | + ->from('group_admin') |
|
| 225 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 226 | + ->setMaxResults(1) |
|
| 227 | + ->execute(); |
|
| 228 | + |
|
| 229 | + $isSubAdmin = $result->fetch(); |
|
| 230 | + $result->closeCursor(); |
|
| 231 | + |
|
| 232 | + $result = $isSubAdmin === false ? false : true; |
|
| 233 | + |
|
| 234 | + return $result; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * checks if a user is a accessible by a subadmin |
|
| 239 | + * @param IUser $subadmin |
|
| 240 | + * @param IUser $user |
|
| 241 | + * @return bool |
|
| 242 | + */ |
|
| 243 | + public function isUserAccessible($subadmin, $user) { |
|
| 244 | + if(!$this->isSubAdmin($subadmin)) { |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + if($this->groupManager->isAdmin($user->getUID())) { |
|
| 248 | + return false; |
|
| 249 | + } |
|
| 250 | + $accessibleGroups = $this->getSubAdminsGroups($subadmin); |
|
| 251 | + foreach($accessibleGroups as $accessibleGroup) { |
|
| 252 | + if($accessibleGroup->inGroup($user)) { |
|
| 253 | + return true; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + return false; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * delete all SubAdmins by $user |
|
| 261 | + * @param IUser $user |
|
| 262 | + * @return boolean |
|
| 263 | + */ |
|
| 264 | + private function post_deleteUser($user) { |
|
| 265 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 266 | + |
|
| 267 | + $qb->delete('group_admin') |
|
| 268 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 269 | + ->execute(); |
|
| 270 | + |
|
| 271 | + return true; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * delete all SubAdmins by $group |
|
| 276 | + * @param IGroup $group |
|
| 277 | + * @return boolean |
|
| 278 | + */ |
|
| 279 | + private function post_deleteGroup($group) { |
|
| 280 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 281 | + |
|
| 282 | + $qb->delete('group_admin') |
|
| 283 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) |
|
| 284 | + ->execute(); |
|
| 285 | + |
|
| 286 | + return true; |
|
| 287 | + } |
|
| 288 | 288 | } |
@@ -119,9 +119,9 @@ discard block |
||
| 119 | 119 | ->execute(); |
| 120 | 120 | |
| 121 | 121 | $groups = []; |
| 122 | - while($row = $result->fetch()) { |
|
| 122 | + while ($row = $result->fetch()) { |
|
| 123 | 123 | $group = $this->groupManager->get($row['gid']); |
| 124 | - if(!is_null($group)) { |
|
| 124 | + if (!is_null($group)) { |
|
| 125 | 125 | $groups[] = $group; |
| 126 | 126 | } |
| 127 | 127 | } |
@@ -144,9 +144,9 @@ discard block |
||
| 144 | 144 | ->execute(); |
| 145 | 145 | |
| 146 | 146 | $users = []; |
| 147 | - while($row = $result->fetch()) { |
|
| 147 | + while ($row = $result->fetch()) { |
|
| 148 | 148 | $user = $this->userManager->get($row['uid']); |
| 149 | - if(!is_null($user)) { |
|
| 149 | + if (!is_null($user)) { |
|
| 150 | 150 | $users[] = $user; |
| 151 | 151 | } |
| 152 | 152 | } |
@@ -167,10 +167,10 @@ discard block |
||
| 167 | 167 | ->execute(); |
| 168 | 168 | |
| 169 | 169 | $subadmins = []; |
| 170 | - while($row = $result->fetch()) { |
|
| 170 | + while ($row = $result->fetch()) { |
|
| 171 | 171 | $user = $this->userManager->get($row['uid']); |
| 172 | 172 | $group = $this->groupManager->get($row['gid']); |
| 173 | - if(!is_null($user) && !is_null($group)) { |
|
| 173 | + if (!is_null($user) && !is_null($group)) { |
|
| 174 | 174 | $subadmins[] = [ |
| 175 | 175 | 'user' => $user, |
| 176 | 176 | 'group' => $group |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
| 201 | 201 | ->execute(); |
| 202 | 202 | |
| 203 | - $fetch = $result->fetch(); |
|
| 203 | + $fetch = $result->fetch(); |
|
| 204 | 204 | $result->closeCursor(); |
| 205 | 205 | $result = !empty($fetch) ? true : false; |
| 206 | 206 | |
@@ -241,15 +241,15 @@ discard block |
||
| 241 | 241 | * @return bool |
| 242 | 242 | */ |
| 243 | 243 | public function isUserAccessible($subadmin, $user) { |
| 244 | - if(!$this->isSubAdmin($subadmin)) { |
|
| 244 | + if (!$this->isSubAdmin($subadmin)) { |
|
| 245 | 245 | return false; |
| 246 | 246 | } |
| 247 | - if($this->groupManager->isAdmin($user->getUID())) { |
|
| 247 | + if ($this->groupManager->isAdmin($user->getUID())) { |
|
| 248 | 248 | return false; |
| 249 | 249 | } |
| 250 | 250 | $accessibleGroups = $this->getSubAdminsGroups($subadmin); |
| 251 | - foreach($accessibleGroups as $accessibleGroup) { |
|
| 252 | - if($accessibleGroup->inGroup($user)) { |
|
| 251 | + foreach ($accessibleGroups as $accessibleGroup) { |
|
| 252 | + if ($accessibleGroup->inGroup($user)) { |
|
| 253 | 253 | return true; |
| 254 | 254 | } |
| 255 | 255 | } |