@@ -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 | }); |
@@ -34,150 +34,150 @@ |
||
| 34 | 34 | |
| 35 | 35 | class Manager { |
| 36 | 36 | |
| 37 | - const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
| 38 | - |
|
| 39 | - /** @var AppManager */ |
|
| 40 | - private $appManager; |
|
| 41 | - |
|
| 42 | - /** @var ISession */ |
|
| 43 | - private $session; |
|
| 44 | - |
|
| 45 | - /** @var IConfig */ |
|
| 46 | - private $config; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param AppManager $appManager |
|
| 50 | - * @param ISession $session |
|
| 51 | - * @param IConfig $config |
|
| 52 | - */ |
|
| 53 | - public function __construct(AppManager $appManager, ISession $session, IConfig $config) { |
|
| 54 | - $this->appManager = $appManager; |
|
| 55 | - $this->session = $session; |
|
| 56 | - $this->config = $config; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Determine whether the user must provide a second factor challenge |
|
| 61 | - * |
|
| 62 | - * @param IUser $user |
|
| 63 | - * @return boolean |
|
| 64 | - */ |
|
| 65 | - public function isTwoFactorAuthenticated(IUser $user) { |
|
| 66 | - $twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0; |
|
| 67 | - return $twoFactorEnabled && count($this->getProviders($user)) > 0; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Disable 2FA checks for the given user |
|
| 72 | - * |
|
| 73 | - * @param IUser $user |
|
| 74 | - */ |
|
| 75 | - public function disableTwoFactorAuthentication(IUser $user) { |
|
| 76 | - $this->config->setUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 1); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Enable all 2FA checks for the given user |
|
| 81 | - * |
|
| 82 | - * @param IUser $user |
|
| 83 | - */ |
|
| 84 | - public function enableTwoFactorAuthentication(IUser $user) { |
|
| 85 | - $this->config->deleteUserValue($user->getUID(), 'core', 'two_factor_auth_disabled'); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Get a 2FA provider by its ID |
|
| 90 | - * |
|
| 91 | - * @param IUser $user |
|
| 92 | - * @param string $challengeProviderId |
|
| 93 | - * @return IProvider|null |
|
| 94 | - */ |
|
| 95 | - public function getProvider(IUser $user, $challengeProviderId) { |
|
| 96 | - $providers = $this->getProviders($user); |
|
| 97 | - return isset($providers[$challengeProviderId]) ? $providers[$challengeProviderId] : null; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get the list of 2FA providers for the given user |
|
| 102 | - * |
|
| 103 | - * @param IUser $user |
|
| 104 | - * @return IProvider[] |
|
| 105 | - */ |
|
| 106 | - public function getProviders(IUser $user) { |
|
| 107 | - $allApps = $this->appManager->getEnabledAppsForUser($user); |
|
| 108 | - $providers = []; |
|
| 109 | - |
|
| 110 | - foreach ($allApps as $appId) { |
|
| 111 | - $info = $this->appManager->getAppInfo($appId); |
|
| 112 | - if (isset($info['two-factor-providers'])) { |
|
| 113 | - $providerClasses = $info['two-factor-providers']; |
|
| 114 | - foreach ($providerClasses as $class) { |
|
| 115 | - try { |
|
| 116 | - $this->loadTwoFactorApp($appId); |
|
| 117 | - $provider = OC::$server->query($class); |
|
| 118 | - $providers[$provider->getId()] = $provider; |
|
| 119 | - } catch (QueryException $exc) { |
|
| 120 | - // Provider class can not be resolved |
|
| 121 | - throw new Exception("Could not load two-factor auth provider $class"); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - return array_filter($providers, function ($provider) use ($user) { |
|
| 128 | - /* @var $provider IProvider */ |
|
| 129 | - return $provider->isTwoFactorAuthEnabledForUser($user); |
|
| 130 | - }); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Load an app by ID if it has not been loaded yet |
|
| 135 | - * |
|
| 136 | - * @param string $appId |
|
| 137 | - */ |
|
| 138 | - protected function loadTwoFactorApp($appId) { |
|
| 139 | - if (!OC_App::isAppLoaded($appId)) { |
|
| 140 | - OC_App::loadApp($appId); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Verify the given challenge |
|
| 146 | - * |
|
| 147 | - * @param string $providerId |
|
| 148 | - * @param IUser $user |
|
| 149 | - * @param string $challenge |
|
| 150 | - * @return boolean |
|
| 151 | - */ |
|
| 152 | - public function verifyChallenge($providerId, IUser $user, $challenge) { |
|
| 153 | - $provider = $this->getProvider($user, $providerId); |
|
| 154 | - if (is_null($provider)) { |
|
| 155 | - return false; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $result = $provider->verifyChallenge($user, $challenge); |
|
| 159 | - if ($result) { |
|
| 160 | - $this->session->remove(self::SESSION_UID_KEY); |
|
| 161 | - } |
|
| 162 | - return $result; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Check if the currently logged in user needs to pass 2FA |
|
| 167 | - * |
|
| 168 | - * @return boolean |
|
| 169 | - */ |
|
| 170 | - public function needsSecondFactor() { |
|
| 171 | - return $this->session->exists(self::SESSION_UID_KEY); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Prepare the 2FA login (set session value) |
|
| 176 | - * |
|
| 177 | - * @param IUser $user |
|
| 178 | - */ |
|
| 179 | - public function prepareTwoFactorLogin(IUser $user) { |
|
| 180 | - $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
| 181 | - } |
|
| 37 | + const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
| 38 | + |
|
| 39 | + /** @var AppManager */ |
|
| 40 | + private $appManager; |
|
| 41 | + |
|
| 42 | + /** @var ISession */ |
|
| 43 | + private $session; |
|
| 44 | + |
|
| 45 | + /** @var IConfig */ |
|
| 46 | + private $config; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param AppManager $appManager |
|
| 50 | + * @param ISession $session |
|
| 51 | + * @param IConfig $config |
|
| 52 | + */ |
|
| 53 | + public function __construct(AppManager $appManager, ISession $session, IConfig $config) { |
|
| 54 | + $this->appManager = $appManager; |
|
| 55 | + $this->session = $session; |
|
| 56 | + $this->config = $config; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Determine whether the user must provide a second factor challenge |
|
| 61 | + * |
|
| 62 | + * @param IUser $user |
|
| 63 | + * @return boolean |
|
| 64 | + */ |
|
| 65 | + public function isTwoFactorAuthenticated(IUser $user) { |
|
| 66 | + $twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0; |
|
| 67 | + return $twoFactorEnabled && count($this->getProviders($user)) > 0; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Disable 2FA checks for the given user |
|
| 72 | + * |
|
| 73 | + * @param IUser $user |
|
| 74 | + */ |
|
| 75 | + public function disableTwoFactorAuthentication(IUser $user) { |
|
| 76 | + $this->config->setUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 1); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Enable all 2FA checks for the given user |
|
| 81 | + * |
|
| 82 | + * @param IUser $user |
|
| 83 | + */ |
|
| 84 | + public function enableTwoFactorAuthentication(IUser $user) { |
|
| 85 | + $this->config->deleteUserValue($user->getUID(), 'core', 'two_factor_auth_disabled'); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Get a 2FA provider by its ID |
|
| 90 | + * |
|
| 91 | + * @param IUser $user |
|
| 92 | + * @param string $challengeProviderId |
|
| 93 | + * @return IProvider|null |
|
| 94 | + */ |
|
| 95 | + public function getProvider(IUser $user, $challengeProviderId) { |
|
| 96 | + $providers = $this->getProviders($user); |
|
| 97 | + return isset($providers[$challengeProviderId]) ? $providers[$challengeProviderId] : null; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get the list of 2FA providers for the given user |
|
| 102 | + * |
|
| 103 | + * @param IUser $user |
|
| 104 | + * @return IProvider[] |
|
| 105 | + */ |
|
| 106 | + public function getProviders(IUser $user) { |
|
| 107 | + $allApps = $this->appManager->getEnabledAppsForUser($user); |
|
| 108 | + $providers = []; |
|
| 109 | + |
|
| 110 | + foreach ($allApps as $appId) { |
|
| 111 | + $info = $this->appManager->getAppInfo($appId); |
|
| 112 | + if (isset($info['two-factor-providers'])) { |
|
| 113 | + $providerClasses = $info['two-factor-providers']; |
|
| 114 | + foreach ($providerClasses as $class) { |
|
| 115 | + try { |
|
| 116 | + $this->loadTwoFactorApp($appId); |
|
| 117 | + $provider = OC::$server->query($class); |
|
| 118 | + $providers[$provider->getId()] = $provider; |
|
| 119 | + } catch (QueryException $exc) { |
|
| 120 | + // Provider class can not be resolved |
|
| 121 | + throw new Exception("Could not load two-factor auth provider $class"); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + return array_filter($providers, function ($provider) use ($user) { |
|
| 128 | + /* @var $provider IProvider */ |
|
| 129 | + return $provider->isTwoFactorAuthEnabledForUser($user); |
|
| 130 | + }); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Load an app by ID if it has not been loaded yet |
|
| 135 | + * |
|
| 136 | + * @param string $appId |
|
| 137 | + */ |
|
| 138 | + protected function loadTwoFactorApp($appId) { |
|
| 139 | + if (!OC_App::isAppLoaded($appId)) { |
|
| 140 | + OC_App::loadApp($appId); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Verify the given challenge |
|
| 146 | + * |
|
| 147 | + * @param string $providerId |
|
| 148 | + * @param IUser $user |
|
| 149 | + * @param string $challenge |
|
| 150 | + * @return boolean |
|
| 151 | + */ |
|
| 152 | + public function verifyChallenge($providerId, IUser $user, $challenge) { |
|
| 153 | + $provider = $this->getProvider($user, $providerId); |
|
| 154 | + if (is_null($provider)) { |
|
| 155 | + return false; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $result = $provider->verifyChallenge($user, $challenge); |
|
| 159 | + if ($result) { |
|
| 160 | + $this->session->remove(self::SESSION_UID_KEY); |
|
| 161 | + } |
|
| 162 | + return $result; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Check if the currently logged in user needs to pass 2FA |
|
| 167 | + * |
|
| 168 | + * @return boolean |
|
| 169 | + */ |
|
| 170 | + public function needsSecondFactor() { |
|
| 171 | + return $this->session->exists(self::SESSION_UID_KEY); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Prepare the 2FA login (set session value) |
|
| 176 | + * |
|
| 177 | + * @param IUser $user |
|
| 178 | + */ |
|
| 179 | + public function prepareTwoFactorLogin(IUser $user) { |
|
| 180 | + $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | 183 | } |
@@ -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,99 +30,99 @@ |
||
| 30 | 30 | |
| 31 | 31 | class DefaultTokenMapper extends Mapper { |
| 32 | 32 | |
| 33 | - public function __construct(IDBConnection $db) { |
|
| 34 | - parent::__construct($db, 'authtoken'); |
|
| 35 | - } |
|
| 33 | + public function __construct(IDBConnection $db) { |
|
| 34 | + parent::__construct($db, 'authtoken'); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Invalidate (delete) a given token |
|
| 39 | - * |
|
| 40 | - * @param string $token |
|
| 41 | - */ |
|
| 42 | - public function invalidate($token) { |
|
| 43 | - $qb = $this->db->getQueryBuilder(); |
|
| 44 | - $qb->delete('authtoken') |
|
| 45 | - ->andWhere($qb->expr()->eq('token', $qb->createParameter('token'))) |
|
| 46 | - ->setParameter('token', $token) |
|
| 47 | - ->execute(); |
|
| 48 | - } |
|
| 37 | + /** |
|
| 38 | + * Invalidate (delete) a given token |
|
| 39 | + * |
|
| 40 | + * @param string $token |
|
| 41 | + */ |
|
| 42 | + public function invalidate($token) { |
|
| 43 | + $qb = $this->db->getQueryBuilder(); |
|
| 44 | + $qb->delete('authtoken') |
|
| 45 | + ->andWhere($qb->expr()->eq('token', $qb->createParameter('token'))) |
|
| 46 | + ->setParameter('token', $token) |
|
| 47 | + ->execute(); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param int $olderThan |
|
| 52 | - */ |
|
| 53 | - public function invalidateOld($olderThan) { |
|
| 54 | - /* @var $qb IQueryBuilder */ |
|
| 55 | - $qb = $this->db->getQueryBuilder(); |
|
| 56 | - $qb->delete('authtoken') |
|
| 57 | - ->where($qb->expr()->lt('last_activity', $qb->createParameter('last_activity'))) |
|
| 58 | - ->andWhere($qb->expr()->eq('type', $qb->createParameter('type'))) |
|
| 59 | - ->setParameter('last_activity', $olderThan, IQueryBuilder::PARAM_INT) |
|
| 60 | - ->setParameter('type', IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT) |
|
| 61 | - ->execute(); |
|
| 62 | - } |
|
| 50 | + /** |
|
| 51 | + * @param int $olderThan |
|
| 52 | + */ |
|
| 53 | + public function invalidateOld($olderThan) { |
|
| 54 | + /* @var $qb IQueryBuilder */ |
|
| 55 | + $qb = $this->db->getQueryBuilder(); |
|
| 56 | + $qb->delete('authtoken') |
|
| 57 | + ->where($qb->expr()->lt('last_activity', $qb->createParameter('last_activity'))) |
|
| 58 | + ->andWhere($qb->expr()->eq('type', $qb->createParameter('type'))) |
|
| 59 | + ->setParameter('last_activity', $olderThan, IQueryBuilder::PARAM_INT) |
|
| 60 | + ->setParameter('type', IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT) |
|
| 61 | + ->execute(); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Get the user UID for the given token |
|
| 66 | - * |
|
| 67 | - * @param string $token |
|
| 68 | - * @throws DoesNotExistException |
|
| 69 | - * @return DefaultToken |
|
| 70 | - */ |
|
| 71 | - public function getToken($token) { |
|
| 72 | - /* @var $qb IQueryBuilder */ |
|
| 73 | - $qb = $this->db->getQueryBuilder(); |
|
| 74 | - $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check') |
|
| 75 | - ->from('authtoken') |
|
| 76 | - ->where($qb->expr()->eq('token', $qb->createParameter('token'))) |
|
| 77 | - ->setParameter('token', $token) |
|
| 78 | - ->execute(); |
|
| 64 | + /** |
|
| 65 | + * Get the user UID for the given token |
|
| 66 | + * |
|
| 67 | + * @param string $token |
|
| 68 | + * @throws DoesNotExistException |
|
| 69 | + * @return DefaultToken |
|
| 70 | + */ |
|
| 71 | + public function getToken($token) { |
|
| 72 | + /* @var $qb IQueryBuilder */ |
|
| 73 | + $qb = $this->db->getQueryBuilder(); |
|
| 74 | + $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check') |
|
| 75 | + ->from('authtoken') |
|
| 76 | + ->where($qb->expr()->eq('token', $qb->createParameter('token'))) |
|
| 77 | + ->setParameter('token', $token) |
|
| 78 | + ->execute(); |
|
| 79 | 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 | - } |
|
| 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 | 87 | |
| 88 | - /** |
|
| 89 | - * Get all token of a user |
|
| 90 | - * |
|
| 91 | - * The provider may limit the number of result rows in case of an abuse |
|
| 92 | - * where a high number of (session) tokens is generated |
|
| 93 | - * |
|
| 94 | - * @param IUser $user |
|
| 95 | - * @return DefaultToken[] |
|
| 96 | - */ |
|
| 97 | - public function getTokenByUser(IUser $user) { |
|
| 98 | - /* @var $qb IQueryBuilder */ |
|
| 99 | - $qb = $this->db->getQueryBuilder(); |
|
| 100 | - $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check') |
|
| 101 | - ->from('authtoken') |
|
| 102 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 103 | - ->setMaxResults(1000); |
|
| 104 | - $result = $qb->execute(); |
|
| 105 | - $data = $result->fetchAll(); |
|
| 106 | - $result->closeCursor(); |
|
| 88 | + /** |
|
| 89 | + * Get all token of a user |
|
| 90 | + * |
|
| 91 | + * The provider may limit the number of result rows in case of an abuse |
|
| 92 | + * where a high number of (session) tokens is generated |
|
| 93 | + * |
|
| 94 | + * @param IUser $user |
|
| 95 | + * @return DefaultToken[] |
|
| 96 | + */ |
|
| 97 | + public function getTokenByUser(IUser $user) { |
|
| 98 | + /* @var $qb IQueryBuilder */ |
|
| 99 | + $qb = $this->db->getQueryBuilder(); |
|
| 100 | + $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check') |
|
| 101 | + ->from('authtoken') |
|
| 102 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) |
|
| 103 | + ->setMaxResults(1000); |
|
| 104 | + $result = $qb->execute(); |
|
| 105 | + $data = $result->fetchAll(); |
|
| 106 | + $result->closeCursor(); |
|
| 107 | 107 | |
| 108 | - $entities = array_map(function ($row) { |
|
| 109 | - return DefaultToken::fromRow($row); |
|
| 110 | - }, $data); |
|
| 108 | + $entities = array_map(function ($row) { |
|
| 109 | + return DefaultToken::fromRow($row); |
|
| 110 | + }, $data); |
|
| 111 | 111 | |
| 112 | - return $entities; |
|
| 113 | - } |
|
| 112 | + return $entities; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * @param IUser $user |
|
| 117 | - * @param int $id |
|
| 118 | - */ |
|
| 119 | - public function deleteById(IUser $user, $id) { |
|
| 120 | - /* @var $qb IQueryBuilder */ |
|
| 121 | - $qb = $this->db->getQueryBuilder(); |
|
| 122 | - $qb->delete('authtoken') |
|
| 123 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 124 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))); |
|
| 125 | - $qb->execute(); |
|
| 126 | - } |
|
| 115 | + /** |
|
| 116 | + * @param IUser $user |
|
| 117 | + * @param int $id |
|
| 118 | + */ |
|
| 119 | + public function deleteById(IUser $user, $id) { |
|
| 120 | + /* @var $qb IQueryBuilder */ |
|
| 121 | + $qb = $this->db->getQueryBuilder(); |
|
| 122 | + $qb->delete('authtoken') |
|
| 123 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 124 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))); |
|
| 125 | + $qb->execute(); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | 128 | } |
@@ -23,64 +23,64 @@ |
||
| 23 | 23 | namespace OC; |
| 24 | 24 | |
| 25 | 25 | class RedisFactory { |
| 26 | - /** @var \Redis */ |
|
| 27 | - private $instance; |
|
| 26 | + /** @var \Redis */ |
|
| 27 | + private $instance; |
|
| 28 | 28 | |
| 29 | - /** @var SystemConfig */ |
|
| 30 | - private $config; |
|
| 29 | + /** @var SystemConfig */ |
|
| 30 | + private $config; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * RedisFactory constructor. |
|
| 34 | - * |
|
| 35 | - * @param SystemConfig $config |
|
| 36 | - */ |
|
| 37 | - public function __construct(SystemConfig $config) { |
|
| 38 | - $this->config = $config; |
|
| 39 | - } |
|
| 32 | + /** |
|
| 33 | + * RedisFactory constructor. |
|
| 34 | + * |
|
| 35 | + * @param SystemConfig $config |
|
| 36 | + */ |
|
| 37 | + public function __construct(SystemConfig $config) { |
|
| 38 | + $this->config = $config; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - private function create() { |
|
| 42 | - $this->instance = new \Redis(); |
|
| 43 | - // TODO allow configuring a RedisArray, see https://github.com/nicolasff/phpredis/blob/master/arrays.markdown#redis-arrays |
|
| 44 | - $config = $this->config->getValue('redis', array()); |
|
| 45 | - if (isset($config['host'])) { |
|
| 46 | - $host = $config['host']; |
|
| 47 | - } else { |
|
| 48 | - $host = '127.0.0.1'; |
|
| 49 | - } |
|
| 50 | - if (isset($config['port'])) { |
|
| 51 | - $port = $config['port']; |
|
| 52 | - } else { |
|
| 53 | - $port = 6379; |
|
| 54 | - } |
|
| 55 | - if (isset($config['timeout'])) { |
|
| 56 | - $timeout = $config['timeout']; |
|
| 57 | - } else { |
|
| 58 | - $timeout = 0.0; // unlimited |
|
| 59 | - } |
|
| 41 | + private function create() { |
|
| 42 | + $this->instance = new \Redis(); |
|
| 43 | + // TODO allow configuring a RedisArray, see https://github.com/nicolasff/phpredis/blob/master/arrays.markdown#redis-arrays |
|
| 44 | + $config = $this->config->getValue('redis', array()); |
|
| 45 | + if (isset($config['host'])) { |
|
| 46 | + $host = $config['host']; |
|
| 47 | + } else { |
|
| 48 | + $host = '127.0.0.1'; |
|
| 49 | + } |
|
| 50 | + if (isset($config['port'])) { |
|
| 51 | + $port = $config['port']; |
|
| 52 | + } else { |
|
| 53 | + $port = 6379; |
|
| 54 | + } |
|
| 55 | + if (isset($config['timeout'])) { |
|
| 56 | + $timeout = $config['timeout']; |
|
| 57 | + } else { |
|
| 58 | + $timeout = 0.0; // unlimited |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - $this->instance->connect($host, $port, $timeout); |
|
| 62 | - if (isset($config['password']) && $config['password'] !== '') { |
|
| 63 | - $this->instance->auth($config['password']); |
|
| 64 | - } |
|
| 61 | + $this->instance->connect($host, $port, $timeout); |
|
| 62 | + if (isset($config['password']) && $config['password'] !== '') { |
|
| 63 | + $this->instance->auth($config['password']); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - if (isset($config['dbindex'])) { |
|
| 67 | - $this->instance->select($config['dbindex']); |
|
| 68 | - } |
|
| 69 | - } |
|
| 66 | + if (isset($config['dbindex'])) { |
|
| 67 | + $this->instance->select($config['dbindex']); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - public function getInstance() { |
|
| 72 | - if (!$this->isAvailable()) { |
|
| 73 | - throw new \Exception('Redis support is not available'); |
|
| 74 | - } |
|
| 75 | - if (!$this->instance instanceof \Redis) { |
|
| 76 | - $this->create(); |
|
| 77 | - } |
|
| 71 | + public function getInstance() { |
|
| 72 | + if (!$this->isAvailable()) { |
|
| 73 | + throw new \Exception('Redis support is not available'); |
|
| 74 | + } |
|
| 75 | + if (!$this->instance instanceof \Redis) { |
|
| 76 | + $this->create(); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - return $this->instance; |
|
| 80 | - } |
|
| 79 | + return $this->instance; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - public function isAvailable() { |
|
| 83 | - return extension_loaded('redis') |
|
| 84 | - && version_compare(phpversion('redis'), '2.2.5', '>='); |
|
| 85 | - } |
|
| 82 | + public function isAvailable() { |
|
| 83 | + return extension_loaded('redis') |
|
| 84 | + && version_compare(phpversion('redis'), '2.2.5', '>='); |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -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 | } |
@@ -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 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | $this->consumers = []; |
| 115 | - foreach($this->consumersClosures as $consumer) { |
|
| 115 | + foreach ($this->consumersClosures as $consumer) { |
|
| 116 | 116 | $c = $consumer(); |
| 117 | 117 | if ($c instanceof IConsumer) { |
| 118 | 118 | $this->consumers[] = $c; |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | $this->extensions = []; |
| 136 | - foreach($this->extensionsClosures as $extension) { |
|
| 136 | + foreach ($this->extensionsClosures as $extension) { |
|
| 137 | 137 | $e = $extension(); |
| 138 | 138 | if ($e instanceof IExtension) { |
| 139 | 139 | $this->extensions[] = $e; |
@@ -669,7 +669,7 @@ discard block |
||
| 669 | 669 | return array(null, null); |
| 670 | 670 | } |
| 671 | 671 | |
| 672 | - return array(' and ((' . implode(') or (', $conditions) . '))', $parameters); |
|
| 672 | + return array(' and (('.implode(') or (', $conditions).'))', $parameters); |
|
| 673 | 673 | } |
| 674 | 674 | |
| 675 | 675 | /** |
@@ -34,501 +34,501 @@ |
||
| 34 | 34 | use OCP\IUserSession; |
| 35 | 35 | |
| 36 | 36 | class Manager implements IManager { |
| 37 | - /** @var IRequest */ |
|
| 38 | - protected $request; |
|
| 39 | - |
|
| 40 | - /** @var IUserSession */ |
|
| 41 | - protected $session; |
|
| 42 | - |
|
| 43 | - /** @var IConfig */ |
|
| 44 | - protected $config; |
|
| 45 | - |
|
| 46 | - /** @var string */ |
|
| 47 | - protected $formattingObjectType; |
|
| 48 | - |
|
| 49 | - /** @var int */ |
|
| 50 | - protected $formattingObjectId; |
|
| 51 | - |
|
| 52 | - /** @var string */ |
|
| 53 | - protected $currentUserId; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * constructor of the controller |
|
| 57 | - * |
|
| 58 | - * @param IRequest $request |
|
| 59 | - * @param IUserSession $session |
|
| 60 | - * @param IConfig $config |
|
| 61 | - */ |
|
| 62 | - public function __construct(IRequest $request, |
|
| 63 | - IUserSession $session, |
|
| 64 | - IConfig $config) { |
|
| 65 | - $this->request = $request; |
|
| 66 | - $this->session = $session; |
|
| 67 | - $this->config = $config; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** @var \Closure[] */ |
|
| 71 | - private $consumersClosures = array(); |
|
| 72 | - |
|
| 73 | - /** @var IConsumer[] */ |
|
| 74 | - private $consumers = array(); |
|
| 75 | - |
|
| 76 | - /** @var \Closure[] */ |
|
| 77 | - private $extensionsClosures = array(); |
|
| 78 | - |
|
| 79 | - /** @var IExtension[] */ |
|
| 80 | - private $extensions = array(); |
|
| 81 | - |
|
| 82 | - /** @var array list of filters "name" => "is valid" */ |
|
| 83 | - protected $validFilters = array( |
|
| 84 | - 'all' => true, |
|
| 85 | - 'by' => true, |
|
| 86 | - 'self' => true, |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - /** @var array list of type icons "type" => "css class" */ |
|
| 90 | - protected $typeIcons = array(); |
|
| 91 | - |
|
| 92 | - /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */ |
|
| 93 | - protected $specialParameters = array(); |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @return \OCP\Activity\IConsumer[] |
|
| 97 | - */ |
|
| 98 | - protected function getConsumers() { |
|
| 99 | - if (!empty($this->consumers)) { |
|
| 100 | - return $this->consumers; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $this->consumers = []; |
|
| 104 | - foreach($this->consumersClosures as $consumer) { |
|
| 105 | - $c = $consumer(); |
|
| 106 | - if ($c instanceof IConsumer) { |
|
| 107 | - $this->consumers[] = $c; |
|
| 108 | - } else { |
|
| 109 | - throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface'); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return $this->consumers; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @return \OCP\Activity\IExtension[] |
|
| 118 | - */ |
|
| 119 | - protected function getExtensions() { |
|
| 120 | - if (!empty($this->extensions)) { |
|
| 121 | - return $this->extensions; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - $this->extensions = []; |
|
| 125 | - foreach($this->extensionsClosures as $extension) { |
|
| 126 | - $e = $extension(); |
|
| 127 | - if ($e instanceof IExtension) { |
|
| 128 | - $this->extensions[] = $e; |
|
| 129 | - } else { |
|
| 130 | - throw new \InvalidArgumentException('The given extension does not implement the \OCP\Activity\IExtension interface'); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return $this->extensions; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Generates a new IEvent object |
|
| 139 | - * |
|
| 140 | - * Make sure to call at least the following methods before sending it to the |
|
| 141 | - * app with via the publish() method: |
|
| 142 | - * - setApp() |
|
| 143 | - * - setType() |
|
| 144 | - * - setAffectedUser() |
|
| 145 | - * - setSubject() |
|
| 146 | - * |
|
| 147 | - * @return IEvent |
|
| 148 | - */ |
|
| 149 | - public function generateEvent() { |
|
| 150 | - return new Event(); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Publish an event to the activity consumers |
|
| 155 | - * |
|
| 156 | - * Make sure to call at least the following methods before sending an Event: |
|
| 157 | - * - setApp() |
|
| 158 | - * - setType() |
|
| 159 | - * - setAffectedUser() |
|
| 160 | - * - setSubject() |
|
| 161 | - * |
|
| 162 | - * @param IEvent $event |
|
| 163 | - * @return null |
|
| 164 | - * @throws \BadMethodCallException if required values have not been set |
|
| 165 | - */ |
|
| 166 | - public function publish(IEvent $event) { |
|
| 167 | - if (!$event->getApp()) { |
|
| 168 | - throw new \BadMethodCallException('App not set', 10); |
|
| 169 | - } |
|
| 170 | - if (!$event->getType()) { |
|
| 171 | - throw new \BadMethodCallException('Type not set', 11); |
|
| 172 | - } |
|
| 173 | - if ($event->getAffectedUser() === null) { |
|
| 174 | - throw new \BadMethodCallException('Affected user not set', 12); |
|
| 175 | - } |
|
| 176 | - if ($event->getSubject() === null || $event->getSubjectParameters() === null) { |
|
| 177 | - throw new \BadMethodCallException('Subject not set', 13); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - if ($event->getAuthor() === null) { |
|
| 181 | - if ($this->session->getUser() instanceof IUser) { |
|
| 182 | - $event->setAuthor($this->session->getUser()->getUID()); |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - if (!$event->getTimestamp()) { |
|
| 187 | - $event->setTimestamp(time()); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - foreach ($this->getConsumers() as $c) { |
|
| 191 | - $c->receive($event); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * @param string $app The app where this event is associated with |
|
| 197 | - * @param string $subject A short description of the event |
|
| 198 | - * @param array $subjectParams Array with parameters that are filled in the subject |
|
| 199 | - * @param string $message A longer description of the event |
|
| 200 | - * @param array $messageParams Array with parameters that are filled in the message |
|
| 201 | - * @param string $file The file including path where this event is associated with |
|
| 202 | - * @param string $link A link where this event is associated with |
|
| 203 | - * @param string $affectedUser Recipient of the activity |
|
| 204 | - * @param string $type Type of the notification |
|
| 205 | - * @param int $priority Priority of the notification |
|
| 206 | - * @return null |
|
| 207 | - */ |
|
| 208 | - public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { |
|
| 209 | - $event = $this->generateEvent(); |
|
| 210 | - $event->setApp($app) |
|
| 211 | - ->setType($type) |
|
| 212 | - ->setAffectedUser($affectedUser) |
|
| 213 | - ->setSubject($subject, $subjectParams) |
|
| 214 | - ->setMessage($message, $messageParams) |
|
| 215 | - ->setObject('', 0, $file) |
|
| 216 | - ->setLink($link); |
|
| 217 | - |
|
| 218 | - $this->publish($event); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 223 | - * activity consumers are actually requested |
|
| 224 | - * |
|
| 225 | - * $callable has to return an instance of OCA\Activity\IConsumer |
|
| 226 | - * |
|
| 227 | - * @param \Closure $callable |
|
| 228 | - */ |
|
| 229 | - public function registerConsumer(\Closure $callable) { |
|
| 230 | - array_push($this->consumersClosures, $callable); |
|
| 231 | - $this->consumers = []; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 236 | - * activity consumers are actually requested |
|
| 237 | - * |
|
| 238 | - * $callable has to return an instance of OCA\Activity\IConsumer |
|
| 239 | - * |
|
| 240 | - * @param \Closure $callable |
|
| 241 | - * @return void |
|
| 242 | - */ |
|
| 243 | - public function registerExtension(\Closure $callable) { |
|
| 244 | - array_push($this->extensionsClosures, $callable); |
|
| 245 | - $this->extensions = []; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Will return additional notification types as specified by other apps |
|
| 250 | - * |
|
| 251 | - * @param string $languageCode |
|
| 252 | - * @return array |
|
| 253 | - */ |
|
| 254 | - public function getNotificationTypes($languageCode) { |
|
| 255 | - $filesNotificationTypes = []; |
|
| 256 | - $sharingNotificationTypes = []; |
|
| 257 | - |
|
| 258 | - $notificationTypes = array(); |
|
| 259 | - foreach ($this->getExtensions() as $c) { |
|
| 260 | - $result = $c->getNotificationTypes($languageCode); |
|
| 261 | - if (is_array($result)) { |
|
| 262 | - if (class_exists('\OCA\Files\Activity', false) && $c instanceof \OCA\Files\Activity) { |
|
| 263 | - $filesNotificationTypes = $result; |
|
| 264 | - continue; |
|
| 265 | - } |
|
| 266 | - if (class_exists('\OCA\Files_Sharing\Activity', false) && $c instanceof \OCA\Files_Sharing\Activity) { |
|
| 267 | - $sharingNotificationTypes = $result; |
|
| 268 | - continue; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - $notificationTypes = array_merge($notificationTypes, $result); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - return array_merge($filesNotificationTypes, $sharingNotificationTypes, $notificationTypes); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * @param string $method |
|
| 280 | - * @return array |
|
| 281 | - */ |
|
| 282 | - public function getDefaultTypes($method) { |
|
| 283 | - $defaultTypes = array(); |
|
| 284 | - foreach ($this->getExtensions() as $c) { |
|
| 285 | - $types = $c->getDefaultTypes($method); |
|
| 286 | - if (is_array($types)) { |
|
| 287 | - $defaultTypes = array_merge($types, $defaultTypes); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - return $defaultTypes; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @param string $type |
|
| 295 | - * @return string |
|
| 296 | - */ |
|
| 297 | - public function getTypeIcon($type) { |
|
| 298 | - if (isset($this->typeIcons[$type])) { |
|
| 299 | - return $this->typeIcons[$type]; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - foreach ($this->getExtensions() as $c) { |
|
| 303 | - $icon = $c->getTypeIcon($type); |
|
| 304 | - if (is_string($icon)) { |
|
| 305 | - $this->typeIcons[$type] = $icon; |
|
| 306 | - return $icon; |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - $this->typeIcons[$type] = ''; |
|
| 311 | - return ''; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * @param string $type |
|
| 316 | - * @param string $id |
|
| 317 | - */ |
|
| 318 | - public function setFormattingObject($type, $id) { |
|
| 319 | - $this->formattingObjectType = $type; |
|
| 320 | - $this->formattingObjectId = (string) $id; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * @return bool |
|
| 325 | - */ |
|
| 326 | - public function isFormattingFilteredObject() { |
|
| 327 | - return $this->formattingObjectType !== null && $this->formattingObjectId !== null |
|
| 328 | - && $this->formattingObjectType === $this->request->getParam('object_type') |
|
| 329 | - && $this->formattingObjectId === $this->request->getParam('object_id'); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * @param string $app |
|
| 334 | - * @param string $text |
|
| 335 | - * @param array $params |
|
| 336 | - * @param boolean $stripPath |
|
| 337 | - * @param boolean $highlightParams |
|
| 338 | - * @param string $languageCode |
|
| 339 | - * @return string|false |
|
| 340 | - */ |
|
| 341 | - public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { |
|
| 342 | - foreach ($this->getExtensions() as $c) { |
|
| 343 | - $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode); |
|
| 344 | - if (is_string($translation)) { |
|
| 345 | - return $translation; |
|
| 346 | - } |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - return false; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * @param string $app |
|
| 354 | - * @param string $text |
|
| 355 | - * @return array|false |
|
| 356 | - */ |
|
| 357 | - public function getSpecialParameterList($app, $text) { |
|
| 358 | - if (isset($this->specialParameters[$app][$text])) { |
|
| 359 | - return $this->specialParameters[$app][$text]; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - if (!isset($this->specialParameters[$app])) { |
|
| 363 | - $this->specialParameters[$app] = array(); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - foreach ($this->getExtensions() as $c) { |
|
| 367 | - $specialParameter = $c->getSpecialParameterList($app, $text); |
|
| 368 | - if (is_array($specialParameter)) { |
|
| 369 | - $this->specialParameters[$app][$text] = $specialParameter; |
|
| 370 | - return $specialParameter; |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - $this->specialParameters[$app][$text] = false; |
|
| 375 | - return false; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * @param array $activity |
|
| 380 | - * @return integer|false |
|
| 381 | - */ |
|
| 382 | - public function getGroupParameter($activity) { |
|
| 383 | - foreach ($this->getExtensions() as $c) { |
|
| 384 | - $parameter = $c->getGroupParameter($activity); |
|
| 385 | - if ($parameter !== false) { |
|
| 386 | - return $parameter; |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - return false; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * @return array |
|
| 395 | - */ |
|
| 396 | - public function getNavigation() { |
|
| 397 | - $entries = array( |
|
| 398 | - 'apps' => array(), |
|
| 399 | - 'top' => array(), |
|
| 400 | - ); |
|
| 401 | - foreach ($this->getExtensions() as $c) { |
|
| 402 | - $additionalEntries = $c->getNavigation(); |
|
| 403 | - if (is_array($additionalEntries)) { |
|
| 404 | - $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']); |
|
| 405 | - $entries['top'] = array_merge($entries['top'], $additionalEntries['top']); |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - return $entries; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * @param string $filterValue |
|
| 414 | - * @return boolean |
|
| 415 | - */ |
|
| 416 | - public function isFilterValid($filterValue) { |
|
| 417 | - if (isset($this->validFilters[$filterValue])) { |
|
| 418 | - return $this->validFilters[$filterValue]; |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - foreach ($this->getExtensions() as $c) { |
|
| 422 | - if ($c->isFilterValid($filterValue) === true) { |
|
| 423 | - $this->validFilters[$filterValue] = true; |
|
| 424 | - return true; |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - $this->validFilters[$filterValue] = false; |
|
| 429 | - return false; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * @param array $types |
|
| 434 | - * @param string $filter |
|
| 435 | - * @return array |
|
| 436 | - */ |
|
| 437 | - public function filterNotificationTypes($types, $filter) { |
|
| 438 | - if (!$this->isFilterValid($filter)) { |
|
| 439 | - return $types; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - foreach ($this->getExtensions() as $c) { |
|
| 443 | - $result = $c->filterNotificationTypes($types, $filter); |
|
| 444 | - if (is_array($result)) { |
|
| 445 | - $types = $result; |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - return $types; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * @param string $filter |
|
| 453 | - * @return array |
|
| 454 | - */ |
|
| 455 | - public function getQueryForFilter($filter) { |
|
| 456 | - if (!$this->isFilterValid($filter)) { |
|
| 457 | - return [null, null]; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - $conditions = array(); |
|
| 461 | - $parameters = array(); |
|
| 462 | - |
|
| 463 | - foreach ($this->getExtensions() as $c) { |
|
| 464 | - $result = $c->getQueryForFilter($filter); |
|
| 465 | - if (is_array($result)) { |
|
| 466 | - list($condition, $parameter) = $result; |
|
| 467 | - if ($condition && is_array($parameter)) { |
|
| 468 | - $conditions[] = $condition; |
|
| 469 | - $parameters = array_merge($parameters, $parameter); |
|
| 470 | - } |
|
| 471 | - } |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - if (empty($conditions)) { |
|
| 475 | - return array(null, null); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - return array(' and ((' . implode(') or (', $conditions) . '))', $parameters); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - /** |
|
| 482 | - * Set the user we need to use |
|
| 483 | - * |
|
| 484 | - * @param string|null $currentUserId |
|
| 485 | - * @throws \UnexpectedValueException If the user is invalid |
|
| 486 | - */ |
|
| 487 | - public function setCurrentUserId($currentUserId) { |
|
| 488 | - if (!is_string($currentUserId) && $currentUserId !== null) { |
|
| 489 | - throw new \UnexpectedValueException('The given current user is invalid'); |
|
| 490 | - } |
|
| 491 | - $this->currentUserId = $currentUserId; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * Get the user we need to use |
|
| 496 | - * |
|
| 497 | - * Either the user is logged in, or we try to get it from the token |
|
| 498 | - * |
|
| 499 | - * @return string |
|
| 500 | - * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
|
| 501 | - */ |
|
| 502 | - public function getCurrentUserId() { |
|
| 503 | - if ($this->currentUserId !== null) { |
|
| 504 | - return $this->currentUserId; |
|
| 505 | - } else if (!$this->session->isLoggedIn()) { |
|
| 506 | - return $this->getUserFromToken(); |
|
| 507 | - } else { |
|
| 508 | - return $this->session->getUser()->getUID(); |
|
| 509 | - } |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * Get the user for the token |
|
| 514 | - * |
|
| 515 | - * @return string |
|
| 516 | - * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
|
| 517 | - */ |
|
| 518 | - protected function getUserFromToken() { |
|
| 519 | - $token = (string) $this->request->getParam('token', ''); |
|
| 520 | - if (strlen($token) !== 30) { |
|
| 521 | - throw new \UnexpectedValueException('The token is invalid'); |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token); |
|
| 525 | - |
|
| 526 | - if (sizeof($users) !== 1) { |
|
| 527 | - // No unique user found |
|
| 528 | - throw new \UnexpectedValueException('The token is invalid'); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - // Token found login as that user |
|
| 532 | - return array_shift($users); |
|
| 533 | - } |
|
| 37 | + /** @var IRequest */ |
|
| 38 | + protected $request; |
|
| 39 | + |
|
| 40 | + /** @var IUserSession */ |
|
| 41 | + protected $session; |
|
| 42 | + |
|
| 43 | + /** @var IConfig */ |
|
| 44 | + protected $config; |
|
| 45 | + |
|
| 46 | + /** @var string */ |
|
| 47 | + protected $formattingObjectType; |
|
| 48 | + |
|
| 49 | + /** @var int */ |
|
| 50 | + protected $formattingObjectId; |
|
| 51 | + |
|
| 52 | + /** @var string */ |
|
| 53 | + protected $currentUserId; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * constructor of the controller |
|
| 57 | + * |
|
| 58 | + * @param IRequest $request |
|
| 59 | + * @param IUserSession $session |
|
| 60 | + * @param IConfig $config |
|
| 61 | + */ |
|
| 62 | + public function __construct(IRequest $request, |
|
| 63 | + IUserSession $session, |
|
| 64 | + IConfig $config) { |
|
| 65 | + $this->request = $request; |
|
| 66 | + $this->session = $session; |
|
| 67 | + $this->config = $config; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** @var \Closure[] */ |
|
| 71 | + private $consumersClosures = array(); |
|
| 72 | + |
|
| 73 | + /** @var IConsumer[] */ |
|
| 74 | + private $consumers = array(); |
|
| 75 | + |
|
| 76 | + /** @var \Closure[] */ |
|
| 77 | + private $extensionsClosures = array(); |
|
| 78 | + |
|
| 79 | + /** @var IExtension[] */ |
|
| 80 | + private $extensions = array(); |
|
| 81 | + |
|
| 82 | + /** @var array list of filters "name" => "is valid" */ |
|
| 83 | + protected $validFilters = array( |
|
| 84 | + 'all' => true, |
|
| 85 | + 'by' => true, |
|
| 86 | + 'self' => true, |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + /** @var array list of type icons "type" => "css class" */ |
|
| 90 | + protected $typeIcons = array(); |
|
| 91 | + |
|
| 92 | + /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */ |
|
| 93 | + protected $specialParameters = array(); |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @return \OCP\Activity\IConsumer[] |
|
| 97 | + */ |
|
| 98 | + protected function getConsumers() { |
|
| 99 | + if (!empty($this->consumers)) { |
|
| 100 | + return $this->consumers; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $this->consumers = []; |
|
| 104 | + foreach($this->consumersClosures as $consumer) { |
|
| 105 | + $c = $consumer(); |
|
| 106 | + if ($c instanceof IConsumer) { |
|
| 107 | + $this->consumers[] = $c; |
|
| 108 | + } else { |
|
| 109 | + throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface'); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return $this->consumers; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @return \OCP\Activity\IExtension[] |
|
| 118 | + */ |
|
| 119 | + protected function getExtensions() { |
|
| 120 | + if (!empty($this->extensions)) { |
|
| 121 | + return $this->extensions; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + $this->extensions = []; |
|
| 125 | + foreach($this->extensionsClosures as $extension) { |
|
| 126 | + $e = $extension(); |
|
| 127 | + if ($e instanceof IExtension) { |
|
| 128 | + $this->extensions[] = $e; |
|
| 129 | + } else { |
|
| 130 | + throw new \InvalidArgumentException('The given extension does not implement the \OCP\Activity\IExtension interface'); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return $this->extensions; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Generates a new IEvent object |
|
| 139 | + * |
|
| 140 | + * Make sure to call at least the following methods before sending it to the |
|
| 141 | + * app with via the publish() method: |
|
| 142 | + * - setApp() |
|
| 143 | + * - setType() |
|
| 144 | + * - setAffectedUser() |
|
| 145 | + * - setSubject() |
|
| 146 | + * |
|
| 147 | + * @return IEvent |
|
| 148 | + */ |
|
| 149 | + public function generateEvent() { |
|
| 150 | + return new Event(); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Publish an event to the activity consumers |
|
| 155 | + * |
|
| 156 | + * Make sure to call at least the following methods before sending an Event: |
|
| 157 | + * - setApp() |
|
| 158 | + * - setType() |
|
| 159 | + * - setAffectedUser() |
|
| 160 | + * - setSubject() |
|
| 161 | + * |
|
| 162 | + * @param IEvent $event |
|
| 163 | + * @return null |
|
| 164 | + * @throws \BadMethodCallException if required values have not been set |
|
| 165 | + */ |
|
| 166 | + public function publish(IEvent $event) { |
|
| 167 | + if (!$event->getApp()) { |
|
| 168 | + throw new \BadMethodCallException('App not set', 10); |
|
| 169 | + } |
|
| 170 | + if (!$event->getType()) { |
|
| 171 | + throw new \BadMethodCallException('Type not set', 11); |
|
| 172 | + } |
|
| 173 | + if ($event->getAffectedUser() === null) { |
|
| 174 | + throw new \BadMethodCallException('Affected user not set', 12); |
|
| 175 | + } |
|
| 176 | + if ($event->getSubject() === null || $event->getSubjectParameters() === null) { |
|
| 177 | + throw new \BadMethodCallException('Subject not set', 13); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + if ($event->getAuthor() === null) { |
|
| 181 | + if ($this->session->getUser() instanceof IUser) { |
|
| 182 | + $event->setAuthor($this->session->getUser()->getUID()); |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + if (!$event->getTimestamp()) { |
|
| 187 | + $event->setTimestamp(time()); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + foreach ($this->getConsumers() as $c) { |
|
| 191 | + $c->receive($event); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * @param string $app The app where this event is associated with |
|
| 197 | + * @param string $subject A short description of the event |
|
| 198 | + * @param array $subjectParams Array with parameters that are filled in the subject |
|
| 199 | + * @param string $message A longer description of the event |
|
| 200 | + * @param array $messageParams Array with parameters that are filled in the message |
|
| 201 | + * @param string $file The file including path where this event is associated with |
|
| 202 | + * @param string $link A link where this event is associated with |
|
| 203 | + * @param string $affectedUser Recipient of the activity |
|
| 204 | + * @param string $type Type of the notification |
|
| 205 | + * @param int $priority Priority of the notification |
|
| 206 | + * @return null |
|
| 207 | + */ |
|
| 208 | + public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { |
|
| 209 | + $event = $this->generateEvent(); |
|
| 210 | + $event->setApp($app) |
|
| 211 | + ->setType($type) |
|
| 212 | + ->setAffectedUser($affectedUser) |
|
| 213 | + ->setSubject($subject, $subjectParams) |
|
| 214 | + ->setMessage($message, $messageParams) |
|
| 215 | + ->setObject('', 0, $file) |
|
| 216 | + ->setLink($link); |
|
| 217 | + |
|
| 218 | + $this->publish($event); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 223 | + * activity consumers are actually requested |
|
| 224 | + * |
|
| 225 | + * $callable has to return an instance of OCA\Activity\IConsumer |
|
| 226 | + * |
|
| 227 | + * @param \Closure $callable |
|
| 228 | + */ |
|
| 229 | + public function registerConsumer(\Closure $callable) { |
|
| 230 | + array_push($this->consumersClosures, $callable); |
|
| 231 | + $this->consumers = []; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * In order to improve lazy loading a closure can be registered which will be called in case |
|
| 236 | + * activity consumers are actually requested |
|
| 237 | + * |
|
| 238 | + * $callable has to return an instance of OCA\Activity\IConsumer |
|
| 239 | + * |
|
| 240 | + * @param \Closure $callable |
|
| 241 | + * @return void |
|
| 242 | + */ |
|
| 243 | + public function registerExtension(\Closure $callable) { |
|
| 244 | + array_push($this->extensionsClosures, $callable); |
|
| 245 | + $this->extensions = []; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Will return additional notification types as specified by other apps |
|
| 250 | + * |
|
| 251 | + * @param string $languageCode |
|
| 252 | + * @return array |
|
| 253 | + */ |
|
| 254 | + public function getNotificationTypes($languageCode) { |
|
| 255 | + $filesNotificationTypes = []; |
|
| 256 | + $sharingNotificationTypes = []; |
|
| 257 | + |
|
| 258 | + $notificationTypes = array(); |
|
| 259 | + foreach ($this->getExtensions() as $c) { |
|
| 260 | + $result = $c->getNotificationTypes($languageCode); |
|
| 261 | + if (is_array($result)) { |
|
| 262 | + if (class_exists('\OCA\Files\Activity', false) && $c instanceof \OCA\Files\Activity) { |
|
| 263 | + $filesNotificationTypes = $result; |
|
| 264 | + continue; |
|
| 265 | + } |
|
| 266 | + if (class_exists('\OCA\Files_Sharing\Activity', false) && $c instanceof \OCA\Files_Sharing\Activity) { |
|
| 267 | + $sharingNotificationTypes = $result; |
|
| 268 | + continue; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + $notificationTypes = array_merge($notificationTypes, $result); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + return array_merge($filesNotificationTypes, $sharingNotificationTypes, $notificationTypes); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * @param string $method |
|
| 280 | + * @return array |
|
| 281 | + */ |
|
| 282 | + public function getDefaultTypes($method) { |
|
| 283 | + $defaultTypes = array(); |
|
| 284 | + foreach ($this->getExtensions() as $c) { |
|
| 285 | + $types = $c->getDefaultTypes($method); |
|
| 286 | + if (is_array($types)) { |
|
| 287 | + $defaultTypes = array_merge($types, $defaultTypes); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + return $defaultTypes; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @param string $type |
|
| 295 | + * @return string |
|
| 296 | + */ |
|
| 297 | + public function getTypeIcon($type) { |
|
| 298 | + if (isset($this->typeIcons[$type])) { |
|
| 299 | + return $this->typeIcons[$type]; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + foreach ($this->getExtensions() as $c) { |
|
| 303 | + $icon = $c->getTypeIcon($type); |
|
| 304 | + if (is_string($icon)) { |
|
| 305 | + $this->typeIcons[$type] = $icon; |
|
| 306 | + return $icon; |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + $this->typeIcons[$type] = ''; |
|
| 311 | + return ''; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * @param string $type |
|
| 316 | + * @param string $id |
|
| 317 | + */ |
|
| 318 | + public function setFormattingObject($type, $id) { |
|
| 319 | + $this->formattingObjectType = $type; |
|
| 320 | + $this->formattingObjectId = (string) $id; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * @return bool |
|
| 325 | + */ |
|
| 326 | + public function isFormattingFilteredObject() { |
|
| 327 | + return $this->formattingObjectType !== null && $this->formattingObjectId !== null |
|
| 328 | + && $this->formattingObjectType === $this->request->getParam('object_type') |
|
| 329 | + && $this->formattingObjectId === $this->request->getParam('object_id'); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * @param string $app |
|
| 334 | + * @param string $text |
|
| 335 | + * @param array $params |
|
| 336 | + * @param boolean $stripPath |
|
| 337 | + * @param boolean $highlightParams |
|
| 338 | + * @param string $languageCode |
|
| 339 | + * @return string|false |
|
| 340 | + */ |
|
| 341 | + public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) { |
|
| 342 | + foreach ($this->getExtensions() as $c) { |
|
| 343 | + $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode); |
|
| 344 | + if (is_string($translation)) { |
|
| 345 | + return $translation; |
|
| 346 | + } |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + return false; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * @param string $app |
|
| 354 | + * @param string $text |
|
| 355 | + * @return array|false |
|
| 356 | + */ |
|
| 357 | + public function getSpecialParameterList($app, $text) { |
|
| 358 | + if (isset($this->specialParameters[$app][$text])) { |
|
| 359 | + return $this->specialParameters[$app][$text]; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + if (!isset($this->specialParameters[$app])) { |
|
| 363 | + $this->specialParameters[$app] = array(); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + foreach ($this->getExtensions() as $c) { |
|
| 367 | + $specialParameter = $c->getSpecialParameterList($app, $text); |
|
| 368 | + if (is_array($specialParameter)) { |
|
| 369 | + $this->specialParameters[$app][$text] = $specialParameter; |
|
| 370 | + return $specialParameter; |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + $this->specialParameters[$app][$text] = false; |
|
| 375 | + return false; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * @param array $activity |
|
| 380 | + * @return integer|false |
|
| 381 | + */ |
|
| 382 | + public function getGroupParameter($activity) { |
|
| 383 | + foreach ($this->getExtensions() as $c) { |
|
| 384 | + $parameter = $c->getGroupParameter($activity); |
|
| 385 | + if ($parameter !== false) { |
|
| 386 | + return $parameter; |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + return false; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * @return array |
|
| 395 | + */ |
|
| 396 | + public function getNavigation() { |
|
| 397 | + $entries = array( |
|
| 398 | + 'apps' => array(), |
|
| 399 | + 'top' => array(), |
|
| 400 | + ); |
|
| 401 | + foreach ($this->getExtensions() as $c) { |
|
| 402 | + $additionalEntries = $c->getNavigation(); |
|
| 403 | + if (is_array($additionalEntries)) { |
|
| 404 | + $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']); |
|
| 405 | + $entries['top'] = array_merge($entries['top'], $additionalEntries['top']); |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + return $entries; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * @param string $filterValue |
|
| 414 | + * @return boolean |
|
| 415 | + */ |
|
| 416 | + public function isFilterValid($filterValue) { |
|
| 417 | + if (isset($this->validFilters[$filterValue])) { |
|
| 418 | + return $this->validFilters[$filterValue]; |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + foreach ($this->getExtensions() as $c) { |
|
| 422 | + if ($c->isFilterValid($filterValue) === true) { |
|
| 423 | + $this->validFilters[$filterValue] = true; |
|
| 424 | + return true; |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + $this->validFilters[$filterValue] = false; |
|
| 429 | + return false; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * @param array $types |
|
| 434 | + * @param string $filter |
|
| 435 | + * @return array |
|
| 436 | + */ |
|
| 437 | + public function filterNotificationTypes($types, $filter) { |
|
| 438 | + if (!$this->isFilterValid($filter)) { |
|
| 439 | + return $types; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + foreach ($this->getExtensions() as $c) { |
|
| 443 | + $result = $c->filterNotificationTypes($types, $filter); |
|
| 444 | + if (is_array($result)) { |
|
| 445 | + $types = $result; |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + return $types; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * @param string $filter |
|
| 453 | + * @return array |
|
| 454 | + */ |
|
| 455 | + public function getQueryForFilter($filter) { |
|
| 456 | + if (!$this->isFilterValid($filter)) { |
|
| 457 | + return [null, null]; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + $conditions = array(); |
|
| 461 | + $parameters = array(); |
|
| 462 | + |
|
| 463 | + foreach ($this->getExtensions() as $c) { |
|
| 464 | + $result = $c->getQueryForFilter($filter); |
|
| 465 | + if (is_array($result)) { |
|
| 466 | + list($condition, $parameter) = $result; |
|
| 467 | + if ($condition && is_array($parameter)) { |
|
| 468 | + $conditions[] = $condition; |
|
| 469 | + $parameters = array_merge($parameters, $parameter); |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + if (empty($conditions)) { |
|
| 475 | + return array(null, null); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + return array(' and ((' . implode(') or (', $conditions) . '))', $parameters); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * Set the user we need to use |
|
| 483 | + * |
|
| 484 | + * @param string|null $currentUserId |
|
| 485 | + * @throws \UnexpectedValueException If the user is invalid |
|
| 486 | + */ |
|
| 487 | + public function setCurrentUserId($currentUserId) { |
|
| 488 | + if (!is_string($currentUserId) && $currentUserId !== null) { |
|
| 489 | + throw new \UnexpectedValueException('The given current user is invalid'); |
|
| 490 | + } |
|
| 491 | + $this->currentUserId = $currentUserId; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * Get the user we need to use |
|
| 496 | + * |
|
| 497 | + * Either the user is logged in, or we try to get it from the token |
|
| 498 | + * |
|
| 499 | + * @return string |
|
| 500 | + * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
|
| 501 | + */ |
|
| 502 | + public function getCurrentUserId() { |
|
| 503 | + if ($this->currentUserId !== null) { |
|
| 504 | + return $this->currentUserId; |
|
| 505 | + } else if (!$this->session->isLoggedIn()) { |
|
| 506 | + return $this->getUserFromToken(); |
|
| 507 | + } else { |
|
| 508 | + return $this->session->getUser()->getUID(); |
|
| 509 | + } |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * Get the user for the token |
|
| 514 | + * |
|
| 515 | + * @return string |
|
| 516 | + * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique |
|
| 517 | + */ |
|
| 518 | + protected function getUserFromToken() { |
|
| 519 | + $token = (string) $this->request->getParam('token', ''); |
|
| 520 | + if (strlen($token) !== 30) { |
|
| 521 | + throw new \UnexpectedValueException('The token is invalid'); |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token); |
|
| 525 | + |
|
| 526 | + if (sizeof($users) !== 1) { |
|
| 527 | + // No unique user found |
|
| 528 | + throw new \UnexpectedValueException('The token is invalid'); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + // Token found login as that user |
|
| 532 | + return array_shift($users); |
|
| 533 | + } |
|
| 534 | 534 | } |
@@ -29,172 +29,172 @@ |
||
| 29 | 29 | * @package OC\Security\CSP |
| 30 | 30 | */ |
| 31 | 31 | class ContentSecurityPolicy extends \OCP\AppFramework\Http\ContentSecurityPolicy { |
| 32 | - /** |
|
| 33 | - * @return boolean |
|
| 34 | - */ |
|
| 35 | - public function isInlineScriptAllowed() { |
|
| 36 | - return $this->inlineScriptAllowed; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param boolean $inlineScriptAllowed |
|
| 41 | - */ |
|
| 42 | - public function setInlineScriptAllowed($inlineScriptAllowed) { |
|
| 43 | - $this->inlineScriptAllowed = $inlineScriptAllowed; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @return boolean |
|
| 48 | - */ |
|
| 49 | - public function isEvalScriptAllowed() { |
|
| 50 | - return $this->evalScriptAllowed; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param boolean $evalScriptAllowed |
|
| 55 | - */ |
|
| 56 | - public function setEvalScriptAllowed($evalScriptAllowed) { |
|
| 57 | - $this->evalScriptAllowed = $evalScriptAllowed; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @return array |
|
| 62 | - */ |
|
| 63 | - public function getAllowedScriptDomains() { |
|
| 64 | - return $this->allowedScriptDomains; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param array $allowedScriptDomains |
|
| 69 | - */ |
|
| 70 | - public function setAllowedScriptDomains($allowedScriptDomains) { |
|
| 71 | - $this->allowedScriptDomains = $allowedScriptDomains; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @return boolean |
|
| 76 | - */ |
|
| 77 | - public function isInlineStyleAllowed() { |
|
| 78 | - return $this->inlineStyleAllowed; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param boolean $inlineStyleAllowed |
|
| 83 | - */ |
|
| 84 | - public function setInlineStyleAllowed($inlineStyleAllowed) { |
|
| 85 | - $this->inlineStyleAllowed = $inlineStyleAllowed; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return array |
|
| 90 | - */ |
|
| 91 | - public function getAllowedStyleDomains() { |
|
| 92 | - return $this->allowedStyleDomains; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @param array $allowedStyleDomains |
|
| 97 | - */ |
|
| 98 | - public function setAllowedStyleDomains($allowedStyleDomains) { |
|
| 99 | - $this->allowedStyleDomains = $allowedStyleDomains; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @return array |
|
| 104 | - */ |
|
| 105 | - public function getAllowedImageDomains() { |
|
| 106 | - return $this->allowedImageDomains; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @param array $allowedImageDomains |
|
| 111 | - */ |
|
| 112 | - public function setAllowedImageDomains($allowedImageDomains) { |
|
| 113 | - $this->allowedImageDomains = $allowedImageDomains; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @return array |
|
| 118 | - */ |
|
| 119 | - public function getAllowedConnectDomains() { |
|
| 120 | - return $this->allowedConnectDomains; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @param array $allowedConnectDomains |
|
| 125 | - */ |
|
| 126 | - public function setAllowedConnectDomains($allowedConnectDomains) { |
|
| 127 | - $this->allowedConnectDomains = $allowedConnectDomains; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @return array |
|
| 132 | - */ |
|
| 133 | - public function getAllowedMediaDomains() { |
|
| 134 | - return $this->allowedMediaDomains; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param array $allowedMediaDomains |
|
| 139 | - */ |
|
| 140 | - public function setAllowedMediaDomains($allowedMediaDomains) { |
|
| 141 | - $this->allowedMediaDomains = $allowedMediaDomains; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @return array |
|
| 146 | - */ |
|
| 147 | - public function getAllowedObjectDomains() { |
|
| 148 | - return $this->allowedObjectDomains; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @param array $allowedObjectDomains |
|
| 153 | - */ |
|
| 154 | - public function setAllowedObjectDomains($allowedObjectDomains) { |
|
| 155 | - $this->allowedObjectDomains = $allowedObjectDomains; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @return array |
|
| 160 | - */ |
|
| 161 | - public function getAllowedFrameDomains() { |
|
| 162 | - return $this->allowedFrameDomains; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * @param array $allowedFrameDomains |
|
| 167 | - */ |
|
| 168 | - public function setAllowedFrameDomains($allowedFrameDomains) { |
|
| 169 | - $this->allowedFrameDomains = $allowedFrameDomains; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @return array |
|
| 174 | - */ |
|
| 175 | - public function getAllowedFontDomains() { |
|
| 176 | - return $this->allowedFontDomains; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @param array $allowedFontDomains |
|
| 181 | - */ |
|
| 182 | - public function setAllowedFontDomains($allowedFontDomains) { |
|
| 183 | - $this->allowedFontDomains = $allowedFontDomains; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * @return array |
|
| 188 | - */ |
|
| 189 | - public function getAllowedChildSrcDomains() { |
|
| 190 | - return $this->allowedChildSrcDomains; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param array $allowedChildSrcDomains |
|
| 195 | - */ |
|
| 196 | - public function setAllowedChildSrcDomains($allowedChildSrcDomains) { |
|
| 197 | - $this->allowedChildSrcDomains = $allowedChildSrcDomains; |
|
| 198 | - } |
|
| 32 | + /** |
|
| 33 | + * @return boolean |
|
| 34 | + */ |
|
| 35 | + public function isInlineScriptAllowed() { |
|
| 36 | + return $this->inlineScriptAllowed; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param boolean $inlineScriptAllowed |
|
| 41 | + */ |
|
| 42 | + public function setInlineScriptAllowed($inlineScriptAllowed) { |
|
| 43 | + $this->inlineScriptAllowed = $inlineScriptAllowed; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @return boolean |
|
| 48 | + */ |
|
| 49 | + public function isEvalScriptAllowed() { |
|
| 50 | + return $this->evalScriptAllowed; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param boolean $evalScriptAllowed |
|
| 55 | + */ |
|
| 56 | + public function setEvalScriptAllowed($evalScriptAllowed) { |
|
| 57 | + $this->evalScriptAllowed = $evalScriptAllowed; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @return array |
|
| 62 | + */ |
|
| 63 | + public function getAllowedScriptDomains() { |
|
| 64 | + return $this->allowedScriptDomains; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param array $allowedScriptDomains |
|
| 69 | + */ |
|
| 70 | + public function setAllowedScriptDomains($allowedScriptDomains) { |
|
| 71 | + $this->allowedScriptDomains = $allowedScriptDomains; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @return boolean |
|
| 76 | + */ |
|
| 77 | + public function isInlineStyleAllowed() { |
|
| 78 | + return $this->inlineStyleAllowed; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param boolean $inlineStyleAllowed |
|
| 83 | + */ |
|
| 84 | + public function setInlineStyleAllowed($inlineStyleAllowed) { |
|
| 85 | + $this->inlineStyleAllowed = $inlineStyleAllowed; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return array |
|
| 90 | + */ |
|
| 91 | + public function getAllowedStyleDomains() { |
|
| 92 | + return $this->allowedStyleDomains; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @param array $allowedStyleDomains |
|
| 97 | + */ |
|
| 98 | + public function setAllowedStyleDomains($allowedStyleDomains) { |
|
| 99 | + $this->allowedStyleDomains = $allowedStyleDomains; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @return array |
|
| 104 | + */ |
|
| 105 | + public function getAllowedImageDomains() { |
|
| 106 | + return $this->allowedImageDomains; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @param array $allowedImageDomains |
|
| 111 | + */ |
|
| 112 | + public function setAllowedImageDomains($allowedImageDomains) { |
|
| 113 | + $this->allowedImageDomains = $allowedImageDomains; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @return array |
|
| 118 | + */ |
|
| 119 | + public function getAllowedConnectDomains() { |
|
| 120 | + return $this->allowedConnectDomains; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param array $allowedConnectDomains |
|
| 125 | + */ |
|
| 126 | + public function setAllowedConnectDomains($allowedConnectDomains) { |
|
| 127 | + $this->allowedConnectDomains = $allowedConnectDomains; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @return array |
|
| 132 | + */ |
|
| 133 | + public function getAllowedMediaDomains() { |
|
| 134 | + return $this->allowedMediaDomains; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param array $allowedMediaDomains |
|
| 139 | + */ |
|
| 140 | + public function setAllowedMediaDomains($allowedMediaDomains) { |
|
| 141 | + $this->allowedMediaDomains = $allowedMediaDomains; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @return array |
|
| 146 | + */ |
|
| 147 | + public function getAllowedObjectDomains() { |
|
| 148 | + return $this->allowedObjectDomains; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @param array $allowedObjectDomains |
|
| 153 | + */ |
|
| 154 | + public function setAllowedObjectDomains($allowedObjectDomains) { |
|
| 155 | + $this->allowedObjectDomains = $allowedObjectDomains; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @return array |
|
| 160 | + */ |
|
| 161 | + public function getAllowedFrameDomains() { |
|
| 162 | + return $this->allowedFrameDomains; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * @param array $allowedFrameDomains |
|
| 167 | + */ |
|
| 168 | + public function setAllowedFrameDomains($allowedFrameDomains) { |
|
| 169 | + $this->allowedFrameDomains = $allowedFrameDomains; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @return array |
|
| 174 | + */ |
|
| 175 | + public function getAllowedFontDomains() { |
|
| 176 | + return $this->allowedFontDomains; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @param array $allowedFontDomains |
|
| 181 | + */ |
|
| 182 | + public function setAllowedFontDomains($allowedFontDomains) { |
|
| 183 | + $this->allowedFontDomains = $allowedFontDomains; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * @return array |
|
| 188 | + */ |
|
| 189 | + public function getAllowedChildSrcDomains() { |
|
| 190 | + return $this->allowedChildSrcDomains; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param array $allowedChildSrcDomains |
|
| 195 | + */ |
|
| 196 | + public function setAllowedChildSrcDomains($allowedChildSrcDomains) { |
|
| 197 | + $this->allowedChildSrcDomains = $allowedChildSrcDomains; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | 200 | } |
@@ -27,48 +27,48 @@ |
||
| 27 | 27 | use OCP\Security\IContentSecurityPolicyManager; |
| 28 | 28 | |
| 29 | 29 | class ContentSecurityPolicyManager implements IContentSecurityPolicyManager { |
| 30 | - /** @var ContentSecurityPolicy[] */ |
|
| 31 | - private $policies = []; |
|
| 30 | + /** @var ContentSecurityPolicy[] */ |
|
| 31 | + private $policies = []; |
|
| 32 | 32 | |
| 33 | - /** {@inheritdoc} */ |
|
| 34 | - public function addDefaultPolicy(EmptyContentSecurityPolicy $policy) { |
|
| 35 | - $this->policies[] = $policy; |
|
| 36 | - } |
|
| 33 | + /** {@inheritdoc} */ |
|
| 34 | + public function addDefaultPolicy(EmptyContentSecurityPolicy $policy) { |
|
| 35 | + $this->policies[] = $policy; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Get the configured default policy. This is not in the public namespace |
|
| 40 | - * as it is only supposed to be used by core itself. |
|
| 41 | - * |
|
| 42 | - * @return ContentSecurityPolicy |
|
| 43 | - */ |
|
| 44 | - public function getDefaultPolicy() { |
|
| 45 | - $defaultPolicy = new \OC\Security\CSP\ContentSecurityPolicy(); |
|
| 46 | - foreach($this->policies as $policy) { |
|
| 47 | - $defaultPolicy = $this->mergePolicies($defaultPolicy, $policy); |
|
| 48 | - } |
|
| 49 | - return $defaultPolicy; |
|
| 50 | - } |
|
| 38 | + /** |
|
| 39 | + * Get the configured default policy. This is not in the public namespace |
|
| 40 | + * as it is only supposed to be used by core itself. |
|
| 41 | + * |
|
| 42 | + * @return ContentSecurityPolicy |
|
| 43 | + */ |
|
| 44 | + public function getDefaultPolicy() { |
|
| 45 | + $defaultPolicy = new \OC\Security\CSP\ContentSecurityPolicy(); |
|
| 46 | + foreach($this->policies as $policy) { |
|
| 47 | + $defaultPolicy = $this->mergePolicies($defaultPolicy, $policy); |
|
| 48 | + } |
|
| 49 | + return $defaultPolicy; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Merges the first given policy with the second one |
|
| 54 | - * |
|
| 55 | - * @param ContentSecurityPolicy $defaultPolicy |
|
| 56 | - * @param EmptyContentSecurityPolicy $originalPolicy |
|
| 57 | - * @return ContentSecurityPolicy |
|
| 58 | - */ |
|
| 59 | - public function mergePolicies(ContentSecurityPolicy $defaultPolicy, |
|
| 60 | - EmptyContentSecurityPolicy $originalPolicy) { |
|
| 61 | - foreach((object)(array)$originalPolicy as $name => $value) { |
|
| 62 | - $setter = 'set'.ucfirst($name); |
|
| 63 | - if(is_array($value)) { |
|
| 64 | - $getter = 'get'.ucfirst($name); |
|
| 65 | - $currentValues = is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : []; |
|
| 66 | - $defaultPolicy->$setter(array_values(array_unique(array_merge($currentValues, $value)))); |
|
| 67 | - } elseif (is_bool($value)) { |
|
| 68 | - $defaultPolicy->$setter($value); |
|
| 69 | - } |
|
| 70 | - } |
|
| 52 | + /** |
|
| 53 | + * Merges the first given policy with the second one |
|
| 54 | + * |
|
| 55 | + * @param ContentSecurityPolicy $defaultPolicy |
|
| 56 | + * @param EmptyContentSecurityPolicy $originalPolicy |
|
| 57 | + * @return ContentSecurityPolicy |
|
| 58 | + */ |
|
| 59 | + public function mergePolicies(ContentSecurityPolicy $defaultPolicy, |
|
| 60 | + EmptyContentSecurityPolicy $originalPolicy) { |
|
| 61 | + foreach((object)(array)$originalPolicy as $name => $value) { |
|
| 62 | + $setter = 'set'.ucfirst($name); |
|
| 63 | + if(is_array($value)) { |
|
| 64 | + $getter = 'get'.ucfirst($name); |
|
| 65 | + $currentValues = is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : []; |
|
| 66 | + $defaultPolicy->$setter(array_values(array_unique(array_merge($currentValues, $value)))); |
|
| 67 | + } elseif (is_bool($value)) { |
|
| 68 | + $defaultPolicy->$setter($value); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - return $defaultPolicy; |
|
| 73 | - } |
|
| 72 | + return $defaultPolicy; |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function getDefaultPolicy() { |
| 45 | 45 | $defaultPolicy = new \OC\Security\CSP\ContentSecurityPolicy(); |
| 46 | - foreach($this->policies as $policy) { |
|
| 46 | + foreach ($this->policies as $policy) { |
|
| 47 | 47 | $defaultPolicy = $this->mergePolicies($defaultPolicy, $policy); |
| 48 | 48 | } |
| 49 | 49 | return $defaultPolicy; |
@@ -58,9 +58,9 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | public function mergePolicies(ContentSecurityPolicy $defaultPolicy, |
| 60 | 60 | EmptyContentSecurityPolicy $originalPolicy) { |
| 61 | - foreach((object)(array)$originalPolicy as $name => $value) { |
|
| 61 | + foreach ((object) (array) $originalPolicy as $name => $value) { |
|
| 62 | 62 | $setter = 'set'.ucfirst($name); |
| 63 | - if(is_array($value)) { |
|
| 63 | + if (is_array($value)) { |
|
| 64 | 64 | $getter = 'get'.ucfirst($name); |
| 65 | 65 | $currentValues = is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : []; |
| 66 | 66 | $defaultPolicy->$setter(array_values(array_unique(array_merge($currentValues, $value)))); |
@@ -32,68 +32,68 @@ |
||
| 32 | 32 | * @package OC\Security |
| 33 | 33 | */ |
| 34 | 34 | class TrustedDomainHelper { |
| 35 | - /** @var IConfig */ |
|
| 36 | - private $config; |
|
| 35 | + /** @var IConfig */ |
|
| 36 | + private $config; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param IConfig $config |
|
| 40 | - */ |
|
| 41 | - function __construct(IConfig $config) { |
|
| 42 | - $this->config = $config; |
|
| 43 | - } |
|
| 38 | + /** |
|
| 39 | + * @param IConfig $config |
|
| 40 | + */ |
|
| 41 | + function __construct(IConfig $config) { |
|
| 42 | + $this->config = $config; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Strips a potential port from a domain (in format domain:port) |
|
| 47 | - * @param string $host |
|
| 48 | - * @return string $host without appended port |
|
| 49 | - */ |
|
| 50 | - private function getDomainWithoutPort($host) { |
|
| 51 | - $pos = strrpos($host, ':'); |
|
| 52 | - if ($pos !== false) { |
|
| 53 | - $port = substr($host, $pos + 1); |
|
| 54 | - if (is_numeric($port)) { |
|
| 55 | - $host = substr($host, 0, $pos); |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - return $host; |
|
| 59 | - } |
|
| 45 | + /** |
|
| 46 | + * Strips a potential port from a domain (in format domain:port) |
|
| 47 | + * @param string $host |
|
| 48 | + * @return string $host without appended port |
|
| 49 | + */ |
|
| 50 | + private function getDomainWithoutPort($host) { |
|
| 51 | + $pos = strrpos($host, ':'); |
|
| 52 | + if ($pos !== false) { |
|
| 53 | + $port = substr($host, $pos + 1); |
|
| 54 | + if (is_numeric($port)) { |
|
| 55 | + $host = substr($host, 0, $pos); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + return $host; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Checks whether a domain is considered as trusted from the list |
|
| 63 | - * of trusted domains. If no trusted domains have been configured, returns |
|
| 64 | - * true. |
|
| 65 | - * This is used to prevent Host Header Poisoning. |
|
| 66 | - * @param string $domainWithPort |
|
| 67 | - * @return bool true if the given domain is trusted or if no trusted domains |
|
| 68 | - * have been configured |
|
| 69 | - */ |
|
| 70 | - public function isTrustedDomain($domainWithPort) { |
|
| 71 | - $domain = $this->getDomainWithoutPort($domainWithPort); |
|
| 61 | + /** |
|
| 62 | + * Checks whether a domain is considered as trusted from the list |
|
| 63 | + * of trusted domains. If no trusted domains have been configured, returns |
|
| 64 | + * true. |
|
| 65 | + * This is used to prevent Host Header Poisoning. |
|
| 66 | + * @param string $domainWithPort |
|
| 67 | + * @return bool true if the given domain is trusted or if no trusted domains |
|
| 68 | + * have been configured |
|
| 69 | + */ |
|
| 70 | + public function isTrustedDomain($domainWithPort) { |
|
| 71 | + $domain = $this->getDomainWithoutPort($domainWithPort); |
|
| 72 | 72 | |
| 73 | - // Read trusted domains from config |
|
| 74 | - $trustedList = $this->config->getSystemValue('trusted_domains', []); |
|
| 75 | - if (!is_array($trustedList)) { |
|
| 76 | - return false; |
|
| 77 | - } |
|
| 73 | + // Read trusted domains from config |
|
| 74 | + $trustedList = $this->config->getSystemValue('trusted_domains', []); |
|
| 75 | + if (!is_array($trustedList)) { |
|
| 76 | + return false; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - // Always allow access from localhost |
|
| 80 | - if (preg_match(Request::REGEX_LOCALHOST, $domain) === 1) { |
|
| 81 | - return true; |
|
| 82 | - } |
|
| 83 | - // Reject misformed domains in any case |
|
| 84 | - if (strpos($domain,'-') === 0 || strpos($domain,'..') !== false) { |
|
| 85 | - return false; |
|
| 86 | - } |
|
| 87 | - // Match, allowing for * wildcards |
|
| 88 | - foreach ($trustedList as $trusted) { |
|
| 89 | - if (gettype($trusted) !== 'string') { |
|
| 90 | - break; |
|
| 91 | - } |
|
| 92 | - $regex = '/^' . join('[-\.a-zA-Z0-9]*', array_map(function($v) { return preg_quote($v, '/'); }, explode('*', $trusted))) . '$/'; |
|
| 93 | - if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) { |
|
| 94 | - return true; |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 79 | + // Always allow access from localhost |
|
| 80 | + if (preg_match(Request::REGEX_LOCALHOST, $domain) === 1) { |
|
| 81 | + return true; |
|
| 82 | + } |
|
| 83 | + // Reject misformed domains in any case |
|
| 84 | + if (strpos($domain,'-') === 0 || strpos($domain,'..') !== false) { |
|
| 85 | + return false; |
|
| 86 | + } |
|
| 87 | + // Match, allowing for * wildcards |
|
| 88 | + foreach ($trustedList as $trusted) { |
|
| 89 | + if (gettype($trusted) !== 'string') { |
|
| 90 | + break; |
|
| 91 | + } |
|
| 92 | + $regex = '/^' . join('[-\.a-zA-Z0-9]*', array_map(function($v) { return preg_quote($v, '/'); }, explode('*', $trusted))) . '$/'; |
|
| 93 | + if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) { |
|
| 94 | + return true; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | return true; |
| 82 | 82 | } |
| 83 | 83 | // Reject misformed domains in any case |
| 84 | - if (strpos($domain,'-') === 0 || strpos($domain,'..') !== false) { |
|
| 84 | + if (strpos($domain, '-') === 0 || strpos($domain, '..') !== false) { |
|
| 85 | 85 | return false; |
| 86 | 86 | } |
| 87 | 87 | // Match, allowing for * wildcards |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | if (gettype($trusted) !== 'string') { |
| 90 | 90 | break; |
| 91 | 91 | } |
| 92 | - $regex = '/^' . join('[-\.a-zA-Z0-9]*', array_map(function($v) { return preg_quote($v, '/'); }, explode('*', $trusted))) . '$/'; |
|
| 92 | + $regex = '/^'.join('[-\.a-zA-Z0-9]*', array_map(function($v) { return preg_quote($v, '/'); }, explode('*', $trusted))).'$/'; |
|
| 93 | 93 | if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) { |
| 94 | 94 | return true; |
| 95 | 95 | } |
@@ -46,116 +46,116 @@ |
||
| 46 | 46 | * @package OC\Security |
| 47 | 47 | */ |
| 48 | 48 | class Hasher implements IHasher { |
| 49 | - /** @var IConfig */ |
|
| 50 | - private $config; |
|
| 51 | - /** @var array Options passed to password_hash and password_needs_rehash */ |
|
| 52 | - private $options = array(); |
|
| 53 | - /** @var string Salt used for legacy passwords */ |
|
| 54 | - private $legacySalt = null; |
|
| 55 | - /** @var int Current version of the generated hash */ |
|
| 56 | - private $currentVersion = 1; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param IConfig $config |
|
| 60 | - */ |
|
| 61 | - function __construct(IConfig $config) { |
|
| 62 | - $this->config = $config; |
|
| 63 | - |
|
| 64 | - $hashingCost = $this->config->getSystemValue('hashingCost', null); |
|
| 65 | - if(!is_null($hashingCost)) { |
|
| 66 | - $this->options['cost'] = $hashingCost; |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Hashes a message using PHP's `password_hash` functionality. |
|
| 72 | - * Please note that the size of the returned string is not guaranteed |
|
| 73 | - * and can be up to 255 characters. |
|
| 74 | - * |
|
| 75 | - * @param string $message Message to generate hash from |
|
| 76 | - * @return string Hash of the message with appended version parameter |
|
| 77 | - */ |
|
| 78 | - public function hash($message) { |
|
| 79 | - return $this->currentVersion . '|' . password_hash($message, PASSWORD_DEFAULT, $this->options); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Get the version and hash from a prefixedHash |
|
| 84 | - * @param string $prefixedHash |
|
| 85 | - * @return null|array Null if the hash is not prefixed, otherwise array('version' => 1, 'hash' => 'foo') |
|
| 86 | - */ |
|
| 87 | - protected function splitHash($prefixedHash) { |
|
| 88 | - $explodedString = explode('|', $prefixedHash, 2); |
|
| 89 | - if(sizeof($explodedString) === 2) { |
|
| 90 | - if((int)$explodedString[0] > 0) { |
|
| 91 | - return array('version' => (int)$explodedString[0], 'hash' => $explodedString[1]); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - return null; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Verify legacy hashes |
|
| 100 | - * @param string $message Message to verify |
|
| 101 | - * @param string $hash Assumed hash of the message |
|
| 102 | - * @param null|string &$newHash Reference will contain the updated hash |
|
| 103 | - * @return bool Whether $hash is a valid hash of $message |
|
| 104 | - */ |
|
| 105 | - protected function legacyHashVerify($message, $hash, &$newHash = null) { |
|
| 106 | - if(empty($this->legacySalt)) { |
|
| 107 | - $this->legacySalt = $this->config->getSystemValue('passwordsalt', ''); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // Verify whether it matches a legacy PHPass or SHA1 string |
|
| 111 | - $hashLength = strlen($hash); |
|
| 112 | - if($hashLength === 60 && password_verify($message.$this->legacySalt, $hash) || |
|
| 113 | - $hashLength === 40 && hash_equals($hash, sha1($message))) { |
|
| 114 | - $newHash = $this->hash($message); |
|
| 115 | - return true; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - return false; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Verify V1 hashes |
|
| 123 | - * @param string $message Message to verify |
|
| 124 | - * @param string $hash Assumed hash of the message |
|
| 125 | - * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one. |
|
| 126 | - * @return bool Whether $hash is a valid hash of $message |
|
| 127 | - */ |
|
| 128 | - protected function verifyHashV1($message, $hash, &$newHash = null) { |
|
| 129 | - if(password_verify($message, $hash)) { |
|
| 130 | - if(password_needs_rehash($hash, PASSWORD_DEFAULT, $this->options)) { |
|
| 131 | - $newHash = $this->hash($message); |
|
| 132 | - } |
|
| 133 | - return true; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return false; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @param string $message Message to verify |
|
| 141 | - * @param string $hash Assumed hash of the message |
|
| 142 | - * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one. |
|
| 143 | - * @return bool Whether $hash is a valid hash of $message |
|
| 144 | - */ |
|
| 145 | - public function verify($message, $hash, &$newHash = null) { |
|
| 146 | - $splittedHash = $this->splitHash($hash); |
|
| 147 | - |
|
| 148 | - if(isset($splittedHash['version'])) { |
|
| 149 | - switch ($splittedHash['version']) { |
|
| 150 | - case 1: |
|
| 151 | - return $this->verifyHashV1($message, $splittedHash['hash'], $newHash); |
|
| 152 | - } |
|
| 153 | - } else { |
|
| 154 | - return $this->legacyHashVerify($message, $hash, $newHash); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - |
|
| 158 | - return false; |
|
| 159 | - } |
|
| 49 | + /** @var IConfig */ |
|
| 50 | + private $config; |
|
| 51 | + /** @var array Options passed to password_hash and password_needs_rehash */ |
|
| 52 | + private $options = array(); |
|
| 53 | + /** @var string Salt used for legacy passwords */ |
|
| 54 | + private $legacySalt = null; |
|
| 55 | + /** @var int Current version of the generated hash */ |
|
| 56 | + private $currentVersion = 1; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param IConfig $config |
|
| 60 | + */ |
|
| 61 | + function __construct(IConfig $config) { |
|
| 62 | + $this->config = $config; |
|
| 63 | + |
|
| 64 | + $hashingCost = $this->config->getSystemValue('hashingCost', null); |
|
| 65 | + if(!is_null($hashingCost)) { |
|
| 66 | + $this->options['cost'] = $hashingCost; |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Hashes a message using PHP's `password_hash` functionality. |
|
| 72 | + * Please note that the size of the returned string is not guaranteed |
|
| 73 | + * and can be up to 255 characters. |
|
| 74 | + * |
|
| 75 | + * @param string $message Message to generate hash from |
|
| 76 | + * @return string Hash of the message with appended version parameter |
|
| 77 | + */ |
|
| 78 | + public function hash($message) { |
|
| 79 | + return $this->currentVersion . '|' . password_hash($message, PASSWORD_DEFAULT, $this->options); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Get the version and hash from a prefixedHash |
|
| 84 | + * @param string $prefixedHash |
|
| 85 | + * @return null|array Null if the hash is not prefixed, otherwise array('version' => 1, 'hash' => 'foo') |
|
| 86 | + */ |
|
| 87 | + protected function splitHash($prefixedHash) { |
|
| 88 | + $explodedString = explode('|', $prefixedHash, 2); |
|
| 89 | + if(sizeof($explodedString) === 2) { |
|
| 90 | + if((int)$explodedString[0] > 0) { |
|
| 91 | + return array('version' => (int)$explodedString[0], 'hash' => $explodedString[1]); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + return null; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Verify legacy hashes |
|
| 100 | + * @param string $message Message to verify |
|
| 101 | + * @param string $hash Assumed hash of the message |
|
| 102 | + * @param null|string &$newHash Reference will contain the updated hash |
|
| 103 | + * @return bool Whether $hash is a valid hash of $message |
|
| 104 | + */ |
|
| 105 | + protected function legacyHashVerify($message, $hash, &$newHash = null) { |
|
| 106 | + if(empty($this->legacySalt)) { |
|
| 107 | + $this->legacySalt = $this->config->getSystemValue('passwordsalt', ''); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // Verify whether it matches a legacy PHPass or SHA1 string |
|
| 111 | + $hashLength = strlen($hash); |
|
| 112 | + if($hashLength === 60 && password_verify($message.$this->legacySalt, $hash) || |
|
| 113 | + $hashLength === 40 && hash_equals($hash, sha1($message))) { |
|
| 114 | + $newHash = $this->hash($message); |
|
| 115 | + return true; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + return false; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Verify V1 hashes |
|
| 123 | + * @param string $message Message to verify |
|
| 124 | + * @param string $hash Assumed hash of the message |
|
| 125 | + * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one. |
|
| 126 | + * @return bool Whether $hash is a valid hash of $message |
|
| 127 | + */ |
|
| 128 | + protected function verifyHashV1($message, $hash, &$newHash = null) { |
|
| 129 | + if(password_verify($message, $hash)) { |
|
| 130 | + if(password_needs_rehash($hash, PASSWORD_DEFAULT, $this->options)) { |
|
| 131 | + $newHash = $this->hash($message); |
|
| 132 | + } |
|
| 133 | + return true; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return false; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @param string $message Message to verify |
|
| 141 | + * @param string $hash Assumed hash of the message |
|
| 142 | + * @param null|string &$newHash Reference will contain the updated hash if necessary. Update the existing hash with this one. |
|
| 143 | + * @return bool Whether $hash is a valid hash of $message |
|
| 144 | + */ |
|
| 145 | + public function verify($message, $hash, &$newHash = null) { |
|
| 146 | + $splittedHash = $this->splitHash($hash); |
|
| 147 | + |
|
| 148 | + if(isset($splittedHash['version'])) { |
|
| 149 | + switch ($splittedHash['version']) { |
|
| 150 | + case 1: |
|
| 151 | + return $this->verifyHashV1($message, $splittedHash['hash'], $newHash); |
|
| 152 | + } |
|
| 153 | + } else { |
|
| 154 | + return $this->legacyHashVerify($message, $hash, $newHash); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + |
|
| 158 | + return false; |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | 161 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | $this->config = $config; |
| 63 | 63 | |
| 64 | 64 | $hashingCost = $this->config->getSystemValue('hashingCost', null); |
| 65 | - if(!is_null($hashingCost)) { |
|
| 65 | + if (!is_null($hashingCost)) { |
|
| 66 | 66 | $this->options['cost'] = $hashingCost; |
| 67 | 67 | } |
| 68 | 68 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * @return string Hash of the message with appended version parameter |
| 77 | 77 | */ |
| 78 | 78 | public function hash($message) { |
| 79 | - return $this->currentVersion . '|' . password_hash($message, PASSWORD_DEFAULT, $this->options); |
|
| 79 | + return $this->currentVersion.'|'.password_hash($message, PASSWORD_DEFAULT, $this->options); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
@@ -86,9 +86,9 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | protected function splitHash($prefixedHash) { |
| 88 | 88 | $explodedString = explode('|', $prefixedHash, 2); |
| 89 | - if(sizeof($explodedString) === 2) { |
|
| 90 | - if((int)$explodedString[0] > 0) { |
|
| 91 | - return array('version' => (int)$explodedString[0], 'hash' => $explodedString[1]); |
|
| 89 | + if (sizeof($explodedString) === 2) { |
|
| 90 | + if ((int) $explodedString[0] > 0) { |
|
| 91 | + return array('version' => (int) $explodedString[0], 'hash' => $explodedString[1]); |
|
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | |
@@ -103,13 +103,13 @@ discard block |
||
| 103 | 103 | * @return bool Whether $hash is a valid hash of $message |
| 104 | 104 | */ |
| 105 | 105 | protected function legacyHashVerify($message, $hash, &$newHash = null) { |
| 106 | - if(empty($this->legacySalt)) { |
|
| 106 | + if (empty($this->legacySalt)) { |
|
| 107 | 107 | $this->legacySalt = $this->config->getSystemValue('passwordsalt', ''); |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // Verify whether it matches a legacy PHPass or SHA1 string |
| 111 | 111 | $hashLength = strlen($hash); |
| 112 | - if($hashLength === 60 && password_verify($message.$this->legacySalt, $hash) || |
|
| 112 | + if ($hashLength === 60 && password_verify($message.$this->legacySalt, $hash) || |
|
| 113 | 113 | $hashLength === 40 && hash_equals($hash, sha1($message))) { |
| 114 | 114 | $newHash = $this->hash($message); |
| 115 | 115 | return true; |
@@ -126,8 +126,8 @@ discard block |
||
| 126 | 126 | * @return bool Whether $hash is a valid hash of $message |
| 127 | 127 | */ |
| 128 | 128 | protected function verifyHashV1($message, $hash, &$newHash = null) { |
| 129 | - if(password_verify($message, $hash)) { |
|
| 130 | - if(password_needs_rehash($hash, PASSWORD_DEFAULT, $this->options)) { |
|
| 129 | + if (password_verify($message, $hash)) { |
|
| 130 | + if (password_needs_rehash($hash, PASSWORD_DEFAULT, $this->options)) { |
|
| 131 | 131 | $newHash = $this->hash($message); |
| 132 | 132 | } |
| 133 | 133 | return true; |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | public function verify($message, $hash, &$newHash = null) { |
| 146 | 146 | $splittedHash = $this->splitHash($hash); |
| 147 | 147 | |
| 148 | - if(isset($splittedHash['version'])) { |
|
| 148 | + if (isset($splittedHash['version'])) { |
|
| 149 | 149 | switch ($splittedHash['version']) { |
| 150 | 150 | case 1: |
| 151 | 151 | return $this->verifyHashV1($message, $splittedHash['hash'], $newHash); |