/
GroupList.php
163 lines (141 loc) · 4.93 KB
/
GroupList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Concrete\Core\User\Group;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Entity\Package;
use Concrete\Core\Search\ItemList\Database\ItemList as DatabaseItemList;
use Concrete\Core\Search\Pagination\Pagination;
use Concrete\Core\Support\Facade\Facade;
use Concrete\Core\User\User;
use Loader;
use Config;
use Pagerfanta\Adapter\DoctrineDbalAdapter;
use Permissions;
class GroupList extends DatabaseItemList
{
protected $includeAllGroups = false;
protected $autoSortColumns = ['gName', 'gID'];
public function includeAllGroups()
{
$this->includeAllGroups = true;
}
/**
* Filters keyword fields by keywords (including name and description).
*
* @param $keywords
*/
public function filterByKeywords($keywords)
{
$expressions = [
$this->query->expr()->like('g.gName', ':keywords'),
$this->query->expr()->like('g.gDescription', ':keywords'),
];
$expr = $this->query->expr();
$this->query->andWhere(call_user_func_array([$expr, 'orX'], $expressions));
$this->query->setParameter('keywords', '%' . $keywords . '%');
}
public function filterByPackage(Package $package)
{
$this->query->andWhere('pkgID = :pkgID');
$this->query->setParameter('pkgID', $package->getPackageID());
}
public function filterByExpirable()
{
$this->query->andWhere('gUserExpirationIsEnabled', ':gUserExpirationIsEnabled');
$this->query->setParameter('gUserExpirationIsEnabled', 1);
}
/**
* Only return groups the user has the ability to assign.
*/
public function filterByAssignable()
{
if (Config::get('concrete.permissions.model') != 'simple') {
// there's gotta be a more reasonable way than this but right now i'm not sure what that is.
$excludeGroupIDs = [GUEST_GROUP_ID, REGISTERED_GROUP_ID];
$db = Loader::db();
$r = $db->executeQuery('select gID from ' . $db->getDatabasePlatform()->quoteSingleIdentifier('Groups') . ' where gID > ?', [REGISTERED_GROUP_ID]);
while ($row = $r->FetchRow()) {
$g = Group::getByID($row['gID']);
$gp = new Permissions($g);
if (!$gp->canAssignGroup()) {
$excludeGroupIDs[] = $row['gID'];
}
}
$this->query->andWhere(
$this->query->expr()->notIn('g.gId', array_map([$db, 'quote'], $excludeGroupIDs))
);
}
}
/**
* Only return groups the user is actually a member of
*/
public function filterByHavingMembership()
{
$inGroupIDs = [];
$app = Facade::getFacadeApplication();
$u = $app->make(User::class);
$db = $app->make(Connection::class);
if ($u->isRegistered()) {
$groups = $u->getUserGroups(); // returns an array of IDs
$this->query->andWhere(
$this->query->expr()->in('g.gId', array_map([$db, 'quote'], $groups))
);
}
}
public function filterByParentGroup(Group $parent)
{
$this->query->andWhere($this->query->expr()->like('g.gPath', ':gPath'));
$this->query->setParameter('gPath', $parent->getGroupPath() . '/%');
}
public function filterByUserID($uID)
{
$this->query->innerJoin('g', 'UserGroups', 'ug', 'g.gID = ug.gID');
$this->query->andWhere('ug.uID = :uID');
$this->query->setParameter('uID', $uID);
}
public function createQuery()
{
$this->query->select('g.gID')
->from($this->query->getConnection()->getDatabasePlatform()->quoteSingleIdentifier('Groups'), 'g');
}
public function finalizeQuery(\Doctrine\DBAL\Query\QueryBuilder $query)
{
if (!$this->includeAllGroups) {
$query->andWhere('g.gID > :minGroupID');
$query->setParameter('minGroupID', REGISTERED_GROUP_ID);
}
return $query;
}
/**
* The total results of the query.
*
* @return int
*/
public function getTotalResults()
{
$query = $this->deliverQueryObject();
return $query->resetQueryParts(['groupBy', 'orderBy'])->select('count(distinct g.gID)')->setMaxResults(1)->execute()->fetchColumn();
}
/**
* Gets the pagination object for the query.
*
* @return Pagination
*/
protected function createPaginationObject()
{
$adapter = new DoctrineDbalAdapter($this->deliverQueryObject(), function ($query) {
$query->resetQueryParts(['groupBy', 'orderBy'])->select('count(distinct g.gID)')->setMaxResults(1);
});
$pagination = new Pagination($this, $adapter);
return $pagination;
}
/**
* @param $queryRow
*
* @return \Concrete\Core\User\Group\Group
*/
public function getResult($queryRow)
{
$g = Group::getByID($queryRow['gID']);
return $g;
}
}