Code

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