1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id: class_opsiLicenses.inc 13520 2009-03-09 14:54:13Z hickert $$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
24 class opsiLicens extends plugin {
26 // The variables this plugin takes care of.
27 var $cn = "";
28 var $description ="";
29 var $telephoneNumber = "";
30 var $facsimileTelephoneNumber = "";
32 // The objects base
33 var $base = "";
35 // Keep track if possible ng aming modifications
36 var $orig_dn = "";
37 var $orig_cn = "";
38 var $orig_base = "";
40 // The object classes written by this plugin
41 var $objectclasses = array("top","organizationalRole");
43 // The list of occupants ([dn])
44 var $licenseOccupant = array();
46 // The licenseOccupant cache, dn=>attrs
47 var $licenseOccCache = array();
49 // A list of attributes managed by this plugin
50 var $attributes = array("cn","description",
51 "telephoneNumber","facsimileTelephoneNumber","licenseOccupant");
54 /* Initialize the class
55 */
56 function __construct($config,$dn){
57 plugin::plugin($config,$dn);
58 $this->is_account = TRUE;
60 // Initialize list of occupants
61 $this->licenseOccupant = array();
62 if(isset($this->attrs['licenseOccupant'])){
63 for($i=0;$i<$this->attrs['licenseOccupant']['count']; $i++){
64 $this->licenseOccupant[] = $this->attrs['licenseOccupant'][$i];
65 }
66 }
68 // Detect the objects base
69 if ($this->dn == "new"){
70 $this->base = session::get('CurrentMainBase');
71 } else {
72 $this->base= preg_replace("/^[^,]+,".preg_quote(get_ou("licenseRDN"), '/i')."/","",$this->dn);
73 }
75 // Keep track of naming attribute modifications
76 $this->orig_base = $this->base;
77 $this->orig_dn = $dn;
78 $this->orig_cn = $this->cn;
80 // Reload the occupant cache.
81 $this->reload();
82 }
85 /* Keep occupant cache up to date.
86 * Else, we may have entries we can't display.
87 */
88 function reload()
89 {
90 // Entries can't be added twice.
91 $attrs = array("description", "objectClass", "uid","cn");
92 $this->licenseOccupant = array_unique($this->licenseOccupant);
93 $this->licenseOccupant = array_values($this->licenseOccupant);
95 $ldap = $this->config->get_ldap_link();
96 foreach($this->licenseOccupant as $dn){
97 if(!isset($this->licenseOccCache[$dn])){
98 if($ldap->dn_exists($dn)){
99 $ldap->cat($dn, $attrs);
101 $tmp = $ldap->fetch();
102 if(!isset($tmp['cn'])){
104 // Extract the namingAttribute out of the dn.
105 $cn = preg_replace("/^[^=]*+=([^,]*).*$/","\\1",$tmp['dn']);
106 if(isset($tmp['uid'])){
107 $cn = $tmp['uid'][0];
108 }
109 if(isset($tmp['description'])){
110 $cn.= " [".$tmp['description'][0]."]";
111 }
112 $tmp['cn'][0] = $cn;
113 }
115 $this->licenseOccCache[$dn] = $tmp;
116 }
117 }
118 }
120 }
123 function getOccupants(){
124 return($this->licenseOccupant);
125 }
127 /* Generate HTML output of this plugin.
128 */
129 function execute()
130 {
131 // Get list of possible ldap bases, will be selectable in the ui.
132 $tmp = $this->allowedBasesToMoveTo();
135 /***************
136 * Dialog handling
137 ***************/
139 if(isset($_POST['edit_membership']) && !$this->dialog instanceOf plugin){
140 $this->dialog = new occupantSelect($this->config,$this->dn,$this);
141 }
142 if(isset($_POST['delete_membership']) && !$this->dialog instanceOf plugin){
143 if(isset($_POST['members'])){
144 foreach($_POST['members'] as $id){
145 if(isset($this->licenseOccupant[$id])){
146 unset($this->licenseOccupant[$id]);
147 }
148 }
149 $this->reload();
150 }
151 }
153 if(isset($_POST['add_object_cancel']) && $this->dialog instanceOf plugin){
154 $this->dialog = NULL;
155 }
156 if(isset($_POST['add_object_finish']) && $this->dialog instanceOf plugin){
157 $ret = $this->dialog->save();
158 foreach($ret as $key => $entry){
159 $this->licenseOccupant[] = $entry['dn'];
160 $this->licenseOccCache[$entry['dn']] = $entry['attrs'];
161 }
162 $this->reload();
163 $this->dialog = NULL;
164 }
166 if($this->dialog instanceOf plugin){
167 $this->dialog->save_object();
168 return($this->dialog->execute());
169 }
172 /***************
173 * Template handling
174 ***************/
176 // Get smarty instance and assign required variables.
177 $smarty = get_smarty();
178 $smarty->assign("bases", $tmp);
179 $smarty->assign("base_select",$this->base);
180 $smarty->assign("members",$this->convert_list());
181 foreach($this->attributes as $attr){
182 $smarty->assign($attr,$this->$attr);
183 }
185 // Assign current permissions for each attribute.
186 $tmp = $this->plInfo();
187 foreach($tmp['plProvidedAcls'] as $attr => $desc){
188 $smarty->assign($attr."ACL",$this->getacl($attr));
189 }
190 return($smarty->fetch(get_template_path('licenseGeneric.tpl',TRUE,dirname(__FILE__))));
191 }
194 /* Check user input and return a list of 'invalid input' messages.
195 */
196 function check()
197 {
198 $message = plugin::check();
200 // Set the new acl base
201 if($this->dn == "new") {
202 $this->set_acl_base($this->base);
203 }
205 // Check if we are allowed to create/move this user
206 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
207 $message[]= msgPool::permCreate();
208 }elseif($this->orig_dn != "new" &&
209 !$this->acl_is_moveable($this->base) &&
210 ($this->orig_base != $this->base || $this->orig_cn != $this->cn )){
211 $message[]= msgPool::permMove();
212 }
214 /* must: cn */
215 if ($this->cn == ""){
216 $message[]= msgPool::required(_("Name"));
217 }
219 // Check if this name is uniq for licenses.
220 $ldap= $this->config->get_ldap_link();
221 $ldap->cd($this->config->current['BASE']);
222 $ldap->search("(&(objectClass=organizationalRole)(cn=$this->cn))", array("cn"));
223 $ldap->fetch();
224 if ($ldap->count() != 0 && ( $this->dn == 'new' || $this->cn != $this->orig_cn)){
225 $message[]= msgPool::duplicated(_("Name"));
226 }
228 return($message);
229 }
232 /* Returns list of occupants as <html><option> statements.
233 */
234 function convert_list()
235 {
236 $temp= "";
237 $icon = " style=\"background-image:url('plugins/generic/images/head.png');\" ";
238 foreach ($this->licenseOccupant as $key => $dn){
239 if(isset($this->licenseOccCache[$dn])){
240 $entry = $this->licenseOccCache[$dn];
241 $name = $entry['cn']['0'];
242 if(isset($entry['description'][0])){
243 $name .= " [".$entry['description'][0]."]";
244 }
245 }else{
246 $name = _("Unknown")." ".$dn;
247 }
248 $temp.= "<option {$icon} title='{$dn}' value='$key' class='select'>{$name}</option>\n";
249 }
250 return ($temp);
251 }
254 /* Removes the object from the ldap database
255 */
256 function remove_from_parent()
257 {
258 plugin::remove_from_parent();
260 // Remove this object.
261 $ldap= $this->config->get_ldap_link();
262 $ldap->rmdir($this->dn);
263 if (!$ldap->success()){
264 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
265 }
267 // Log action.
268 new log("remove","licenses/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
270 // Trigger remove signal
271 $this->handle_post_events("remove");
272 }
275 /* Saves object modifications
276 */
277 function save(){
278 plugin::save();
280 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
281 new entries. So do a check first... */
282 $ldap = $this->config->get_ldap_link();
283 $ldap->cat ($this->dn, array('dn'));
284 if ($ldap->fetch()){
285 $mode= "modify";
286 } else {
287 $mode= "add";
288 $ldap->cd($this->config->current['BASE']);
289 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
290 }
291 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,$this->attributes, "Save via $mode");
293 // Finally write data with selected 'mode'
294 $this->cleanup();
295 $ldap->cd ($this->dn);
296 $ldap->$mode ($this->attrs);
297 if (!$ldap->success()){
298 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(),
299 $this->dn, LDAP_MOD, get_class()));
300 return (1);
301 }
303 // Send modify/add events
304 $this->handle_post_events($mode);
306 // Update ACL dependencies too
307 if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
308 $tmp = new acl($this->config,$this->parent,$this->dn);
309 $tmp->update_acl_membership($this->orig_dn,$this->dn);
310 }
312 // Log action
313 if($mode == "modify"){
314 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
315 }else{
316 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
317 }
319 return 0;
320 }
323 /* This avoids that users move themselves out of their rights.
324 */
325 function allowedBasesToMoveTo()
326 {
327 $bases = $this->get_allowed_bases();
328 return($bases);
329 }
332 /* Save HTML inputs
333 */
334 function save_object()
335 {
336 plugin::save_object();
337 if(isset($_POST['base'])){
338 $this->base = get_post('base');
339 }
340 }
343 function PrepareForCopyPaste($source)
344 {
345 plugin::PrepareForCopyPaste($source);
347 /* Load member objects */
348 $this->licenseOccupant = array();
349 if (isset($source['licenseOccupant'])){
350 foreach ($source['licenseOccupant'] as $key => $value){
351 if ("$key" != "count"){
352 $value= @LDAP::convert($value);
353 $this->licenseOccupant["$value"]= "$value";
354 }
355 }
356 }
357 $this->reload();
358 }
361 function getCopyDialog()
362 {
363 $smarty = get_smarty();
364 $smarty->assign("cn", $this->cn);
365 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
366 $ret = array();
367 $ret['string'] = $str;
368 $ret['status'] = "";
369 return($ret);
370 }
372 function saveCopyDialog()
373 {
374 if(isset($_POST['cn'])){
375 $this->cn = $_POST['cn'];
376 }
377 }
380 static function plInfo()
381 {
382 return (array(
383 "plShortName" => _("Generic"),
384 "plDescription" => _("Role generic"),
385 "plSelfModify" => FALSE,
386 "plDepends" => array(),
387 "plPriority" => 1,
388 "plSection" => array("administration"),
389 "plCategory" => array("licenses" => array("description" => _("Licenses"),
390 "objectClass" => "organizationalRole")),
391 "plProvidedAcls"=> array(
392 "cn" => _("Name"),
393 "description" => _("Description"),
394 "base" => _("Base"),
395 "telephoneNumber" => _("Telefon number"),
396 "facsimileTelephoneNumber" => _("Fax number"),
397 "licenseOccupant" => _("Occupants"))
398 ));
399 }
400 }
403 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
404 ?>