Browse Source

Add maximum number of invites setting

TehMuffinMoo 4 years ago
parent
commit
a8e0c58cc7
3 changed files with 45 additions and 12 deletions
  1. 3 1
      api/plugins/invites/config.php
  2. 5 2
      api/plugins/invites/main.js
  3. 37 9
      api/plugins/invites/plugin.php

+ 3 - 1
api/plugins/invites/config.php

@@ -8,5 +8,7 @@ return array(
 	'INVITES-EmbyTemplate' => '',
 	'INVITES-plex-tv-labels' => '',
 	'INVITES-plex-music-labels' => '',
-	'INVITES-plex-movies-labels' => ''
+	'INVITES-plex-movies-labels' => '',
+	'INVITES-allow-delete-include' => false,
+	'INVITES-maximum-invites' => '0'
 );

+ 5 - 2
api/plugins/invites/main.js

@@ -303,7 +303,7 @@ function buildInvites(array){
 			<td>`+v.ip+`</td>
 			<td>`+v.invitedby+`</td>
 			<td>`+v.valid+`</td>
-			<td><button type="button" class="btn btn-danger btn-outline btn-circle btn-lg m-r-5" onclick="deleteInvite('`+v.code+`','`+v.id+`');"><i class="ti-trash"></i></button></td>
+			<td class="deleteButton"><button type="button" class="btn btn-danger btn-outline btn-circle btn-lg m-r-5" onclick="deleteInvite('`+v.code+`','`+v.id+`');"><i class="ti-trash"></i></button></td>
 		</tr>
 		`;
 	});
@@ -337,7 +337,7 @@ $(document).on('click', '.inviteModal', function() {
 									<th lang="en">IP ADDRESS</th>
 									<th lang="en">INVITED BY</th>
 									<th lang="en">VALID</th>
-									<th lang="en">DELETE</th>
+									<th lang="en" class="deleteButton">DELETE</th>
 								</tr>
 							</thead>
 							<tbody id="manageInviteTable">
@@ -350,6 +350,9 @@ $(document).on('click', '.inviteModal', function() {
 			<div class="clearfix"></div>
 			`;
 			$('.invite-div').html(htmlDOM);
+			if (activeInfo.plugins.includes["INVITES-allow-delete-include"] === false && activeInfo.user.groupID > 1) {
+				$('.deleteButton').hide();
+			}
 		}).fail(function(xhr) {
 			console.error("Organizr Function: API Connection Failed");
 		});

+ 37 - 9
api/plugins/invites/plugin.php

@@ -97,6 +97,14 @@ class Invites extends Organizr
 		$code = ($array['code']) ?? null;
 		$username = ($array['username']) ?? null;
 		$email = ($array['email']) ?? null;
+		$invites = $this->_invitesPluginGetCodes();
+		$inviteCount = count($invites);
+		if (!$this->qualifyRequest(1, false)) {
+			if ($this->config['INVITES-maximum-invites'] != 0 && $inviteCount >= $this->config['INVITES-maximum-invites']) {
+			$this->setAPIResponse('error', 'Maximum number of invites reached', 409);
+			return false;
+			}
+		}
 		if (!$code) {
 			$this->setAPIResponse('error', 'Code not supplied', 409);
 			return false;
@@ -188,16 +196,21 @@ class Invites extends Organizr
 				)
 			];
 		} else {
-			$response = [
-				array(
-					'function' => 'fetch',
-					'query' => array(
-						'SELECT * FROM invites WHERE invitedby = ? AND code = ? COLLATE NOCASE',
-						$this->user['username'],
-						$code
+			if ($this->config['INVITES-allow-delete']) {
+				$response = [
+					array(
+						'function' => 'fetch',
+						'query' => array(
+							'SELECT * FROM invites WHERE invitedby = ? AND code = ? COLLATE NOCASE',
+							$this->user['username'],
+							$code
+						)
 					)
-				)
-			];
+				];
+			} else {
+				$this->setAPIResponse('error', 'You are not permitted to delete invites.', 409);
+				return false;
+			}
 		}
 		$info = $this->processQueries($response);
 		if (!$info) {
@@ -343,6 +356,21 @@ class Invites extends Organizr
 					'value' => $this->config['INVITES-Auth-include'],
 					'options' => $this->groupSelect()
 				),
+				array(
+					'type' => 'switch',
+					'name' => 'INVITES-allow-delete-include',
+					'label' => 'Allow users to delete invites',
+					'help' => 'This must be disabled to enforce invitation limits.',
+					'value' => $this->config['INVITES-allow-delete-include']
+				),
+				array(
+					'type' => 'number',
+					'name' => 'INVITES-maximum-invites',
+					'label' => 'Maximum number of invites permitted for users.',
+					'help' => 'Set to 0 to disable the limit.',
+					'value' => $this->config['INVITES-maximum-invites'],
+					'placeholder' => '0'
+				),
 			),
 			'Plex Settings' => array(
 				array(