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

Public Member Functions

 Auth_Container_MDB ($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 MDB.php.

Member Function Documentation

Auth_Container_MDB::_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 MDB.php.

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

Referenced by _prepare().

{
$this->log('Auth_Container_MDB::_connect() called.', AUTH_LOG_DEBUG);
if (is_string($dsn) || is_array($dsn)) {
$this->db =& MDB::connect($dsn, $this->options['db_options']);
} elseif (is_subclass_of($dsn, 'mdb_common')) {
$this->db = $dsn;
} elseif (is_object($dsn) && MDB::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 (MDB::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']);
} else {
$t = explode('.', $this->options['table']);
for ($i = 0, $count = count($t); $i < $count; $i++)
$t[$i] = $this->db->quoteIdentifier($t[$i]);
$this->options['final_table'] = implode('.', $t);
}
$this->options['final_usernamecol'] = $this->db->quoteIdentifier($this->options['usernamecol']);
$this->options['final_passwordcol'] = $this->db->quoteIdentifier($this->options['passwordcol']);
} 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_MDB::_parseOptions (   $array)

Parse options passed to the container class.

private

Parameters
array

Definition at line 229 of file MDB.php.

Referenced by Auth_Container_MDB().

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

+ Here is the caller graph for this function:

Auth_Container_MDB::_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 MDB.php.

References _connect().

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

{
if (is_subclass_of($this->db, 'mdb_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_MDB::_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 MDB.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);
}
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']);
} else {
return $this->options['db_fields'];
}
}
}
}
return '';
}

+ Here is the caller graph for this function:

Auth_Container_MDB::_setDefaults ( )

Set some default options.

private

Returns
void

Definition at line 207 of file MDB.php.

Referenced by Auth_Container_MDB().

{
$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_MDB::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 450 of file MDB.php.

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

{
$this->log('Auth_Container_MDB::addUser() called.', AUTH_LOG_DEBUG);
$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);
} else {
$additional_key .= ', ' . $key;
}
$additional_value .= ', ' . $this->db->getTextValue($value);
}
}
$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->getTextValue($username),
$this->db->getTextValue($password),
$additional_value
);
$this->log('Running SQL against MDB: '.$query, AUTH_LOG_DEBUG);
$res = $this->query($query);
if (MDB::isError($res)) {
return PEAR::raiseError($res->getMessage(), $res->code);
}
return true;
}

+ Here is the call graph for this function:

Auth_Container_MDB::Auth_Container_MDB (   $dsn)

Constructor of the container class.

Initate connection to the database via PEAR::MDB

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

Definition at line 86 of file MDB.php.

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

{
$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:

Auth_Container_MDB::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 552 of file MDB.php.

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

{
$this->log('Auth_Container_MDB::changePassword() called.', AUTH_LOG_DEBUG);
$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->getTextValue($password),
$this->options['final_usernamecol'],
$this->db->getTextValue($username)
);
// 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 MDB: '.$query, AUTH_LOG_DEBUG);
$res = $this->query($query);
if (MDB::isError($res)) {
return PEAR::raiseError($res->getMessage(), $res->code);
}
return true;
}

+ Here is the call graph for this function:

Auth_Container_MDB::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.

Definition at line 294 of file MDB.php.

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

{
$this->log('Auth_Container_MDB::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->getTextValue($username)
);
// 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 MDB: '.$query, AUTH_LOG_DEBUG);
$res = $this->db->getRow($query, null, null, null, MDB_FETCHMODE_ASSOC);
if (MDB::isError($res) || PEAR::isError($res)) {
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_MDB::getCryptType ( )

Returns the selected crypt type for this container.

Returns
string Function used to crypt the password

Reimplemented from Auth_Container.

Definition at line 617 of file MDB.php.

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

Returns a list of users from the container.

Returns
mixed array|PEAR_Error public

Reimplemented from Auth_Container.

Definition at line 387 of file MDB.php.

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

{
$this->log('Auth_Container_MDB::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 MDB: '.$query, AUTH_LOG_DEBUG);
$res = $this->db->getAll($query, null, null, null, MDB_FETCHMODE_ASSOC);
if (MDB::isError($res)) {
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_MDB::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 MDB.php.

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

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

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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Auth_Container_MDB::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 513 of file MDB.php.

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

{
$this->log('Auth_Container_MDB::removeUser() called.', AUTH_LOG_DEBUG);
$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->getTextValue($username)
);
// 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 MDB: '.$query, AUTH_LOG_DEBUG);
$res = $this->query($query);
if (MDB::isError($res)) {
return PEAR::raiseError($res->getMessage(), $res->code);
}
return true;
}

+ Here is the call graph for this function:

Auth_Container_MDB::supportsChallengeResponse ( )

Determine if this container supports password authentication with challenge response.

Returns
bool public

Reimplemented from Auth_Container.

Definition at line 604 of file MDB.php.

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

Field Documentation

Auth_Container_MDB::$activeUser = ''

Definition at line 73 of file MDB.php.

Auth_Container_MDB::$db = null

Definition at line 66 of file MDB.php.

Auth_Container_MDB::$dsn = ''

Definition at line 67 of file MDB.php.

Referenced by _connect(), and Auth_Container_MDB().

Auth_Container_MDB::$options = array()

Definition at line 60 of file MDB.php.


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