| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <html lang="en"><head><title>Module HowTo - Account pages</title> |
| 3 | |
| 4 | |
| 5 | |
| 6 | <link rel="stylesheet" type="text/css" href="style/layout.css"><link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico"></head><body> |
| 7 | <div style="text-align: center;"> |
| 8 | <h1>Module HowTo - Account pages<br> |
| 9 | </h1> |
| 10 | <br> |
| 11 | <br> |
| 12 | <div style="text-align: left;"><br> |
| 13 | <h2>1. Loading the LDAP attributes<br> |
| 14 | </h2> |
| 15 | Every time the user selects an existing account to modify LAM will load |
| 16 | the complete LDAP entry of it. Your module then should select the |
| 17 | attributes which are useful for it.<br> |
| 18 | There are two variables in <span style="font-style: italic;">baseModule</span> |
| 19 | which should be used to store the attributes. The <span style="font-weight: bold;">$attributes</span> variable stores the |
| 20 | current attributes including changes the user made. The <span style="font-weight: bold;">$orig</span> variable stores the attributes |
| 21 | as they were originally when the account was loaded. This allows you to |
| 22 | see what changes were made.<br> |
| 23 | <br> |
| 24 | The <span style="font-weight: bold;">load_attributes()</span> function |
| 25 | in your module gets the complete attribute list from LDAP.<br> |
| 26 | In most cases you will not need to implement this function because the |
| 27 | parent class baseModule loads attributes based on your meta data.<br> |
| 28 | <br> |
| 29 | <span style="font-weight: bold; text-decoration: underline;">Example:</span><br style="font-weight: bold; text-decoration: underline;"> |
| 30 | <br> |
| 31 | The <span style="font-style: italic;">ieee802Device</span> uses an |
| 32 | object class and the <span style="font-style: italic;">'macAddress'</span> |
| 33 | attribute. Therefore we will save these two values.<br> |
| 34 | <br> |
| 35 | <div class="mod-code"> |
| 36 | * This function loads all needed attributes into the |
| 37 | object.<br> |
| 38 | *<br> |
| 39 | * @param array $attr an array as it is returned from |
| 40 | ldap_get_attributes<br> |
| 41 | */<br> |
| 42 | <span style="font-weight: bold;">function</span> <span style="color: rgb(255, 0, 0);">load_attributes</span>($attr) {<br> |
| 43 | |
| 44 | $this->attributes['objectClass'] = array();<br> |
| 45 | |
| 46 | $this->attributes['macAddress'] = array();<br> |
| 47 | $this->orig['objectClass'] = |
| 48 | array();<br> |
| 49 | $this->orig['macAddress'] = |
| 50 | array();<br> |
| 51 | if (isset($attr['objectClass'])) {<br> |
| 52 | |
| 53 | $this->attributes['objectClass'] = $attr['objectClass'];<br> |
| 54 | |
| 55 | $this->orig['objectClass'] = $attr['objectClass'];<br> |
| 56 | }<br> |
| 57 | if (isset($attr['macAddress'])) {<br> |
| 58 | |
| 59 | $this->attributes['macAddress'] = $attr['macAddress'];<br> |
| 60 | |
| 61 | $this->orig['macAddress'] = $attr['macAddress'];<br> |
| 62 | }<br> |
| 63 | return 0;<br> |
| 64 | }<br> |
| 65 | </div> |
| 66 | <br> |
| 67 | <br> |
| 68 | <h2>2. Page display</h2> |
| 69 | Now that you have defined your subpages you will need one function for |
| 70 | each page to display it. The function must return <span style="font-style: italic;">meta HTML code</span> as defined in the <span style="font-style: italic;">modules specification</span>.<br> |
| 71 | This function is called <span style="font-weight: bold;">display_html_<page |
| 72 | name>()</span> where <span style="font-style: italic;"><page |
| 73 | name></span> is the name of your subpage.<br> |
| 74 | <br> |
| 75 | See also baseModule::addSimpleInputTextField() and |
| 76 | baseModule::addMultiValueInputTextField()/processMultiValueInputTextField() |
| 77 | if you only want to add some simple text fields.<br> |
| 78 | <br> |
| 79 | <span style="font-weight: bold; text-decoration: underline;">Example:</span><br style="font-weight: bold; text-decoration: underline;"> |
| 80 | <br> |
| 81 | The |
| 82 | <span style="font-style: italic;">ieee802Device</span> |
| 83 | module has only one subpage called <span style="font-style: italic;">'attributes'</span>.<br> |
| 84 | <br> |
| 85 | The first half of the code displays the existing MAC addresses and the |
| 86 | second an input field for new values.<br> |
| 87 | The variable <span style="font-style: italic;">$this->attributes</span> |
| 88 | contains the LDAP attributes which are useful for this module.<br> |
| 89 | <br> |
| 90 | <div class="mod-code"> |
| 91 | * This function will create the meta HTML code to |
| 92 | show a page with all attributes.<br> |
| 93 | *<br> |
| 94 | * @return htmlElement HTML meta data<br> |
| 95 | */<br> |
| 96 | <span style="font-weight: bold;">function</span> <span style="color: rgb(255, 0, 0);">display_html_attributes</span>() {<br> $return = new htmlTable();<br> |
| 97 | $macCount = 0;<br> |
| 98 | // list current MACs<br> |
| 99 | if (isset($this->attributes['macAddress'])) {<br> |
| 100 | $macCount = sizeof($this->attributes['macAddress']);<br> |
| 101 | for ($i = 0; |
| 102 | $i < sizeof($this->attributes['macAddress']); $i++) {<br> |
| 103 | |
| 104 | $return->addElement(new htmlOutputText(_('MAC |
| 105 | address')));<br> |
| 106 | |
| 107 | $macInput = new htmlInputField('macAddress' . $i, |
| 108 | $this->attributes['macAddress'][$i]);<br> |
| 109 | $macInput->setFieldSize(17);<br> |
| 110 | $macInput->setFieldMaxLength(17);<br> |
| 111 | $return->addElement($macInput);<br> |
| 112 | |
| 113 | $return->addElement(new htmlButton('delMAC' . $i, |
| 114 | 'del.png', true));<br> |
| 115 | |
| 116 | $return->addElement(new htmlHelpLink('mac'), |
| 117 | true);<br> |
| 118 | }<br> |
| 119 | }<br> |
| 120 | // input box for new MAC<br> |
| 121 | $return->addElement(new htmlOutputText(_('New MAC address')));<br> |
| 122 | $newMacInput = new htmlInputField('macAddress', '');<br> |
| 123 | $newMacInput->setFieldSize(17);<br> |
| 124 | $newMacInput->setFieldMaxLength(17);<br> |
| 125 | $return->addElement($newMacInput);<br> |
| 126 | $return->addElement(new htmlButton('addMAC', 'add.png', true));<br> |
| 127 | $return->addElement(new htmlHelpLink('mac'));<br> |
| 128 | $return->addElement(new htmlHiddenInput('mac_number', $macCount));<br> |
| 129 | return $return;<br> |
| 130 | }<br> |
| 131 | </div> |
| 132 | <br> |
| 133 | <br> |
| 134 | <h2>3. Processing input data<br> |
| 135 | </h2> |
| 136 | Every time the user clicks on a submit button while your page is |
| 137 | displayed LAM will call a function in your module.<br> |
| 138 | This function is called <span style="font-weight: bold;">process_<page |
| 139 | name>()</span> where <span style="font-style: italic;"><page |
| 140 | name></span> is the name of your subpage.<br> |
| 141 | <br> |
| 142 | If all input data is ok then return an empty array. If you return one or more error messages then the user will be |
| 143 | redirected to your page.<br> |
| 144 | <br> |
| 145 | <span style="font-weight: bold; text-decoration: underline;">Example:</span><br style="font-weight: bold; text-decoration: underline;"> |
| 146 | <br> |
| 147 | The |
| 148 | <span style="font-style: italic;">ieee802Device</span> |
| 149 | module has only one subpage called <span style="font-style: italic;">'attributes'</span> |
| 150 | and therefore only <span style="font-style: italic;">process_attributes()</span>.<br> |
| 151 | <br> |
| 152 | The function checks the input fields and fills the LDAP attributes. If |
| 153 | all is ok it will enable the user to move to another module page.<br> |
| 154 | <br> |
| 155 | <div class="mod-code"> |
| 156 | * Write variables into object and do some regex |
| 157 | checks<br> |
| 158 | *<br> |
| 159 | * @param array $post HTTP-POST values<br> |
| 160 | */<br> |
| 161 | <span style="font-weight: bold;">function</span> <span style="color: rgb(255, 0, 0);">process_attributes</span>($post) {<br> |
| 162 | $errors = array();<br> |
| 163 | |
| 164 | $this->attributes['macAddress'] = array();<br> |
| 165 | // check old MACs<br> |
| 166 | if (isset($post['mac_number'])) {<br> |
| 167 | for ($i = 0; |
| 168 | $i < $post['mac_number']; $i++) {<br> |
| 169 | |
| 170 | if (isset($post['delMAC' . $i])) continue;<br> |
| 171 | |
| 172 | if (isset($post['macAddress' . $i]) && |
| 173 | ($post['macAddress' . $i] != "")) {<br> |
| 174 | |
| 175 | // check if address has correct |
| 176 | format<br> |
| 177 | |
| 178 | if (!get_preg($post['macAddress' |
| 179 | . $i], 'macAddress')) {<br> |
| 180 | |
| 181 | $message = |
| 182 | $this->messages['mac'][0];<br> |
| 183 | |
| 184 | $message[] = |
| 185 | $post['macAddress' . $i];<br> |
| 186 | |
| 187 | $errors[] = $message;<br> |
| 188 | |
| 189 | }<br> |
| 190 | |
| 191 | |
| 192 | $this->attributes['macAddress'][] = $post['macAddress' . $i];<br> |
| 193 | |
| 194 | }<br> |
| 195 | }<br> |
| 196 | }<br> |
| 197 | // check new MAC<br> |
| 198 | if (isset($post['macAddress']) |
| 199 | && ($post['macAddress'] != "")) {<br> |
| 200 | // check if |
| 201 | address has correct format<br> |
| 202 | if |
| 203 | (get_preg($post['macAddress'], 'macAddress')) {<br> |
| 204 | |
| 205 | $this->attributes['macAddress'][] = |
| 206 | $post['macAddress'];<br> |
| 207 | }<br> |
| 208 | else {<br> |
| 209 | |
| 210 | $message = |
| 211 | $this->messages['mac'][0];<br> |
| 212 | |
| 213 | $message[] = $post['macAddress'];<br> |
| 214 | |
| 215 | $errors[] = $message;<br> |
| 216 | }<br> |
| 217 | }<br> |
| 218 | |
| 219 | $this->attributes['macAddress'] = |
| 220 | array_unique($this->attributes['macAddress']);<br> |
| 221 | return $errors;<br> |
| 222 | }<br> |
| 223 | </div> |
| 224 | <br> |
| 225 | <br> |
| 226 | <h2>4. Defining that your module is ready for user input and LDAP |
| 227 | add/modify</h2> |
| 228 | In most cases you will not need to implement these functions. The <span style="font-style: italic;">baseModule</span> will return <span style="font-style: italic;">true</span> for both functions.<br> |
| 229 | <br> |
| 230 | <span style="text-decoration: underline;"><br> |
| 231 | There are two functions which control the module status:</span><br style="text-decoration: underline;"> |
| 232 | <br> |
| 233 | The <span style="font-weight: bold;">module_ready()</span> function |
| 234 | has to |
| 235 | return <span style="font-style: italic;">true</span> if the user may |
| 236 | move to your module page. If it is <span style="font-style: italic;">false</span> |
| 237 | the user will be shown an error message that your module is not yet |
| 238 | ready. You can use this if your module depends on input data from other |
| 239 | modules (e.g. you need the user name from posixAccount first).<br> |
| 240 | <br> |
| 241 | The second function is |
| 242 | <span style="font-weight: bold;">module_complete()</span>. The user |
| 243 | cannot do the LDAP operation if one or more modules return <span style="font-style: italic;">false</span>. This defines if all needed |
| 244 | input data for your module was entered.<br> |
| 245 | Use this function if you want to check that all required attributes are |
| 246 | set.<br> |
| 247 | <br> |
| 248 | <span style="font-weight: bold; text-decoration: underline;">Example:</span><br style="font-weight: bold; text-decoration: underline;"> |
| 249 | <br> |
| 250 | The <span style="font-style: italic;">sambaSamAccount</span> |
| 251 | module needs the user's <span style="font-style: italic;">uidNumber</span> |
| 252 | and <span style="font-style: italic;">gidNumber</span> before it can |
| 253 | accept input and the account needs a <span style="font-style: italic;">sambaSID</span> |
| 254 | before it can be saved.<br> |
| 255 | <br> |
| 256 | <div class="mod-code"> |
| 257 | * This function is used to check if this module page |
| 258 | can be displayed.<br> |
| 259 | * It returns false if a module depends on data from |
| 260 | other modules which was not yet entered.<br> |
| 261 | *<br> |
| 262 | * @return boolean true, if page can be displayed<br> |
| 263 | */<br> |
| 264 | <span style="font-weight: bold;">function</span> <span style="color: rgb(255, 0, 0);">module_ready</span>() {<br> |
| 265 | if |
| 266 | ($_SESSION[$this->base]->module['posixAccount']->attributes['gidNumber'][0]=='') |
| 267 | return false;<br> |
| 268 | if |
| 269 | ($_SESSION[$this->base]->module['posixAccount']->attributes['uidNumber'][0]=='') |
| 270 | return false;<br> |
| 271 | if |
| 272 | ($this->attributes['uid'][0]=='') return false;<br> |
| 273 | return true;<br> |
| 274 | }<br> |
| 275 | <br> |
| 276 | /**<br> |
| 277 | * This functions is used to check if all settings |
| 278 | for this module have been made.<br> |
| 279 | *<br> |
| 280 | * @return boolean true, if settings are complete<br> |
| 281 | */<br> |
| 282 | <span style="font-weight: bold;">function</span> <span style="color: rgb(255, 0, 0);">module_complete</span>() {<br> |
| 283 | if (!$this->module_ready()) |
| 284 | return false;<br> |
| 285 | if |
| 286 | ($this->attributes['sambaSID'][0] == '') return false;<br> |
| 287 | return true;<br> |
| 288 | }<br> |
| 289 | <br> |
| 290 | </div> |
| 291 | <br> |
| 292 | <br> |
| 293 | <h2>5. Saving the LDAP attributes<br> |
| 294 | </h2> |
| 295 | In most cases you will not have to implement this option if you use <span style="font-weight: bold;">$this->attributes</span> and <span style="font-weight: bold;">$this->orig</span> to manage the LDAP |
| 296 | attributes. The <span style="font-style: italic;">baseModule</span> |
| 297 | will generate the save commands for you.<br> |
| 298 | <br> |
| 299 | When all modules report that they are ready for LDAP add/modify and the |
| 300 | user clicks on the add/modify button your module will be asked what |
| 301 | changes have to be made.<br> |
| 302 | This is done in the function <span style="font-weight: bold;">save_attributes()</span>.<br> |
| 303 | <br> |
| 304 | <span style="font-weight: bold; text-decoration: underline;">Example:</span><br style="font-weight: bold; text-decoration: underline;"> |
| 305 | <br> |
| 306 | The <span style="font-style: italic;">kolabUser</span> module uses |
| 307 | this function to make sure that its object class is saved. Other |
| 308 | modules (e.g. quota) use it build the lamdaemon commands.<br> |
| 309 | <br> |
| 310 | <div class="mod-code"> |
| 311 | * Returns a list of modifications which have to be |
| 312 | made to the LDAP account.<br> |
| 313 | *<br> |
| 314 | * @return array list of modifications<br> |
| 315 | * <br>This function returns an array with 3 |
| 316 | entries:<br> |
| 317 | * <br>array( DN1 ('add' => array($attr), |
| 318 | 'remove' => array($attr), 'modify' => array($attr)), DN2 .... )<br> |
| 319 | * <br>DN is the DN to change. It may be |
| 320 | possible to change several DNs (e.g. create a new user and add him to |
| 321 | some groups via attribute memberUid)<br> |
| 322 | * <br>"add" are attributes which have to be |
| 323 | added to LDAP entry<br> |
| 324 | * <br>"remove" are attributes which have to be |
| 325 | removed from LDAP entry<br> |
| 326 | * <br>"modify" are attributes which have to |
| 327 | been modified in LDAP entry<br> |
| 328 | */<br> |
| 329 | function save_attributes() {<br> |
| 330 | // add object class if needed<br> |
| 331 | if |
| 332 | (!isset($this->attributes['objectClass']) || |
| 333 | !in_array('kolabInetOrgPerson', $this->attributes['objectClass'])) {<br> |
| 334 | |
| 335 | $this->attributes['objectClass'][] = 'kolabInetOrgPerson';<br> |
| 336 | }<br> |
| 337 | return parent::save_attributes();<br> |
| 338 | }<br> |
| 339 | </div> |
| 340 | <br> |
| 341 | <br> |
| 342 | </div> |
| 343 | </div> |
| 344 | </body></html> |