Parcourir la source

Add ability for non-admin users to use invites

TehMuffinMoo il y a 4 ans
Parent
commit
9494d10c7b

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

@@ -239,7 +239,7 @@ $app->get('/plugins/invites', function ($request, $response, $args) {
 	 */
 	$Invites = new Invites();
 	if ($Invites->checkRoute($request)) {
-		if ($Invites->qualifyRequest(1, true)) {
+		if ($Invites->qualifyRequest($Invites->config['INVITES-Auth-include'], true)) {
 			$GLOBALS['api']['response']['data'] = $Invites->_invitesPluginGetCodes();
 		}
 	}
@@ -265,7 +265,7 @@ $app->post('/plugins/invites', function ($request, $response, $args) {
 	 */
 	$Invites = new Invites();
 	if ($Invites->checkRoute($request)) {
-		if ($Invites->qualifyRequest(1, true)) {
+		if ($Invites->qualifyRequest($Invites->config['INVITES-Auth-include'], true)) {
 			$Invites->_invitesPluginCreateCode($Invites->apiData($request));
 		}
 	}
@@ -371,7 +371,7 @@ $app->delete('/plugins/invites/{code}', function ($request, $response, $args) {
 	 */
 	$Invites = new Invites();
 	if ($Invites->checkRoute($request)) {
-		if ($Invites->qualifyRequest(1, true)) {
+		if ($Invites->qualifyRequest($Invites->config['INVITES-Auth-include'], true)) {
 			$Invites->_invitesPluginDeleteCode($args['code']);
 		}
 	}

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

@@ -1,10 +1,12 @@
 <?php
 return array(
 	'INVITES-enabled' => false,
+	'INVITES-Auth-include' => '1',
 	'INVITES-type-include' => 'plex',
 	'INVITES-plexLibraries' => '',
 	'INVITES-EmbyTemplate' => '',
 	'INVITES-plex-tv-labels' => '',
 	'INVITES-plex-music-labels' => '',
-	'INVITES-plex-movies-labels' => ''
+	'INVITES-plex-movies-labels' => '',
+	'INVITES-db-version' => '1.1'
 );

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

@@ -13,7 +13,7 @@ function inviteLaunch(){
 	</div>
 	`;
 	if(activeInfo.plugins["INVITES-enabled"] == true){
-		if (activeInfo.user.loggedin === true && activeInfo.user.groupID <= 1) {
+		if (activeInfo.user.loggedin === true && activeInfo.user.groupID <= activeInfo.plugins.includes["INVITES-Auth-include"]) {
 			menuList = `<li><a class="inline-popups inviteModal" href="#invite-area" data-effect="mfp-zoom-out"><i class="fa fa-ticket fa-fw"></i> <span lang="en">Manage Invites</span></a></li>`;
 			htmlDOM += `
 			<div id="new-invite-area" class="white-popup mfp-with-anim mfp-hide">
@@ -301,6 +301,7 @@ function buildInvites(array){
 			<td>`+v.dateused+`</td>
 			<td>`+v.usedby+`</td>
 			<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>
 		</tr>
@@ -310,7 +311,7 @@ function buildInvites(array){
 }
 $(document).on('click', '.inviteModal', function() {
 	var htmlDOM = '';
-	if (activeInfo.user.loggedin === true && activeInfo.user.groupID <= 1) {
+	if (activeInfo.user.loggedin === true && activeInfo.user.groupID <= activeInfo.plugins.includes["INVITES-Auth-include"]) {
 		ajaxloader(".content-wrap","in");
 		organizrAPI2('GET','api/v2/plugins/invites').success(function(data) {
 			var response = data.response;
@@ -334,6 +335,7 @@ $(document).on('click', '.inviteModal', function() {
 									<th lang="en">DATE USED</th>
 									<th lang="en">USED BY</th>
 									<th lang="en">IP ADDRESS</th>
+									<th lang="en">INVITED BY</th>
 									<th lang="en">VALID</th>
 									<th lang="en">DELETE</th>
 								</tr>

+ 79 - 7
api/plugins/invites/plugin.php

@@ -20,17 +20,32 @@ class Invites extends Organizr
 {
 	public function _invitesPluginGetCodes()
 	{
-		$response = [
-			array(
-				'function' => 'fetchAll',
-				'query' => 'SELECT * FROM invites'
-			)
-		];
+		if ($this->qualifyRequest(1, false)) {
+			$response = [
+				array(
+					'function' => 'fetchAll',
+					'query' => 'SELECT * FROM invites'
+				)
+			];
+		} else {
+			$query = 'SELECT * FROM invites WHERE invitedby="'.$this->user['username'].'";';
+			$response = [
+				array(
+					'function' => 'fetchAll',
+					'query' => $query
+				)
+			];
+		}
+
 		return $this->processQueries($response);
 	}
 	
 	public function _invitesPluginCreateCode($array)
 	{
+		if (!$this->_invitesPluginUpgradeDB()) {
+			$this->setAPIResponse('error', 'Invites Plugin - Error Upgrading Database', 409);
+			return $false;
+		}
 		$code = ($array['code']) ?? null;
 		$username = ($array['username']) ?? null;
 		$email = ($array['email']) ?? null;
@@ -52,6 +67,7 @@ class Invites extends Organizr
 			'username' => $username,
 			'valid' => 'Yes',
 			'type' => $this->config['INVITES-type-include'],
+			'invitedby' => $this->user['username'],
 		];
 		$response = [
 			array(
@@ -258,7 +274,14 @@ class Invites extends Organizr
 							'value' => 'emby'
 						)
 					)
-				)
+				),
+				array(
+					'type' => 'select',
+					'name' => 'INVITES-Auth-include',
+					'label' => 'Minimum Authentication',
+					'value' => $this->config['INVITES-Auth-include'],
+					'options' => $this->groupSelect()
+				),
 			),
 			'Plex Settings' => array(
 				array(
@@ -483,5 +506,54 @@ class Invites extends Organizr
 		}
 		return (!empty($plexUser) ? $plexUser : null);
 	}
+
+	public function _invitesPluginUpgradeDB()
+	{
+		$DBVersion = "1.1";
+		if ($this->config['INVITES-db-version'] < $DBVersion) {
+			$response = [
+				array(
+					'function' => 'fetchAll',
+					'query' => 'PRAGMA table_info("invites")'
+				)
+			];
+			$sqlquery = $this->processQueries($response);
+			$key = array_search("invitedby", array_column($sqlquery, 'name'));
+			if (!$key) {
+				$sqlalterquery = [
+					array(
+						'function' => 'fetchAll',
+						'query' => 'ALTER TABLE invites ADD invitedby text;'
+					)
+				];
+				$sqlalter = $this->processQueries($sqlalterquery);
+				$sqlquery = $this->processQueries($response);
+				$key = array_search("invitedby", array_column($sqlquery, 'name'));
+				if ($key) {
+					$dbVersion = array (
+						"INVITES-db-version" => $DBVersion,
+					);
+					$this->updateConfigItems($dbVersion);
+					$this->setAPIResponse('success', 'Database upgraded successfully.', 200);
+					$this->writeLog('info', 'Invites Plugin - Database upgraded successfully.', 'SYSTEM');
+					return true;
+				} else {
+					$this->writeLog('error', 'Invites Plugin - Error Upgrading Database', 'SYSTEM');
+					$this->setAPIResponse('error', 'Invites Plugin - Error Upgrading Database', 409);
+					return false;
+				}
+			} else {
+				$dbVersion = array (
+					"INVITES-db-version" => $DBVersion,
+				);
+				$this->updateConfigItems($dbVersion);
+				$this->setAPIResponse('success', 'Database upgraded successfully.', 200);
+				return true;
+			}
+		} else {
+			$this->setAPIResponse('success', 'Database up to date.', 200);
+			return true;
+		}
+	}
 	
 }