1
0
mirror of https://github.com/moparisthebest/user_sql synced 2024-11-30 12:52:16 -05:00

Add ability to synchronise E-Mail address

This commit is contained in:
Andreas Boehler 2015-07-07 22:54:05 +02:00
parent 7b722f31c3
commit 032c2b5bd2
6 changed files with 100 additions and 13 deletions

View File

@ -15,7 +15,8 @@ $params = array('sql_host', 'sql_user', 'sql_database', 'sql_password',
'sql_table', 'sql_column_username', 'sql_column_password', 'sql_type', 'sql_table', 'sql_column_username', 'sql_column_password', 'sql_type',
'sql_column_active', 'strip_domain', 'default_domain', 'crypt_type', 'sql_column_active', 'strip_domain', 'default_domain', 'crypt_type',
'sql_column_displayname', 'domain_settings', 'map_array', 'domain_array', 'sql_column_displayname', 'domain_settings', 'map_array', 'domain_array',
'allow_password_change', 'sql_column_active_invert'); 'allow_password_change', 'sql_column_active_invert', 'sql_column_email',
'mail_sync_mode');
if(isset($_POST['appname']) && $_POST['appname'] == "user_sql") if(isset($_POST['appname']) && $_POST['appname'] == "user_sql")
{ {

View File

@ -4,7 +4,7 @@
<name>SQL user backend</name> <name>SQL user backend</name>
<description>Authenticate Users by SQL</description> <description>Authenticate Users by SQL</description>
<licence>AGPL</licence> <licence>AGPL</licence>
<author>Andreas Boehler &lt;andreas (at) aboehler <author>Andreas Boehler &lt;dev (at) aboehler
(dot) at &gt;</author> (dot) at &gt;</author>
<require>5.0</require> <require>5.0</require>
<shipped>false</shipped> <shipped>false</shipped>

View File

@ -1 +1 @@
1.4 1.5

View File

@ -23,7 +23,8 @@
$params = array('sql_host', 'sql_user', 'sql_database', 'sql_password', 'sql_table', $params = array('sql_host', 'sql_user', 'sql_database', 'sql_password', 'sql_table',
'sql_column_username', 'sql_column_password', 'sql_type', 'sql_column_active', 'sql_column_username', 'sql_column_password', 'sql_type', 'sql_column_active',
'strip_domain', 'default_domain', 'crypt_type', 'sql_column_displayname', 'strip_domain', 'default_domain', 'crypt_type', 'sql_column_displayname',
'domain_map', 'domain_settings', 'sql_column_active_invert'); 'domain_map', 'domain_settings', 'sql_column_active_invert', 'sql_column_email',
'mail_sync_mode');
OCP\Util::addStyle('user_sql', 'settings'); OCP\Util::addStyle('user_sql', 'settings');
OCP\Util::addScript('user_sql', 'settings'); OCP\Util::addScript('user_sql', 'settings');
@ -47,6 +48,8 @@ $tmpl -> assign('sql_column_password', OCP\Config::getAppValue('user_sql', 'sql_
$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)); $tmpl -> assign('sql_type', OCP\Config::getAppValue('user_sql', 'sql_type', OC_USER_BACKEND_SQL_DEFAULT_DRIVER));
$tmpl -> assign('sql_column_active', OCP\Config::getAppValue('user_sql', 'sql_column_active', '')); $tmpl -> assign('sql_column_active', OCP\Config::getAppValue('user_sql', 'sql_column_active', ''));
$tmpl -> assign('sql_column_email', OCP\Config::getAppValue('user_sql', 'sql_column_email', ''));
$tmpl -> assign('mail_sync_mode', OCP\Config::getAppValue('user_sql', 'mail_sync_mode', 'none'));
$tmpl -> assign('strip_domain', OCP\Config::getAppValue('user_sql', 'strip_domain', 0)); $tmpl -> assign('strip_domain', OCP\Config::getAppValue('user_sql', 'strip_domain', 0));
$tmpl -> assign('default_domain', OCP\Config::getAppValue('user_sql', 'default_domain', '')); $tmpl -> assign('default_domain', OCP\Config::getAppValue('user_sql', 'default_domain', ''));
$tmpl -> assign('crypt_type', OCP\Config::getAppValue('user_sql', 'crypt_type', 'mysql_encrypt')); $tmpl -> assign('crypt_type', OCP\Config::getAppValue('user_sql', 'crypt_type', 'mysql_encrypt'));

View File

@ -27,7 +27,7 @@ $cfgClass = $ocVersion >= 7 ? 'section' : 'personalblock';
<option value="<?php echo $driver; ?>"><?php echo $name; ?></option> <option value="<?php echo $driver; ?>"><?php echo $name; ?></option>
<?php endif; <?php endif;
endforeach; endforeach;
?> ?>
</select></td> </select></td>
</tr> </tr>
@ -58,8 +58,8 @@ $cfgClass = $ocVersion >= 7 ? 'section' : 'personalblock';
<?php else: ?> <?php else: ?>
<option value="<?php echo $driver; ?>"><?php echo $name; ?></option> <option value="<?php echo $driver; ?>"><?php echo $name; ?></option>
<?php endif; <?php endif;
endforeach; endforeach;
?> ?>
</select></td> </select></td>
</tr> </tr>
<tr><td><label for="sql_column_active"><?php echo $l -> t('User Active Column'); ?></label></td><td><input type="text" id="sql_column_active" name="sql_column_active" value="<?php echo $_['sql_column_active']; ?>" /></td></tr> <tr><td><label for="sql_column_active"><?php echo $l -> t('User Active Column'); ?></label></td><td><input type="text" id="sql_column_active" name="sql_column_active" value="<?php echo $_['sql_column_active']; ?>" /></td></tr>
@ -67,6 +67,22 @@ $cfgClass = $ocVersion >= 7 ? 'section' : 'personalblock';
if($_['sql_column_active_invert']) if($_['sql_column_active_invert'])
echo ' checked'; echo ' checked';
?> title="Invert the logic of the active column (for blocked users in the SQL DB)" /></td></tr> ?> title="Invert the logic of the active column (for blocked users in the SQL DB)" /></td></tr>
<tr><td><label for="sql_column_email"><?php echo $l -> t('E-Mail Column'); ?></label></td><td><input type="text" id="sql_column_email" name="sql_column_email" value="<?php echo $_['sql_column_email']; ?>" /></td></tr>
<tr><td><label for="mail_sync_mode"><?php echo $l -> t('E-Mail address sync mode'); ?></label></td>
<?php $mail_modes = array('none' => 'No Synchronisation', 'initial' => 'Synchronise only once', 'forceoc' => 'ownCloud always wins', 'forcesql' => 'SQL always wins'); ?>
<td><select id="mail_sync_mode" name="mail_sync_mode">
<?php
foreach ($mail_modes as $mode => $name):
echo $_['mail_sync_mode'];
if($_['mail_sync_mode'] == $mode): ?>
<option selected="selected" value="<?php echo $mode; ?>"><?php echo $name; ?></option>
<?php else: ?>
<option value="<?php echo $mode; ?>"><?php echo $name; ?></option>
<?php endif;
endforeach;
?>
</select>
</td></tr>
</table> </table>
</fieldset> </fieldset>
<fieldset id="sql-3"> <fieldset id="sql-3">
@ -75,19 +91,19 @@ $cfgClass = $ocVersion >= 7 ? 'section' : 'personalblock';
<tr><td><input type="radio" name="domain_settings" id="domain_none" value="none" <?php <tr><td><input type="radio" name="domain_settings" id="domain_none" value="none" <?php
if($_['domain_settings'] == "" || $_['domain_settings'] == "none") if($_['domain_settings'] == "" || $_['domain_settings'] == "none")
echo 'checked="checked"'; echo 'checked="checked"';
?>><?php echo $l->t('No Mapping') ?></td></tr> ?>><?php echo $l->t('No Mapping') ?></td></tr>
<tr><td><input type="radio" name="domain_settings" id="domain_server" value="server" <?php <tr><td><input type="radio" name="domain_settings" id="domain_server" value="server" <?php
if($_['domain_settings'] == "server") if($_['domain_settings'] == "server")
echo 'checked="checked"'; echo 'checked="checked"';
?>><?php echo $l->t('Append Server Hostname') ?></td><td></td></tr> ?>><?php echo $l->t('Append Server Hostname') ?></td><td></td></tr>
<tr><td><input type="radio" name="domain_settings" id="domain_default" value="default" <?php <tr><td><input type="radio" name="domain_settings" id="domain_default" value="default" <?php
if($_['domain_settings'] == "default") if($_['domain_settings'] == "default")
echo 'checked="checked"'; echo 'checked="checked"';
?>><?php echo $l->t('Append Default') ?></td><td><input type="text" id="default_domain" name="default_domain" value="<?php echo $_['default_domain']; ?>" /></td></tr> ?>><?php echo $l->t('Append Default') ?></td><td><input type="text" id="default_domain" name="default_domain" value="<?php echo $_['default_domain']; ?>" /></td></tr>
<tr><td><input type="radio" name="domain_settings" id="domain_mapping" value="mapping" <?php <tr><td><input type="radio" name="domain_settings" id="domain_mapping" value="mapping" <?php
if($_['domain_settings'] == "mapping") if($_['domain_settings'] == "mapping")
echo 'checked="checked"'; echo 'checked="checked"';
?>><?php echo $l->t('Map Domains') ?></td><td> ?>><?php echo $l->t('Map Domains') ?></td><td>
<table id="domain_map_entries" cellspacing="2" cellpadding="2"> <table id="domain_map_entries" cellspacing="2" cellpadding="2">
<tbody> <tbody>
<tr><th><input type="text" placeholder="Server Domain" id="inputServerDomain"></th><th><input type="text" placeholder="Map to Domain" id="inputMapDomain"></th><th><input id="domainAddMap" type="submit" value="<?php echo $l -> t('Add Entry'); ?>" /></th></tr> <tr><th><input type="text" placeholder="Server Domain" id="inputServerDomain"></th><th><input type="text" placeholder="Map to Domain" id="inputMapDomain"></th><th><input id="domainAddMap" type="submit" value="<?php echo $l -> t('Add Entry'); ?>" /></th></tr>
@ -105,7 +121,7 @@ $cfgClass = $ocVersion >= 7 ? 'section' : 'personalblock';
<tr><td><label for="strip_domain"><?php echo $l -> t('Strip Domain Part from Username'); ?></label></td><td><input type="checkbox" id="strip_domain" name="strip_domain" value="1"<?php <tr><td><label for="strip_domain"><?php echo $l -> t('Strip Domain Part from Username'); ?></label></td><td><input type="checkbox" id="strip_domain" name="strip_domain" value="1"<?php
if($_['strip_domain']) if($_['strip_domain'])
echo ' checked'; echo ' checked';
?> title="Strip Domain Part from Username when logging in and retrieving username lists"></td></tr> ?> title="Strip Domain Part from Username when logging in and retrieving username lists"></td></tr>
</table> </table>
</fieldset> </fieldset>

View File

@ -4,7 +4,7 @@
* ownCloud - user_sql * ownCloud - user_sql
* *
* @author Andreas Böhler and contributors * @author Andreas Böhler and contributors
* @copyright 2012/2013 Andreas Böhler <andreas (at) aboehler (dot) at> * @copyright 2012-2015 Andreas Böhler <dev (at) aboehler (dot) at>
* *
* credits go to Ed W for several SQL injection fixes and caching support * credits go to Ed W for several SQL injection fixes and caching support
* credits go to Frédéric France for providing Joomla support * credits go to Frédéric France for providing Joomla support
@ -40,6 +40,8 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface
protected $sql_column_active; protected $sql_column_active;
protected $sql_column_active_invert; protected $sql_column_active_invert;
protected $sql_column_displayname; protected $sql_column_displayname;
protected $sql_column_email;
protected $mail_sync_mode;
protected $sql_type; protected $sql_type;
protected $db_conn; protected $db_conn;
protected $db; protected $db;
@ -63,6 +65,7 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface
$this -> sql_column_username = OCP\Config::getAppValue('user_sql', 'sql_column_username', ''); $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_column_password = OCP\Config::getAppValue('user_sql', 'sql_column_password', '');
$this -> sql_column_displayname = OCP\Config::getAppValue('user_sql', 'sql_column_displayname', $this->sql_column_username); $this -> sql_column_displayname = OCP\Config::getAppValue('user_sql', 'sql_column_displayname', $this->sql_column_username);
$this -> sql_column_email = OCP\Config::getAppValue('user_sql', 'sql_column_email', '');
$this -> sql_column_active = OCP\Config::getAppValue('user_sql', 'sql_column_active', ''); $this -> sql_column_active = OCP\Config::getAppValue('user_sql', 'sql_column_active', '');
$this -> sql_column_active_invert = OCP\Config::getAppValue('user_sql', 'sql_column_active_invert', 0); $this -> sql_column_active_invert = OCP\Config::getAppValue('user_sql', 'sql_column_active_invert', 0);
$this -> sql_type = OCP\Config::getAppValue('user_sql', 'sql_type', ''); $this -> sql_type = OCP\Config::getAppValue('user_sql', 'sql_type', '');
@ -73,6 +76,7 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface
$this -> domain_settings = OCP\Config::getAppValue('user_sql', 'domain_settings', 'none'); $this -> domain_settings = OCP\Config::getAppValue('user_sql', 'domain_settings', 'none');
$this -> domain_array = explode(",", OCP\Config::getAppValue('user_sql', 'domain_array', '')); $this -> domain_array = explode(",", OCP\Config::getAppValue('user_sql', 'domain_array', ''));
$this -> map_array = explode(",", OCP\Config::getAppValue('user_sql', 'map_array', '')); $this -> map_array = explode(",", OCP\Config::getAppValue('user_sql', 'map_array', ''));
$this -> mail_sync_mode = OCP\Config::getAppValue('user_sql', 'mail_sync_mode', 'none');
$dsn = $this -> sql_type . ":host=" . $this -> sql_host . ";dbname=" . $this -> sql_database; $dsn = $this -> sql_type . ":host=" . $this -> sql_host . ";dbname=" . $this -> sql_database;
try try
{ {
@ -86,6 +90,68 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface
return false; return false;
} }
private function doEmailSync($uid)
{
OC_Log::write('OC_USER_SQL', "Entering doEmailSync for UID: $uid", OC_Log::DEBUG);
if($this -> sql_column_email === '')
return false;
if($this -> mail_sync_mode == 'none')
return false;
$ocUid = $uid;
$uid = $this -> doUserDomainMapping($uid);
$query = "SELECT $this->sql_column_email FROM $this->sql_table WHERE $this->sql_column_username = :uid";
OC_Log::write('OC_USER_SQL', "Preparing query: $query", OC_Log::DEBUG);
$result = $this -> db -> prepare($query);
$result -> bindParam(":uid", $uid);
OC_Log::write('OC_USER_SQL', "Executing query...", OC_Log::DEBUG);
if(!$result -> execute())
{
return false;
}
OC_Log::write('OC_USER_SQL', "Fetching result...", OC_Log::DEBUG);
$row = $result -> fetch();
if(!$row)
{
return false;
}
$newMail = $row[$this -> sql_column_email];
$currMail = OCP\Config::getUserValue($ocUid, 'settings', 'email', '');
switch($this -> mail_sync_mode)
{
case 'initial':
if($currMail === '')
OCP\Config::setUserValue($ocUid, 'settings', 'email', $newMail);
break;
case 'forcesql':
if($currMail != $newMail)
OCP\Config::setUserValue($ocUid, 'settings', 'email', $newMail);
break;
case 'forceoc':
if(($currMail !== '') && ($currMail != $newMail))
{
$query = "UPDATE $this->sql_table SET $this->sql_column_email = :currMail WHERE $this->sql_column_username = :uid";
OC_Log::write('OC_USER_SQL', "Preapring query: $query", OC_Log::DEBUG);
$result = $this -> db -> prepare($query);
$result -> bindParam(":currMail", $currMail);
$result -> bindParam(":uid", $uid);
OC_Log::write('OC_USER_SQL', "Executing query...", OC_Log::DEBUG);
if(!$result -> execute())
{
$err = $result -> errorInfo();
OC_Log::write('OC_USER_SQL', "Query failed: " . $err[2], OC_Log::DEBUG);
OC_Log::write('OC_USER_SQL', "Could not update E-Mail address in SQL database!", OC_Log::ERROR);
}
}
break;
}
return true;
}
private function doUserDomainMapping($uid) private function doUserDomainMapping($uid)
{ {
$uid = trim($uid); $uid = trim($uid);
@ -422,6 +488,7 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface
{ {
return false; return false;
} }
$this -> doEmailSync($uid);
$uid = $this -> doUserDomainMapping($uid); $uid = $this -> doUserDomainMapping($uid);
if(!$this -> userExists($uid)) if(!$this -> userExists($uid))