-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUnityOrg.php
117 lines (94 loc) · 3.09 KB
/
UnityOrg.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
<?php
namespace UnityWebPortal\lib;
use Exception;
use PHPOpenLDAPer\LdapEntry;
class UnityOrg
{
private $orgid;
private $MAILER;
private $SQL;
private $LDAP;
private $REDIS;
private $WEBHOOK;
public function __construct($orgid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK)
{
$this->orgid = $orgid;
$this->LDAP = $LDAP;
$this->SQL = $SQL;
$this->MAILER = $MAILER;
$this->WEBHOOK = $WEBHOOK;
$this->REDIS = $REDIS;
}
public function init()
{
$entry = $this->getLDAPEntry();
if (!$entry->exists()) {
$nextGID = $this->LDAP->getNextOrgGIDNumber($this->SQL);
$entry->setAttribute("objectclass", UnityLDAP::POSIX_GROUP_CLASS);
$entry->setAttribute("gidnumber", strval($nextGID));
if (!$entry->write()) {
throw new Exception("Failed to create POSIX group for " . $this->orgid); // this shouldn't execute
}
}
$this->REDIS->appendCacheArray("sorted_orgs", "", $this->getOrgID());
}
public function exists()
{
return $this->getLDAPEntry()->exists();
}
public function getLDAPEntry(): LdapEntry
{
return $this->LDAP->getOrgGroupEntry($this->orgid);
}
public function getOrgID()
{
return $this->orgid;
}
public function userExists(UnityUser $user, $ignorecache = false): bool
{
$members = $this->getOrgMemberUIDs($ignorecache);
return in_array($user->getUID(), $members);
}
public function getOrgMemberUIDs($ignorecache = false): array
{
if (!$ignorecache) {
$cached_val = $this->REDIS->getCache($this->getOrgID(), "members");
if (!is_null($cached_val)) {
return $cached_val;
}
}
$entry = $this->getLDAPEntry();
$members = $entry->getAttribute("memberuid") ?? [];
sort($members);
$this->REDIS->setCache($this->getOrgID(), "members", $members);
return $members;
}
public function getOrgMembers($ignorecache = false)
{
$memberuids = $this->getOrgMemberUIDs($ignorecache);
$out = array();
foreach ($memberuids as $uid) {
$user_obj = new UnityUser($uid, $this->LDAP, $this->SQL, $this->MAILER, $this->REDIS, $this->WEBHOOK);
array_push($out, $user_obj);
}
return $out;
}
public function addUser($user)
{
$entry = $this->getLDAPEntry();
$entry->appendAttribute("memberuid", $user->getUID());
if (!$entry->write()) {
throw new Exception("Unable to write to org group");
}
$this->REDIS->appendCacheArray($this->getOrgID(), "members", $user->getUID());
}
public function removeUser($user)
{
$entry = $this->getLDAPEntry();
$entry->removeAttributeEntryByValue("memberuid", $user->getUID());
if (!$entry->write()) {
throw new Exception("Unable to write to org group");
}
$this->REDIS->removeCacheArray($this->getOrgID(), "members", $user->getUID());
}
}