Initial import.

This commit is contained in:
Andreas Boehler 2012-11-27 12:29:42 +01:00
commit 78847181ef
8 changed files with 333 additions and 0 deletions

10
README.md Normal file
View File

@ -0,0 +1,10 @@
user_sql
========
Owncloud SQL authentification
This is plugin is heavily based on user_imap, user_pwauth and user_ldap!
Enable it in your Admin -> Apps section and configure your server's details.
Currently, it only works with mySQL and the crypt() password encryption string.
It was tested and developed for a postfixadmin database.

49
appinfo/app.php Normal file
View File

@ -0,0 +1,49 @@
<?php
/**
* ownCloud - user_sql
*
* @author Andreas Böhler
* @copyright 2012 Andreas Böhler <andreas (at) aboehler (dot) at>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
require_once('apps/user_sql/user_sql.php');
OC_App::registerAdmin('user_sql','settings');
// define IMAP_DEFAULTs
define('OC_USER_BACKEND_SQL_DEFAULT_HOST', 'localhost');
define('OC_USER_BACKEND_SQL_DEFAULT_USER', 'mail_admin');
define('OC_USER_BACKEND_SQL_DEFAULT_DB', 'postfixadmin');
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');
// register user backend
OC_User::registerBackend('SQL');
OC_User::useBackend('SQL');
// add settings page to navigation
$entry = array(
'id' => "user_sql_settings",
'order'=>1,
'href' => OC_Helper::linkTo( "user_sql", "settings.php" ),
'name' => 'SQL'
);

13
appinfo/info.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0"?>
<info>
<id>user_sql</id>
<name>SQL user backend</name>
<description>Authenticate Users by SQL</description>
<licence>AGPL</licence>
<author>Andreas Boehler &lt;andreas.boehler@pmu.ac.at&gt;</author>
<require>4.5</require>
<shipped>false</shipped>
<types>
<authentication/>
</types>
</info>

1
appinfo/version Normal file
View File

@ -0,0 +1 @@
0.1

1
js/settings.js Normal file
View File

@ -0,0 +1 @@
//

51
settings.php Normal file
View File

@ -0,0 +1,51 @@
<?php
/**
* ownCloud - user_sql
*
* @author Andreas Böhler
* @copyright 2012 Andreas Böhler <andreas (at) aboehler (dot) at>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* 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');
OCP\Util::addscript('user_sql', 'settings');
if ($_POST) {
foreach($params as $param){
if(isset($_POST[$param])){
OCP\Config::setAppValue('user_sql', $param, $_POST[$param]);
}
}
}
// fill template
$tmpl = new OCP\Template( 'user_sql', 'settings');
foreach($params as $param){
$value = htmlentities(OCP\Config::getAppValue('user_sql', $param,''));
$tmpl->assign($param, $value);
}
// settings with default values
$tmpl->assign( 'sql_host', OCP\Config::getAppValue('user_sql', 'sql_host', OC_USER_BACKEND_SQL_DEFAULT_HOST));
$tmpl->assign( 'sql_user', OCP\Config::getAppValue('user_sql', 'sql_user', OC_USER_BACKEND_SQL_DEFAULT_USER));
$tmpl->assign( 'sql_database', OCP\Config::getAppValue( 'user_sql', 'sql_database', OC_USER_BACKEND_SQL_DEFAULT_DB));
$tmpl->assign( 'sql_password', OCP\Config::getAppValue( 'user_sql', 'sql_password', OC_USER_BACKEND_SQL_DEFAULT_PASSWORD));
$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));
return $tmpl->fetchPage();

14
templates/settings.php Normal file
View File

@ -0,0 +1,14 @@
<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>
<input type="submit" value="<?php echo $l->t('Save'); ?>" />
</fieldset>
</form>

194
user_sql.php Normal file
View File

@ -0,0 +1,194 @@
<?php
/**
* ownCloud - user_sql
*
* @author Andreas Böhler
* @copyright 2012 Andreas Böhler <andreas (at) aboehler (dot) at>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface {
// cached settings
protected $sql_host;
protected $sql_username;
protected $sql_database;
protected $sql_password;
protected $sql_table;
protected $sql_column_username;
protected $sql_column_password;
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 implementsAction($actions) {
return (bool)((OC_USER_BACKEND_CHECK_PASSWORD) & $actions);
}
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);
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);
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);
return false;
}
/**
* @brief Check if the password is correct
* @param $uid The username
* @param $password The password
* @returns true/false
*
* 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)
{
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)
{
return false;
}
if(mysqli_num_rows($result) == 0)
{
return false;
}
$row = mysqli_fetch_row($result);
if(crypt($password, $row[1]) == $row[1])
{
return $uid;
}
else
{
return false;
}
}
/**
* @brief Get a list of all users
* @returns array with all uids
*
* Get a list of all users.
*/
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
{
return false;
}
$query = "SELECT $this->sql_column_username FROM $this->sql_table";
if($search != '')
$query .= " WHERE $this->sql_column_username LIKE '%$search%'";
if($limit != null)
$query .= " LIMIT $limit";
if($offset != null)
$query .= " OFFSET $offset";
$result = mysqli_query($db, $query);
if(!$result)
{
return array();
}
if(mysqli_num_rows($result) == 0)
{
return array();
}
while($row = mysqli_fetch_row($result))
{
$users[] = $row[0];
}
return $users;
}
/**
* @brief check if a user exists
* @param string $uid the username
* @return boolean
*/
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)
{
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)
{
return false;
}
if(mysqli_num_rows($result) == 0)
{
return false;
}
return true;
}
else
{
return false;
}
}
}
?>