ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Auth_Container_MDB2 Class Reference
+ Inheritance diagram for Auth_Container_MDB2:
+ Collaboration diagram for Auth_Container_MDB2:

Public Member Functions

 Auth_Container_MDB2 ($dsn)
 Constructor of the container class.
 _connect ($dsn)
 Connect to database by using the given DSN string.
 _prepare ()
 Prepare database connection.
 query ($query)
 Prepare query to the database.
 _setDefaults ()
 Set some default options.
 _parseOptions ($array)
 Parse options passed to the container class.
 _quoteDBFields ()
 Quote the db_fields option to avoid the possibility of SQL injection.
 fetchData ($username, $password, $isChallengeResponse=false)
 Get user information from database.
 listUsers ()
 Returns a list of users from the container.
 addUser ($username, $password, $additional="")
 Add user to the storage container.
 removeUser ($username)
 Remove user from the storage container.
 changePassword ($username, $password)
 Change password for user in the storage container.
 supportsChallengeResponse ()
 Determine if this container supports password authentication with challenge response.
 getCryptType ()
 Returns the selected crypt type for this container.
- Public Member Functions inherited from Auth_Container
 Auth_Container ()
 Constructor.
 verifyPassword ($password1, $password2, $cryptType="md5")
 Crypt and verfiy the entered password.
 getUser ($username)
 Returns a user assoc array.
 log ($message, $level=AUTH_LOG_DEBUG)
 Log a message to the Auth log.
- Public Member Functions inherited from ilAuthContainerBase
 loginObserver ($a_username, $a_auth)
 Called after successful login.
 failedLoginObserver ($a_username, $a_auth)
 Called after failed login.
 checkAuthObserver ($a_username, $a_auth)
 Called after check auth requests.
 logoutObserver ($a_username, $a_auth)
 Called after logout.

Data Fields

 $options = array()
 $db = null
 $dsn = ''
 $activeUser = ''
- Data Fields inherited from Auth_Container
 $activeUser = ""
 User that is currently selected from the storage container.
 $_auth_obj = null
 The Auth object this container is attached to.

Detailed Description

Definition at line 51 of file MDB2.php.

Member Function Documentation

Auth_Container_MDB2::_connect (   $dsn)

Connect to database by using the given DSN string.

private

Parameters
mixedDSN string | array | mdb object
Returns
mixed Object on error, otherwise bool

Definition at line 110 of file MDB2.php.

References $dsn, $t, AUTH_LOG_DEBUG, MDB2\connect(), PEAR\isError(), MDB2\isError(), Auth_Container\log(), PEAR_ERROR_RETURN, and PEAR\raiseError().

Referenced by _prepare().

{
$this->log('Auth_Container_MDB2::_connect() called.', AUTH_LOG_DEBUG);
if (is_string($dsn) || is_array($dsn)) {
$this->db =& MDB2::connect($dsn, $this->options['db_options']);
} elseif (is_subclass_of($dsn, 'MDB2_Driver_Common')) {
$this->db = $dsn;
} elseif (is_object($dsn) && MDB2::isError($dsn)) {
return PEAR::raiseError($dsn->getMessage(), $dsn->code);
} else {
return PEAR::raiseError('The given dsn was not valid in file ' . __FILE__ . ' at line ' . __LINE__,
41,
null,
null
);
}
if (MDB2::isError($this->db) || PEAR::isError($this->db)) {
return PEAR::raiseError($this->db->getMessage(), $this->db->code);
}
if ($this->options['auto_quote']) {
if (strpos('.', $this->options['table']) === false) {
$this->options['final_table'] = $this->db->quoteIdentifier($this->options['table'], true);
} else {
$t = explode('.', $this->options['table']);
for ($i = 0, $count = count($t); $i < $count; $i++)
$t[$i] = $this->db->quoteIdentifier($t[$i], true);
$this->options['final_table'] = implode('.', $t);
}
$this->options['final_usernamecol'] = $this->db->quoteIdentifier($this->options['usernamecol'], true);
$this->options['final_passwordcol'] = $this->db->quoteIdentifier($this->options['passwordcol'], true);
} else {
$this->options['final_table'] = $this->options['table'];
$this->options['final_usernamecol'] = $this->options['usernamecol'];
$this->options['final_passwordcol'] = $this->options['passwordcol'];
}
return true;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Auth_Container_MDB2::_parseOptions (   $array)

Parse options passed to the container class.

private

Parameters
array

Definition at line 229 of file MDB2.php.

Referenced by Auth_Container_MDB2().

{
foreach ($array as $key => $value) {
if (isset($this->options[$key])) {
$this->options[$key] = $value;
}
}
}

+ Here is the caller graph for this function:

Auth_Container_MDB2::_prepare ( )

Prepare database connection.

This function checks if we have already opened a connection to the database. If that's not the case, a new connection is opened.

private

Returns
mixed True or a MDB error object.

Definition at line 165 of file MDB2.php.

References _connect().

Referenced by addUser(), changePassword(), fetchData(), listUsers(), query(), and removeUser().

{
if (is_subclass_of($this->db, 'MDB2_Driver_Common')) {
return true;
}
return $this->_connect($this->options['dsn']);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Auth_Container_MDB2::_quoteDBFields ( )

Quote the db_fields option to avoid the possibility of SQL injection.

private

Returns
string A properly quoted string that can be concatenated into a SELECT clause.

Definition at line 248 of file MDB2.php.

Referenced by fetchData(), and listUsers().

{
if (isset($this->options['db_fields'])) {
if (is_array($this->options['db_fields'])) {
if ($this->options['auto_quote']) {
$fields = array();
foreach ($this->options['db_fields'] as $field) {
$fields[] = $this->db->quoteIdentifier($field, true);
}
return implode(', ', $fields);
} else {
return implode(', ', $this->options['db_fields']);
}
} else {
if (strlen($this->options['db_fields']) > 0) {
if ($this->options['auto_quote']) {
return $this->db->quoteIdentifier($this->options['db_fields'], true);
} else {
return $this->options['db_fields'];
}
}
}
}
return '';
}

+ Here is the caller graph for this function:

Auth_Container_MDB2::_setDefaults ( )

Set some default options.

private

Returns
void

Definition at line 207 of file MDB2.php.

Referenced by Auth_Container_MDB2().

{
$this->options['table'] = 'auth';
$this->options['usernamecol'] = 'username';
$this->options['passwordcol'] = 'password';
$this->options['dsn'] = '';
$this->options['db_fields'] = '';
$this->options['cryptType'] = 'md5';
$this->options['db_options'] = array();
$this->options['db_where'] = '';
$this->options['auto_quote'] = true;
}

+ Here is the caller graph for this function:

Auth_Container_MDB2::addUser (   $username,
  $password,
  $additional = "" 
)

Add user to the storage container.

public

Parameters
stringUsername
stringPassword
mixedAdditional information that are stored in the DB
Returns
mixed True on success, otherwise error object

Reimplemented from Auth_Container.

Definition at line 445 of file MDB2.php.

References $additional, $query, $res, _prepare(), AUTH_LOG_DEBUG, MDB2\isError(), Auth_Container\log(), query(), and PEAR\raiseError().

{
$this->log('Auth_Container_MDB2::addUser() called.', AUTH_LOG_DEBUG);
// Prepare for a database query
$err = $this->_prepare();
if ($err !== true) {
return PEAR::raiseError($err->getMessage(), $err->getCode());
}
if (isset($this->options['cryptType']) && $this->options['cryptType'] == 'none') {
$cryptFunction = 'strval';
} elseif (isset($this->options['cryptType']) && function_exists($this->options['cryptType'])) {
$cryptFunction = $this->options['cryptType'];
} else {
$cryptFunction = 'md5';
}
$password = $cryptFunction($password);
$additional_key = '';
$additional_value = '';
if (is_array($additional)) {
foreach ($additional as $key => $value) {
if ($this->options['auto_quote']) {
$additional_key .= ', ' . $this->db->quoteIdentifier($key, true);
} else {
$additional_key .= ', ' . $key;
}
$additional_value .= ', ' . $this->db->quote($value, 'text');
}
}
$query = sprintf("INSERT INTO %s (%s, %s%s) VALUES (%s, %s%s)",
$this->options['final_table'],
$this->options['final_usernamecol'],
$this->options['final_passwordcol'],
$additional_key,
$this->db->quote($username, 'text'),
$this->db->quote($password, 'text'),
$additional_value
);
$this->log('Running SQL against MDB2: '.$query, AUTH_LOG_DEBUG);
$res = $this->query($query);
return PEAR::raiseError($res->getMessage(), $res->code);
}
return true;
}

+ Here is the call graph for this function:

Auth_Container_MDB2::Auth_Container_MDB2 (   $dsn)

Constructor of the container class.

Initate connection to the database via PEAR::MDB2

Parameters
stringConnection data or MDB2 object
Returns
object Returns an error object if something went wrong

Definition at line 86 of file MDB2.php.

References $dsn, _parseOptions(), _setDefaults(), and PEAR\raiseError().

Referenced by ilAuthContainerDatabase\ilAuthContainerDatabase().

{
$this->_setDefaults();
if (is_array($dsn)) {
if (empty($this->options['dsn'])) {
PEAR::raiseError('No connection parameters specified!');
}
} else {
$this->options['dsn'] = $dsn;
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Auth_Container_MDB2::changePassword (   $username,
  $password 
)

Change password for user in the storage container.

Parameters
stringUsername
stringThe new password (plain text)

Reimplemented from Auth_Container.

Definition at line 550 of file MDB2.php.

References $query, $res, _prepare(), AUTH_LOG_DEBUG, MDB2\isError(), Auth_Container\log(), query(), and PEAR\raiseError().

{
$this->log('Auth_Container_MDB2::changePassword() called.', AUTH_LOG_DEBUG);
// Prepare for a database query
$err = $this->_prepare();
if ($err !== true) {
return PEAR::raiseError($err->getMessage(), $err->getCode());
}
if (isset($this->options['cryptType']) && $this->options['cryptType'] == 'none') {
$cryptFunction = 'strval';
} elseif (isset($this->options['cryptType']) && function_exists($this->options['cryptType'])) {
$cryptFunction = $this->options['cryptType'];
} else {
$cryptFunction = 'md5';
}
$password = $cryptFunction($password);
$query = sprintf("UPDATE %s SET %s = %s WHERE %s = %s",
$this->options['final_table'],
$this->options['final_passwordcol'],
$this->db->quote($password, 'text'),
$this->options['final_usernamecol'],
$this->db->quote($username, 'text')
);
// check if there is an optional parameter db_where
if ($this->options['db_where'] != '') {
// there is one, so add it to the query
$query .= " AND ".$this->options['db_where'];
}
$this->log('Running SQL against MDB2: '.$query, AUTH_LOG_DEBUG);
$res = $this->query($query);
return PEAR::raiseError($res->getMessage(), $res->code);
}
return true;
}

+ Here is the call graph for this function:

Auth_Container_MDB2::fetchData (   $username,
  $password,
  $isChallengeResponse = false 
)

Get user information from database.

This function uses the given username to fetch the corresponding login data from the database table. If an account that matches the passed username and password is found, the function returns true. Otherwise it returns false.

Parameters
stringUsername
stringPassword
booleanIf true password is secured using a md5 hash the frontend and auth are responsible for making sure the container supports challenge response password authentication
Returns
mixed Error object or boolean

Reimplemented from Auth_Container.

Reimplemented in ilAuthContainerDatabase.

Definition at line 294 of file MDB2.php.

References $query, $res, _prepare(), _quoteDBFields(), AUTH_LOG_DEBUG, PEAR\isError(), MDB2\isError(), Auth_Container\log(), MDB2_FETCHMODE_ASSOC, PEAR\raiseError(), and Auth_Container\verifyPassword().

{
$this->log('Auth_Container_MDB2::fetchData() called.', AUTH_LOG_DEBUG);
// Prepare for a database query
$err = $this->_prepare();
if ($err !== true) {
return PEAR::raiseError($err->getMessage(), $err->getCode());
}
//Check if db_fields contains a *, if so assume all columns are selected
if (is_string($this->options['db_fields'])
&& strstr($this->options['db_fields'], '*')) {
$sql_from = '*';
} else {
$sql_from = $this->options['final_usernamecol'].
", ".$this->options['final_passwordcol'];
if (strlen($fields = $this->_quoteDBFields()) > 0) {
$sql_from .= ', '.$fields;
}
}
$query = sprintf("SELECT %s FROM %s WHERE %s = %s",
$sql_from,
$this->options['final_table'],
$this->options['final_usernamecol'],
$this->db->quote($username, 'text')
);
// check if there is an optional parameter db_where
if ($this->options['db_where'] != '') {
// there is one, so add it to the query
$query .= " AND ".$this->options['db_where'];
}
$this->log('Running SQL against MDB2: '.$query, AUTH_LOG_DEBUG);
$res = $this->db->queryRow($query, null, MDB2_FETCHMODE_ASSOC);
return PEAR::raiseError($res->getMessage(), $res->getCode());
}
if (!is_array($res)) {
$this->activeUser = '';
return false;
}
// Perform trimming here before the hashing
$password = trim($password, "\r\n");
$res[$this->options['passwordcol']] = trim($res[$this->options['passwordcol']], "\r\n");
// If using Challenge Response md5 the pass with the secret
if ($isChallengeResponse) {
$res[$this->options['passwordcol']] =
md5($res[$this->options['passwordcol']].$this->_auth_obj->session['loginchallenege']);
// UGLY cannot avoid without modifying verifyPassword
if ($this->options['cryptType'] == 'md5') {
$res[$this->options['passwordcol']] = md5($res[$this->options['passwordcol']]);
}
}
if ($this->verifyPassword($password,
$res[$this->options['passwordcol']],
$this->options['cryptType'])) {
// Store additional field values in the session
foreach ($res as $key => $value) {
if ($key == $this->options['passwordcol'] ||
$key == $this->options['usernamecol']) {
continue;
}
$this->log('Storing additional field: '.$key, AUTH_LOG_DEBUG);
// Use reference to the auth object if exists
// This is because the auth session variable can change so a static call to setAuthData does not make sense
$this->_auth_obj->setAuthData($key, $value);
}
return true;
}
$this->activeUser = $res[$this->options['usernamecol']];
return false;
}

+ Here is the call graph for this function:

Auth_Container_MDB2::getCryptType ( )

Returns the selected crypt type for this container.

Returns
string Function used to crypt the password

Reimplemented from Auth_Container.

Definition at line 616 of file MDB2.php.

{
return $this->options['cryptType'];
}
Auth_Container_MDB2::listUsers ( )

Returns a list of users from the container.

Returns
mixed array|PEAR_Error public

Reimplemented from Auth_Container.

Definition at line 383 of file MDB2.php.

References $query, $res, _prepare(), _quoteDBFields(), AUTH_LOG_DEBUG, MDB2\isError(), Auth_Container\log(), MDB2_FETCHMODE_ASSOC, and PEAR\raiseError().

{
$this->log('Auth_Container_MDB2::listUsers() called.', AUTH_LOG_DEBUG);
$err = $this->_prepare();
if ($err !== true) {
return PEAR::raiseError($err->getMessage(), $err->getCode());
}
$retVal = array();
//Check if db_fields contains a *, if so assume all columns are selected
if ( is_string($this->options['db_fields'])
&& strstr($this->options['db_fields'], '*')) {
$sql_from = '*';
} else {
$sql_from = $this->options['final_usernamecol'].
", ".$this->options['final_passwordcol'];
if (strlen($fields = $this->_quoteDBFields()) > 0) {
$sql_from .= ', '.$fields;
}
}
$query = sprintf('SELECT %s FROM %s',
$sql_from,
$this->options['final_table']
);
// check if there is an optional parameter db_where
if ($this->options['db_where'] != '') {
// there is one, so add it to the query
$query .= " WHERE ".$this->options['db_where'];
}
$this->log('Running SQL against MDB2: '.$query, AUTH_LOG_DEBUG);
$res = $this->db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
return PEAR::raiseError($res->getMessage(), $res->getCode());
} else {
foreach ($res as $user) {
$user['username'] = $user[$this->options['usernamecol']];
$retVal[] = $user;
}
}
$this->log('Found '.count($retVal).' users.', AUTH_LOG_DEBUG);
return $retVal;
}

+ Here is the call graph for this function:

Auth_Container_MDB2::query (   $query)

Prepare query to the database.

This function checks if we have already opened a connection to the database. If that's not the case, a new connection is opened. After that the query is passed to the database.

public

Parameters
stringQuery string
Returns
mixed a MDB_result object or MDB_OK on success, a MDB or PEAR error on failure

Definition at line 188 of file MDB2.php.

References $query, _prepare(), AUTH_LOG_DEBUG, and Auth_Container\log().

Referenced by addUser(), changePassword(), and removeUser().

{
$this->log('Auth_Container_MDB2::query() called.', AUTH_LOG_DEBUG);
$err = $this->_prepare();
if ($err !== true) {
return $err;
}
return $this->db->exec($query);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Auth_Container_MDB2::removeUser (   $username)

Remove user from the storage container.

public

Parameters
stringUsername
Returns
mixed True on success, otherwise error object

Reimplemented from Auth_Container.

Definition at line 510 of file MDB2.php.

References $query, $res, _prepare(), AUTH_LOG_DEBUG, MDB2\isError(), Auth_Container\log(), query(), and PEAR\raiseError().

{
$this->log('Auth_Container_MDB2::removeUser() called.', AUTH_LOG_DEBUG);
// Prepare for a database query
$err = $this->_prepare();
if ($err !== true) {
return PEAR::raiseError($err->getMessage(), $err->getCode());
}
$query = sprintf("DELETE FROM %s WHERE %s = %s",
$this->options['final_table'],
$this->options['final_usernamecol'],
$this->db->quote($username, 'text')
);
// check if there is an optional parameter db_where
if ($this->options['db_where'] != '') {
// there is one, so add it to the query
$query .= " AND ".$this->options['db_where'];
}
$this->log('Running SQL against MDB2: '.$query, AUTH_LOG_DEBUG);
$res = $this->query($query);
return PEAR::raiseError($res->getMessage(), $res->code);
}
return true;
}

+ Here is the call graph for this function:

Auth_Container_MDB2::supportsChallengeResponse ( )

Determine if this container supports password authentication with challenge response.

Returns
bool public

Reimplemented from Auth_Container.

Definition at line 603 of file MDB2.php.

{
return in_array($this->options['cryptType'], array('md5', 'none', ''));
}

Field Documentation

Auth_Container_MDB2::$activeUser = ''

Definition at line 73 of file MDB2.php.

Auth_Container_MDB2::$db = null

Definition at line 66 of file MDB2.php.

Auth_Container_MDB2::$dsn = ''
Auth_Container_MDB2::$options = array()

Definition at line 60 of file MDB2.php.

Referenced by ilAuthContainerMDB2\__construct().


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