Code

7995b8b7c509fbfc73cf0fbf76adbf20e738c4b2
[gosa.git] / gosa-plugins / goto / admin / systems / goto / Device / class_Device.inc
1 <?php
4 class Device extends plugin
5 {
7     public $objectclasses = array('top','device');
8     public $attributes = array("cn","serialNumber","seeAlso","owner","ou","o","ipHostNumber",
9             "l","description","manager","deviceUUID","deviceStatus","macAddress","deviceType");
11     public $dynClasses = array(
12             'ieee802Device' => array('macAddress'),
13             'registeredDevice' => array('deviceStatus','deviceUUID','manager','deviceType'),
14             'ipHost' => array('ipHostNumber'));
16     public $cn = "";
17     public $serialNumber = "";
18     public $seeAlso = "";
19     public $owner = "";
20     public $ou = "";
21     public $o = "";
22     public $l = "";
23     public $description = "";
24     public $manager = "";
25     public $deviceUUID = "";
26     public $deviceStatus = "";
27     public $deviceType = "";
28     public $ipHostNumber = "";
29     public $macAddress = "";
31     public $base = "";
32     public $orig_dn ="";
33     
34     private $resolvedNamesCache = array();
37     /*! \brief  Constructs the device object and keep some 
38      *           initial values.
39      */
40     function __construct(&$config, $dn)
41     {
42         plugin::plugin($config, $dn);
43         $this->orig_dn = $this->dn;
45         // Initialize the object base 
46         if ($this->dn == "new"){
47             $ui= get_userinfo();
48             $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
49         } else {
50             $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("Device", "DeviceRDN"), '/')."/i", "", $this->dn);
51         }
53         // Prepare the base selector
54         $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
55         $this->baseSelector->setSubmitButton(false);
56         $this->baseSelector->setHeight(300);
57         $this->baseSelector->update(true);
58     }
60     
61     /*! \brief  Generate a fake uuid, it is not a real uuid.
62      *  @return String  A fake uuid.
63      */
64     function genFakeUuid()
65     {
66         $strfmt = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
67         for($i=0;$i<strlen($strfmt);$i++)
68             if($strfmt[$i] == 'x') $strfmt[$i] = dechex(rand(0,15));
69         return($strfmt);
70     }
73     /*! \brief  Generate the plugins HTML content.
74      *  @return String  HTML content
75      */
76     function execute()
77     {
78         plugin::execute();    
80         if($this->dialog instanceOf singleUserSelect) return($this->dialog->execute());
82         $smarty = get_smarty();
84         // Assign ACL settings 
85         $plInfo = $this->plInfo();
86         foreach($plInfo['plProvidedAcls'] as $name => $desc){
87             $smarty->assign("{$name}ACL", $this->getacl($name));
88         }
89     
90         // Assign just user names instead of the complete dn.
91         $smarty->assign("owner_name", $this->getUserName($this->owner));
92         $smarty->assign("manager_name", $this->getUserName($this->manager));
93         $smarty->assign("base", $this->baseSelector->render());
95         // Assign attribute values 
96         foreach($this->attributes as $attr){
97             $smarty->assign($attr, $this->$attr);
98         }
99         return($smarty->fetch(get_template_path('goto/Device/Device.tpl', TRUE)));
100     }
103     /*! \brief  Validate the user input.
104      */ 
105     function check()
106     {   
107         $message = plugin::check();
108        
109         // If one of the registered user attributes is set, 
110         //  the others have to be set too.
111         $str = $this->manager.$this->deviceUUID.$this->deviceStatus;
112         if(!empty($str)){        
113             foreach($this->dynClasses['registeredDevice'] as $attr){
114                 if(empty($this->$attr)){
115                     $message[] = "Please set all values for 'registered device' or leave all blank! {$attr}";
116                     break;
117                 }
118             }
119         }
121         // Check ip-address
122         if (!empty($this->ipHostNumber) && !tests::is_ip($this->ipHostNumber)){
123             $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
124         }
126         // Check if there a cn given
127         if (empty($this->cn)){
128             $message[]= msgPool::required(_("Name"));
129         }
131         // Check mac-address
132         if (!empty($this->ipHostNumber) && !tests::is_ip($this->macAddress)){
133             $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
134         }
135  
136         return($message);
137     }
139    
140     /*! \brief  Detect an object's name by querying the ldap
141      *           for the object's cn.
142      * @param   String  The object's dn to query for.
143      */ 
144     function getUserName($dn)
145     {   
146         // First asked the cache if we've already queried this name.
147         if(isset($this->resolvedNamesCache[$dn])) {
148             return($this->resolvedNamesCache[$dn]);
149         }
151         // Try to detect the object's name via ldap search.
152         $ldap = $this->config->get_ldap_link();
153         $this->resolvedNamesCache[$dn] = "";
154         if(!empty($dn)){
155             $ldap->cat($dn, array('cn'));
156             if($ldap->count()){
157                 $attrs = $ldap->fetch();
158                 $this->resolvedNamesCache[$dn] = $attrs['cn'][0];
159                 return( $attrs['cn'][0]);
160             }else{
161                 $this->resolvedNamesCache[$dn] = "("._("unknown")."!): ".$dn;
162             }
163         }
164         return($this->resolvedNamesCache[$dn]);
165     }   
166     
167  
168     /*! \brief  Detects and stores relevant values which where
169      *           currently transmitted via $_GET/$_POST.
170      */ 
171     function save_object()
172     {
173         plugin::save_object();
175         // Change owner requested, initiate a user selection dialog.
176         if(isset($_POST['editOwner'])){
177             $this->currentUserSelect = "owner";
178             $this->dialog = new singleUserSelect($this->config, get_userinfo());
179         }
181         // Change manager requested, initiate a user selection dialog.
182         if(isset($_POST['editManager'])){
183             $this->currentUserSelect = "manager";
184             $this->dialog = new singleUserSelect($this->config, get_userinfo());
185         }
187         // Remove owner initiated 
188         if(isset($_POST['removeManager']))  $this->manager = "";
189         if(isset($_POST['removeOwner']))  $this->owner = "";
191         // The user selection dialog has send that it has finished its job.
192         // Store the posted user-dn as manager or owner. 
193         if($this->dialog && $this->dialog instanceOf singleUserSelect && count($this->dialog->detectPostActions())){
194             $users = $this->dialog->detectPostActions();
195             if(isset($users['action']) && $users['action'] == 'userSelected' && isset($users['targets']) && count($users['targets'])){
196                 $headpage = $this->dialog->getHeadpage();
197                 $dn = $users['targets'][0];
198                 $attr = $this->currentUserSelect;
199                 $this->$attr = $dn;
200                 $this->dialog = NULL;
201             }
202         }
203         
204         // User selection canceled
205         if(isset($_POST['add_users_cancel'])){
206             $this->dialog = NULL;
207         }
208       
209         // Regenerate a new uuid 
210         if(isset($_POST['reloadUUID']))  $this->deviceUUID = $this->genFakeUuid();
212         // Update the base 
213         if ($this->acl_is_moveable($this->base)){
214             if (!$this->baseSelector->update()) {
215                 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
216             }
217             if ($this->base != $this->baseSelector->getBase()) {
218                 $this->base= $this->baseSelector->getBase();
219                 $this->is_modified= TRUE;
220             }
221         }
223     }
226     /*! \brief  Save the modified object back to the ldap.
227      */ 
228     function save()
229     {
230         plugin::save();
232         // Append and remove dynmic object classes
233         foreach($this->dynClasses as $oc => $attrs){
234             $this->attrs['objectClass'] = array_remove_entries(array($oc), $this->attrs['objectClass']);
235             foreach($attrs as $attr){
236                 if(isset($this->attrs[$attr]) && !empty($this->attrs[$attr])){
237                     $this->attrs['objectClass'][] = $oc;
238                     break;
239                 }
240             }
241         }
243         $this->cleanup();
244         $ldap=$this->config->get_ldap_link();
245         $ldap->cd($this->config->current['BASE']);
246         $ldap->cd($this->dn);
248         // Perform action modify/create
249         if($this->initially_was_account){
250             $ldap->modify($this->attrs);
251             new log("modify","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
252             if (!$ldap->success()){
253                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
254             }else{
255                 $this->handle_post_events("modify");
256             }
257         }else{
258             $ldap->add($this->attrs);
259             new log("create","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
260             if (!$ldap->success()){
261                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_ADD, get_class()));
262             }else{
263                 $this->handle_post_events("add");
264             }
265         }
266     }
269     static function plInfo()
270     {
271         return (array(
272                     "plShortName"   => _("Device"),
273                     "plDescription" => _("Device"),
274                     "plSelfModify"  => FALSE,
275                     "plDepends"     => array(),
276                     "plPriority"    => 1,
277                     "plSection"     => array("administration"),
278                     "plCategory"    => 
279                     array(
280                         "Device" => array( 
281                             "description"  => _("Device"),
282                             "objectClass"  => "Device")
283                         ),
284                     "plProvidedAcls" => 
285                     array(
286                         "base" => _("Base"),
287                         "cn" => _("Name"),
288                         "serialNumber" => _("Serial number"),
289                         "seeAlso" => _("See also"),
290                         "owner" => _("Owner"),
291                         "ou" => _("Organizational unit"),
292                         "o" => _("Organization"),
293                         "l" => _("Location"),
294                         "description" => _("Description"),
295                         "manager" => _("Manager"),
296                         "deviceUUID" => _("Uuid"),
297                         "deviceStatus" => _("Status"),
298                         "deviceType" => _("Type"),
299                         "macAddress" => _("MAC address"),
300                         "ipHostNumber" => _("IP address")
301                         ),
302             "plProperties" =>
303                 array(
304                         array(
305                             "name"          => "DeviceRDN",
306                             "type"          => "rdn",
307                             "default"       => "ou=devices,ou=systems,",
308                             "description"   => _("The 'deviceRDN' statement defines the location where new devices will be created. The   default is 'ou=device,ou=systems,'."),
309                             "check"         => "gosaProperty::isRdn",
310                             "migrate"       => "migrate_deviceRDN",
311                             "group"         => "plugin",
312                             "mandatory"     => FALSE
313                             ),
315                      )
316                 )
317                 );
318     }
321 ?>