Odyssey
UserRepo.php
1 <?php
2 class UserRepo {
3  /**
4  * CuUser Repository
5  */
6 
7  const TABLE_NAME = '%suser';
8  private $dbh;
9  private $creditUnion;
10  private $logger;
11 
12  /**
13  * @param resource $dbh Database handle
14  * @param object $logger Logger to log
15  * @param String $creditUnion
16  */
17  public function __construct($dbh, $logger, String $creditUnion) {
18  $this->dbh = $dbh;
19  $this->logger = $logger;
20  $this->creditUnion = $creditUnion;
21  }
22 
23  protected function TableName($creditUnion) {
24  return sprintf(static::TABLE_NAME, $creditUnion);
25  }
26 
27  /**
28  * Return user.
29  *
30  * @param string $username User to retrieve
31  * @return array|bool
32  */
33  public function Read($username) {
34  $sql = "SELECT user_id,
35  TRIM(user_name) AS user_name,
36  TRIM(passwd) AS passwd,
37  forcechange AS fchange,
38  COALESCE(forceremain, 0) AS fremain,
39  failedremain,
40  pwchange AS pchange,
41  LOWER(TRIM(email)) AS email,
42  TRIM(confidence) As confidence,
43  group_id AS cuuser_group_id,
44  TRIM(lastlogin) AS llog,
45  TRIM(failedlogin) AS flog,
46  COALESCE(msg_tx, 0) AS msg_tx,
47  userflags,
48  COALESCE(challenge_quest_id, 0) AS savecqid,
49  mfaquest,
50  TRIM(primary_account) As primary_account
51  FROM %s
52  WHERE LOWER(user_name) = '" . strtolower(prep_save($username)) ."'";
53 
54  $sql = sprintf($sql, $this->TableName($this->creditUnion));
55 
56  $query = db_query($sql, $this->dbh);
57 
58  if (!$query) {
59  $error = db_last_error();
60  $this->logger->error('Could not read table ' . $this->TableName($this->creditUnion) . ". Error: $error");
61  return false;
62  }
63  $rows = db_fetch_all($query);
64  if (!$rows) {
65  return [];
66  }
67  return $rows[0];
68  }
69 
70  /**
71  * Create
72  *
73  * @param array $data Array of data to be inserted
74  * [
75  * 'column_name' => 'my new value',
76  * ]
77  * An empty array or no valid columns will return a False
78  * @return bool
79  *
80  * @throws Exception
81  */
82  public function Create($data) {
83  }
84  /**
85  * Update
86  *
87  * @param array $identifiers User identifiers
88  * @param array $data Array of data to be updated
89  * [
90  * 'column_name' => 'my new value',
91  * ]
92  * An empty array or no valid columns will return a False
93  * @return bool
94  * @throws Exception
95  */
96  public function Update($identifiers, $data) {
97  if (empty($identifiers)) {
98  throw new Exception(static::class . ': Did not pass an identifier to update');
99  }
100  if (empty($data)) {
101  throw new Exception(static::class . ': Did not pass any valid columns to update in table ' . $this->TableName($this->creditUnion));
102  }
103  $meta = db_meta_data($this->dbh, $this->TableName($this->creditUnion));
104  list($set, $values) = $this->BuildSetColumns($data, $meta);
105  if (empty($set)) {
106  throw new Exception(static::class . ': Did not pass any valid columns to update in table ' . $this->TableName($this->creditUnion) );
107  }
108  $key = '';
109  $index = count($values) + 1;
110  foreach ($identifiers as $value) {
111  $key .= "\$$index,";
112  $index++;
113  }
114  $key = rtrim($key, ',');
115  $sql = <<< SQL
116 UPDATE %s
117 $set
118 WHERE user_name IN ($key);
119 SQL;
120  $sql = sprintf($sql, $this->TableName($this->creditUnion) );
121  $query = db_query_params($sql, array_merge($values, $identifiers), $this->dbh);
122  if (!$query) {
123  $error = db_last_error();
124  $this->logger->error('Failed updating identifier [' . implode(',', $identifiers) . ": $error");
125  return false;
126  }
127  return true;
128  }
129 
130  /**
131  * Delete feature gate
132  *
133  * @param array $keys Identifiers
134  * @return bool|int
135  *
136  * @throws Exception
137  */
138  public function Delete($keys) {
139  }
140 
141  /**
142  * Simple helper to collect the set values for update
143  *
144  * @param array $data Array of columns and values
145  * @param array $meta Meta information about the table structure
146  * @return array
147  */
148  private function BuildSetColumns($data, $meta) {
149  $result = 'SET ';
150  $values = [];
151  $index = 1;
152  foreach ($data as $column => $value) {
153  $type = $meta[$column]['type'];
154  $result .= "$column = \$$index,\n";
155  $index++;
156  $values[] = $this->ColumnByType($value, $type);
157  }
158  return [rtrim($result, ",\n"), $values];
159  }
160 
161  /**
162  * Simple helper to collect the column & value strings for inserts
163  *
164  * @param array $data Array of columns and values
165  * @param array $meta Meta information about the table structure
166  * @return array
167  */
168  private function BuildInsertColumns($data, $meta) {
169  $columns = [];
170  $values = [];
171  $place = [];
172  $count = 1;
173  foreach ($data as $column => $value) {
174  if (!array_key_exists($column, $meta)) {
175  $this->logger->debug("Tried to insert invalid column: $column, into table " . static::TABLE_NAME);
176  continue;
177  }
178  $type = $meta[$column]['type'];
179  $columns[] = $column;
180  $place[] = "\$$count";
181  $values[] = $this->ColumnByType($value, $type);
182  $count++;
183  }
184  return [
185  implode(',', $columns),
186  implode(',', $place),
187  $values,
188  ];
189  }
190 
191  /**
192  * Simple helper to create a database value based on type
193  *
194  * @param string $value Column value
195  * @param string $type Column type
196  * @return string
197  */
198  private function ColumnByType($value, $type) {
199  switch (true) {
200  case strpos($type, 'int') !== false:
201  return intval($value);
202  break;
203  case strpos($type, 'double') !== false:
204  case 'numeric' === $type:
205  return floatval($value);
206  break;
207  case strpos($type, 'char') !== false:
208  case 'text' === $type:
209  return $value;
210  break;
211  case strpos($type, 'timestamp') !== false:
212  case 'date' === $type:
213  return $value;
214  break;
215  case strpos($type, 'bool') !== false:
216  return $value ? 'true' : 'false';
217  break;
218  default:
219  return $value;
220  }
221  }
222 }
const TABLE_NAME
Definition: UserRepo.php:7
BuildSetColumns($data, $meta)
Definition: UserRepo.php:148
ColumnByType($value, $type)
Definition: UserRepo.php:198
__construct($dbh, $logger, String $creditUnion)
Definition: UserRepo.php:17
Read($username)
Definition: UserRepo.php:33
BuildInsertColumns($data, $meta)
Definition: UserRepo.php:168
Delete($keys)
Definition: UserRepo.php:138
Update($identifiers, $data)
Definition: UserRepo.php:96
Create($data)
Definition: UserRepo.php:82