forked from SFWLtd/plg_user_teamroles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeamRolesUserInfo.php
171 lines (149 loc) · 5.29 KB
/
TeamRolesUserInfo.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
<?php
/**
* @version 1.3
* @package plg_user_teamroles
* @author Simon Champion
* @copyright SFW Ltd
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
* @requires Joomdle >= 1.0.6
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class TeamRolesUserInfo
{
public $userID = 0;
public $username = '';
public $userGroups = [];
public $iAmAParent = false;
public $updated = null;
public $configGroups;
public $configAccess;
public function __construct($params, $userID, $username, $activeUser = true)
{
if (!(int)$userID) {
return;
}
$this->userID = (int)$userID;
$this->username = $username;
if (!$params)
return;
$this->configGroups = $this->subGroupsOfConfigTopGroup($params->get('topgroup'));
$this->configAccess = $params->get('parent');
if ($activeUser) {
//get the users groups, and filter it to only include the ones that are configured for parent/child relationships.
$this->userGroups = array_intersect(JAccess::getGroupsByUser($userID), $this->configGroups);
//check whether the user has parent access level
$this->iAmAParent = in_array($this->configAccess, JAccess::getAuthorisedViewLevels($userID));
} else {
//inactive or blocked user should be treated as not belonging to any groups nor being a parent.
$this->userGroups = [];
$this->iAmAParent = false;
}
}
/**
* Get all subgroups of a given usergroup.
* (I'm sure there must be a standard Joomla way of doing this but for the life of me I can't find it)
*/
private function subGroupsOfConfigTopGroup($topGroup)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__usergroups'));
$query->where($db->quoteName('parent_id').'='.(int)$topGroup);
$db->setQuery($query);
return $db->loadColumn();
}
public function loadTeamRoleToggleFromProfile($teamMemberID)
{
$teamMemberID = (int)$teamMemberID;
if (!$teamMemberID || $teamMemberID === $this->userID) {
return true;
}
$db = JFactory::getDbo();
$db->setQuery("SELECT profile_value FROM #__user_profiles"
." WHERE user_id = {$this->userID} AND profile_key = 'teamroles.show.{$teamMemberID}' "
." ORDER BY ordering");
$result = $db->loadResult();
if ($result === null) {
return true; //not set yet, so default to true.
}
return !!$result;
}
public function saveTeamRoleToggleToProfile($teamMemberID, $value)
{
$value = (bool)$value;
$teamMemberID = (int)$teamMemberID;
if (!$teamMemberID || $teamMemberID == $this->userID) {
return true;
}
$db = JFactory::getDbo();
$db->setQuery("DELETE FROM #__user_profiles WHERE user_id={$this->userID} AND profile_key = 'teamroles.show.{$teamMemberID}'");
if (!$db->query()) {
throw new Exception($db->getErrorMsg());
}
$saveValue = $value ? 1 : 0;
$db->setQuery("INSERT INTO #__user_profiles SET "
." user_id={$this->userID},"
." profile_key='teamroles.show.{$teamMemberID}',"
." profile_value={$saveValue},"
." ordering=1");
if (!$db->query()) {
throw new Exception($db->getErrorMsg());
}
return $value;
}
public function addComparitor(TeamRolesUserInfo $updated)
{
$this->updated = $updated;
if (!$this->userID) {
$this->userID = $updated->userID;
$this->username = $updated->username;
$this->configGroups = $updated->configGroups;
$this->configAccess = $updated->configAccess;
}
}
public function sameAsComparitor()
{
return !($this->isParentRoleChanging() || $this->isGroupListChanging());
}
public function isParentRoleChanging()
{
return ($this->iAmAParent != $this->updated->iAmAParent);
}
public function isBecomingAParent()
{
return (!$this->iAmAParent && $this->updated->iAmAParent);
}
public function isNoLongerAParent()
{
return ($this->iAmAParent && !$this->updated->iAmAParent);
}
public function isGroupListChanging()
{
return ($this->userGroups != $this->updated->userGroups);
}
public function groupsAdded()
{
return array_diff($this->updated->userGroups, $this->userGroups);
}
public function groupsRemoved()
{
return array_diff($this->userGroups, $this->updated->userGroups);
}
public function groupsUnchanged()
{
return array_intersect($this->userGroups, $this->updated->userGroups);
}
public static function usersInGroup($group)
{
$users = JAccess::getUsersByGroup($group);
foreach ($users as $userID) {
$userObj = JFactory::getUser($userID);
//exclude users who are either blocked or not yet activated.
if (!$userObj->block && !(isset($userObj->activation) && $userObj->activation)) {
yield $userID;
}
}
}
}