Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 30 | use OCP\AppFramework\Db\QBMapper; |
||
| 31 | use OCP\DB\QueryBuilder\IQueryBuilder; |
||
| 32 | use OCP\IDBConnection; |
||
| 33 | |||
| 34 | class CredentialMapper extends QBMapper { |
||
| 35 | const TABLE_NAME = 'passman_credentials'; |
||
| 36 | private Utils $utils; |
||
|
|
|||
| 37 | |||
| 38 | public function __construct(IDBConnection $db, Utils $utils) { |
||
| 39 | parent::__construct($db, self::TABLE_NAME); |
||
| 40 | $this->utils = $utils; |
||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Obtains the credentials by vault id (not guid) |
||
| 46 | * |
||
| 47 | * @param string $vault_id |
||
| 48 | * @param string $user_id |
||
| 49 | * @return Entity[] |
||
| 50 | */ |
||
| 51 | public function getCredentialsByVaultId(string $vault_id, string $user_id) { |
||
| 52 | $qb = $this->db->getQueryBuilder(); |
||
| 53 | $qb->select('*') |
||
| 54 | ->from(self::TABLE_NAME) |
||
| 55 | ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR))) |
||
| 56 | ->andWhere($qb->expr()->eq('vault_id', $qb->createNamedParameter($vault_id, IQueryBuilder::PARAM_STR))); |
||
| 57 | |||
| 58 | return $this->findEntities($qb); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get a random credential from a vault |
||
| 63 | * |
||
| 64 | * @param string $vault_id |
||
| 65 | * @param string $user_id |
||
| 66 | * @return Credential[] |
||
| 67 | */ |
||
| 68 | public function getRandomCredentialByVaultId(string $vault_id, string $user_id) { |
||
| 69 | $qb = $this->db->getQueryBuilder(); |
||
| 70 | $qb->select('*') |
||
| 71 | ->from(self::TABLE_NAME) |
||
| 72 | ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR))) |
||
| 73 | ->andWhere($qb->expr()->eq('vault_id', $qb->createNamedParameter($vault_id, IQueryBuilder::PARAM_STR))) |
||
| 74 | ->andWhere($qb->expr()->isNull('shared_key')) |
||
| 75 | ->setMaxResults(20); |
||
| 76 | |||
| 77 | $entities = $this->findEntities($qb); |
||
| 78 | $count = count($entities) - 1; |
||
| 79 | |||
| 80 | /** @var Credential[] $entity */ |
||
| 81 | $entity = array_splice($entities, rand(0, $count), 1); |
||
| 82 | return $entity; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get expired credentials |
||
| 87 | * |
||
| 88 | * @param int $timestamp |
||
| 89 | * @return Entity[] |
||
| 90 | */ |
||
| 91 | public function getExpiredCredentials(int $timestamp) { |
||
| 92 | $qb = $this->db->getQueryBuilder(); |
||
| 93 | $qb->select('*') |
||
| 94 | ->from(self::TABLE_NAME) |
||
| 95 | ->where($qb->expr()->gt('expire_time', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))) |
||
| 96 | ->andWhere($qb->expr()->lt('expire_time', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT))); |
||
| 97 | |||
| 98 | return $this->findEntities($qb); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get an credential by id. |
||
| 103 | * Optional user id |
||
| 104 | * |
||
| 105 | * @param int $credential_id |
||
| 106 | * @param string|null $user_id |
||
| 107 | * @return Entity |
||
| 108 | * @throws DoesNotExistException |
||
| 109 | * @throws MultipleObjectsReturnedException |
||
| 110 | */ |
||
| 111 | public function getCredentialById(int $credential_id, string $user_id = null) { |
||
| 112 | $qb = $this->db->getQueryBuilder(); |
||
| 113 | $qb->select('*') |
||
| 114 | ->from(self::TABLE_NAME) |
||
| 115 | ->where($qb->expr()->eq('id', $qb->createNamedParameter($credential_id, IQueryBuilder::PARAM_INT))); |
||
| 116 | |||
| 117 | if ($user_id !== null) { |
||
| 118 | $qb->andWhere($qb->expr()->eq('user_id', $qb->createNamedParameter($user_id, IQueryBuilder::PARAM_STR))); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $this->findEntity($qb); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get credential label by id |
||
| 126 | * |
||
| 127 | * @param int $credential_id |
||
| 128 | * @return Entity |
||
| 129 | * @throws DoesNotExistException |
||
| 130 | * @throws MultipleObjectsReturnedException |
||
| 131 | */ |
||
| 132 | public function getCredentialLabelById(int $credential_id) { |
||
| 133 | $qb = $this->db->getQueryBuilder(); |
||
| 134 | $qb->select(['id', 'label']) |
||
| 135 | ->from(self::TABLE_NAME) |
||
| 136 | ->where($qb->expr()->eq('id', $qb->createNamedParameter($credential_id, IQueryBuilder::PARAM_INT))); |
||
| 137 | |||
| 138 | return $this->findEntity($qb); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Save credential to the database. |
||
| 143 | * |
||
| 144 | * @param $raw_credential |
||
| 145 | * @return Credential |
||
| 146 | */ |
||
| 147 | public function create($raw_credential) { |
||
| 148 | $credential = new Credential(); |
||
| 149 | |||
| 150 | $credential->setGuid($this->utils->GUID()); |
||
| 151 | $credential->setVaultId($raw_credential['vault_id']); |
||
| 152 | $credential->setUserId($raw_credential['user_id']); |
||
| 153 | $credential->setLabel($raw_credential['label']); |
||
| 154 | $credential->setDescription($raw_credential['description']); |
||
| 155 | $credential->setCreated($this->utils->getTime()); |
||
| 156 | $credential->setChanged($this->utils->getTime()); |
||
| 157 | $credential->setTags($raw_credential['tags']); |
||
| 158 | $credential->setEmail($raw_credential['email']); |
||
| 159 | $credential->setUsername($raw_credential['username']); |
||
| 160 | $credential->setPassword($raw_credential['password']); |
||
| 161 | $credential->setUrl($raw_credential['url']); |
||
| 162 | $credential->setIcon($raw_credential['icon']); |
||
| 163 | $credential->setRenewInterval($raw_credential['renew_interval']); |
||
| 164 | $credential->setExpireTime($raw_credential['expire_time']); |
||
| 165 | $credential->setDeleteTime($raw_credential['delete_time']); |
||
| 166 | $credential->setFiles($raw_credential['files']); |
||
| 167 | $credential->setCustomFields($raw_credential['custom_fields']); |
||
| 168 | $credential->setOtp($raw_credential['otp']); |
||
| 169 | $credential->setHidden($raw_credential['hidden']); |
||
| 170 | $credential->setCompromised($raw_credential['compromised']); |
||
| 171 | if (isset($raw_credential['shared_key'])) { |
||
| 172 | $credential->setSharedKey($raw_credential['shared_key']); |
||
| 173 | } |
||
| 174 | return parent::insert($credential); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param $raw_credential array An array containing all the credential fields |
||
| 179 | * @param $useRawUser bool |
||
| 180 | * @return Credential|Entity The updated credential |
||
| 181 | * @throws DoesNotExistException |
||
| 182 | * @throws MultipleObjectsReturnedException |
||
| 183 | */ |
||
| 184 | public function updateCredential($raw_credential, bool $useRawUser) { |
||
| 185 | $original = $this->getCredentialByGUID($raw_credential['guid']); |
||
| 186 | $uid = ($useRawUser) ? $raw_credential['user_id'] : $original->getUserId(); |
||
| 187 | |||
| 188 | $credential = new Credential(); |
||
| 189 | $credential->setId($original->getId()); |
||
| 190 | $credential->setGuid($original->getGuid()); |
||
| 191 | $credential->setVaultId($original->getVaultId()); |
||
| 192 | $credential->setUserId($uid); |
||
| 193 | $credential->setLabel($raw_credential['label']); |
||
| 194 | $credential->setDescription($raw_credential['description']); |
||
| 195 | $credential->setCreated($original->getCreated()); |
||
| 196 | $credential->setChanged($this->utils->getTime()); |
||
| 197 | $credential->setTags($raw_credential['tags']); |
||
| 198 | $credential->setEmail($raw_credential['email']); |
||
| 199 | $credential->setUsername($raw_credential['username']); |
||
| 200 | $credential->setPassword($raw_credential['password']); |
||
| 201 | $credential->setUrl($raw_credential['url']); |
||
| 202 | $credential->setIcon($raw_credential['icon']); |
||
| 203 | $credential->setRenewInterval($raw_credential['renew_interval']); |
||
| 204 | $credential->setExpireTime($raw_credential['expire_time']); |
||
| 205 | $credential->setFiles($raw_credential['files']); |
||
| 206 | $credential->setCustomFields($raw_credential['custom_fields']); |
||
| 207 | $credential->setOtp($raw_credential['otp']); |
||
| 208 | $credential->setHidden($raw_credential['hidden']); |
||
| 209 | $credential->setDeleteTime($raw_credential['delete_time']); |
||
| 210 | $credential->setCompromised($raw_credential['compromised']); |
||
| 211 | |||
| 212 | if (isset($raw_credential['shared_key'])) { |
||
| 213 | $credential->setSharedKey($raw_credential['shared_key']); |
||
| 248 |