Code

Updated completer
[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>&nbsp;-&nbsp;%s</td>".
151                 "\n </tr>".
152                 "\n %s".
153                 "\n <tr>".
154                 "\n  <td colspan=3><hr></td>".
155                 "\n </tr>";
157             // If the acl consists of a user-object-filter then this template is used.        
158             $filter_tpl = 
159                 "\n <tr class='%s'>".
160                 "\n  <td></td>".
161                 "\n  <td><b>"._("Filter")."</b></td>".
162                 "\n  <td><ul><li>%s</li></ul></td>".
163                 "\n </tr>";
165             // Used to display ACL owner of type "group"
166             $gmem_tpl = 
167                 "\n <tr class='%s'>".
168                 "\n  <td></td>".
169                 "\n  <td><b>"._("Group members")."</b></td>".
170                 "\n  <td><ul>%s</ul></td>".
171                 "\n </tr>";
173             // Used to display ACL owner of type "user"
174             $umem_tpl = 
175                 "\n <tr class='%s'>".
176                 "\n  <td></td>".
177                 "\n  <td><b>"._("Members")."</b></td>".
178                 "\n  <td><ul>%s</ul></td>".
179                 "\n </tr>";
181             // Used to display the acl contents, except 'reset' and 'role'
182             $acl_tpl = 
183                 "\n <tr class='%s'>".
184                 "\n  <td></td>".
185                 "\n  <td><b>"._("Acls")."</b></td>".
186                 "\n  <td><ul>%s</ul></td>".
187                 "\n </tr>";
190             $user = sprintf(_("ACL from the viewpoint of user '%s'"), "<b>".$this->validateUid."</b>");
191             $str  = "<table summary='"._("Object permissions")."'>";
192             $str .= " <tr><td>".$user."</td><td>".$autocompleter."</td></tr>";
193             $str .= "</table>";
194             $str .= "<table summary='"._("Object permissions")."' class='acl-viewer'>";
195             $ldap = $this->config->get_ldap_link();
196             $ldap->cd($this->config->current['BASE']);
197             $ui = get_userinfo();
198             foreach($this->affectingACLs as $dn => $acls){
199                 foreach($acls as $acl){
200                     $gmem = $umem = $defs = "";
202                     // Prepare entry icon (department or element?)
203                     $image = (isset($this->config->idepartments[$dn]))? "images/select_department.png":"images/lists/element.png";
205                     // The acl type (sub,psub,reset...)
206                     $aclType = $this->aclTypes[$acl['type']];
208                     // Does the filter match for current object? 
209                     $filter ="";
210                     $match = TRUE;
211                     if(!empty($acl['filter'])){
212                         $match = $ldap->object_match_filter($this->dn,$acl['filter']);
213                         $filter= $acl['filter'];
214                         if(!$match){
215                             $filter= "<span>".$filter."</span>";
216                         }
217                     }
219                     // Check if we are part of the member list 
220                     if($match){
221                         $found = FALSE;
222                         foreach($acl['members'] as $mem => $desc){
223                             if($mem == "U:{$this->validateDn}"){
224                                 $found = TRUE;
225                                 break;
226                             }
227                             if($mem == "G:*"){
228                                 $found = TRUE;
229                                 break;
230                             }
231                             if(preg_match("/^G:/", $mem)){
232                                 $gdn = preg_replace("/^G:/","",$mem);
233                                 $ldap->cat($gdn,array('memberUid'));
234                                 if($ldap->count()){
235                                     $attrs = $ldap->fetch();
236                                     if(isset($attrs['memberUid']) && in_array($this->validateUid, $attrs['memberUid'])){
237                                         $found = TRUE;
238                                     }
239                                 }
240                                 break;
241                             }
242                         }
243                         $match = $found;
244                     }
246                     $class = "";
247                     if(!$match){
248                         $class = "acl-viewer-blocked";
249                     }
251                     if(!empty($filter)) $filter =sprintf($filter_tpl,$class,$filter);
253                     foreach($acl['members'] as $type => $name){
254                         if(preg_match("/^G/", $type))
255                             $gmem .= "\n        <li>".$name."</li>";
256                     }
257                     if(!empty($gmem)) $gmem =sprintf($gmem_tpl,$class,$gmem);
259                     foreach($acl['members'] as $type => $name){
260                         if(!preg_match("/^G/", $type))
261                             $umem .= "\n        <li>".$name."</li>";
262                     }
263                     if(!empty($umem)) $umem = sprintf($umem_tpl,$class,$umem);
265                     if($acl['type']!='reset'){
266                         foreach($acl['acl'] as $type => $acl){
267                             if(isset($this->classMapping[$type])){
268                                 $defs .= "<li>".$this->classMapping[$type].": ".$this->aclToString($acl)."</li>";
269                             }else{
270                                 $defs .= "<li>".$type.": ".$this->aclToString($acl)."</li>";
271                             }
272                         }
273                         if(!empty($defs)) $defs = sprintf($acl_tpl, $class,$defs);
274                     }
275                     $str.= sprintf($tpl,$class, image($image), $dn, $aclType, $filter.$gmem.$umem.$defs);
276                 }
277             }
278             $str .= "</table>"; 
279             $this->renderedList = $str;
280         }
281         return($this->renderedList);
282     }
284     function aclToString($acls)
285     {
286         $str ="<ul>";
287         foreach($acls as $name => $acl){
288             if($name == "0") $name = _("All");
289             $str .= "<li>".$name;
290             $str .= "<ul>";
291             if(preg_match("/s/", $acl)){
292                 $str.="<li>"._("Grant permission to owner").'</li>'; 
293             }
294             if(preg_match("/r/", $acl)) $str.="<li>"._("read").'</li>'; 
295             if(preg_match("/w/", $acl)) $str.="<li>"._("write").'</li>'; 
296             if(preg_match("/c/", $acl)) $str.="<li>"._("Create").'</li>'; 
297             if(preg_match("/d/", $acl)) $str.="<li>"._("Remove").'</li>'; 
298             if(preg_match("/m/", $acl)) $str.="<li>"._("Move").'</li>'; 
299             $str.= "</ul>";
300         }
301         return($str."</ul>");
302     }
304     function processAutocomplete()
305     {
306             $ldap = $this->config->get_ldap_link();
307             $ldap->cd($this->config->current['BASE']);
308             $ldap->search("(&(objectClass=gosaAccount)(|(sn=*".get_post('aclTarget')."*)".
309                     "(uid=*".get_post('aclTarget')."*)(givenName=*".get_post('aclTarget')."*)))",
310                     array('uid','dn','sn','givenName'));
311             echo "<ul>";
312             while($attrs = $ldap->fetch()){
313                 $display = $attrs['givenName'][0]." ".$attrs['sn'][0]." [".$attrs['uid'][0]."]";
314                 echo "<li>{$display}</li>";
315                 $this->userMap[$display] = $attrs;
316             }
317             echo "</ul>";
318     }
321 ?>