1
0
mirror of https://github.com/moparisthebest/user_sql synced 2025-02-17 15:30:13 -05:00

Ported do PDO, based on user_redmine for configuration pages.

This commit is contained in:
Andreas Boehler 2012-11-27 15:43:08 +01:00
parent 78847181ef
commit 5d4557a2d3
4 changed files with 87 additions and 80 deletions

View File

@ -33,6 +33,7 @@ define('OC_USER_BACKEND_SQL_DEFAULT_PASSWORD', 'password');
define('OC_USER_BACKEND_SQL_DEFAULT_TABLE', 'users'); define('OC_USER_BACKEND_SQL_DEFAULT_TABLE', 'users');
define('OC_USER_BACKEND_SQL_DEFAULT_PW_COLUMN', 'password'); define('OC_USER_BACKEND_SQL_DEFAULT_PW_COLUMN', 'password');
define('OC_USER_BACKEND_SQL_DEFAULT_USER_COLUMN', 'username'); define('OC_USER_BACKEND_SQL_DEFAULT_USER_COLUMN', 'username');
define('OC_USER_BACKEND_SQL_DEFAULT_DRIVER', 'mysql');
// register user backend // register user backend
OC_User::registerBackend('SQL'); OC_User::registerBackend('SQL');

View File

@ -20,7 +20,7 @@
* License along with this library. If not, see <http://www.gnu.org/licenses/>. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
$params = array('sql_host', 'sql_user', 'sql_database', 'sql_password', 'sql_table', 'sql_column_username', 'sql_column_password'); $params = array('sql_host', 'sql_user', 'sql_database', 'sql_password', 'sql_table', 'sql_column_username', 'sql_column_password', 'sql_type');
OCP\Util::addscript('user_sql', 'settings'); OCP\Util::addscript('user_sql', 'settings');
@ -47,5 +47,6 @@ $tmpl->assign( 'sql_password', OCP\Config::getAppValue( 'user_sql', 'sql_passwor
$tmpl->assign( 'sql_table', OCP\Config::getAppValue( 'user_sql', 'sql_table', OC_USER_BACKEND_SQL_DEFAULT_TABLE)); $tmpl->assign( 'sql_table', OCP\Config::getAppValue( 'user_sql', 'sql_table', OC_USER_BACKEND_SQL_DEFAULT_TABLE));
$tmpl->assign( 'sql_column_password', OCP\Config::getAppValue( 'user_sql', 'sql_column_password', OC_USER_BACKEND_SQL_DEFAULT_PW_COLUMN)); $tmpl->assign( 'sql_column_password', OCP\Config::getAppValue( 'user_sql', 'sql_column_password', OC_USER_BACKEND_SQL_DEFAULT_PW_COLUMN));
$tmpl->assign( 'sql_column_username', OCP\Config::getAppValue( 'user_sql', 'sql_column_username', OC_USER_BACKEND_SQL_DEFAULT_USER_COLUMN)); $tmpl->assign( 'sql_column_username', OCP\Config::getAppValue( 'user_sql', 'sql_column_username', OC_USER_BACKEND_SQL_DEFAULT_USER_COLUMN));
$tmpl->assign( 'sql_type', OCP\Config::getAppValue( 'user_sql', 'sql_type', OC_USER_BACKEND_SQL_DEFAULT_DRIVER));
return $tmpl->fetchPage(); return $tmpl->fetchPage();

View File

@ -1,14 +1,29 @@
<form id="sql" action="#" method="post"> <form id="sql" action="#" method="post">
<fieldset class="personalblock"> <fieldset class="personalblock">
<legend><?php echo $l->t('SQL'); ?></legend> <legend><?php echo $l->t('SQL'); ?></legend>
<p><label for="sql_host"><?php echo $l->t('Host');?></label><input type="text" id="sql_host" name="sql_host" value="<?php echo $_['sql_host']; ?>"></p> <p><label for="sql_type"><?php echo $l->t('SQL Driver');?></label>
<p><label for="sql_user"><?php echo $l->t('Username');?></label><input type="text" id="sql_user" name="sql_user" value="<?php echo $_['sql_user']; ?>" /></p> <?php $db_driver = array('mysql' => 'MySQL', 'pgsql' => 'PostgreSQL');?>
<p><label for="sql_database"><?php echo $l->t('Database');?></label><input type="text" id="sql_database" name="sql_database" value="<?php echo $_['sql_database']; ?>" /></p> <select id="sql_type" name="sql_type">
<p><label for="sql_password"><?php echo $l->t('Password');?></label><input type="password" id="sql_password" name="sql_password" value="<?php echo $_['sql_password']; ?>" /></p> <?php
<p><label for="sql_table"><?php echo $l->t('Table');?></label><input type="text" id="sql_table" name="sql_table" value="<?php echo $_['sql_table']; ?>" /></p> foreach ($db_driver as $driver => $name):
<p><label for="sql_column_username"><?php echo $l->t('Username Column');?></label><input type="text" id="sql_column_username" name="sql_column_username" value="<?php echo $_['sql_column_username']; ?>" /></p> echo $_['sql_type'];
<p><label for="sql_column_password"><?php echo $l->t('Password Column');?></label><input type="text" id="sql_column_password" name="sql_column_password" value="<?php echo $_['sql_column_password']; ?>" /></p> if($_['sql_type'] == $driver): ?>
<option selected="selected" value="<?php echo $driver; ?>"><?php echo $name; ?></option>
<?php else: ?>
<option value="<?php echo $driver; ?>"><?php echo $name; ?></option>
<?php endif;
endforeach; ?>
</select>
</p>
<input type="submit" value="<?php echo $l->t('Save'); ?>" /> <p><label for="sql_host"><?php echo $l->t('Host');?></label><input type="text" id="sql_host" name="sql_host" value="<?php echo $_['sql_host']; ?>"></p>
</fieldset> <p><label for="sql_user"><?php echo $l->t('Username');?></label><input type="text" id="sql_user" name="sql_user" value="<?php echo $_['sql_user']; ?>" /></p>
<p><label for="sql_database"><?php echo $l->t('Database');?></label><input type="text" id="sql_database" name="sql_database" value="<?php echo $_['sql_database']; ?>" /></p>
<p><label for="sql_password"><?php echo $l->t('Password');?></label><input type="password" id="sql_password" name="sql_password" value="<?php echo $_['sql_password']; ?>" /></p>
<p><label for="sql_table"><?php echo $l->t('Table');?></label><input type="text" id="sql_table" name="sql_table" value="<?php echo $_['sql_table']; ?>" /></p>
<p><label for="sql_column_username"><?php echo $l->t('Username Column');?></label><input type="text" id="sql_column_username" name="sql_column_username" value="<?php echo $_['sql_column_username']; ?>" /></p>
<p><label for="sql_column_password"><?php echo $l->t('Password Column');?></label><input type="text" id="sql_column_password" name="sql_column_password" value="<?php echo $_['sql_column_password']; ?>" /></p>
<input type="submit" value="<?php echo $l->t('Save'); ?>" />
</fieldset>
</form> </form>

View File

@ -31,16 +31,32 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
protected $sql_table; protected $sql_table;
protected $sql_column_username; protected $sql_column_username;
protected $sql_column_password; protected $sql_column_password;
protected $sql_type;
protected $db_conn;
protected $db;
public function __construct() { public function __construct() {
$this->sql_host = OCP\Config::getAppValue('user_sql', 'sql_host', ''); $this->db_conn = false;
$this->sql_username = OCP\Config::getAppValue('user_sql', 'sql_user', ''); $this->sql_host = OCP\Config::getAppValue('user_sql', 'sql_host', '');
$this->sql_database = OCP\Config::getAppValue('user_sql', 'sql_database', ''); $this->sql_username = OCP\Config::getAppValue('user_sql', 'sql_user', '');
$this->sql_password = OCP\Config::getAppValue('user_sql', 'sql_password', ''); $this->sql_database = OCP\Config::getAppValue('user_sql', 'sql_database', '');
$this->sql_table = OCP\Config::getAppValue('user_sql', 'sql_table', ''); $this->sql_password = OCP\Config::getAppValue('user_sql', 'sql_password', '');
$this->sql_column_username = OCP\Config::getAppValue('user_sql', 'sql_column_username', ''); $this->sql_table = OCP\Config::getAppValue('user_sql', 'sql_table', '');
$this->sql_column_password = OCP\Config::getAppValue('user_sql', 'sql_column_password', ''); $this->sql_column_username = OCP\Config::getAppValue('user_sql', 'sql_column_username', '');
} $this->sql_column_password = OCP\Config::getAppValue('user_sql', 'sql_column_password', '');
$this->sql_type = OCP\Config::getAppValue('user_sql', 'sql_type', '');
$dsn = $this->sql_type.":host=".$this->sql_host.";dbname=".$this->sql_database;
try
{
$this->db = new PDO($dsn, $this->sql_username, $this->sql_password);
$this->db_conn = true;
}
catch (PDOException $e)
{
OC_Log::write('OC_USER_SQL', 'OC_USER_SQL, Failed to connect to the database: ' . $e->getMessage(), OC_Log::ERROR);
}
return false;
}
public function implementsAction($actions) { public function implementsAction($actions) {
return (bool)((OC_USER_BACKEND_CHECK_PASSWORD) & $actions); return (bool)((OC_USER_BACKEND_CHECK_PASSWORD) & $actions);
@ -48,19 +64,19 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
public function createUser() { public function createUser() {
// Can't create user // Can't create user
OC_Log::write('OC_USER_SQL', 'Not possible to create local users from web frontend using SQL user backend',3); OC_Log::write('OC_USER_SQL', 'Not possible to create local users from web frontend using SQL user backend', OC_Log::Error);
return false; return false;
} }
public function deleteUser( $uid ) { public function deleteUser( $uid ) {
// Can't delete user // Can't delete user
OC_Log::write('OC_USER_SQL', 'Not possible to delete local users from web frontend using SQL user backend',3); OC_Log::write('OC_USER_SQL', 'Not possible to delete local users from web frontend using SQL user backend', OC_Log::Error);
return false; return false;
} }
public function setPassword ( $uid, $password ) { public function setPassword ( $uid, $password ) {
// We can't change user password // We can't change user password
OC_Log::write('OC_USER_SQL', 'Not possible to change password for local users from web frontend using SQL user backend',3); OC_Log::write('OC_USER_SQL', 'Not possible to change password for local users from web frontend using SQL user backend', OC_Log::Error);
return false; return false;
} }
@ -72,32 +88,25 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
* *
* Check if the password is correct without logging in the user * Check if the password is correct without logging in the user
*/ */
public function checkPassword($uid, $password){ public function checkPassword($uid, $password)
$db = mysqli_connect ($this->sql_host, $this->sql_username, $this->sql_password); {
if ($db) if(!$this->db_conn)
{ {
$success = mysqli_select_db ($db, $this->sql_database);
if(!$success)
{
return false; return false;
} }
}
else
{
return false;
}
$query = "SELECT $this->sql_column_username, $this->sql_column_password FROM $this->sql_table WHERE $this->sql_column_username = '$uid';"; $query = "SELECT $this->sql_column_username, $this->sql_column_password FROM $this->sql_table WHERE $this->sql_column_username = '$uid';";
$result = mysqli_query($db, $query); $result = $this->db->prepare($query);
if(!$result) if(!$result->execute())
{ {
return false; return false;
} }
if(mysqli_num_rows($result) == 0) $row = $result->fetch();
if(!$row)
{ {
return false; return false;
} }
$row = mysqli_fetch_row($result); if(crypt($password, $row[$this->sql_column_password]) == $row[$this->sql_column_password])
if(crypt($password, $row[1]) == $row[1])
{ {
return $uid; return $uid;
} }
@ -116,16 +125,7 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
public function getUsers($search = '', $limit = null, $offset = null){ public function getUsers($search = '', $limit = null, $offset = null){
$users = array(); $users = array();
$db = mysqli_connect ($this->sql_host, $this->sql_username, $this->sql_password); if(!$this->db_conn)
if ($db)
{
$success = mysqli_select_db ($db, $this->sql_database);
if(!$success)
{
return false;
}
}
else
{ {
return false; return false;
} }
@ -136,18 +136,14 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
$query .= " LIMIT $limit"; $query .= " LIMIT $limit";
if($offset != null) if($offset != null)
$query .= " OFFSET $offset"; $query .= " OFFSET $offset";
$result = mysqli_query($db, $query); $result = $this->db->prepare($query);
if(!$result) if(!$result->execute())
{ {
return array(); return array();
} }
if(mysqli_num_rows($result) == 0) while($row = $result->fetch())
{ {
return array(); $users[] = $row[$this->sql_column_username];
}
while($row = mysqli_fetch_row($result))
{
$users[] = $row[0];
} }
return $users; return $users;
} }
@ -160,32 +156,26 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
public function userExists($uid) public function userExists($uid)
{ {
$db = mysqli_connect ($this->sql_host, $this->sql_username, $this->sql_password); if(!$this->db_conn)
if ($db) {
{
$success = mysqli_select_db ($db, $this->sql_database);
if(!$success)
{
return false; return false;
} }
$query = "SELECT $this->sql_column_username FROM $this->sql_table WHERE $this->sql_column_username = '$uid';"; $query = "SELECT $this->sql_column_username FROM $this->sql_table WHERE $this->sql_column_username = '$uid';";
$result = mysqli_query($db, $query); $result = $this->db->prepare($query);
if(!$result) if(!$result->execute())
{ {
return false; return false;
} }
if(mysqli_num_rows($result) == 0) $row = $result->fetch();
{ if(!$row)
{
return false; return false;
} }
return true; else
{
return true;
} }
else
{
return false;
}
} }