Odyssey
Public Member Functions | Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
UserRepo Class Reference

Public Member Functions

 __construct ($dbh, $logger, String $creditUnion)
 
 Read ($username)
 
 Create ($data)
 
 Update ($identifiers, $data)
 
 Delete ($keys)
 

Public Attributes

const TABLE_NAME = '%suser'
 

Protected Member Functions

 TableName ($creditUnion)
 

Private Member Functions

 BuildSetColumns ($data, $meta)
 
 BuildInsertColumns ($data, $meta)
 
 ColumnByType ($value, $type)
 

Private Attributes

 $dbh
 
 $creditUnion
 
 $logger
 

Detailed Description

Definition at line 2 of file UserRepo.php.

Constructor & Destructor Documentation

◆ __construct()

UserRepo::__construct (   $dbh,
  $logger,
String  $creditUnion 
)
Parameters
resource$dbhDatabase handle
object$loggerLogger to log
String$creditUnion

Definition at line 17 of file UserRepo.php.

17  {
18  $this->dbh = $dbh;
19  $this->logger = $logger;
20  $this->creditUnion = $creditUnion;
21  }

Member Function Documentation

◆ BuildInsertColumns()

UserRepo::BuildInsertColumns (   $data,
  $meta 
)
private

Simple helper to collect the column & value strings for inserts

Parameters
array$dataArray of columns and values
array$metaMeta information about the table structure
Returns
array

Definition at line 168 of file UserRepo.php.

168  {
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  }
ColumnByType($value, $type)
Definition: UserRepo.php:198

◆ BuildSetColumns()

UserRepo::BuildSetColumns (   $data,
  $meta 
)
private

Simple helper to collect the set values for update

Parameters
array$dataArray of columns and values
array$metaMeta information about the table structure
Returns
array

Definition at line 148 of file UserRepo.php.

148  {
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  }
ColumnByType($value, $type)
Definition: UserRepo.php:198

◆ ColumnByType()

UserRepo::ColumnByType (   $value,
  $type 
)
private

Simple helper to create a database value based on type

Parameters
string$valueColumn value
string$typeColumn type
Returns
string

Definition at line 198 of file UserRepo.php.

198  {
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  }

◆ Create()

UserRepo::Create (   $data)

Create

Parameters
array$dataArray of data to be inserted [ 'column_name' => 'my new value', ] An empty array or no valid columns will return a False
Returns
bool
Exceptions
Exception

Definition at line 82 of file UserRepo.php.

82  {
83  }

◆ Delete()

UserRepo::Delete (   $keys)

Delete feature gate

Parameters
array$keysIdentifiers
Returns
bool|int
Exceptions
Exception

Definition at line 138 of file UserRepo.php.

138  {
139  }

◆ Read()

UserRepo::Read (   $username)

Return user.

Parameters
string$usernameUser to retrieve
Returns
array|bool

Definition at line 33 of file UserRepo.php.

33  {
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  }

◆ Update()

UserRepo::Update (   $identifiers,
  $data 
)

Update

Parameters
array$identifiersUser identifiers
array$dataArray of data to be updated [ 'column_name' => 'my new value', ] An empty array or no valid columns will return a False
Returns
bool
Exceptions
Exception

Definition at line 96 of file UserRepo.php.

96  {
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  }
BuildSetColumns($data, $meta)
Definition: UserRepo.php:148

Member Data Documentation

◆ TABLE_NAME

const UserRepo::TABLE_NAME = '%suser'

CuUser Repository

Definition at line 7 of file UserRepo.php.


The documentation for this class was generated from the following file: