This repository was archived by the owner on Dec 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAuth.php
More file actions
202 lines (176 loc) · 6.63 KB
/
Auth.php
File metadata and controls
202 lines (176 loc) · 6.63 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpBB\SessionsAuthBundle\Authentication;
/**
* Permission/Auth class
*
* This is a copy based on the original phpBB Auth class. It has been modified to work with
* Doctrine entities for the ACL_OPTIONS_TABLE, and all unneeded stuff has been removed.
*
* This version does not support recreating the user_permissions field in the user table.
* If there is no user_permissions field, it will result in a exception.
*
*/
class Auth
{
private $acl = array();
private $cache = array();
private $aclOptions = array();
private $aclForumIds = false;
/**
* Init permissions
* @param $userPermissions string
* @throws \Exception
*/
public function acl($userPermissions)
{
$this->acl = array();
$this->cache = array();
$this->aclOptions = array();
$this->aclForumIds = false;
if (($this->aclOptions = $cache->get('_acl_options')) === false)
{
$sql = 'SELECT auth_option_id, auth_option, is_global, is_local
FROM ' . ACL_OPTIONS_TABLE . '
ORDER BY auth_option_id';
$result = $db->sql_query($sql);
$global = 0;
$local = 0;
$this->aclOptions = array();
while ($row = $db->sql_fetchrow($result))
{
if ($row['is_global'])
{
$this->aclOptions['global'][$row['auth_option']] = $global++;
}
if ($row['is_local'])
{
$this->aclOptions['local'][$row['auth_option']] = $local++;
}
$this->aclOptions['id'][$row['auth_option']] = (int) $row['auth_option_id'];
$this->aclOptions['option'][(int) $row['auth_option_id']] = $row['auth_option'];
}
$db->sql_freeresult($result);
$cache->put('_acl_options', $this->aclOptions);
}
if (!trim($userPermissions))
{
throw new \Exception('We require user_permissions set by phpBB.');
}
// Fill ACL array
$this->_fill_acl($userPermissions);
// Verify bitstring length with options provided...
$renew = false;
$global_length = sizeof($this->aclOptions['global']);
$local_length = sizeof($this->aclOptions['local']);
// Specify comparing length (bitstring is padded to 31 bits)
$global_length = ($global_length % 31) ? ($global_length - ($global_length % 31) + 31) : $global_length;
$local_length = ($local_length % 31) ? ($local_length - ($local_length % 31) + 31) : $local_length;
// You thought we are finished now? Noooo... now compare them.
foreach ($this->acl as $forum_id => $bitstring)
{
if (($forum_id && strlen($bitstring) != $local_length) || (!$forum_id && strlen($bitstring) != $global_length))
{
$renew = true;
break;
}
}
// If a bitstring within the list does not match the options, we have a user with incorrect permissions set and need to renew them
if ($renew)
{
throw new \Exception('Renewing is not supported.');
}
return;
}
/**
* Fill ACL array with relevant bitstrings from user_permissions column
* @param $userPermissions
*/
private function _fill_acl($userPermissions)
{
$seq_cache = array();
$this->acl = array();
$userPermissions = explode("\n", $userPermissions);
foreach ($userPermissions as $f => $seq)
{
if ($seq)
{
$i = 0;
if (!isset($this->acl[$f]))
{
$this->acl[$f] = '';
}
while ($subseq = substr($seq, $i, 6))
{
if (isset($seq_cache[$subseq]))
{
$converted = $seq_cache[$subseq];
}
else
{
$result = str_pad(base_convert($subseq, 36, 2), 31, 0, STR_PAD_LEFT);
$converted = $result;
$seq_cache[$subseq] = $result;
}
// We put the original bitstring into the acl array
$this->acl[$f] .= $converted;
$i += 6;
}
}
}
}
/**
* Look up an option
* if the option is prefixed with !, then the result becomes negated
*
* If a forum id is specified the local option will be combined with a global option if one exist.
* If a forum id is not specified, only the global option will be checked.
* @param $opt string option
* @param int $forumId int forum_id
* @return bool
*/
public function aclGet($opt, $forumId = 0)
{
$negate = false;
if (strpos($opt, '!') === 0)
{
$negate = true;
$opt = substr($opt, 1);
}
if (!isset($this->cache[$forumId][$opt]))
{
// We combine the global/local option with an OR because some options are global and local.
// If the user has the global permission the local one is true too and vice versa
$this->cache[$forumId][$opt] = false;
// Is this option a global permission setting?
if (isset($this->aclOptions['global'][$opt]))
{
if (isset($this->acl[0]))
{
$this->cache[$forumId][$opt] = $this->acl[0][$this->aclOptions['global'][$opt]];
}
}
// Is this option a local permission setting?
// But if we check for a global option only, we won't combine the options...
if ($forumId != 0 && isset($this->aclOptions['local'][$opt]))
{
if (isset($this->acl[$forumId]) && isset($this->acl[$forumId][$this->aclOptions['local'][$opt]]))
{
$this->cache[$forumId][$opt] |= $this->acl[$forumId][$this->aclOptions['local'][$opt]];
}
}
}
// Founder always has all global options set to true...
return ($negate) ? !$this->cache[$forumId][$opt] : $this->cache[$forumId][$opt];
}
}