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