ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SQL.php
Go to the documentation of this file.
1 <?php
2 
13 {
17  private $dsn;
18 
22  private $username;
23 
27  private $password;
28 
32  private $options;
33 
39  private $query;
40 
47  public function __construct($info, $config)
48  {
49  assert(is_array($info));
50  assert(is_array($config));
51 
52  // Call the parent constructor first, as required by the interface
53  parent::__construct($info, $config);
54 
55  // Make sure that all required parameters are present.
56  foreach (array('dsn', 'username', 'password', 'query') as $param) {
57  if (!array_key_exists($param, $config)) {
58  throw new Exception('Missing required attribute \'' . $param .
59  '\' for authentication source ' . $this->authId);
60  }
61 
62  if (!is_string($config[$param])) {
63  throw new Exception('Expected parameter \'' . $param .
64  '\' for authentication source ' . $this->authId .
65  ' to be a string. Instead it was: ' .
66  var_export($config[$param], true));
67  }
68  }
69 
70  $this->dsn = $config['dsn'];
71  $this->username = $config['username'];
72  $this->password = $config['password'];
73  $this->query = $config['query'];
74  if (isset($config['options'])) {
75  $this->options = $config['options'];
76  }
77  }
78 
79 
85  private function connect()
86  {
87  try {
88  $db = new PDO($this->dsn, $this->username, $this->password, $this->options);
89  } catch (PDOException $e) {
90  throw new Exception('sqlauth:' . $this->authId . ': - Failed to connect to \'' .
91  $this->dsn . '\': '. $e->getMessage());
92  }
93 
94  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
95 
96  $driver = explode(':', $this->dsn, 2);
97  $driver = strtolower($driver[0]);
98 
99  /* Driver specific initialization. */
100  switch ($driver) {
101  case 'mysql':
102  /* Use UTF-8. */
103  $db->exec("SET NAMES 'utf8mb4'");
104  break;
105  case 'pgsql':
106  /* Use UTF-8. */
107  $db->exec("SET NAMES 'UTF8'");
108  break;
109  }
110 
111  return $db;
112  }
113 
114 
128  protected function login($username, $password)
129  {
130  assert(is_string($username));
131  assert(is_string($password));
132 
133  $db = $this->connect();
134 
135  try {
136  $sth = $db->prepare($this->query);
137  } catch (PDOException $e) {
138  throw new Exception('sqlauth:' . $this->authId .
139  ': - Failed to prepare query: ' . $e->getMessage());
140  }
141 
142  try {
143  $sth->execute(array('username' => $username, 'password' => $password));
144  } catch (PDOException $e) {
145  throw new Exception('sqlauth:' . $this->authId .
146  ': - Failed to execute query: ' . $e->getMessage());
147  }
148 
149  try {
150  $data = $sth->fetchAll(PDO::FETCH_ASSOC);
151  } catch (PDOException $e) {
152  throw new Exception('sqlauth:' . $this->authId .
153  ': - Failed to fetch result set: ' . $e->getMessage());
154  }
155 
156  SimpleSAML\Logger::info('sqlauth:' . $this->authId . ': Got ' . count($data) .
157  ' rows from database');
158 
159  if (count($data) === 0) {
160  /* No rows returned - invalid username/password. */
161  SimpleSAML\Logger::error('sqlauth:' . $this->authId .
162  ': No rows in result set. Probably wrong username/password.');
163  throw new SimpleSAML_Error_Error('WRONGUSERPASS');
164  }
165 
166  /* Extract attributes. We allow the resultset to consist of multiple rows. Attributes
167  * which are present in more than one row will become multivalued. null values and
168  * duplicate values will be skipped. All values will be converted to strings.
169  */
170  $attributes = array();
171  foreach ($data as $row) {
172  foreach ($row as $name => $value) {
173 
174  if ($value === null) {
175  continue;
176  }
177 
178  $value = (string)$value;
179 
180  if (!array_key_exists($name, $attributes)) {
181  $attributes[$name] = array();
182  }
183 
184  if (in_array($value, $attributes[$name], true)) {
185  /* Value already exists in attribute. */
186  continue;
187  }
188 
189  $attributes[$name][] = $value;
190  }
191  }
192 
193  SimpleSAML\Logger::info('sqlauth:' . $this->authId . ': Attributes: ' .
194  implode(',', array_keys($attributes)));
195 
196  return $attributes;
197  }
198 }
$dsn
The DSN we should connect to.
Definition: SQL.php:17
$username
The username we should connect to the database with.
Definition: SQL.php:22
$password
The password we should connect to the database with.
Definition: SQL.php:27
$query
The query we should use to retrieve the attributes for the user.
Definition: SQL.php:39
$config
Definition: bootstrap.php:15
connect()
Create a database connection.
Definition: SQL.php:85
$options
The options that we should connect to the database with.
Definition: SQL.php:32
__construct($info, $config)
Constructor for this authentication source.
Definition: SQL.php:47
$info
Definition: index.php:5