enable user to inform recipients about a shared file by mail

This commit is contained in:
Bjoern Schiessle 2013-08-30 13:53:49 +02:00
parent 0a23ac18bc
commit 77adaee645
12 changed files with 292 additions and 31 deletions

View File

@ -151,5 +151,6 @@ if ($needUpgrade) {
$tmpl->assign('isPublic', false);
$tmpl->assign('publicUploadEnabled', $publicUploadEnabled);
$tmpl->assign("encryptedFiles", \OCP\Util::encryptedFiles());
$tmpl->assign("mailNotificationEnabled", \OC_Appconfig::getValue('core', 'shareapi_allow_mail_notification', 'yes'));
$tmpl->printPage();
}

View File

@ -120,3 +120,4 @@
<input type="hidden" name="allowZipDownload" id="allowZipDownload" value="<?php p($_['allowZipDownload']); ?>" />
<input type="hidden" name="usedSpacePercent" id="usedSpacePercent" value="<?php p($_['usedSpacePercent']); ?>" />
<input type="hidden" name="encryptedFiles" id="encryptedFiles" value="<?php $_['encryptedFiles'] ? p('1') : p('0'); ?>" />
<input type="hidden" name="mailNotificationEnabled" id="mailNotificationEnabled" value="<?php p($_['mailNotificationEnabled']) ?>" />

View File

@ -23,6 +23,8 @@ OC_JSON::checkLoggedIn();
OCP\JSON::callCheck();
OC_App::loadApps();
$defaults = new \OCP\Defaults();
if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['itemSource'])) {
switch ($_POST['action']) {
case 'share':
@ -81,6 +83,102 @@ if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['itemSo
($return) ? OC_JSON::success() : OC_JSON::error();
}
break;
case 'informRecipients':
$l = OC_L10N::get('core');
$shareType = (int) $_POST['shareType'];
$itemType = $_POST['itemType'];
$itemSource = $_POST['itemSource'];
$recipient = $_POST['recipient'];
$from = \OCP\Util::getDefaultEmailAddress('sharing-noreply');
$subject = $defaults->getShareNotificationSubject($itemType);
$noMail = array();
$recipientList = array();
if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
$users = \OC_Group::usersInGroup($recipient);
foreach ($users as $user) {
$email = OC_Preferences::getValue($user, 'settings', 'email', '');
if ($email !== '' || $recipient === \OCP\User::getUser()) {
$recipientList[] = array(
'email' => $email,
'displayName' => \OCP\User::getDisplayName($user),
'uid' => $user,
);
} else {
$noMail[] = \OCP\User::getDisplayName($user);
}
}
} else { // shared to a single user
$email = OC_Preferences::getValue($recipient, 'settings', 'email', '');
if ($email !== '') {
$recipientList[] = array(
'email' => $email,
'displayName' => \OCP\User::getDisplayName($recipient),
'uid' => $recipient,
);
} else {
$noMail[] = \OCP\User::getDisplayName($recipient);
}
}
// send mail to all recipients with an email address
foreach ($recipientList as $recipient) {
//get correct target folder name
$users = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient['uid']);
$targetName = $users[0]['file_target'];
//$share = $shareManager->getShares($itemType, array('shareWith' => $recipient['uid'], 'isShareWithUser' => true, 'itemSource' => $itemSource));
//$targetName = $share[0]->getItemTarget();
if ($itemType === 'folder') {
$foldername = "/Shared/" . $targetName;
$filename = $targetName;
} else {
// if it is a file we can just link to the Shared folder,
// that's the place where the user will find the file
$foldername = "/Shared";
$filename = $targetName;
}
$url = \OCP\Util::linkToAbsolute('files', 'index.php', array("dir" => $foldername));
$text = $defaults->getShareNotificationText(\OCP\User::getDisplayName(), $filename, $itemType, $url);
try {
OCP\Util::sendMail(
$recipient['email'],
$recipient['displayName'],
$subject,
$text,
$from,
\OCP\User::getDisplayName()
);
} catch (Exception $exception) {
$noMail[] = \OCP\User::getDisplayName($recipient['displayName']);
}
}
\OCP\Share::setSendMailStatus($itemType, $itemSource, $shareType, true);
if (empty($noMail)) {
OCP\JSON::success();
} else {
OCP\JSON::error(array('data' => array('message' => $l->t("Couldn't send mail to following users: %s ", implode(', ', $noMail)))));
}
break;
case 'informRecipientsDisabled':
$itemSource = $_POST['itemSource'];
$itemType = $_POST['itemType'];
$recipient = $_POST['recipient'];
//$share = $shareManager->getShares($itemType, array('shareWith' => $recipient, 'isShareWithUser' => true, 'itemSource' => $itemSource));
//$share[0]->setMailSend(false);
//$shareManager->update($share[0]);
//write status to db
OCP\JSON::success();
break;
case 'email':
// read post variables
$user = OCP\USER::getUser();

View File

@ -11,7 +11,7 @@
margin-right:7em;
position:absolute;
right:0;
width:19em;
width:25em;
z-index:500;
padding:1em;
}

View File

@ -217,9 +217,9 @@ OC.Share={
OC.Share.showLink(share.token, share.share_with, itemSource);
} else {
if (share.collection) {
OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.collection);
OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, share.mail_send, share.collection);
} else {
OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.permissions, possiblePermissions, false);
OC.Share.addShareWith(share.share_type, share.share_with, share.share_with_displayname, share.mail_send, share.permissions, possiblePermissions, share.mail_send, false);
}
}
if (share.expiration != null) {
@ -299,7 +299,7 @@ OC.Share={
}
});
},
addShareWith:function(shareType, shareWith, shareWithDisplayName, permissions, possiblePermissions, collection) {
addShareWith:function(shareType, shareWith, shareWithDisplayName, permissions, possiblePermissions, mailSend, collection) {
if (!OC.Share.itemShares[shareType]) {
OC.Share.itemShares[shareType] = [];
}
@ -341,6 +341,14 @@ OC.Share={
}else{
html += escapeHTML(shareWithDisplayName);
}
mailNotificationEnabled = $('input:hidden[name=mailNotificationEnabled]').val();
if (mailNotificationEnabled === 'yes') {
checked = '';
if (mailSend === true) {
checked = 'checked';
}
html += '<input type="checkbox" name="mailNotification" class="mailNotification" ' + checked + ' />'+t('core', 'notify user by email')+'</label>';
}
if (possiblePermissions & OC.PERMISSION_CREATE || possiblePermissions & OC.PERMISSION_UPDATE || possiblePermissions & OC.PERMISSION_DELETE) {
if (editChecked == '') {
html += '<label style="display:none;">';
@ -686,5 +694,28 @@ $(document).ready(function() {
}
});
$(document).on('click', '#dropdown input[name=mailNotification]', function(event) {
event.preventDefault();
event.stopPropagation();
var li = $(this).parent();
var itemType = $('#dropdown').data('item-type');
var itemSource = $('#dropdown').data('item-source');
if (this.checked) {
action = 'informRecipients';
} else {
action = 'informRecipientsDisabled';
}
shareType = $(li).data('share-type');
shareWith = $(li).data('share-with');
$.post(OC.filePath('core', 'ajax', 'share.php'), {action: action, recipient: shareWith, shareType: shareType, itemSource: itemSource, itemType: itemType}, function(result) {
if (result.status !== 'success') {
OC.dialogs.alert(t('core', result.data.message), t('core', 'Warning'));
}
});
});
});

View File

@ -846,6 +846,14 @@
<length>32</length>
</field>
<field>
<name>mail_send</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<length>1</length>
</field>
<index>
<name>token_index</name>
<field>

View File

@ -13,6 +13,7 @@ if (file_exists(OC::$SERVERROOT . '/themes/' . OC_Util::getTheme() . '/defaults.
class OC_Defaults {
private $theme;
private $l;
private $defaultEntity;
private $defaultName;
@ -24,7 +25,7 @@ class OC_Defaults {
private $defaultLogoClaim;
function __construct() {
$l = OC_L10N::get('core');
$this->l = OC_L10N::get('core');
$this->defaultEntity = "ownCloud"; /* e.g. company name, used for footers and copyright notices */
$this->defaultName = "ownCloud"; /* short name, used when referring to the software */
@ -32,7 +33,7 @@ class OC_Defaults {
$this->defaultBaseUrl = "http://owncloud.org";
$this->defaultSyncClientUrl = " http://owncloud.org/sync-clients/";
$this->defaultDocBaseUrl = "http://doc.owncloud.org";
$this->defaultSlogan = $l->t("web services under your control");
$this->defaultSlogan = $this->l->t("web services under your control");
$this->defaultLogoClaim = "";
if (class_exists("OC_Theme")) {
@ -47,6 +48,32 @@ class OC_Defaults {
return false;
}
/**
*
* @param string $itemType typically "file" or "folder"
*/
public function getShareNotificationSubject($itemType) {
if ($this->themeExist('getShareNotificationSubject')) {
return $this->theme->getShareNotificationSubject($itemType);
} else {
return $this->l->t("A %s was shared with you", array($itemType));
}
}
/**
* @param string $sender owner of the file/folder
* @param string $itemName name of the file/folder
* @param string $itemType typically "file" or "folder"
* @param string $link link directly to the file/folder in your ownCloud
*/
public function getShareNotificationText($sender, $itemName, $itemType, $link) {
if ($this->themeExist('getShareNotificationText')) {
return $this->theme->getShareNotificationText($sender, $itemName, $itemType, $link);
} else {
return $this->l->t("%s shared a %s called %s with you. You can find the %s here: %s", array($sender, $itemType, $itemName, $itemType, $link));
}
}
public function getBaseUrl() {
if ($this->themeExist('getBaseUrl')) {
return $this->theme->getBaseUrl();

View File

@ -34,6 +34,25 @@ class Defaults {
$this->defaults = new \OC_Defaults();
}
/**
* @brief subject for notification mails if a new file was shared
* @param string $itemType typically "file" or "folder"
*/
public function getShareNotificationSubject($itemType = "file") {
return $this->defaults->getShareNotificationSubject($itemType);
}
/**
* @brief mail body for notification mails if a new file was shared
* @param string $sender owner of the file/folder
* @param string $itemName name of the file/folder
* @param string $itemType typically "file" or "folder"
* @param string $link link directly to the file/folder in your ownCloud
*/
public function getShareNotificationText($sender, $itemName, $itemType, $link) {
return $this->defaults->getShareNotificationText($sender, $itemName, $itemType, $link);
}
/**
* @breif get base URL for the organisation behind your ownCloud instance
* @return string

View File

@ -252,6 +252,56 @@ class Share {
$parameters, 1, $includeCollections);
}
/**
* @brief Get the item of item type shared with a given user by source
* @param string Item type
* @param string Item source
* @param string User user to whom the item was shared
* @return Return list of items with file_target, permissions and expiration
*/
public static function getItemSharedWithUser($itemType, $itemSource, $user) {
$shares = array();
// first check if there is a db entry for the specific user
$query = \OC_DB::prepare(
'SELECT `file_target`, `permissions`, `expiration`
FROM
`*PREFIX*share`
WHERE
`item_source` = ? AND `item_type` = ? AND `share_with` = ?'
);
$result = $query->execute(array($itemSource, $itemType, $user));
while ($row = $result->fetchRow()) {
$shares[] = $row;
}
//if didn't found a result than let's look for a group share.
if(empty($shares)) {
$groups = \OC_Group::getUserGroups($user);
$query = \OC_DB::prepare(
'SELECT `file_target`, `permissions`, `expiration`
FROM
`*PREFIX*share`
WHERE
`item_source` = ? AND `item_type` = ? AND `share_with` in (?)'
);
$result = $query->execute(array($itemSource, $itemType, implode(',', $groups)));
while ($row = $result->fetchRow()) {
$shares[] = $row;
}
}
return $shares;
}
/**
* @brief Get the item of item type shared with the current user by source
* @param string Item type
@ -633,6 +683,23 @@ class Share {
}
return false;
}
/**
* @brief sent status if users got informed by mail about share
* @param string $itemType
* @param string $itemSource
* @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
* @param bool $status
*/
public static function setSendMailStatus($itemType, $itemSource, $shareType, $status) {
$status = $status ? 1 : 0;
$query = \OC_DB::prepare(
'UPDATE `*PREFIX*share`
SET `mail_send` = ?
WHERE `item_type` = ? AND `item_source` = ? AND `share_type` = ?');
$query->execute(array($status, $itemType, $itemSource, $shareType));
}
/**
* @brief Set the permissions of an item for a specific user or group

View File

@ -98,7 +98,7 @@ class OC_Util {
public static function getVersion() {
// hint: We only can count up. Reset minor/patchlevel when
// updating major/minor version number.
return array(5, 80, 05);
return array(5, 80, 06);
}
/**

View File

@ -43,6 +43,7 @@ $tmpl->assign('enforceHTTPSEnabled', OC_Config::getValue( "forcessl", false));
$tmpl->assign('allowLinks', OC_Appconfig::getValue('core', 'shareapi_allow_links', 'yes'));
$tmpl->assign('allowPublicUpload', OC_Appconfig::getValue('core', 'shareapi_allow_public_upload', 'yes'));
$tmpl->assign('allowResharing', OC_Appconfig::getValue('core', 'shareapi_allow_resharing', 'yes'));
$tmpl->assign('allowMailNotification', OC_Appconfig::getValue('core', 'shareapi_allow_mail_notification', 'yes'));
$tmpl->assign('sharePolicy', OC_Appconfig::getValue('core', 'shareapi_share_policy', 'global'));
$tmpl->assign('forms', array());
foreach($forms as $form) {

View File

@ -163,6 +163,14 @@ if (!$_['internetconnectionworking']) {
<label for="sharePolicyGroupsOnly"><?php p($l->t('Allow users to only share with users in their groups'));?></label><br/>
</td>
</tr>
<tr>
<td <?php if ($_['shareAPIEnabled'] === 'no') print_unescaped('style="display:none"');?>>
<input type="checkbox" name="shareapi_allow_mail_notification" id="allowMailNotification"
value="1" <?php if ($_['allowMailNotification'] === 'yes') print_unescaped('checked="checked"'); ?> />
<label for="allowMailNotification"><?php p($l->t('Allow mail notification'));?></label><br/>
<em><?php p($l->t('Allow user to send mail notification for shared files')); ?></em>
</td>
</tr>
</table>
</fieldset>