Code

Updated styles and display
[gosa.git] / gosa-core / plugins / generic / references / class_aclResolver.inc
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"));
69     
70         // Enforce to reload acl result 
71         $this->renderedList = "";
72     }
74     
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     }
110     
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         }
125        
126         if(empty($this->renderedList)){
128             $this->reload();
130             // Autocompleter template 
131             $autocompleter ="
132                 <input class='filter_textfield' placeholder='"._("Search")."...' 
133                 id='aclTarget' name='aclTarget' type='text' value='' 
134                 onChange=\"\$('aclTargetSubmit').focus();\">
136                 <div id='autocomplete' class='autocomplete'></div>
137                 <script type='text/javascript'>
138                 new Ajax.Autocompleter(
139                         'aclTarget', 
140                         'autocomplete', 
141                         'autocomplete.php', 
142                         { minChars: 3, frequency: 0.5 });
143                 </script>
144             ".image("images/lists/submit.png","aclTargetSubmit");
146             // Base template - each entry start with this 
147             $tpl = 
148                 "\n <tr class='acl-viewer-head %s'>".
149                 "\n  <td>%s</td>".
150                 "\n  <td colspan=2><b>%s</b><div class='right'>%s</div></td>".
151                 "\n </tr>".
152                 "\n %s";
154             // If the acl consists of a user-object-filter then this template is used.        
155             $filter_tpl = 
156                 "\n <tr class='%s'>".
157                 "\n  <td></td>".
158                 "\n  <td><b>"._("Filter")."</b></td>".
159                 "\n  <td><ul class='acl-viewer-items'><li>%s</li></ul></td>".
160                 "\n </tr>";
162             // Used to display ACL owner of type "group"
163             $gmem_tpl = 
164                 "\n <tr class='%s'>".
165                 "\n  <td></td>".
166                 "\n  <td><b>"._("Groups")."</b></td>".
167                 "\n  <td><ul class='acl-viewer-items'>%s</ul></td>".
168                 "\n </tr>";
170             // Used to display ACL owner of type "user"
171             $umem_tpl = 
172                 "\n <tr class='%s'>".
173                 "\n  <td></td>".
174                 "\n  <td><b>"._("Users")."</b></td>".
175                 "\n  <td><ul class='acl-viewer-items'>%s</ul></td>".
176                 "\n </tr>";
178             // Used to display the acl contents, except 'reset' and 'role'
179             $acl_tpl = 
180                 "\n <tr class='%s'>".
181                 "\n  <td></td>".
182                 "\n  <td><b>"._("Acls")."</b></td>".
183                 "\n  <td><ul class='acl-viewer-items'>%s</ul></td>".
184                 "\n </tr>";
187             $user = "<h3>".sprintf(_("List of effective ACL for '%s'"), $this->validateUid)."</h3>";
188             $str  = "<table summary='"._("Object permissions")."'>";
189             $str .= " <tr><td>".$user."</td><td>".$autocompleter."</td></tr>";
190             $str .= "</table>";
191             $str .= "<div class='acl-viewer-container'>";
192             $str .= "<table summary='"._("Object permissions")."' cellpadding='0' cellspacing='0' class='acl-viewer'>";
193             $ldap = $this->config->get_ldap_link();
194             $ldap->cd($this->config->current['BASE']);
195             $ui = get_userinfo();
196             foreach($this->affectingACLs as $dn => $acls){
197                 foreach($acls as $acl){
199                     // Prepare entry icon (department or element?)
200                     $image = (isset($this->config->idepartments[$dn]))? "images/select_department.png":"images/lists/element.png";
202                     // The acl type (sub,psub,reset...)
203                     $aclType = $this->aclTypes[$acl['type']];
205                     // Does the filter match for current object? 
206                     $filter ="";
207                     $match = TRUE;
208                     if(!empty($acl['filter'])){
209                         $match = $ldap->object_match_filter($this->dn,$acl['filter']);
210                         $filter= $acl['filter'];
211                         if(!$match){
212                             $filter= "<span>".$filter."</span>";
213                         }
214                     }
216                     // Check membership 
217                     $gmem = $umem = "";
218                     $users = $groups = array();
219                     $gfound = $ufound = FALSE;
220                     foreach($acl['members'] as $type => $name){
222                         // Check if we're part of the members 
223                         if(preg_match("/^U:/", $type)){
224                             if(preg_match("/^U:".preg_quote($this->validateDn,'/')."/", $type)){
225                                 $users[] = $name;
226                                 $ufound = TRUE;
227                                 continue;
228                             }
229                             $users[] = "<span>".$name."</span>";
230                         }
232                         // Check if we're part of the group members 
233                         if(preg_match("/^G/", $type)){
234                             if($type == "G:*"){
235                                 $gfound = TRUE;
236                                 $groups[] = $name;
237                                 continue;
238                             }
239                             if(preg_match("/^G:/", $type)){
240                                 $gdn = preg_replace("/^G:/","",$type);
241                                 $ldap->cat($gdn,array('memberUid'));
242                                 if($ldap->count()){
243                                     $attrs = $ldap->fetch();
244                                     if(isset($attrs['memberUid']) && in_array($this->validateUid, $attrs['memberUid'])){
245                                         $gfound = TRUE;
246                                         $groups[] = $name;
247                                     }
248                                 }
249                                 continue;
250                             }
252                             // Group member doesn't mactch
253                             $groups[] = "<span>".$name."</span>";
254                         }
255                     }
256                     if(!count($groups)) $gfound = TRUE;
257                     if(!count($users)) $ufound = TRUE;
259                     // Build up ACL definition list 
260                     $defs ="";
261                     if($acl['type']!='reset'){
262                         foreach($acl['acl'] as $type => $acl){
263                             if(isset($this->classMapping[$type])){
264                                 $defs .= "<li>".$this->classMapping[$type].": ".$this->aclToString($acl)."</li>";
265                             }else{
266                                 $defs .= "<li>".$type.": ".$this->aclToString($acl)."</li>";
267                             }
268                         }
269                     }
270                    
271                     // Display the acl block in a special color if its not matching 
272                     $class="";
273                     if(!($gfound || $ufound) || !$match){
274                         $class = "acl-viewer-blocked";
275                     }                    
277                     if(!empty($filter)) $filter =sprintf($filter_tpl,$class,$filter);
278                     if(!empty($defs)) $defs = sprintf($acl_tpl,$class,$defs);
279                     if(count($users))  $umem = sprintf($umem_tpl,$class,"<li>".implode($users,'</li><li>')."</li>");
280                     if(count($groups)) $gmem = sprintf($gmem_tpl,$class,"<li>".implode($groups,'</li><li>')."</li>");
281                     $str.= sprintf($tpl,$class, image($image), $dn, $aclType, $filter.$gmem.$umem.$defs);
282                 }
283             }
284             $str .= "</table>"; 
285             $str .= "</div>"; 
286             $this->renderedList = $str;
287         }
288         return($this->renderedList);
289     }
291     function aclToString($acls)
292     {
293         $str ="<ul>";
294         foreach($acls as $name => $acl){
295             if($name == "0") $name = _("All");
296             $str .= "<li>".$name;
297             $str .= "<ul>";
298             if(preg_match("/s/", $acl)){
299                 $str.="<li><u>"._("Grant permission to owner").'</u></li>'; 
300             }
301             if(preg_match("/r/", $acl)) $str.="<li>"._("read").'</li>'; 
302             if(preg_match("/w/", $acl)) $str.="<li>"._("write").'</li>'; 
303             if(preg_match("/c/", $acl)) $str.="<li>"._("Create").'</li>'; 
304             if(preg_match("/d/", $acl)) $str.="<li>"._("Remove").'</li>'; 
305             if(preg_match("/m/", $acl)) $str.="<li>"._("Move").'</li>'; 
306             $str.= "</ul>";
307         }
308         return($str."</ul>");
309     }
311     function processAutocomplete()
312     {
313             $ldap = $this->config->get_ldap_link();
314             $ldap->cd($this->config->current['BASE']);
315             $ldap->search("(&(objectClass=gosaAccount)(|(sn=*".get_post('aclTarget')."*)".
316                     "(uid=*".get_post('aclTarget')."*)(givenName=*".get_post('aclTarget')."*)))",
317                     array('uid','dn','sn','givenName'));
318             echo "<ul>";
319             while($attrs = $ldap->fetch()){
320                 $display = $attrs['givenName'][0]." ".$attrs['sn'][0]." [".$attrs['uid'][0]."]";
321                 echo "<li>{$display}</li>";
322                 $this->userMap[$display] = $attrs;
323             }
324             echo "</ul>";
325     }
328 ?>