Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 91 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| RdsRequestLogStorage | |
0.00% |
0 / 91 |
|
0.00% |
0 / 8 |
552 | |
0.00% |
0 / 1 |
| log | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| find | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
| count | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| addFilter | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 | |||
| getColumnNames | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| getPersistence | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getQueryBuilder | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| install | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoEventLog\model\requestLog\rds; |
| 24 | |
| 25 | use common_Logger; |
| 26 | use common_persistence_SqlPersistence; |
| 27 | use common_report_Report; |
| 28 | use Psr\Http\Message\RequestInterface; |
| 29 | use oat\oatbox\user\User; |
| 30 | use Doctrine\DBAL\Schema\SchemaException; |
| 31 | use Doctrine\DBAL\Query\QueryBuilder; |
| 32 | use oat\taoEventLog\model\requestLog\AbstractRequestLogStorage; |
| 33 | use oat\taoEventLog\model\requestLog\RequestLogStorageReadable; |
| 34 | use oat\taoEventLog\model\requestLog\RequestLogService; |
| 35 | |
| 36 | /** |
| 37 | * Class RdsRequestLogStorage |
| 38 | * @package oat\taoEventLog\model\requestLog\rds |
| 39 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 40 | */ |
| 41 | class RdsRequestLogStorage extends AbstractRequestLogStorage implements RequestLogStorageReadable |
| 42 | { |
| 43 | public const OPTION_PERSISTENCE = 'persistence_id'; |
| 44 | public const TABLE_NAME = 'request_log'; |
| 45 | |
| 46 | public const COLUMN_USER_ID = RequestLogService::USER_ID; |
| 47 | public const COLUMN_USER_ROLES = RequestLogService::USER_ROLES; |
| 48 | public const COLUMN_ACTION = RequestLogService::ACTION; |
| 49 | public const COLUMN_EVENT_TIME = RequestLogService::EVENT_TIME; |
| 50 | public const COLUMN_DETAILS = RequestLogService::DETAILS; |
| 51 | |
| 52 | /** @var \Doctrine\DBAL\Connection */ |
| 53 | private $connection; |
| 54 | |
| 55 | /** |
| 56 | * @inheritdoc |
| 57 | */ |
| 58 | public function log(RequestInterface $request, User $user) |
| 59 | { |
| 60 | $data = $this->prepareData($request, $user); |
| 61 | $this->getPersistence()->insert(self::TABLE_NAME, $data); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @inheritdoc |
| 66 | */ |
| 67 | public function find(array $filters = [], array $options = []) |
| 68 | { |
| 69 | $queryBuilder = $this->getQueryBuilder(); |
| 70 | $queryBuilder->select('*'); |
| 71 | if (isset($options['limit'])) { |
| 72 | $queryBuilder->setMaxResults(intval($options['limit'])); |
| 73 | } |
| 74 | if (isset($options['offset'])) { |
| 75 | $queryBuilder->setFirstResult(intval($options['offset'])); |
| 76 | } |
| 77 | if (isset($options['group']) && in_array($options['group'], $this->getColumnNames())) { |
| 78 | $queryBuilder->groupBy($options['group']); |
| 79 | } |
| 80 | |
| 81 | foreach ($filters as $filter) { |
| 82 | $this->addFilter($queryBuilder, $filter); |
| 83 | } |
| 84 | return new UserActivityLogIterator($this->getPersistence(), $queryBuilder); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @inheritdoc |
| 89 | */ |
| 90 | public function count(array $filters = [], array $options = []) |
| 91 | { |
| 92 | $queryBuilder = $this->getQueryBuilder(); |
| 93 | $queryBuilder->select('user_id'); |
| 94 | |
| 95 | foreach ($filters as $filter) { |
| 96 | $this->addFilter($queryBuilder, $filter); |
| 97 | } |
| 98 | if (isset($options['group']) && in_array($options['group'], $this->getColumnNames())) { |
| 99 | $queryBuilder->select($options['group']); |
| 100 | $queryBuilder->groupBy($options['group']); |
| 101 | } |
| 102 | |
| 103 | $stmt = $this->getPersistence()->query( |
| 104 | 'SELECT count(*) as count FROM (' . $queryBuilder->getSQL() . ') as group_q', |
| 105 | $queryBuilder->getParameters() |
| 106 | ); |
| 107 | $data = $stmt->fetch(\PDO::FETCH_ASSOC); |
| 108 | return intval($data['count']); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param QueryBuilder $queryBuilder |
| 113 | * @param array $filter |
| 114 | */ |
| 115 | private function addFilter(QueryBuilder $queryBuilder, array $filter) |
| 116 | { |
| 117 | $colName = strtolower($filter[0]); |
| 118 | $operation = strtolower($filter[1]); |
| 119 | $val = $filter[2]; |
| 120 | $val2 = isset($filter[3]) ? $filter[3] : null; |
| 121 | |
| 122 | if (!in_array($colName, $this->getColumnNames())) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if (!in_array($operation, ['<', '>', '<>', '<=', '>=', '=', 'between', 'like'])) { |
| 127 | return; |
| 128 | } |
| 129 | $params = []; |
| 130 | if ($operation === 'between') { |
| 131 | $condition = "r.$colName between ? AND ?"; |
| 132 | $params[] = $val; |
| 133 | $params[] = $val2; |
| 134 | } else { |
| 135 | $condition = "r.$colName $operation ?"; |
| 136 | $params[] = $val; |
| 137 | } |
| 138 | |
| 139 | $queryBuilder->andWhere($condition); |
| 140 | |
| 141 | $params = array_merge($queryBuilder->getParameters(), $params); |
| 142 | $queryBuilder->setParameters($params); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @return array |
| 147 | */ |
| 148 | private function getColumnNames() |
| 149 | { |
| 150 | return [ |
| 151 | self::COLUMN_USER_ID, |
| 152 | self::COLUMN_USER_ROLES, |
| 153 | self::COLUMN_ACTION, |
| 154 | self::COLUMN_EVENT_TIME, |
| 155 | self::COLUMN_DETAILS, |
| 156 | ]; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return common_persistence_SqlPersistence |
| 161 | */ |
| 162 | private function getPersistence() |
| 163 | { |
| 164 | $persistenceManager = $this->getServiceManager()->get(\common_persistence_Manager::SERVICE_ID); |
| 165 | return $persistenceManager->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE)); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @return \Doctrine\DBAL\Query\QueryBuilder |
| 170 | */ |
| 171 | private function getQueryBuilder() |
| 172 | { |
| 173 | if ($this->connection === null) { |
| 174 | $this->connection = \Doctrine\DBAL\DriverManager::getConnection( |
| 175 | $this->getPersistence()->getDriver()->getParams(), |
| 176 | new \Doctrine\DBAL\Configuration() |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | return $this->connection->createQueryBuilder()->from(self::TABLE_NAME, 'r'); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Initialize RDS Request log storage |
| 185 | * |
| 186 | * @param string $persistenceId |
| 187 | * @return common_report_Report |
| 188 | */ |
| 189 | public static function install($persistenceId = 'default') |
| 190 | { |
| 191 | $persistence = \common_persistence_Manager::getPersistence($persistenceId); |
| 192 | |
| 193 | $schemaManager = $persistence->getDriver()->getSchemaManager(); |
| 194 | $schema = $schemaManager->createSchema(); |
| 195 | $fromSchema = clone $schema; |
| 196 | |
| 197 | try { |
| 198 | $table = $schema->createTable(self::TABLE_NAME); |
| 199 | $table->addOption('engine', 'InnoDB'); |
| 200 | $table->addColumn(static::COLUMN_USER_ID, "string", ["length" => 255]); |
| 201 | $table->addColumn(static::COLUMN_USER_ROLES, "string", ["notnull" => true, "length" => 4096]); |
| 202 | $table->addColumn(static::COLUMN_ACTION, "string", ["notnull" => false, "length" => 4096]); |
| 203 | $table->addColumn( |
| 204 | static::COLUMN_EVENT_TIME, |
| 205 | 'decimal', |
| 206 | ['precision' => 14, 'scale' => 4, "notnull" => true] |
| 207 | ); |
| 208 | $table->addColumn(static::COLUMN_DETAILS, "text", ["notnull" => false]); |
| 209 | |
| 210 | $table->addIndex( |
| 211 | [static::COLUMN_USER_ID], |
| 212 | 'IDX_' . static::TABLE_NAME . '_' . static::COLUMN_USER_ID |
| 213 | ); |
| 214 | $table->addIndex( |
| 215 | [static::COLUMN_EVENT_TIME], |
| 216 | 'IDX_' . static::TABLE_NAME . '_' . static::COLUMN_EVENT_TIME |
| 217 | ); |
| 218 | } catch (SchemaException $e) { |
| 219 | common_Logger::i('Database Schema already up to date.'); |
| 220 | } |
| 221 | |
| 222 | $queries = $persistence->getPlatform()->getMigrateSchemaSql($fromSchema, $schema); |
| 223 | foreach ($queries as $query) { |
| 224 | $persistence->exec($query); |
| 225 | } |
| 226 | |
| 227 | return new common_report_Report( |
| 228 | common_report_Report::TYPE_SUCCESS, |
| 229 | __('User activity log successfully registered.') |
| 230 | ); |
| 231 | } |
| 232 | } |