1 <?php
3 class aclResolver
4 {
5 private $classMapping = array();
6 private $aclTypes = array();
7 private $affectingACLs = array();
9 private $renderedList = "";
11 // The users 'dn' and 'uid' used to verify the collected acls
12 private $validateDn;
13 private $validateUid;
14 private $acl_category;
15 private $parent;
17 // Used for the autocompleter
18 private $userMap;
20 function __construct($config, $dn, $parent)
21 {
22 $this->config = &$config;
23 $this->dn = $dn;
24 $this->parent = $parent;
26 // Replace this with a user defined one later.
27 $ui = get_userinfo();
28 $this->validateUid = $ui->uid;
29 $this->validateDn = $ui->dn;
31 // Build class mapping - only once, will not change during session.
32 if(!session::is_set('aclConverter::classMapping')){
33 $tmp= session::global_get('plist');
34 $plist= $tmp->info;
35 $map = array();
36 $map['all']= _("All categories");
37 foreach($plist as $class => $plInfo){
38 if(isset($plInfo['plCategory']) && is_array($plInfo['plCategory'])){
39 foreach($plInfo['plCategory'] as $category => $desc){
40 if(!is_numeric($category)){
41 $map[$category] = $desc['description'];
42 }
43 }
44 }
45 }
46 foreach($plist as $class => $plInfo){
47 if(isset($plInfo['plCategory']) && is_array($plInfo['plCategory'])){
48 foreach($plInfo['plCategory'] as $category => $desc){
49 if(!is_numeric($category)){
50 $map[$category."/".$class] = $map[$category]." - ".$plInfo['plDescription'];
51 }else{
52 $map[$desc."/".$class] = $map[$desc]." - ".$plInfo['plDescription'];
53 }
54 }
56 }
57 }
58 session::set('aclConverter::classMapping', $map);
59 }
60 $this->classMapping = session::get('aclConverter::classMapping');
62 // Define ACL type translations
63 $this->aclTypes= array("reset" => _("Reset ACLs"),
64 "one" => _("One level"),
65 "base" => _("Current object"),
66 "sub" => _("Complete subtree"),
67 "psub" => _("Complete subtree (permanent)"),
68 "role" => _("Use ACL defined in role"));
70 // Enforce to reload acl result
71 $this->renderedList = "";
72 }
75 function reload()
76 {
77 // Go through all ACLs and get those matching the objects dn.
78 $ui = get_userinfo();
79 $ui->reset_acl_cache();
80 $ui->loadACL();
82 // Get ACL category for the current object.
83 if(isset($this->parent->acl_category) && !empty($this->parent->acl_category)){
84 $this->acl_category = preg_replace("/\/$/","",$this->parent->acl_category);
85 }
87 foreach($ui->allACLs as $dn => $acls){
88 if(preg_match("/".preg_quote($dn,'/')."$/i", $this->dn)){
90 // Foreach dn there is a collection of ACLs indexed by their priority
91 foreach($acls as $prio => $acl){
92 if($acl['type'] == "reset"){
93 $this->affectingACLs[$dn][$prio] = $acl;
94 continue;
95 }else{
97 // Only get those entries with a relevant acl-category
98 foreach($acl['acl'] as $category => $attributes){
99 if(preg_match("/^all($|\/)/", $category) ||
100 preg_match("/^".$this->acl_category."($|\/)/", $category)){
101 $this->affectingACLs[$dn][$prio] = $acl;
102 continue;
103 }
104 }
105 }
106 }
107 }
108 }
109 }
113 /*! \brief Create a human readable HTML result
114 */
115 function getReadableACL()
116 {
117 if(isset($_POST['aclTarget'])){
118 $d = get_post('aclTarget');
119 if(isset($this->userMap[$d])){
120 $this->validateDn = $this->userMap[$d]['dn'];
121 $this->validateUid = $this->userMap[$d]['uid'][0];
122 $this->renderedList = "";
123 }
124 }
126 if(empty($this->renderedList)){
128 $this->reload();
130 // Base template - each entry start with this
131 $tpl =
132 "\n <tr class='acl-viewer-head %s'>".
133 "\n <td>%s</td>".
134 "\n <td colspan=2><b>%s</b> - %s</td>".
135 "\n </tr>".
136 "\n %s".
137 "\n <tr>".
138 "\n <td colspan=3><hr></td>".
139 "\n </tr>";
141 // If the acl consists of a user-object-filter then this template is used.
142 $filter_tpl =
143 "\n <tr class='%s'>".
144 "\n <td></td>".
145 "\n <td><b>"._("Filter")."</b></td>".
146 "\n <td><ul><li>%s</li></ul></td>".
147 "\n </tr>";
149 // Used to display ACL owner of type "group"
150 $gmem_tpl =
151 "\n <tr class='%s'>".
152 "\n <td></td>".
153 "\n <td><b>"._("Group members")."</b></td>".
154 "\n <td><ul>%s</ul></td>".
155 "\n </tr>";
157 // Used to display ACL owner of type "user"
158 $umem_tpl =
159 "\n <tr class='%s'>".
160 "\n <td></td>".
161 "\n <td><b>"._("Members")."</b></td>".
162 "\n <td><ul>%s</ul></td>".
163 "\n </tr>";
165 // Used to display the acl contents, except 'reset' and 'role'
166 $acl_tpl =
167 "\n <tr class='%s'>".
168 "\n <td></td>".
169 "\n <td><b>"._("Acls")."</b></td>".
170 "\n <td><ul>%s</ul></td>".
171 "\n </tr>";
174 $str = "<br>".sprintf(_("ACL from the viewpoint of user '%s'"), "<b>".$this->validateUid."</b>");
175 $str.= "<table summary='"._("Object permissions")."' class='acl-viewer'>";
176 $ldap = $this->config->get_ldap_link();
177 $ldap->cd($this->config->current['BASE']);
178 $ui = get_userinfo();
179 foreach($this->affectingACLs as $dn => $acls){
180 foreach($acls as $acl){
181 $gmem = $umem = $defs = "";
183 // Prepare entry icon (department or element?)
184 $image = (isset($this->config->idepartments[$dn]))? "images/select_department.png":"images/lists/element.png";
186 // The acl type (sub,psub,reset...)
187 $aclType = $this->aclTypes[$acl['type']];
189 // Does the filter match for current object?
190 $filter ="";
191 $match = TRUE;
192 if(!empty($acl['filter'])){
193 $match = $ldap->object_match_filter($this->dn,$acl['filter']);
194 $filter= $acl['filter'];
195 if(!$match){
196 $filter= "<span>".$filter."</span>";
197 }
198 }
200 // Check if we are part of the member list
201 if($match){
202 $found = FALSE;
203 foreach($acl['members'] as $mem => $desc){
204 if($mem == "U:{$this->validateDn}"){
205 $found = TRUE;
206 break;
207 }
208 if($mem == "G:*"){
209 $found = TRUE;
210 break;
211 }
212 if(preg_match("/^G:/", $mem)){
213 $gdn = preg_replace("/^G:/","",$mem);
214 $ldap->cat($gdn,array('memberUid'));
215 if($ldap->count()){
216 $attrs = $ldap->fetch();
217 if(isset($attrs['memberUid']) && in_array($this->validateUid, $attrs['memberUid'])){
218 $found = TRUE;
219 }
220 }
221 break;
222 }
223 }
224 $match = $found;
225 }
227 $class = "";
228 if(!$match){
229 $class = "acl-viewer-blocked";
230 }
232 if(!empty($filter)) $filter =sprintf($filter_tpl,$class,$filter);
234 foreach($acl['members'] as $type => $name){
235 if(preg_match("/^G/", $type))
236 $gmem .= "\n <li>".$name."</li>";
237 }
238 if(!empty($gmem)) $gmem =sprintf($gmem_tpl,$class,$gmem);
240 foreach($acl['members'] as $type => $name){
241 if(!preg_match("/^G/", $type))
242 $umem .= "\n <li>".$name."</li>";
243 }
244 if(!empty($umem)) $umem = sprintf($umem_tpl,$class,$umem);
246 if($acl['type']!='reset'){
247 foreach($acl['acl'] as $type => $acl){
248 if(isset($this->classMapping[$type])){
249 $defs .= "<li>".$this->classMapping[$type].": ".$this->aclToString($acl)."</li>";
250 }else{
251 $defs .= "<li>".$type.": ".$this->aclToString($acl)."</li>";
252 }
253 }
254 if(!empty($defs)) $defs = sprintf($acl_tpl, $class,$defs);
255 }
256 $str.= sprintf($tpl,$class, image($image), $dn, $aclType, $filter.$gmem.$umem.$defs);
257 }
258 }
259 $str .= "</table>";
260 $this->renderedList = $str;
261 }
262 return($this->renderedList);
263 }
265 function aclToString($acls)
266 {
267 $str ="<ul>";
268 foreach($acls as $name => $acl){
269 if($name == "0") $name = _("All");
270 $str .= "<li>".$name;
271 $str .= "<ul>";
272 if(preg_match("/s/", $acl)){
273 $str.="<li>"._("Grant permission to owner").'</li>';
274 }
275 if(preg_match("/r/", $acl)) $str.="<li>"._("read").'</li>';
276 if(preg_match("/w/", $acl)) $str.="<li>"._("write").'</li>';
277 if(preg_match("/c/", $acl)) $str.="<li>"._("Create").'</li>';
278 if(preg_match("/d/", $acl)) $str.="<li>"._("Remove").'</li>';
279 if(preg_match("/m/", $acl)) $str.="<li>"._("Move").'</li>';
280 $str.= "</ul>";
281 }
282 return($str."</ul>");
283 }
285 function processAutocomplete()
286 {
287 $ldap = $this->config->get_ldap_link();
288 $ldap->cd($this->config->current['BASE']);
289 $ldap->search("(&(objectClass=gosaAccount)(|(sn=*".get_post('aclTarget')."*)".
290 "(uid=*".get_post('aclTarget')."*)(givenName=*".get_post('aclTarget')."*)))",
291 array('uid','dn','sn','givenName'));
292 echo "<ul>";
293 while($attrs = $ldap->fetch()){
294 $display = $attrs['givenName'][0]." ".$attrs['sn'][0]." [".$attrs['uid'][0]."]";
295 echo "<li>{$display}</li>";
296 $this->userMap[$display] = $attrs;
297 }
298 echo "</ul>";
299 }
300 }
302 ?>