mirror of
https://github.com/moparisthebest/user_sql
synced 2024-11-24 01:52:18 -05:00
Add ability to synchronise E-Mail address
This commit is contained in:
parent
7b722f31c3
commit
032c2b5bd2
@ -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_column_active', 'strip_domain', 'default_domain', 'crypt_type',
|
||||
'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")
|
||||
{
|
||||
|
@ -4,7 +4,7 @@
|
||||
<name>SQL user backend</name>
|
||||
<description>Authenticate Users by SQL</description>
|
||||
<licence>AGPL</licence>
|
||||
<author>Andreas Boehler <andreas (at) aboehler
|
||||
<author>Andreas Boehler <dev (at) aboehler
|
||||
(dot) at ></author>
|
||||
<require>5.0</require>
|
||||
<shipped>false</shipped>
|
||||
|
@ -1 +1 @@
|
||||
1.4
|
||||
1.5
|
||||
|
@ -23,7 +23,8 @@
|
||||
$params = array('sql_host', 'sql_user', 'sql_database', 'sql_password', 'sql_table',
|
||||
'sql_column_username', 'sql_column_password', 'sql_type', 'sql_column_active',
|
||||
'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::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_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_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('default_domain', OCP\Config::getAppValue('user_sql', 'default_domain', ''));
|
||||
$tmpl -> assign('crypt_type', OCP\Config::getAppValue('user_sql', 'crypt_type', 'mysql_encrypt'));
|
||||
|
@ -27,7 +27,7 @@ $cfgClass = $ocVersion >= 7 ? 'section' : 'personalblock';
|
||||
<option value="<?php echo $driver; ?>"><?php echo $name; ?></option>
|
||||
<?php endif;
|
||||
endforeach;
|
||||
?>
|
||||
?>
|
||||
</select></td>
|
||||
</tr>
|
||||
|
||||
@ -58,8 +58,8 @@ $cfgClass = $ocVersion >= 7 ? 'section' : 'personalblock';
|
||||
<?php else: ?>
|
||||
<option value="<?php echo $driver; ?>"><?php echo $name; ?></option>
|
||||
<?php endif;
|
||||
endforeach;
|
||||
?>
|
||||
endforeach;
|
||||
?>
|
||||
</select></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'])
|
||||
echo ' checked';
|
||||
?> 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>
|
||||
</fieldset>
|
||||
<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
|
||||
if($_['domain_settings'] == "" || $_['domain_settings'] == "none")
|
||||
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
|
||||
if($_['domain_settings'] == "server")
|
||||
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
|
||||
if($_['domain_settings'] == "default")
|
||||
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
|
||||
if($_['domain_settings'] == "mapping")
|
||||
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">
|
||||
<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>
|
||||
@ -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
|
||||
if($_['strip_domain'])
|
||||
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>
|
||||
</fieldset>
|
||||
|
69
user_sql.php
69
user_sql.php
@ -4,7 +4,7 @@
|
||||
* ownCloud - user_sql
|
||||
*
|
||||
* @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 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_invert;
|
||||
protected $sql_column_displayname;
|
||||
protected $sql_column_email;
|
||||
protected $mail_sync_mode;
|
||||
protected $sql_type;
|
||||
protected $db_conn;
|
||||
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_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_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_invert = OCP\Config::getAppValue('user_sql', 'sql_column_active_invert', 0);
|
||||
$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_array = explode(",", OCP\Config::getAppValue('user_sql', 'domain_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;
|
||||
try
|
||||
{
|
||||
@ -86,6 +90,68 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface
|
||||
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)
|
||||
{
|
||||
$uid = trim($uid);
|
||||
@ -422,6 +488,7 @@ class OC_USER_SQL extends OC_User_Backend implements OC_User_Interface
|
||||
{
|
||||
return false;
|
||||
}
|
||||
$this -> doEmailSync($uid);
|
||||
$uid = $this -> doUserDomainMapping($uid);
|
||||
|
||||
if(!$this -> userExists($uid))
|
||||
|
Loading…
Reference in New Issue
Block a user