Code

Added user selection
[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();
36     function __construct(&$config, $dn)
37     {
38         plugin::plugin($config, $dn);
39         $this->base = $this->config->current['BASE'];
40         $this->orig_dn = $this->dn;
41     }
44     function execute()
45     {
46         plugin::execute();    
48         if($this->dialog instanceOf singleUserSelect) return($this->dialog->execute());
51         $smarty = get_smarty();
53         // Assign ACL settings 
54         $plInfo = $this->plInfo();
55         foreach($plInfo['plProvidedAcls'] as $name => $desc){
56             $smarty->assign("{$name}ACL", $this->getacl($name));
57         }
58     
59         // Assign just user names instead of the complete dn.
60         $smarty->assign("owner_name", $this->getUserName($this->owner));
61         $smarty->assign("manager_name", $this->getUserName($this->manager));
63         foreach($this->attributes as $attr){
64             $smarty->assign($attr, $this->$attr);
65         }
66         return($smarty->fetch(get_template_path('goto/Device/Device.tpl', TRUE)));
67     }
69    
70     /*! \brief  Detect an object's name by querying the ldap
71      *           for the object's cn.
72      * @param   String  The object's dn to query for.
73      */ 
74     function getUserName($dn)
75     {   
76         // First asked the cache if we've already queried this name.
77         if(isset($this->resolvedNamesCache[$dn])) {
78             return($this->resolvedNamesCache[$dn]);
79         }
81         // Try to detect the object's name via ldap search.
82         $ldap = $this->config->get_ldap_link();
83         $this->resolvedNamesCache[$dn] = "";
84         if(!empty($dn)){
85             $ldap->cat($dn, array('cn'));
86             if($ldap->count()){
87                 $attrs = $ldap->fetch();
88                 $this->resolvedNamesCache[$dn] = $attrs['cn'][0];
89                 return( $attrs['cn'][0]);
90             }else{
91                 $this->resolvedNamesCache[$dn] = "("._("unknown")."!): ".$dn;
92             }
93         }
94         return($this->resolvedNamesCache[$dn]);
95     }   
96     
97  
98     function save_object()
99     {
100         plugin::save_object();
102         if(isset($_POST['editOwner'])){
103             $this->currentUserSelect = "owner";
104             $this->dialog = new singleUserSelect($this->config, get_userinfo());
105         }
106         if(isset($_POST['editManager'])){
107             $this->currentUserSelect = "manager";
108             $this->dialog = new singleUserSelect($this->config, get_userinfo());
109         }
110         if($this->dialog && $this->dialog instanceOf singleUserSelect && count($this->dialog->detectPostActions())){
111             $users = $this->dialog->detectPostActions();
112             if(isset($users['action']) && $users['action'] == 'userSelected' && isset($users['targets']) && count($users['targets'])){
113                 $headpage = $this->dialog->getHeadpage();
114                 $dn = $users['targets'][0];
115                 $attr = $this->currentUserSelect;
116                 $this->$attr = $dn;
117                 $this->dialog = NULL;
118             }
119         }
120         if(isset($_POST['add_users_cancel'])){
121             $this->dialog = NULL;
122         }
123         
124     }
127     function save()
128     {
129         plugin::save();
131         // Append and remove dynmic object classes
132         foreach($this->dynClasses as $oc => $attrs){
133             $this->attrs['objectClass'] = array_remove_entries(array($oc), $this->attrs['objectClass']);
134             foreach($attrs as $attr){
135                 if(isset($this->attrs[$attr]) && !empty($this->attrs[$attr])){
136                     $this->attrs['objectClass'][] = $oc;
137                     break;
138                 }
139             }
140         }
142         $this->cleanup();
143         $ldap=$this->config->get_ldap_link();
144         $ldap->cd($this->config->current['BASE']);
145         $ldap->cd($this->dn);
147         if($this->initially_was_account){
148             $ldap->modify($this->attrs);
149         }else{
150             $ldap->add($this->attrs);
151         }
152         echo $ldap->get_error();
153     }
156     static function plInfo()
157     {
158         return (array(
159                     "plShortName"   => _("Device"),
160                     "plDescription" => _("Device"),
161                     "plSelfModify"  => FALSE,
162                     "plDepends"     => array(),
163                     "plPriority"    => 1,
164                     "plSection"     => array("administration"),
165                     "plCategory"    => array(
166                         "Device" => array( 
167                             "description"  => _("Device"),
168                             "objectClass"  => "Device")),
169                     "plProvidedAcls" => array(
170                         "cn" => _("Name"),
171                         "serialNumber" => _("Serial number"),
172                         "seeAlso" => _("See also"),
173                         "owner" => _("Owner"),
174                         "ou" => _("Organizational unit"),
175                         "o" => _("Organization"),
176                         "l" => _("Location"),
177                         "description" => _("Description"),
178                         "manager" => _("Manager"),
179                         "deviceUUID" => _("Uuid"),
180                         "deviceStatus" => _("Stauts"),
181                         "macAddress" => _("MAC address"),
182                         "ipHostNumber" => _("IP address")
183                         )
184                         )
185                         );
186     }
189 ?>