-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
UserFilter.php
185 lines (161 loc) · 5.61 KB
/
UserFilter.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\user\models;
use humhub\modules\user\components\ActiveQueryUser;
use Yii;
/**
* Special user model class for the purpose of searching users.
*
* @author Julian Harrer
*/
class UserFilter extends User
{
/**
* Returns a UserFilter instance for the given $user or the current user identity
* if $user is not provided.
*
* @param type $user
* @return type
*/
public static function forUser($user = null)
{
if ($user == null) {
$user = Yii::$app->user->getIdentity();
}
$userId = ($user instanceof User) ? $user->id : $user;
return self::findIdentity($userId);
}
/**
* Default implementation for user picker filter.
*
* @param type $keywords
* @param type $maxResults
* @param type $friendsOnly
* @param type $permission
* @deprecated since 1.2 use
* @return type
*/
public function getUserPickerResult($keywords = null, $maxResults = null, $friendsOnly = false, $permission = null)
{
if (!Yii::$app->getModule('friendship')->getIsEnabled()) {
//We don't use the permission here for filtering since we include user with no permission as disabled in the result.
//The problem here is we do not prefer users with permission in the query.
$users = $this->getUserByFilter($keywords, $maxResults);
return UserPicker::asJSON($users, $permission);
}
$friends = $this->getFriendsByFilter($keywords, $maxResults);
//Create userinfo json with with set 'disabled' field if the user is not permitted
$jsonResult = UserPicker::asJSON($friends, $permission);
//Fill the remaining space with member users with the given permission
if (!$friendsOnly && count($friends) < $maxResults) {
$additionalUser = [];
//Here we filter with permission since we don't want to have non friend user without the permission in the result
foreach($this->getUserByFilter($keywords, ($maxResults - count($friends)), $permission) as $user) {
if(!$this->containsUser($friends, $user)) {
$additionalUser[] = $user;
}
}
$jsonResult = array_merge($jsonResult, UserPicker::asJSON($additionalUser));
}
return $jsonResult;
}
private function containsUser($userArr, $user)
{
foreach($userArr as $currentUser) {
if($currentUser->id === $user->id) {
return true;
}
}
return false;
}
/**
* Searches for all active users by the given keyword and permission.
*
* @param type $keywords
* @param type $maxResults
* @param type $permission
* @return type
*/
public static function getUserByFilter($keywords = null, $maxResults = null, $permission = null)
{
return self::filter(User::find(), $keywords, $maxResults, $permission);
}
/**
* Search for all active friends by the given keyword and permission
*
* @param type $keywords search keyword
* @param type $maxResults
* @param type $permission
* @return type
*/
public function getFriendsByFilter($keywords = null, $maxResults = null, $permission = null)
{
return self::filter($this->getFriends(), $keywords, $maxResults, $permission);
}
/**
* Returns an array of user models filtered by a $keyword and $permission. These filters
* are added to the provided $query. The $keyword filter can be used to filter the users
* by email, username, firstname, lastname and title. By default this functions does not
* consider inactive user.
*
* @param type $query
* @param type $keywords
* @param type $maxResults
* @param type $permission
* @param type $active
* @return type
*/
public static function filter($query, $keywords = null, $maxResults = null, $permission = null, $active = null)
{
$user = self::addQueryFilter($query, $keywords, $maxResults, $active)->all();
return self::filterByPermission($user, $permission);
}
public static function addQueryFilter($query, $keywords = null, $maxResults = null, $active = null)
{
self::addKeywordFilter($query, $keywords);
if ($maxResults != null) {
$query->limit($maxResults);
}
//We filter active user by default
if(($active != null && $active) || $active == null) {
$query->active();
}
return $query;
}
/**
* Filter users by keyword
*
* @param ActiveQueryUser $query
* @param string|array $keyword
* @return ActiveQueryUser
*/
public static function addKeywordFilter($query, $keyword)
{
return $query->search($keyword);
}
/**
* Returns a subset of the given array containing all users of the given set
* which are permitted. If the permission is null this method returns the
*
* @param $users
* @param $permission
* @return array
*/
public static function filterByPermission($users, $permission)
{
if ($permission === null) {
return $users;
}
$result = [];
foreach ($users as $user) {
if ($user->getPermissionManager()->can($permission)) {
$result[] = $user;
}
}
return $result;
}
}