Completed
Branch master (8ca3ab)
by Sander
03:07
created

getRequestsByItemGuidGroupedByUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Passman\Db;
25
26
27
use Icewind\SMB\Share;
28
use OCA\Passman\Utility\Utils;
29
use OCP\AppFramework\Db\DoesNotExistException;
30
use OCP\AppFramework\Db\Mapper;
31
use OCP\IDBConnection;
32
33
class ShareRequestMapper extends Mapper {
34
    const TABLE_NAME = 'passman_share_request';
35
36
    public function __construct(IDBConnection $db) {
37
        parent::__construct($db, self::TABLE_NAME);
38
    }
39
40 1
    public function createRequest(ShareRequest $request){
41 1
        return $this->insert($request);
42
    }
43
44
    /**
45
     * Obtains a request by the given item and vault GUID pair
46
     * @param $item_guid
47
     * @param $target_vault_guid
48
     * @return ShareRequest
49
     */
50 1
    public function getRequestByItemAndVaultGuid($item_guid, $target_vault_guid){
51 1
        $q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_guid = ? AND target_vault_guid = ?";
52 1
        return $this->findEntity($q, [$item_guid, $target_vault_guid]);
53
    }
54
55
    /**
56
     * Get shared items for the given item_guid
57
     * @param $item_guid
58
     * @return ShareRequest[]
59
     */
60 1
    public function getRequestsByItemGuidGroupedByUser($item_guid){
61 1
    	if (strtolower($this->db->getDatabasePlatform()->getName()) === 'mysql'){
62 1
    		$this->db->executeQuery("SET sql_mode = '';");
63
		}
64 1
        $q = "SELECT *, target_user_id FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_guid = ? GROUP BY target_user_id;";
65 1
        return $this->findEntities($q, [$item_guid]);
66
    }
67
68
    /**
69
     * Deletes all pending requests for the given user to the given item
70
     * @param $item_id          The item ID
71
     * @param $target_user_id   The target user
72
     * @return \PDOStatement    The result of running the db query
73
     */
74 1
    public function cleanItemRequestsForUser($item_id, $target_user_id){
75 1
		$q = "DELETE FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_id = ? AND target_user_id = ?";
76 1
		$this->execute($q, [$item_id, $target_user_id]);
77 1
        return $this->execute($q, [$item_id, $target_user_id]);
78
    }
79
80
    /**
81
     * Obtains all pending share requests for the given user ID
82
     * @param $user_id
83
     * @return ShareRequest[]
84
     */
85 1
    public function getUserPendingRequests($user_id){
86 1
        $q = "SELECT * FROM *PREFIX*". self::TABLE_NAME ." WHERE target_user_id = ?";
87 1
        return $this->findEntities($q, [$user_id]);
88
    }
89
90
    /**
91
     * Deletes the given share request
92
     * @param ShareRequest $shareRequest    Request to delete
93
     * @return ShareRequest                 The deleted request
94
     */
95 1
    public function deleteShareRequest(ShareRequest $shareRequest){
96 1
    	return $this->delete($shareRequest);
97
	}
98
99
    /**
100
     * Gets a share request by it's unique incremental id
101
     * @param $id
102
     * @return ShareRequest
103
	 * @throws DoesNotExistException
104
     */
105 1
	public function getShareRequestById($id){
106 1
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE id = ?";
107 1
		return $this->findEntity($q, [$id]);
108
	}
109
110
    /**
111
     * Gets all share requests by a given item GUID
112
     * @param $item_guid
113
     * @return ShareRequest[]
114
     */
115 1
	public function getShareRequestsByItemGuid($item_guid){
116 1
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE 	item_guid = ?";
117 1
		return $this->findEntities($q, [$item_guid]);
118
	}
119
120
    /**
121
     * Updates the given share request,
122
     * @param ShareRequest $shareRequest
123
     * @return ShareRequest
124
     */
125 1
	public function updateShareRequest(ShareRequest $shareRequest){
126 1
		return $this->update($shareRequest);
127
	}
128
129
    /**
130
     * Finds pending requests sent to the given user to the given item.
131
     * @param $item_guid
132
     * @param $user_id
133
     * @return ShareRequest[]
134
     */
135 1
	public function getPendingShareRequests($item_guid, $user_id){
136 1
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE 	item_guid = ? and target_user_id= ?";
137 1
		return $this->findEntities($q, [$item_guid, $user_id]);
138
	}
139
140
    /**
141
     * Updates all pending requests with the given permissions
142
     * @param $item_guid        The item for which to update the requests
143
     * @param $user_id          The user for which to update the requests
144
     * @param $permissions      The new permissions to apply
145
     * @return \PDOStatement    The result of the operation
146
     */
147 1
	public function updatePendingRequestPermissions($item_guid, $user_id, $permissions){
148 1
	    $q = "UPDATE *PREFIX*" . self::TABLE_NAME . " SET permissions = ? WHERE item_guid = ? AND target_user_id = ?";
149 1
        return $this->execute($q, [$permissions, $item_guid, $user_id]);
150
    }
151
152
}