Code

Updated the device templates
[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 if there a cn given
112         if (empty($this->cn)){
113             $message[]= msgPool::required(_("Name"));
114         }
116         // Check mac-address
117         if (!empty($this->ipHostNumber) && !tests::is_ip($this->macAddress)){
118             $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
119         }
120  
121         return($message);
122     }
124    
125     /*! \brief  Detect an object's name by querying the ldap
126      *           for the object's cn.
127      * @param   String  The object's dn to query for.
128      */ 
129     function getUserName($dn)
130     {   
131         // First asked the cache if we've already queried this name.
132         if(isset($this->resolvedNamesCache[$dn])) {
133             return($this->resolvedNamesCache[$dn]);
134         }
136         // Try to detect the object's name via ldap search.
137         $ldap = $this->config->get_ldap_link();
138         $this->resolvedNamesCache[$dn] = "";
139         if(!empty($dn)){
140             $ldap->cat($dn, array('cn'));
141             if($ldap->count()){
142                 $attrs = $ldap->fetch();
143                 $this->resolvedNamesCache[$dn] = $attrs['cn'][0];
144                 return( $attrs['cn'][0]);
145             }else{
146                 $this->resolvedNamesCache[$dn] = "("._("unknown")."!): ".$dn;
147             }
148         }
149         return($this->resolvedNamesCache[$dn]);
150     }   
151     
152  
153     /*! \brief  Detects and stores relevant values which where
154      *           currently transmitted via $_GET/$_POST.
155      */ 
156     function save_object()
157     {
158         plugin::save_object();
160         // Change owner requested, initiate a user selection dialog.
161         if(isset($_POST['editOwner'])){
162             $this->currentUserSelect = "owner";
163             $this->dialog = new singleUserSelect($this->config, get_userinfo());
164         }
166         // Change manager requested, initiate a user selection dialog.
167         if(isset($_POST['editManager'])){
168             $this->currentUserSelect = "manager";
169             $this->dialog = new singleUserSelect($this->config, get_userinfo());
170         }
172         // Remove owner initiated 
173         if(isset($_POST['removeManager']))  $this->manager = "";
174         if(isset($_POST['removeOwner']))  $this->owner = "";
176         // The user selection dialog has send that it has finished its job.
177         // Store the posted user-dn as manager or owner. 
178         if($this->dialog && $this->dialog instanceOf singleUserSelect && count($this->dialog->detectPostActions())){
179             $users = $this->dialog->detectPostActions();
180             if(isset($users['action']) && $users['action'] == 'userSelected' && isset($users['targets']) && count($users['targets'])){
181                 $headpage = $this->dialog->getHeadpage();
182                 $dn = $users['targets'][0];
183                 $attr = $this->currentUserSelect;
184                 $this->$attr = $dn;
185                 $this->dialog = NULL;
186             }
187         }
188         
189         // User selection canceled
190         if(isset($_POST['add_users_cancel'])){
191             $this->dialog = NULL;
192         }
193       
194         // Regenerate a new uuid 
195         if(isset($_POST['reloadUUID']))  $this->deviceUUID = $this->genFakeUuid();
196     }
199     /*! \brief  Save the modified object back to the ldap.
200      */ 
201     function save()
202     {
203         plugin::save();
205         // Append and remove dynmic object classes
206         foreach($this->dynClasses as $oc => $attrs){
207             $this->attrs['objectClass'] = array_remove_entries(array($oc), $this->attrs['objectClass']);
208             foreach($attrs as $attr){
209                 if(isset($this->attrs[$attr]) && !empty($this->attrs[$attr])){
210                     $this->attrs['objectClass'][] = $oc;
211                     break;
212                 }
213             }
214         }
216         $this->cleanup();
217         $ldap=$this->config->get_ldap_link();
218         $ldap->cd($this->config->current['BASE']);
219         $ldap->cd($this->dn);
221         // Perform action modify/create
222         if($this->initially_was_account){
223             $ldap->modify($this->attrs);
224             new log("modify","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
225             if (!$ldap->success()){
226                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
227             }else{
228                 $this->handle_post_events("modify");
229             }
230         }else{
231             $ldap->add($this->attrs);
232             new log("create","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
233             if (!$ldap->success()){
234                 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_ADD, get_class()));
235             }else{
236                 $this->handle_post_events("add");
237             }
238         }
239     }
242     static function plInfo()
243     {
244         return (array(
245                     "plShortName"   => _("Device"),
246                     "plDescription" => _("Device"),
247                     "plSelfModify"  => FALSE,
248                     "plDepends"     => array(),
249                     "plPriority"    => 1,
250                     "plSection"     => array("administration"),
251                     "plCategory"    => array(
252                         "Device" => array( 
253                             "description"  => _("Device"),
254                             "objectClass"  => "Device")),
255                     "plProvidedAcls" => array(
256                         "cn" => _("Name"),
257                         "serialNumber" => _("Serial number"),
258                         "seeAlso" => _("See also"),
259                         "owner" => _("Owner"),
260                         "ou" => _("Organizational unit"),
261                         "o" => _("Organization"),
262                         "l" => _("Location"),
263                         "description" => _("Description"),
264                         "manager" => _("Manager"),
265                         "deviceUUID" => _("Uuid"),
266                         "deviceStatus" => _("Status"),
267                         "deviceType" => _("Type"),
268                         "macAddress" => _("MAC address"),
269                         "ipHostNumber" => _("IP address")
270                         )
271                         )
272                         );
273     }
276 ?>