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

ShareRequestMapper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 120
ccs 33
cts 33
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A createRequest() 0 3 1
A getRequestByItemAndVaultGuid() 0 4 1
A cleanItemRequestsForUser() 0 5 1
A getUserPendingRequests() 0 4 1
A deleteShareRequest() 0 3 1
A getShareRequestById() 0 4 1
A getShareRequestsByItemGuid() 0 4 1
A updateShareRequest() 0 3 1
A getPendingShareRequests() 0 4 1
A updatePendingRequestPermissions() 0 4 1
A __construct() 0 3 1
A getRequestsByItemGuidGroupedByUser() 0 7 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
}