11<?php
22
3+ namespace SimpleSAML \Module \sqlattribs \Auth \Process ;
4+
35/**
46 * Filter to add attributes from a SQL data source
57 *
68 * This filter allows you to add attributes from a SQL datasource
79 *
810 * @author Guy Halse http://orcid.org/0000-0002-9388-8592
9- * @copyright Copyright (c) 2016 , SAFIRE - South African Identity Federation
11+ * @copyright Copyright (c) 2019 , SAFIRE - South African Identity Federation
1012 * @license https://github.com/safire-ac-za/simplesamlphp-module-sqlattribs/blob/master/LICENSE MIT License
1113 * @package SimpleSAMLphp
1214 */
13- class sspmod_sqlattribs_Auth_Process_AttributeFromSQL extends SimpleSAML_Auth_ProcessingFilter
15+ class AttributeFromSQL extends \ SimpleSAML \ Auth \ProcessingFilter
1416{
1517 /** @var string The DSN we should connect to. */
1618 private $ dsn = 'mysql:host=localhost;dbname=simplesamlphp ' ;
@@ -41,7 +43,7 @@ class sspmod_sqlattribs_Auth_Process_AttributeFromSQL extends SimpleSAML_Auth_Pr
4143 *
4244 * @param array $config Configuration information about this filter.
4345 * @param mixed $reserved For future use.
44- * @throws SimpleSAML_Error_Exception
46+ * @throws \SimpleSAML\Error\Exception
4547 */
4648 public function __construct ($ config , $ reserved )
4749 {
@@ -52,7 +54,7 @@ public function __construct($config, $reserved)
5254 $ this ->attribute = $ config ['attribute ' ];
5355 }
5456 if (!is_string ($ this ->attribute ) || !$ this ->attribute ) {
55- throw new SimpleSAML_Error_Exception ('AttributeFromSQL: attribute name not valid ' );
57+ throw new \ SimpleSAML \ Error \ Exception ('AttributeFromSQL: attribute name not valid ' );
5658 }
5759
5860 if (array_key_exists ('database ' , $ config )) {
@@ -70,10 +72,10 @@ public function __construct($config, $reserved)
7072 }
7173 }
7274 if (!is_string ($ this ->dsn ) || !$ this ->dsn ) {
73- throw new SimpleSAML_Error_Exception ('AttributeFromSQL: invalid database DSN given ' );
75+ throw new \ SimpleSAML \ Error \ Exception ('AttributeFromSQL: invalid database DSN given ' );
7476 }
7577 if (!is_string ($ this ->table ) || !$ this ->table ) {
76- throw new SimpleSAML_Error_Exception ('AttributeFromSQL: invalid database table ' );
78+ throw new \ SimpleSAML \ Error \ Exception ('AttributeFromSQL: invalid database table ' );
7779 }
7880
7981 if (array_key_exists ('replace ' , $ config )) {
@@ -82,7 +84,7 @@ public function __construct($config, $reserved)
8284
8385 if (array_key_exists ('limit ' , $ config )) {
8486 if (!is_array ($ config ['limit ' ])) {
85- throw new SimpleSAML_Error_Exception ('AttributeFromSQL: limit must be an array of attribute names ' );
87+ throw new \ SimpleSAML \ Error \ Exception ('AttributeFromSQL: limit must be an array of attribute names ' );
8688 }
8789 $ this ->limit = $ config ['limit ' ];
8890 }
@@ -95,18 +97,18 @@ public function __construct($config, $reserved)
9597 /**
9698 * Create a database connection.
9799 *
98- * @return PDO The database connection.
99- * @throws SimpleSAML_Error_Exception
100+ * @return \ PDO The database connection.
101+ * @throws \SimpleSAML\Error\Exception
100102 */
101103 private function connect ()
102104 {
103105 try {
104- $ db = new PDO ($ this ->dsn , $ this ->username , $ this ->password );
105- } catch (PDOException $ e ) {
106- throw new SimpleSAML_Error_Exception ('AttributeFromSQL: Failed to connect to \'' .
106+ $ db = new \ PDO ($ this ->dsn , $ this ->username , $ this ->password );
107+ } catch (\ PDOException $ e ) {
108+ throw new \ SimpleSAML \ Error \ Exception ('AttributeFromSQL: Failed to connect to \'' .
107109 $ this ->dsn . '\': ' . $ e ->getMessage ());
108110 }
109- $ db ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
111+ $ db ->setAttribute (\ PDO ::ATTR_ERRMODE , \ PDO ::ERRMODE_EXCEPTION );
110112
111113 $ driver = explode (': ' , $ this ->dsn , 2 );
112114 $ driver = strtolower ($ driver [0 ]);
@@ -129,7 +131,7 @@ private function connect()
129131 *
130132 * Logic is largely the same as (and lifted from) sqlauth:sql
131133 * @param mixed &$request
132- * @throws SimpleSAML_Error_Exception
134+ * @throws \SimpleSAML\Error\Exception
133135 */
134136 public function process (&$ request )
135137 {
@@ -140,34 +142,34 @@ public function process(&$request)
140142 $ attributes =& $ request ['Attributes ' ];
141143
142144 if (!array_key_exists ($ this ->attribute , $ attributes )) {
143- SimpleSAML \Logger::info ('AttributeFromSQL: attribute \'' . $ this ->attribute . '\' not set, declining ' );
145+ \ SimpleSAML \Logger::info ('AttributeFromSQL: attribute \'' . $ this ->attribute . '\' not set, declining ' );
144146 return ;
145147 }
146148
147149 $ db = $ this ->connect ();
148150
149151 try {
150152 $ sth = $ db ->prepare ('SELECT `attribute`,`value` FROM ' . $ this ->table . ' WHERE `uid`=? AND (`sp`= \'% \' OR `sp`=?) ' . ($ this ->ignoreExpiry ? '' : ' AND `expires`>CURRENT_DATE ' ) . '; ' );
151- } catch (PDOException $ e ) {
152- throw new SimpleSAML_Error_Exception ('AttributeFromSQL: prepare() failed: ' . $ e ->getMessage ());
153+ } catch (\ PDOException $ e ) {
154+ throw new \ SimpleSAML \ Error \ Exception ('AttributeFromSQL: prepare() failed: ' . $ e ->getMessage ());
153155 }
154156
155157 try {
156- $ res = $ sth ->execute (array ( $ attributes [$ this ->attribute ][0 ], $ request ["Destination " ]["entityid " ]) );
157- } catch (PDOException $ e ) {
158- throw new SimpleSAML_Error_Exception ('AttributeFromSQL: execute( ' . $ attributes [$ this ->attribute ][0 ] .
158+ $ res = $ sth ->execute ([ $ attributes [$ this ->attribute ][0 ], $ request ["Destination " ]["entityid " ]] );
159+ } catch (\ PDOException $ e ) {
160+ throw new \ SimpleSAML \ Error \ Exception ('AttributeFromSQL: execute( ' . $ attributes [$ this ->attribute ][0 ] .
159161 ', ' . $ request ["Destination " ]["entityid " ] . ') failed: ' . $ e ->getMessage ());
160162 }
161163
162164 try {
163- $ data = $ sth ->fetchAll (PDO ::FETCH_ASSOC );
165+ $ data = $ sth ->fetchAll (\ PDO ::FETCH_ASSOC );
164166
165- } catch (PDOException $ e ) {
166- throw new SimpleSAML_Error_Exception ('AttributeFromSQL: fetchAll() failed: ' . $ e ->getMessage ());
167+ } catch (\ PDOException $ e ) {
168+ throw new \ SimpleSAML \ Error \ Exception ('AttributeFromSQL: fetchAll() failed: ' . $ e ->getMessage ());
167169 }
168170
169171 if (count ($ data ) === 0 ) {
170- SimpleSAML \Logger::info ('AttributeFromSQL: no additional attributes for ' . $ this ->attribute . '= \'' . $ attributes [$ this ->attribute ][0 ] . '\'' );
172+ \ SimpleSAML \Logger::info ('AttributeFromSQL: no additional attributes for ' . $ this ->attribute . '= \'' . $ attributes [$ this ->attribute ][0 ] . '\'' );
171173 return ;
172174 }
173175
@@ -177,7 +179,7 @@ public function process(&$request)
177179 */
178180 foreach ($ data as $ row ) {
179181 if (empty ($ row ['attribute ' ]) || $ row ['value ' ] === null ) {
180- SimpleSAML \Logger::debug ('AttributeFromSQL: skipping invalid attribute/value tuple: ' . var_export ($ row , true ));
182+ \ SimpleSAML \Logger::debug ('AttributeFromSQL: skipping invalid attribute/value tuple: ' . var_export ($ row , true ));
181183 continue ;
182184 }
183185
@@ -186,17 +188,17 @@ public function process(&$request)
186188
187189 /* Limit the attribute set returned */
188190 if ($ this ->limit !== null && !in_array ($ name , $ this ->limit , true )) {
189- SimpleSAML \Logger::notice ('AttributeFromSQL: skipping unwanted attribute ' . $ name . ' [limited to: ' . var_export ($ this ->limit , true ) . '] ' );
191+ \ SimpleSAML \Logger::notice ('AttributeFromSQL: skipping unwanted attribute ' . $ name . ' [limited to: ' . var_export ($ this ->limit , true ) . '] ' );
190192 continue ;
191193 }
192194
193195 if (!array_key_exists ($ name , $ attributes ) || $ this ->replace === true ) {
194- $ attributes [$ name ] = array () ;
196+ $ attributes [$ name ] = [] ;
195197 }
196198
197199 if (in_array ($ value , $ attributes [$ name ], true )) {
198200 /* Value already exists in attribute. */
199- SimpleSAML \Logger::debug ('AttributeFromSQL: skipping duplicate attribute/value tuple ' . $ name . '= \'' . $ value . '\'' );
201+ \ SimpleSAML \Logger::debug ('AttributeFromSQL: skipping duplicate attribute/value tuple ' . $ name . '= \'' . $ value . '\'' );
200202 continue ;
201203 }
202204
0 commit comments