Code

ccbc6a7a75d29f133657080e52091aad6245f133
[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->base = $this->config->current['BASE'];
44         $this->orig_dn = $this->dn;
45     }
47     
48     /*! \brief  Generate a fake uuid, it is not a real uuid.
49      *  @return String  A fake uuid.
50      */
51     function genFakeUuid()
52     {
53         $strfmt = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
54         for($i=0;$i<strlen($strfmt);$i++)
55             if($strfmt[$i] == 'x') $strfmt[$i] = dechex(rand(0,15));
56         return($strfmt);
57     }
60     /*! \brief  Generate the plugins HTML content.
61      *  @return String  HTML content
62      */
63     function execute()
64     {
65         plugin::execute();    
67         if($this->dialog instanceOf singleUserSelect) return($this->dialog->execute());
69         $smarty = get_smarty();
71         // Assign ACL settings 
72         $plInfo = $this->plInfo();
73         foreach($plInfo['plProvidedAcls'] as $name => $desc){
74             $smarty->assign("{$name}ACL", $this->getacl($name));
75         }
76     
77         // Assign just user names instead of the complete dn.
78         $smarty->assign("owner_name", $this->getUserName($this->owner));
79         $smarty->assign("manager_name", $this->getUserName($this->manager));
81         foreach($this->attributes as $attr){
82             $smarty->assign($attr, $this->$attr);
83         }
84         return($smarty->fetch(get_template_path('goto/Device/Device.tpl', TRUE)));
85     }
88     /*! \brief  Validate the user input.
89      */ 
90     function check()
91     {   
92         $message = plugin::check();
93        
94         // If one of the registered user attributes is set, 
95         //  the others have to be set too.
96         $str = $this->manager.$this->deviceUUID.$this->deviceStatus;
97         if(!empty($str)){        
98             foreach($this->dynClasses['registeredDevice'] as $attr){
99                 if(empty($this->$attr)){
100                     $message[] = "Please set all values for 'registered device' or leave all blank! {$attr}";
101                     break;
102                 }
103             }
104         }
106         // Check ip-address
107         if (!empty($this->ipHostNumber) && !tests::is_ip($this->ipHostNumber)){
108             $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
109         }
111         // Check mac-address
112         if (!empty($this->ipHostNumber) && !tests::is_ip($this->macAddress)){
113             $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
114         }
115  
116         return($message);
117     }
119    
120     /*! \brief  Detect an object's name by querying the ldap
121      *           for the object's cn.
122      * @param   String  The object's dn to query for.
123      */ 
124     function getUserName($dn)
125     {   
126         // First asked the cache if we've already queried this name.
127         if(isset($this->resolvedNamesCache[$dn])) {
128             return($this->resolvedNamesCache[$dn]);
129         }
131         // Try to detect the object's name via ldap search.
132         $ldap = $this->config->get_ldap_link();
133         $this->resolvedNamesCache[$dn] = "";
134         if(!empty($dn)){
135             $ldap->cat($dn, array('cn'));
136             if($ldap->count()){
137                 $attrs = $ldap->fetch();
138                 $this->resolvedNamesCache[$dn] = $attrs['cn'][0];
139                 return( $attrs['cn'][0]);
140             }else{
141                 $this->resolvedNamesCache[$dn] = "("._("unknown")."!): ".$dn;
142             }
143         }
144         return($this->resolvedNamesCache[$dn]);
145     }   
146     
147  
148     /*! \brief  Detects and stores relevant values which where
149      *           currently transmitted via $_GET/$_POST.
150      */ 
151     function save_object()
152     {
153         plugin::save_object();
155         // Change owner requested, initiate a user selection dialog.
156         if(isset($_POST['editOwner'])){
157             $this->currentUserSelect = "owner";
158             $this->dialog = new singleUserSelect($this->config, get_userinfo());
159         }
161         // Change manager requested, initiate a user selection dialog.
162         if(isset($_POST['editManager'])){
163             $this->currentUserSelect = "manager";
164             $this->dialog = new singleUserSelect($this->config, get_userinfo());
165         }
167         // The user selection dialog has send that it has finished its job.
168         // Store the posted user-dn as manager or owner. 
169         if($this->dialog && $this->dialog instanceOf singleUserSelect && count($this->dialog->detectPostActions())){
170             $users = $this->dialog->detectPostActions();
171             if(isset($users['action']) && $users['action'] == 'userSelected' && isset($users['targets']) && count($users['targets'])){
172                 $headpage = $this->dialog->getHeadpage();
173                 $dn = $users['targets'][0];
174                 $attr = $this->currentUserSelect;
175                 $this->$attr = $dn;
176                 $this->dialog = NULL;
177             }
178         }
179         
180         // User selection canceled
181         if(isset($_POST['add_users_cancel'])){
182             $this->dialog = NULL;
183         }
184       
185         // Regenerate a new uuid 
186         if(isset($_POST['reloadUUID']))  $this->deviceUUID = $this->genFakeUuid();
187     }
190     /*! \brief  Save the modified object back to the ldap.
191      */ 
192     function save()
193     {
194         plugin::save();
196         // Append and remove dynmic object classes
197         foreach($this->dynClasses as $oc => $attrs){
198             $this->attrs['objectClass'] = array_remove_entries(array($oc), $this->attrs['objectClass']);
199             foreach($attrs as $attr){
200                 if(isset($this->attrs[$attr]) && !empty($this->attrs[$attr])){
201                     $this->attrs['objectClass'][] = $oc;
202                     break;
203                 }
204             }
205         }
207         $this->cleanup();
208         $ldap=$this->config->get_ldap_link();
209         $ldap->cd($this->config->current['BASE']);
210         $ldap->cd($this->dn);
212         // Perform action modify/create
213         if($this->initially_was_account){
214             $ldap->modify($this->attrs);
215             new log("modify","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
216             if (!$ldap->success()){
217                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
218             }else{
219                 $this->handle_post_events("modify");
220             }
221         }else{
222             $ldap->add($this->attrs);
223             new log("create","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
224             if (!$ldap->success()){
225                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_ADD, get_class()));
226             }else{
227                 $this->handle_post_events("add");
228             }
229         }
230     }
233     static function plInfo()
234     {
235         return (array(
236                     "plShortName"   => _("Device"),
237                     "plDescription" => _("Device"),
238                     "plSelfModify"  => FALSE,
239                     "plDepends"     => array(),
240                     "plPriority"    => 1,
241                     "plSection"     => array("administration"),
242                     "plCategory"    => array(
243                         "Device" => array( 
244                             "description"  => _("Device"),
245                             "objectClass"  => "Device")),
246                     "plProvidedAcls" => array(
247                         "cn" => _("Name"),
248                         "serialNumber" => _("Serial number"),
249                         "seeAlso" => _("See also"),
250                         "owner" => _("Owner"),
251                         "ou" => _("Organizational unit"),
252                         "o" => _("Organization"),
253                         "l" => _("Location"),
254                         "description" => _("Description"),
255                         "manager" => _("Manager"),
256                         "deviceUUID" => _("Uuid"),
257                         "deviceStatus" => _("Status"),
258                         "deviceType" => _("Type"),
259                         "macAddress" => _("MAC address"),
260                         "ipHostNumber" => _("IP address")
261                         )
262                         )
263                         );
264     }
267 ?>