Add option to notify users about calendar permission changes.
authorJan Schneider <jan@horde.org>
Thu, 27 May 2010 12:25:33 +0000 (14:25 +0200)
committerJan Schneider <jan@horde.org>
Thu, 27 May 2010 12:35:32 +0000 (14:35 +0200)
I'm not sure about which text, information and links to include with the
message by default, any ideas are welcome.

kronolith/config/conf.xml
kronolith/config/hooks.php.dist
kronolith/docs/CHANGES
kronolith/lib/Kronolith.php

index 3cf477a..46e7661 100644 (file)
  </configsection>
 
  <configsection name="autoshare">
-   <configheader>Calendar Auto-Sharing</configheader>
-     <configenum name="shareperms" desc="When a new user is created, his
-     default calendar can be automatically shared with his group(s). Which
-     permissions should the group have?">
-       <values>
-         <value desc="None (no sharing)">none</value>
-         <value desc="Read-only">read</value>
-         <value desc="Read and write">edit</value>
-         <value desc="Read, write and delete">full</value>
-       </values>
-     </configenum>
+  <configheader>Share Settings</configheader>
+  <configenum name="shareperms" desc="When a new user is created, his
+   default calendar can be automatically shared with his group(s). Which
+   permissions should the group have?">
+   <values>
+    <value desc="None (no sharing)">none</value>
+    <value desc="Read-only">read</value>
+    <value desc="Read and write">edit</value>
+    <value desc="Read, write and delete">full</value>
+   </values>
+  </configenum>
+ </configsection>
+
+ <configsection name="share">
+  <configboolean name="notify" desc="Notify users and groups per e-mail if
+  they have been granted access to a shared calendar (or tasklist), or are the
+  new owner of the share?">false</configboolean>
  </configsection>
 
  <configsection name="holidays">
index a121df0..a6e89ee 100644 (file)
@@ -14,7 +14,7 @@
 class Kronolith_Hooks
 {
     /**
-     * AJAX HOOK: Add user-defined AJAX action handlers.
+     * Adds user-defined AJAX action handlers.
      *
      * @param string $action         The AJAX action.
      * @param Horde_Variables $vars  The URL parameters.
@@ -32,4 +32,39 @@ class Kronolith_Hooks
 //        throw new Horde_Ajax_Exception('Unknown action');
 //     }
 
+    /**
+     * Generates the e-mail notification message when the share owner changes.
+     *
+     * @param string $user        The new owner.
+     * @param Horde_Share $share  A calendar or tasklist.
+     */
+//     public function shareOwnerNotification($user, Horde_Share $share)
+//     {
+//         return 'Some fancy text including the ' . $user . ' and the ' . $share->get('name');
+//     }
+
+    /**
+     * Generates the e-mail notification message when the share permissions
+     * change.
+     *
+     * @param string $user        A user who has been added to the permissions.
+     * @param Horde_Share $share  A calendar or tasklist.
+     */
+//     public function shareUserNotification($user, Horde_Share $share)
+//     {
+//         return 'Some fancy text including the ' . $user . ' and the ' . $share->get('name');
+//     }
+
+    /**
+     * Generates the e-mail notification message when the share permissions
+     * change.
+     *
+     * @param mixed $group        The id of a group that has been added to the
+     *                            permissions.
+     * @param Horde_Share $share  A calendar or tasklist.
+     */
+//     public function shareGroupNotification($group, Horde_Share $share)
+//     {
+//         return 'Some fancy text including the ' . $group . ' and the ' . $share->get('name');
+//     }
 }
index 19d21fe..c83431e 100644 (file)
@@ -2,6 +2,7 @@
 v3.0-git
 --------
 
+[jan] Add option to notify users about calendar permission changes.
 [mjr] listTimeObject calendars are now viewable independently in the calendar
       portal blocks (Request #8530).
 [mjr] Recurring event exceptions are now indicated as such in the UI.
index 4064091..99bca32 100644 (file)
@@ -1593,6 +1593,18 @@ class Kronolith
         $perm = $share->getPermission();
         $errors = array();
 
+        if ($GLOBALS['conf']['share']['notify']) {
+            $identity = $GLOBALS['injector']
+                ->getInstance('Horde_Prefs_Identity')
+                ->getIdentity();
+            $userName = $identity->getName();
+            $mail = new Horde_Mime_Mail(
+                array('from' => $identity->getDefaultFromAddress(true),
+                      'charset' => Horde_Nls::getCharset())
+                );
+            $mail->addHeader('User-Agent', 'Kronolith ' . $GLOBALS['registry']->getVersion());
+        }
+
         // Process owner and owner permissions.
         $old_owner = $share->get('owner');
         $new_owner_backend = Horde_Util::getFormData('owner_select', Horde_Util::getFormData('owner_input', $old_owner));
@@ -1605,6 +1617,23 @@ class Kronolith
             } else {
                 $share->set('owner', $new_owner);
                 $share->save();
+                if ($GLOBALS['conf']['share']['notify']) {
+                    $to = $GLOBALS['injector']
+                        ->getInstance('Horde_Prefs_Identity')
+                        ->getIdentity($new_owner)
+                        ->getDefaultFromAddress(true);
+                    try {
+                        $message = Horde::callHook('shareOwnerNotification', array($new_owner, $share));
+                    } catch (Horde_Exception_HookNotSet $e) {
+                        $message = sprintf(_("%s has assigned the ownership of \"%s\" to you"),
+                                           $userName,
+                                           $share->get('name'));
+                    }
+                    $mail->addHeader('Subject', _("Ownership assignment"));
+                    $mail->addHeader('To', $to);
+                    $mail->setBody($message, Horde_Nls::getCharset());
+                    $mail->send($GLOBALS['injector']->getInstance('Horde_Mail'));
+                }
             }
         }
 
@@ -1700,6 +1729,11 @@ class Kronolith
         $u_delete = Horde_Util::getFormData('u_delete');
         $u_delegate = Horde_Util::getFormData('u_delegate');
 
+        $current = $perm->getUserPermissions();
+        if ($GLOBALS['conf']['share']['notify']) {
+            $mail->addHeader('Subject', _("Access permissions"));
+        }
+
         $perm->removeUserPermission(null, null, false);
         foreach ($u_names as $key => $user_backend) {
             // Apply backend hooks
@@ -1714,20 +1748,45 @@ class Kronolith
                 continue;
             }
 
+            $has_perms = false;
             if (!empty($u_show[$key])) {
                 $perm->addUserPermission($user, Horde_Perms::SHOW, false);
+                $has_perms = true;
             }
             if (!empty($u_read[$key])) {
                 $perm->addUserPermission($user, Horde_Perms::READ, false);
+                $has_perms = true;
             }
             if (!empty($u_edit[$key])) {
                 $perm->addUserPermission($user, Horde_Perms::EDIT, false);
+                $has_perms = true;
             }
             if (!empty($u_delete[$key])) {
                 $perm->addUserPermission($user, Horde_Perms::DELETE, false);
+                $has_perms = true;
             }
             if (!empty($u_delegate[$key])) {
                 $perm->addUserPermission($user, Kronolith::PERMS_DELEGATE, false);
+                $has_perms = true;
+            }
+
+            // Notify users that have been added.
+            if ($GLOBALS['conf']['share']['notify'] &&
+                !isset($current[$user]) && $has_perms) {
+                $to = $GLOBALS['injector']
+                    ->getInstance('Horde_Prefs_Identity')
+                    ->getIdentity($user)
+                    ->getDefaultFromAddress(true);
+                try {
+                    $message = Horde::callHook('shareUserNotification', array($user, $share));
+                } catch (Horde_Exception_HookNotSet $e) {
+                    $message = sprintf(_("%s has given you access to \"%s\"."),
+                                       $userName,
+                                       $share->get('name'));
+                }
+                $mail->addHeader('To', $to);
+                $mail->setBody($message, Horde_Nls::getCharset());
+                $mail->send($GLOBALS['injector']->getInstance('Horde_Mail'));
             }
         }
 
@@ -1739,26 +1798,52 @@ class Kronolith
         $g_delete = Horde_Util::getFormData('g_delete');
         $g_delegate = Horde_Util::getFormData('g_delegate');
 
+        $current = $perm->getGroupPermissions();
         $perm->removeGroupPermission(null, null, false);
         foreach ($g_names as $key => $group) {
             if (empty($group)) {
                 continue;
             }
 
+            $has_perms = false;
             if (!empty($g_show[$key])) {
                 $perm->addGroupPermission($group, Horde_Perms::SHOW, false);
+                $has_perms = true;
             }
             if (!empty($g_read[$key])) {
                 $perm->addGroupPermission($group, Horde_Perms::READ, false);
+                $has_perms = true;
             }
             if (!empty($g_edit[$key])) {
                 $perm->addGroupPermission($group, Horde_Perms::EDIT, false);
+                $has_perms = true;
             }
             if (!empty($g_delete[$key])) {
                 $perm->addGroupPermission($group, Horde_Perms::DELETE, false);
+                $has_perms = true;
             }
             if (!empty($g_delegate[$key])) {
                 $perm->addGroupPermission($group, Kronolith::PERMS_DELEGATE, false);
+                $has_perms = true;
+            }
+
+            // Notify users that have been added.
+            if ($GLOBALS['conf']['share']['notify'] &&
+                !isset($current[$group]) && $has_perms) {
+                $groupOb = Group::singleton()->getGroupById($group);
+                if (!empty($groupOb->data['email'])) {
+                    try {
+                        $message = Horde::callHook('shareGroupNotification', array($group, $share));
+                    } catch (Horde_Exception_HookNotSet $e) {
+                        $message = sprintf(_("%s has given your group \"%s\" access to \"%s\"."),
+                                           $userName,
+                                           $groupOb->getName(),
+                                           $share->get('name'));
+                    }
+                    $mail->addHeader('To', $groupOb->getName() . ' <' . $groupOb->data['email'] . '>');
+                    $mail->setBody($message, Horde_Nls::getCharset());
+                    $mail->send($GLOBALS['injector']->getInstance('Horde_Mail'));
+                }
             }
         }
         try {