1
0
mirror of https://github.com/moparisthebest/user_sql synced 2024-11-24 10:02:19 -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_PW_COLUMN', 'password');
define('OC_USER_BACKEND_SQL_DEFAULT_USER_COLUMN', 'username');
define('OC_USER_BACKEND_SQL_DEFAULT_DRIVER', 'mysql');
// register user backend
OC_User::registerBackend('SQL');

View File

@ -20,7 +20,7 @@
* 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');
@ -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_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_type', OCP\Config::getAppValue( 'user_sql', 'sql_type', OC_USER_BACKEND_SQL_DEFAULT_DRIVER));
return $tmpl->fetchPage();

View File

@ -1,14 +1,29 @@
<form id="sql" action="#" method="post">
<fieldset class="personalblock">
<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_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>
<fieldset class="personalblock">
<legend><?php echo $l->t('SQL'); ?></legend>
<p><label for="sql_type"><?php echo $l->t('SQL Driver');?></label>
<?php $db_driver = array('mysql' => 'MySQL', 'pgsql' => 'PostgreSQL');?>
<select id="sql_type" name="sql_type">
<?php
foreach ($db_driver as $driver => $name):
echo $_['sql_type'];
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'); ?>" />
</fieldset>
<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_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>

View File

@ -31,16 +31,32 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
protected $sql_table;
protected $sql_column_username;
protected $sql_column_password;
protected $sql_type;
protected $db_conn;
protected $db;
public function __construct() {
$this->sql_host = OCP\Config::getAppValue('user_sql', 'sql_host', '');
$this->sql_username = OCP\Config::getAppValue('user_sql', 'sql_user', '');
$this->sql_database = OCP\Config::getAppValue('user_sql', 'sql_database', '');
$this->sql_password = OCP\Config::getAppValue('user_sql', 'sql_password', '');
$this->sql_table = OCP\Config::getAppValue('user_sql', 'sql_table', '');
$this->sql_column_username = OCP\Config::getAppValue('user_sql', 'sql_column_username', '');
$this->sql_column_password = OCP\Config::getAppValue('user_sql', 'sql_column_password', '');
}
public function __construct() {
$this->db_conn = false;
$this->sql_host = OCP\Config::getAppValue('user_sql', 'sql_host', '');
$this->sql_username = OCP\Config::getAppValue('user_sql', 'sql_user', '');
$this->sql_database = OCP\Config::getAppValue('user_sql', 'sql_database', '');
$this->sql_password = OCP\Config::getAppValue('user_sql', 'sql_password', '');
$this->sql_table = OCP\Config::getAppValue('user_sql', 'sql_table', '');
$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) {
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() {
// 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;
}
public function deleteUser( $uid ) {
// 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;
}
public function setPassword ( $uid, $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;
}
@ -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
*/
public function checkPassword($uid, $password){
$db = mysqli_connect ($this->sql_host, $this->sql_username, $this->sql_password);
if ($db)
{
$success = mysqli_select_db ($db, $this->sql_database);
if(!$success)
{
public function checkPassword($uid, $password)
{
if(!$this->db_conn)
{
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';";
$result = mysqli_query($db, $query);
if(!$result)
$result = $this->db->prepare($query);
if(!$result->execute())
{
return false;
}
if(mysqli_num_rows($result) == 0)
$row = $result->fetch();
if(!$row)
{
return false;
}
$row = mysqli_fetch_row($result);
if(crypt($password, $row[1]) == $row[1])
if(crypt($password, $row[$this->sql_column_password]) == $row[$this->sql_column_password])
{
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){
$users = array();
$db = mysqli_connect ($this->sql_host, $this->sql_username, $this->sql_password);
if ($db)
{
$success = mysqli_select_db ($db, $this->sql_database);
if(!$success)
{
return false;
}
}
else
if(!$this->db_conn)
{
return false;
}
@ -136,18 +136,14 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
$query .= " LIMIT $limit";
if($offset != null)
$query .= " OFFSET $offset";
$result = mysqli_query($db, $query);
if(!$result)
$result = $this->db->prepare($query);
if(!$result->execute())
{
return array();
}
if(mysqli_num_rows($result) == 0)
while($row = $result->fetch())
{
return array();
}
while($row = mysqli_fetch_row($result))
{
$users[] = $row[0];
$users[] = $row[$this->sql_column_username];
}
return $users;
}
@ -160,32 +156,26 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
public function userExists($uid)
{
$db = mysqli_connect ($this->sql_host, $this->sql_username, $this->sql_password);
if ($db)
{
$success = mysqli_select_db ($db, $this->sql_database);
if(!$success)
{
if(!$this->db_conn)
{
return false;
}
}
$query = "SELECT $this->sql_column_username FROM $this->sql_table WHERE $this->sql_column_username = '$uid';";
$result = mysqli_query($db, $query);
if(!$result)
$result = $this->db->prepare($query);
if(!$result->execute())
{
return false;
}
if(mysqli_num_rows($result) == 0)
{
$row = $result->fetch();
if(!$row)
{
return false;
}
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}