Participant limit is now properly enforced
authorAlexander Ebert <ebert@woltlab.com>
Tue, 9 Apr 2013 14:03:05 +0000 (16:03 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 9 Apr 2013 14:03:05 +0000 (16:03 +0200)
files/lib/data/conversation/Conversation.class.php
files/lib/data/conversation/ConversationAction.class.php
files/lib/data/conversation/ConversationEditor.class.php

index d51dbb878c3e9d35569d87b39e0f17a6baa29a74..c4c2753bbf8ae799d6861ba30fb323216aa9b306 100644 (file)
@@ -140,11 +140,18 @@ class Conversation extends DatabaseObject implements IBreadcrumbProvider, IRoute
         * @return      boolean
         */
        public function canAddParticipants() {
-               if (WCF::getUser()->userID == $this->userID || $this->participantCanInvite) {
-                       return true;
+               // check permissions
+               if (WCF::getUser()->userID != $this->userID && !$this->participantCanInvite) {
+                       return false;
                }
                
-               return false;
+               // check for maximum number of participants
+               // note: 'participants' does not track invisible participants, this will be checked on the fly!
+               if ($this->participants >= WCF::getSession()->getPermission('user.conversation.maxParticipants')) {
+                       return false;
+               }
+               
+               return true;
        }
        
        /**
index 0a3be262c4fef1cde87d1629b4ad2ad24cd44d87..0c3e8366b8d015b1cc5462ced2c80a053ad386ab 100644 (file)
@@ -666,6 +666,15 @@ class ConversationAction extends AbstractDatabaseObjectAction implements IClipbo
                        );
                }
                
+               // validate limit
+               $newCount = $this->conversation->participants + count($participantIDs);
+               if ($newCount > WCF::getSession()->getPermission('user.conversation.maxParticipants')) {
+                       return array(
+                               'actionName' => 'addParticipants',
+                               'errorMessage' => WCF::getLanguage()->getDynamicVariable('wcf.conversation.participants.error.tooManyParticipants', array('remaining' => WCF::getSession()->getPermission('user.conversation.maxParticipants') - $newCount))
+                       );
+               }
+               
                $count = 0;
                $successMessage = '';
                if (!empty($participantIDs)) {
index d66a963d5fb6acdc4b62d299d1731cd99e88ca65..de184038e96bb7a1de53388ae3a680a7e4572386 100644 (file)
@@ -82,6 +82,29 @@ class ConversationEditor extends DatabaseObjectEditor {
                        }
                        WCF::getDB()->commitTransaction();
                }
+               
+               $this->updateParticipantCount();
+       }
+       
+       /**
+        * Updates participant count.
+        */
+       public function updateParticipantCount() {
+               $sql = "UPDATE  wcf".WCF_N."_conversation
+                       SET     participants = (
+                                       SELECT  COUNT(*) AS count
+                                       FROM    wcf".WCF_N."_conversation_to_user conversation_to_user
+                                       WHERE   conversation_to_user.conversationID = conversationID
+                                               AND hideConversation <> ?
+                                               AND participantID <> ?
+                               )
+                       WHERE   conversationID = ?";
+               $statement = WCF::getDB()->prepareStatement($sql);
+               $statement->execute(array(
+                       Conversation::STATE_LEFT,
+                       $this->userID,
+                       $this->conversationID
+               ));
        }
        
        /**
@@ -124,6 +147,13 @@ class ConversationEditor extends DatabaseObjectEditor {
                        $this->conversationID,
                        $userID
                ));
+               
+               // decrease participant count unless it is the author
+               if ($userID != $this->userID) {
+                       $this->updateCounters(array(
+                               'participants' => -1
+                       ));
+               }
        }
        
        /**