Completed
Push — phpunit ( 8a3032...ce49ca )
by Sander
07:26
created

updatePendinRequestPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: wolfi
5
 * Date: 1/10/16
6
 * Time: 23:15
7
 */
8
9
namespace OCA\Passman\Db;
10
11
12
use Icewind\SMB\Share;
13
use OCA\Passman\Utility\Utils;
14
use OCP\AppFramework\Db\DoesNotExistException;
15
use OCP\AppFramework\Db\Mapper;
16
use OCP\IDBConnection;
17
18
class ShareRequestMapper extends Mapper {
19
    const TABLE_NAME = 'passman_share_request';
20
21
    public function __construct(IDBConnection $db) {
22
        parent::__construct($db, self::TABLE_NAME);
23
    }
24
25
    public function createRequest(ShareRequest $request){
26
        return $this->insert($request);
27
    }
28
29
    /**
30
     * Obtains a request by the given item and vault GUID pair
31
     * @param $item_guid
32
     * @param $target_vault_guid
33
     * @return ShareRequest
34
     */
35
    public function getRequestByItemAndVaultGuid($item_guid, $target_vault_guid){
36
        $q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_guid = ? AND target_vault_guid = ?";
37
        return $this->findEntity($q, [$item_guid, $target_vault_guid]);
38
    }
39
40
    /**
41
     * Get shared items for the given item_guid
42
     * @param $item_guid
43
     * @return ShareRequest[]
44
     */
45
    public function getRequestsByItemGuidGroupedByUser($item_guid){
46
    	if (strtolower($this->db->getDatabasePlatform()->getName()) === 'mysql'){
47
    		$this->db->executeQuery("SET sql_mode = '';");
48
		}
49
        $q = "SELECT *, target_user_id FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_guid = ? GROUP BY target_user_id;";
50
        return $this->findEntities($q, [$item_guid]);
51
    }
52
53
    /**
54
     * Deletes all pending requests for the given user to the given item
55
     * @param $item_id          The item ID
56
     * @param $target_user_id   The target user
57
     * @return \PDOStatement    The result of running the db query
58
     */
59
    public function cleanItemRequestsForUser($item_id, $target_user_id){
60
		$q = "DELETE FROM *PREFIX*" . self::TABLE_NAME . " WHERE item_id = ? AND target_user_id = ?";
61
		$this->execute($q, [$item_id, $target_user_id]);
62
        return $this->execute($q, [$item_id, $target_user_id]);
63
    }
64
65
    /**
66
     * Obtains all pending share requests for the given user ID
67
     * @param $user_id
68
     * @return ShareRequest[]
69
     */
70
    public function getUserPendingRequests($user_id){
71
        $q = "SELECT * FROM *PREFIX*". self::TABLE_NAME ." WHERE target_user_id = ?";
72
        return $this->findEntities($q, [$user_id]);
73
    }
74
75
    /**
76
     * Deletes the given share request
77
     * @param ShareRequest $shareRequest    Request to delete
78
     * @return ShareRequest                 The deleted request
79
     */
80
    public function deleteShareRequest(ShareRequest $shareRequest){
81
    	return $this->delete($shareRequest);
82
	}
83
84
    /**
85
     * Gets a share request by it's unique incremental id
86
     * @param $id
87
     * @return ShareRequest
88
	 * @throws DoesNotExistException
89
     */
90
	public function getShareRequestById($id){
91
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE id = ?";
92
		return $this->findEntity($q, [$id]);
93
	}
94
95
    /**
96
     * Gets all share requests by a given item GUID
97
     * @param $item_guid
98
     * @return ShareRequest[]
99
     */
100
	public function getShareRequestsByItemGuid($item_guid){
101
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE 	item_guid = ?";
102
		return $this->findEntities($q, [$item_guid]);
103
	}
104
105
    /**
106
     * Updates the given share request,
107
     * @param ShareRequest $shareRequest
108
     * @return ShareRequest
109
     */
110
	public function updateShareRequest(ShareRequest $shareRequest){
111
		return $this->update($shareRequest);
112
	}
113
114
    /**
115
     * Finds pending requests sent to the given user to the given item.
116
     * @param $item_guid
117
     * @param $user_id
118
     * @return ShareRequest[]
119
     */
120
	public function getPendingShareRequests($item_guid, $user_id){
121
		$q = "SELECT * FROM *PREFIX*" . self::TABLE_NAME . " WHERE 	item_guid = ? and target_user_id= ?";
122
		return $this->findEntities($q, [$item_guid, $user_id]);
123
	}
124
125
    /**
126
     * Updates all pending requests with the given permissions
127
     * @param $item_guid        The item for which to update the requests
128
     * @param $user_id          The user for which to update the requests
129
     * @param $permissions      The new permissions to apply
130
     * @return \PDOStatement    The result of the operation
131
     */
132
	public function updatePendingRequestPermissions($item_guid, $user_id, $permissions){
133
	    $q = "UPDATE *PREFIX*" . self::TABLE_NAME . " SET permissions = ? WHERE item_guid = ? AND target_user_id = ?";
134
        return $this->execute($q, [$permissions, $item_guid, $user_id]);
135
    }
136
137
}