LdapUserProfile.java 21.2 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
7 8
 *
 * This software is published under the terms of the GNU Public License (GPL),
9 10
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
11 12 13 14
 */

package org.jivesoftware.admin;

15 16
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
17 18
import org.jivesoftware.openfire.ldap.LdapManager;
import org.jivesoftware.openfire.ldap.LdapVCardProvider;
19
import org.jivesoftware.util.JiveGlobals;
20 21 22 23 24
import org.jivesoftware.util.Log;
import org.jivesoftware.util.XMLWriter;

import java.io.IOException;
import java.io.StringWriter;
25
import java.util.Iterator;
26 27 28

/**
 * Bean that stores the vcard mapping. It is also responsible for saving the mapping
29
 * as a system property and retrieving it.
30 31 32 33 34
 *
 * @author Gaston Dombiak
 */
public class LdapUserProfile {

35 36 37 38 39
    private String name = "";
    private String email = "";
    private String fullName = "";
    private String nickname = "";
    private String birthday = "";
40
    private String photo = "";
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    private String homeStreet = "";
    private String homeCity = "";
    private String homeState = "";
    private String homeZip = "";
    private String homeCountry = "";
    private String homePhone = "";
    private String homeMobile = "";
    private String homeFax = "";
    private String homePager = "";
    private String businessStreet = "";
    private String businessCity = "";
    private String businessState = "";
    private String businessZip = "";
    private String businessCountry = "";
    private String businessJobTitle = "";
    private String businessDepartment = "";
    private String businessPhone = "";
    private String businessMobile = "";
    private String businessFax = "";
    private String businessPager = "";
61
    private Boolean avatarStoredInDB = false;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

103 104 105 106 107 108 109 110
    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

111 112
    public String getHomeStreet() {
        return homeStreet;
113 114
    }

115 116
    public void setHomeStreet(String homeStreet) {
        this.homeStreet = homeStreet;
117 118
    }

119 120
    public String getHomeCity() {
        return homeCity;
121 122
    }

123 124
    public void setHomeCity(String homeCity) {
        this.homeCity = homeCity;
125 126
    }

127 128
    public String getHomeState() {
        return homeState;
129 130
    }

131 132
    public void setHomeState(String homeState) {
        this.homeState = homeState;
133 134
    }

135 136
    public String getHomeZip() {
        return homeZip;
137 138
    }

139 140
    public void setHomeZip(String homeZip) {
        this.homeZip = homeZip;
141 142
    }

143 144
    public String getHomeCountry() {
        return homeCountry;
145 146
    }

147 148
    public void setHomeCountry(String homeCountry) {
        this.homeCountry = homeCountry;
149 150
    }

151 152
    public String getHomePhone() {
        return homePhone;
153 154
    }

155 156
    public void setHomePhone(String homePhone) {
        this.homePhone = homePhone;
157 158
    }

159 160
    public String getHomeMobile() {
        return homeMobile;
161 162
    }

163 164
    public void setHomeMobile(String homeMobile) {
        this.homeMobile = homeMobile;
165 166
    }

167 168
    public String getHomeFax() {
        return homeFax;
169 170
    }

171 172
    public void setHomeFax(String homeFax) {
        this.homeFax = homeFax;
173 174
    }

175 176
    public String getHomePager() {
        return homePager;
177 178
    }

179 180
    public void setHomePager(String homePager) {
        this.homePager = homePager;
181 182
    }

183 184
    public String getBusinessStreet() {
        return businessStreet;
185 186
    }

187 188
    public void setBusinessStreet(String businessStreet) {
        this.businessStreet = businessStreet;
189 190
    }

191 192
    public String getBusinessCity() {
        return businessCity;
193 194
    }

195 196
    public void setBusinessCity(String businessCity) {
        this.businessCity = businessCity;
197 198
    }

199 200
    public String getBusinessState() {
        return businessState;
201 202
    }

203 204
    public void setBusinessState(String businessState) {
        this.businessState = businessState;
205 206
    }

207 208
    public String getBusinessZip() {
        return businessZip;
209 210
    }

211 212
    public void setBusinessZip(String businessZip) {
        this.businessZip = businessZip;
213 214
    }

215 216
    public String getBusinessCountry() {
        return businessCountry;
217 218
    }

219 220
    public void setBusinessCountry(String businessCountry) {
        this.businessCountry = businessCountry;
221 222
    }

223 224
    public String getBusinessJobTitle() {
        return businessJobTitle;
225 226
    }

227 228
    public void setBusinessJobTitle(String businessJobTitle) {
        this.businessJobTitle = businessJobTitle;
229 230
    }

231 232
    public String getBusinessDepartment() {
        return businessDepartment;
233 234
    }

235 236
    public void setBusinessDepartment(String businessDepartment) {
        this.businessDepartment = businessDepartment;
237 238
    }

239 240
    public String getBusinessPhone() {
        return businessPhone;
241 242
    }

243 244
    public void setBusinessPhone(String businessPhone) {
        this.businessPhone = businessPhone;
245 246
    }

247 248
    public String getBusinessMobile() {
        return businessMobile;
249 250
    }

251 252
    public void setBusinessMobile(String businessMobile) {
        this.businessMobile = businessMobile;
253 254
    }

255 256
    public String getBusinessFax() {
        return businessFax;
257 258
    }

259 260
    public void setBusinessFax(String businessFax) {
        this.businessFax = businessFax;
261 262
    }

263 264
    public String getBusinessPager() {
        return businessPager;
265 266
    }

267 268
    public void setBusinessPager(String businessPager) {
        this.businessPager = businessPager;
269 270
    }

271 272 273 274 275 276 277 278 279 280 281 282 283
    public Boolean getAvatarStoredInDB() {
        return avatarStoredInDB;
    }

    public void setAvatarStoredInDB(Boolean avatarStoredInDB) {
        if (avatarStoredInDB == null) {
            this.avatarStoredInDB = false;
        }
        else {
            this.avatarStoredInDB = avatarStoredInDB;
        }
    }

284 285 286 287 288 289 290 291 292
    /**
     * Sets default mapping values when using an Active Directory server.
     */
    public void initForActiveDirectory() {
        name = "{cn}";
        email = "{mail}";
        fullName = "{displayName}";
        nickname = "";
        birthday = "";
293
        photo = "{jpegPhoto}";
294 295 296 297
        homeStreet = "{homePostalAddress}";
        homeCity = "";
        homeState = "";
        homeZip = "{homeZip}";
298
        homeCountry = "{co}";
299
        homePhone = "{homePhone}";
300
        homeMobile = "{mobile}";
301 302
        homeFax = "";
        homePager = "";
303
        businessStreet = "{streetAddress}";
304 305 306
        businessCity = "{l}";
        businessState = "{st}";
        businessZip = "{postalCode}";
307
        businessCountry = "{co}";
308 309
        businessJobTitle = "{title}";
        businessDepartment = "{department}";
310
        businessPhone = "{telephoneNumber}";
311 312 313
        businessMobile = "{mobile}";
        businessFax = "{facsimileTelephoneNumber}";
        businessPager = "{pager}";
314
        avatarStoredInDB = false;
315 316 317 318 319 320 321 322 323 324 325
    }

    /**
     * Sets default mapping values when using an Active Directory server.
     */
    public void initForOpenLDAP() {
        name = "{cn}";
        email = "{mail}";
        fullName = "{displayName}";
        nickname = "{uid}";
        birthday = "";
326
        photo = "{jpegPhoto}";
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        homeStreet = "{homePostalAddress}";
        homeCity = "";
        homeState = "";
        homeZip = "";
        homeCountry = "";
        homePhone = "{homePhone}";
        homeMobile = "";
        homeFax = "";
        homePager = "";
        businessStreet = "{postalAddress}";
        businessCity = "{l}";
        businessState = "{st}";
        businessZip = "{postalCode}";
        businessCountry = "";
        businessJobTitle = "{title}";
        businessDepartment = "{departmentNumber}";
        businessPhone = "{telephoneNumber}";
        businessMobile = "{mobile}";
        businessFax = "";
        businessPager = "{pager}";
347
        avatarStoredInDB = false;
348 349 350
    }

    /**
351
     * Saves current configuration as XML/DB properties.
352
     */
353
    public void saveProperties() {
354 355 356 357 358 359
        Element vCard = DocumentHelper.createElement(QName.get("vCard", "vcard-temp"));
        Element subelement;

        // Add name
        if (name != null && name.trim().length() > 0) {
            subelement = vCard.addElement("N");
360
            subelement.addElement("GIVEN").setText(name.trim());
361 362 363 364 365
        }
        // Add email
        if (email != null && email.trim().length() > 0) {
            subelement = vCard.addElement("EMAIL");
            subelement.addElement("INTERNET");
366
            subelement.addElement("USERID").setText(email.trim());
367 368
        }
        // Add Full Name
369
        vCard.addElement("FN").setText(fullName.trim());
370 371
        // Add nickname
        if (nickname != null && nickname.trim().length() > 0) {
372
            vCard.addElement("NICKNAME").setText(nickname.trim());
373 374 375
        }
        // Add birthday
        if (birthday != null && birthday.trim().length() > 0) {
376
            vCard.addElement("BDAY").setText(birthday.trim());
377
        }
378 379 380 381 382 383
        // Add photo/avatar
        if (photo != null && photo.trim().length() > 0) {
            Element element = vCard.addElement("PHOTO");
            element.addElement("TYPE").setText("image/jpeg");
            element.addElement("BINVAL").setText(photo.trim());
        }
384 385 386
        // Add home address
        subelement = vCard.addElement("ADR");
        subelement.addElement("HOME");
387 388
        if (homeStreet != null && homeStreet.trim().length() > 0) {
            subelement.addElement("STREET").setText(homeStreet.trim());
389
        }
390 391
        if (homeCity != null && homeCity.trim().length() > 0) {
            subelement.addElement("LOCALITY").setText(homeCity.trim());
392
        }
393 394
        if (homeState != null && homeState.trim().length() > 0) {
            subelement.addElement("REGION").setText(homeState.trim());
395
        }
396 397
        if (homeZip != null && homeZip.trim().length() > 0) {
            subelement.addElement("PCODE").setText(homeZip.trim());
398
        }
399 400
        if (homeCountry != null && homeCountry.trim().length() > 0) {
            subelement.addElement("CTRY").setText(homeCountry.trim());
401 402 403 404
        }
        // Add business address
        subelement = vCard.addElement("ADR");
        subelement.addElement("WORK");
405 406
        if (businessStreet != null && businessStreet.trim().length() > 0) {
            subelement.addElement("STREET").setText(businessStreet.trim());
407
        }
408 409
        if (businessCity != null && businessCity.trim().length() > 0) {
            subelement.addElement("LOCALITY").setText(businessCity.trim());
410
        }
411 412
        if (businessState != null && businessState.trim().length() > 0) {
            subelement.addElement("REGION").setText(businessState.trim());
413
        }
414
        if (businessZip != null && businessZip.trim().length() > 0) {
415
            subelement.addElement("PCODE").setText(businessZip.trim());
416
        }
417 418
        if (businessCountry != null && businessCountry.trim().length() > 0) {
            subelement.addElement("CTRY").setText(businessCountry.trim());
419 420
        }
        // Add home phone
421
        if (homePhone != null && homePhone.trim().length() > 0) {
422 423 424
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("VOICE");
425
            subelement.addElement("NUMBER").setText(homePhone.trim());
426 427
        }
        // Add home mobile
428
        if (homeMobile != null && homeMobile.trim().length() > 0) {
429 430 431
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("CELL");
432
            subelement.addElement("NUMBER").setText(homeMobile.trim());
433 434
        }
        // Add home fax
435
        if (homeFax != null && homeFax.trim().length() > 0) {
436 437 438
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("FAX");
439
            subelement.addElement("NUMBER").setText(homeFax.trim());
440 441
        }
        // Add home pager
442
        if (homePager != null && homePager.trim().length() > 0) {
443 444 445
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("PAGER");
446
            subelement.addElement("NUMBER").setText(homePager.trim());
447 448
        }
        // Add business phone
449
        if (businessPhone != null && businessPhone.trim().length() > 0) {
450 451 452
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("VOICE");
453
            subelement.addElement("NUMBER").setText(businessPhone.trim());
454 455
        }
        // Add business mobile
456
        if (businessMobile != null && businessMobile.trim().length() > 0) {
457 458 459
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("CELL");
460
            subelement.addElement("NUMBER").setText(businessMobile.trim());
461 462
        }
        // Add business fax
463
        if (businessFax != null && businessFax.trim().length() > 0) {
464 465 466
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("FAX");
467
            subelement.addElement("NUMBER").setText(businessFax.trim());
468 469
        }
        // Add business pager
470
        if (businessPager != null && businessPager.trim().length() > 0) {
471 472 473
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("PAGER");
474
            subelement.addElement("NUMBER").setText(businessPager.trim());
475 476
        }
        // Add job title
477 478
        if (businessJobTitle != null && businessJobTitle.trim().length() > 0) {
            vCard.addElement("TITLE").setText(businessJobTitle.trim());
479
        }
480 481 482 483
        // Add job department
        if (businessDepartment != null && businessDepartment.trim().length() > 0) {
            vCard.addElement("ORG").addElement("ORGUNIT").setText(businessDepartment.trim());
        }
484 485 486 487 488 489 490 491 492 493 494 495 496 497
        // Generate content to store in property
        String vcardXML;
        StringWriter writer = new StringWriter();
        OutputFormat prettyPrinter = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, prettyPrinter);
        try {
            xmlWriter.write(vCard);
            vcardXML = writer.toString();
        }
        catch (IOException e) {
            Log.error("Error pretty formating XML", e);
            vcardXML = vCard.asXML();
        }

498 499
        StringBuilder sb = new StringBuilder(vcardXML.length());
        sb.append("<![CDATA[").append(vcardXML).append("]]>");
500
        // Save mapping as an XML property
501
        JiveGlobals.setProperty("ldap.vcard-mapping", sb.toString());
502 503

        // Set that the vcard provider is LdapVCardProvider
504
        JiveGlobals.setProperty("provider.vcard.className", LdapVCardProvider.class.getName());
505 506

        // Save duplicated fields in LdapManager (should be removed in the future)
507 508
        LdapManager.getInstance().setNameField(name.replaceAll("(\\{)([\\d\\D&&[^}]]+)(})", "$2"));
        LdapManager.getInstance().setEmailField(email.replaceAll("(\\{)([\\d\\D&&[^}]]+)(})", "$2"));
509 510

        // Store the DB storage variable in the actual database.
511
        JiveGlobals.setProperty("ldap.override.avatar", avatarStoredInDB.toString());
512 513 514
    }

    /**
515 516
     * Returns true if the vCard mappings where successfully loaded from the XML/DB
     * properties.
517 518 519 520
     *
     * @return true if mappings where loaded from saved property.
     */
    public boolean loadFromProperties() {
521
        String xmlProperty = JiveGlobals.getProperty("ldap.vcard-mapping");
522 523 524 525 526
        if (xmlProperty == null || xmlProperty.trim().length() == 0) {
            return false;
        }

        try {
527 528 529 530 531
            // Remove CDATA wrapping element
            if (xmlProperty.startsWith("<![CDATA[")) {
                xmlProperty = xmlProperty.substring(9, xmlProperty.length()-3);
            }
            // Parse XML
532 533 534 535 536 537 538 539 540 541 542 543 544
            Document document = DocumentHelper.parseText(xmlProperty);
            Element vCard = document.getRootElement();

            Element element = vCard.element("N");
            if (element != null) {
                name = element.elementTextTrim("GIVEN");
            }
            element = vCard.element("EMAIL");
            if (element != null) {
                email = element.elementTextTrim("USERID");
            }
            element = vCard.element("FN");
            if (element != null) {
545
                fullName = element.getTextTrim();
546 547 548
            }
            element = vCard.element("NICKNAME");
            if (element != null) {
549
                nickname = element.getTextTrim();
550 551 552
            }
            element = vCard.element("BDAY");
            if (element != null) {
553
                birthday = element.getTextTrim();
554
            }
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
            // Parse addresses
            Iterator addresses = vCard.elementIterator("ADR");
            while (addresses.hasNext()) {
                element = (Element) addresses.next();
                if (element.element("HOME") != null) {
                    if (element.element("STREET") != null) {
                        homeStreet = element.elementTextTrim("STREET");
                    }
                    if (element.element("LOCALITY") != null) {
                        homeCity = element.elementTextTrim("LOCALITY");
                    }
                    if (element.element("REGION") != null) {
                        homeState = element.elementTextTrim("REGION");
                    }
                    if (element.element("PCODE") != null) {
                        homeZip = element.elementTextTrim("PCODE");
                    }
                    if (element.element("CTRY") != null) {
                        homeCountry = element.elementTextTrim("CTRY");
                    }
                }
                else if (element.element("WORK") != null) {
                    if (element.element("STREET") != null) {
                        businessStreet = element.elementTextTrim("STREET");
                    }
                    if (element.element("LOCALITY") != null) {
                        businessCity = element.elementTextTrim("LOCALITY");
                    }
                    if (element.element("REGION") != null) {
                        businessState = element.elementTextTrim("REGION");
                    }
                    if (element.element("PCODE") != null) {
                        businessZip = element.elementTextTrim("PCODE");
                    }
                    if (element.element("CTRY") != null) {
                        businessCountry = element.elementTextTrim("CTRY");
                    }
                }
            }
            // Parse telephones
            Iterator telephones = vCard.elementIterator("TEL");
            while (telephones.hasNext()) {
                element = (Element) telephones.next();
                if (element.element("HOME") != null) {
                    if (element.element("VOICE") != null) {
                        homePhone = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("CELL") != null) {
                        homeMobile = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("FAX") != null) {
                        homeFax = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("PAGER") != null) {
                        homePager = element.elementTextTrim("NUMBER");
                    }
                }
                else if (element.element("WORK") != null) {
                    if (element.element("VOICE") != null) {
                        businessPhone = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("CELL") != null) {
                        businessMobile = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("FAX") != null) {
                        businessFax = element.elementTextTrim("NUMBER");
                    }
                    else if (element.element("PAGER") != null) {
                        businessPager = element.elementTextTrim("NUMBER");
                    }
                }
            }
            element = vCard.element("TITLE");
            if (element != null) {
                businessJobTitle = element.getTextTrim();
            }
            element = vCard.element("ORG");
            if (element != null) {
                if (element.element("ORGUNIT") != null) {
                    businessDepartment = element.elementTextTrim("ORGUNIT");
                }
            }
637
            avatarStoredInDB = JiveGlobals.getBooleanProperty("ldap.override.avatar", false);
638 639 640 641 642
        }
        catch (DocumentException e) {
            Log.error("Error loading vcard mappings from property", e);
            return false;
        }
643

644
        return true;
645 646
    }
}