LdapUserProfile.java 21.6 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: $
 * $Date: $
 *
6
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
19 20 21 22
 */

package org.jivesoftware.admin;

23 24
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
25 26
import org.jivesoftware.openfire.ldap.LdapManager;
import org.jivesoftware.openfire.ldap.LdapVCardProvider;
27
import org.jivesoftware.util.JiveGlobals;
28 29 30 31 32
import org.jivesoftware.util.Log;
import org.jivesoftware.util.XMLWriter;

import java.io.IOException;
import java.io.StringWriter;
33
import java.util.Iterator;
34 35 36

/**
 * Bean that stores the vcard mapping. It is also responsible for saving the mapping
37
 * as a system property and retrieving it.
38 39 40 41 42
 *
 * @author Gaston Dombiak
 */
public class LdapUserProfile {

43 44 45 46 47
    private String name = "";
    private String email = "";
    private String fullName = "";
    private String nickname = "";
    private String birthday = "";
48
    private String photo = "";
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    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 = "";
69
    private Boolean avatarStoredInDB = false;
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 103 104 105 106 107 108 109 110

    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;
    }

111 112 113 114 115 116 117 118
    public String getPhoto() {
        return photo;
    }

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

119 120
    public String getHomeStreet() {
        return homeStreet;
121 122
    }

123 124
    public void setHomeStreet(String homeStreet) {
        this.homeStreet = homeStreet;
125 126
    }

127 128
    public String getHomeCity() {
        return homeCity;
129 130
    }

131 132
    public void setHomeCity(String homeCity) {
        this.homeCity = homeCity;
133 134
    }

135 136
    public String getHomeState() {
        return homeState;
137 138
    }

139 140
    public void setHomeState(String homeState) {
        this.homeState = homeState;
141 142
    }

143 144
    public String getHomeZip() {
        return homeZip;
145 146
    }

147 148
    public void setHomeZip(String homeZip) {
        this.homeZip = homeZip;
149 150
    }

151 152
    public String getHomeCountry() {
        return homeCountry;
153 154
    }

155 156
    public void setHomeCountry(String homeCountry) {
        this.homeCountry = homeCountry;
157 158
    }

159 160
    public String getHomePhone() {
        return homePhone;
161 162
    }

163 164
    public void setHomePhone(String homePhone) {
        this.homePhone = homePhone;
165 166
    }

167 168
    public String getHomeMobile() {
        return homeMobile;
169 170
    }

171 172
    public void setHomeMobile(String homeMobile) {
        this.homeMobile = homeMobile;
173 174
    }

175 176
    public String getHomeFax() {
        return homeFax;
177 178
    }

179 180
    public void setHomeFax(String homeFax) {
        this.homeFax = homeFax;
181 182
    }

183 184
    public String getHomePager() {
        return homePager;
185 186
    }

187 188
    public void setHomePager(String homePager) {
        this.homePager = homePager;
189 190
    }

191 192
    public String getBusinessStreet() {
        return businessStreet;
193 194
    }

195 196
    public void setBusinessStreet(String businessStreet) {
        this.businessStreet = businessStreet;
197 198
    }

199 200
    public String getBusinessCity() {
        return businessCity;
201 202
    }

203 204
    public void setBusinessCity(String businessCity) {
        this.businessCity = businessCity;
205 206
    }

207 208
    public String getBusinessState() {
        return businessState;
209 210
    }

211 212
    public void setBusinessState(String businessState) {
        this.businessState = businessState;
213 214
    }

215 216
    public String getBusinessZip() {
        return businessZip;
217 218
    }

219 220
    public void setBusinessZip(String businessZip) {
        this.businessZip = businessZip;
221 222
    }

223 224
    public String getBusinessCountry() {
        return businessCountry;
225 226
    }

227 228
    public void setBusinessCountry(String businessCountry) {
        this.businessCountry = businessCountry;
229 230
    }

231 232
    public String getBusinessJobTitle() {
        return businessJobTitle;
233 234
    }

235 236
    public void setBusinessJobTitle(String businessJobTitle) {
        this.businessJobTitle = businessJobTitle;
237 238
    }

239 240
    public String getBusinessDepartment() {
        return businessDepartment;
241 242
    }

243 244
    public void setBusinessDepartment(String businessDepartment) {
        this.businessDepartment = businessDepartment;
245 246
    }

247 248
    public String getBusinessPhone() {
        return businessPhone;
249 250
    }

251 252
    public void setBusinessPhone(String businessPhone) {
        this.businessPhone = businessPhone;
253 254
    }

255 256
    public String getBusinessMobile() {
        return businessMobile;
257 258
    }

259 260
    public void setBusinessMobile(String businessMobile) {
        this.businessMobile = businessMobile;
261 262
    }

263 264
    public String getBusinessFax() {
        return businessFax;
265 266
    }

267 268
    public void setBusinessFax(String businessFax) {
        this.businessFax = businessFax;
269 270
    }

271 272
    public String getBusinessPager() {
        return businessPager;
273 274
    }

275 276
    public void setBusinessPager(String businessPager) {
        this.businessPager = businessPager;
277 278
    }

279 280 281 282 283 284 285 286 287 288 289 290 291
    public Boolean getAvatarStoredInDB() {
        return avatarStoredInDB;
    }

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

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

    /**
     * Sets default mapping values when using an Active Directory server.
     */
    public void initForOpenLDAP() {
        name = "{cn}";
        email = "{mail}";
        fullName = "{displayName}";
        nickname = "{uid}";
        birthday = "";
334
        photo = "{jpegPhoto}";
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        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}";
355
        avatarStoredInDB = false;
356 357 358
    }

    /**
359
     * Saves current configuration as XML/DB properties.
360
     */
361
    public void saveProperties() {
362 363 364 365 366 367
        Element vCard = DocumentHelper.createElement(QName.get("vCard", "vcard-temp"));
        Element subelement;

        // Add name
        if (name != null && name.trim().length() > 0) {
            subelement = vCard.addElement("N");
368
            subelement.addElement("GIVEN").setText(name.trim());
369 370 371 372 373
        }
        // Add email
        if (email != null && email.trim().length() > 0) {
            subelement = vCard.addElement("EMAIL");
            subelement.addElement("INTERNET");
374
            subelement.addElement("USERID").setText(email.trim());
375 376
        }
        // Add Full Name
377
        vCard.addElement("FN").setText(fullName.trim());
378 379
        // Add nickname
        if (nickname != null && nickname.trim().length() > 0) {
380
            vCard.addElement("NICKNAME").setText(nickname.trim());
381 382 383
        }
        // Add birthday
        if (birthday != null && birthday.trim().length() > 0) {
384
            vCard.addElement("BDAY").setText(birthday.trim());
385
        }
386 387 388 389 390 391
        // 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());
        }
392 393 394
        // Add home address
        subelement = vCard.addElement("ADR");
        subelement.addElement("HOME");
395 396
        if (homeStreet != null && homeStreet.trim().length() > 0) {
            subelement.addElement("STREET").setText(homeStreet.trim());
397
        }
398 399
        if (homeCity != null && homeCity.trim().length() > 0) {
            subelement.addElement("LOCALITY").setText(homeCity.trim());
400
        }
401 402
        if (homeState != null && homeState.trim().length() > 0) {
            subelement.addElement("REGION").setText(homeState.trim());
403
        }
404 405
        if (homeZip != null && homeZip.trim().length() > 0) {
            subelement.addElement("PCODE").setText(homeZip.trim());
406
        }
407 408
        if (homeCountry != null && homeCountry.trim().length() > 0) {
            subelement.addElement("CTRY").setText(homeCountry.trim());
409 410 411 412
        }
        // Add business address
        subelement = vCard.addElement("ADR");
        subelement.addElement("WORK");
413 414
        if (businessStreet != null && businessStreet.trim().length() > 0) {
            subelement.addElement("STREET").setText(businessStreet.trim());
415
        }
416 417
        if (businessCity != null && businessCity.trim().length() > 0) {
            subelement.addElement("LOCALITY").setText(businessCity.trim());
418
        }
419 420
        if (businessState != null && businessState.trim().length() > 0) {
            subelement.addElement("REGION").setText(businessState.trim());
421
        }
422
        if (businessZip != null && businessZip.trim().length() > 0) {
423
            subelement.addElement("PCODE").setText(businessZip.trim());
424
        }
425 426
        if (businessCountry != null && businessCountry.trim().length() > 0) {
            subelement.addElement("CTRY").setText(businessCountry.trim());
427 428
        }
        // Add home phone
429
        if (homePhone != null && homePhone.trim().length() > 0) {
430 431 432
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("VOICE");
433
            subelement.addElement("NUMBER").setText(homePhone.trim());
434 435
        }
        // Add home mobile
436
        if (homeMobile != null && homeMobile.trim().length() > 0) {
437 438 439
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("CELL");
440
            subelement.addElement("NUMBER").setText(homeMobile.trim());
441 442
        }
        // Add home fax
443
        if (homeFax != null && homeFax.trim().length() > 0) {
444 445 446
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("FAX");
447
            subelement.addElement("NUMBER").setText(homeFax.trim());
448 449
        }
        // Add home pager
450
        if (homePager != null && homePager.trim().length() > 0) {
451 452 453
            subelement = vCard.addElement("TEL");
            subelement.addElement("HOME");
            subelement.addElement("PAGER");
454
            subelement.addElement("NUMBER").setText(homePager.trim());
455 456
        }
        // Add business phone
457
        if (businessPhone != null && businessPhone.trim().length() > 0) {
458 459 460
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("VOICE");
461
            subelement.addElement("NUMBER").setText(businessPhone.trim());
462 463
        }
        // Add business mobile
464
        if (businessMobile != null && businessMobile.trim().length() > 0) {
465 466 467
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("CELL");
468
            subelement.addElement("NUMBER").setText(businessMobile.trim());
469 470
        }
        // Add business fax
471
        if (businessFax != null && businessFax.trim().length() > 0) {
472 473 474
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("FAX");
475
            subelement.addElement("NUMBER").setText(businessFax.trim());
476 477
        }
        // Add business pager
478
        if (businessPager != null && businessPager.trim().length() > 0) {
479 480 481
            subelement = vCard.addElement("TEL");
            subelement.addElement("WORK");
            subelement.addElement("PAGER");
482
            subelement.addElement("NUMBER").setText(businessPager.trim());
483 484
        }
        // Add job title
485 486
        if (businessJobTitle != null && businessJobTitle.trim().length() > 0) {
            vCard.addElement("TITLE").setText(businessJobTitle.trim());
487
        }
488 489 490 491
        // Add job department
        if (businessDepartment != null && businessDepartment.trim().length() > 0) {
            vCard.addElement("ORG").addElement("ORGUNIT").setText(businessDepartment.trim());
        }
492 493 494 495 496 497 498 499 500 501 502 503 504 505
        // 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();
        }

506 507
        StringBuilder sb = new StringBuilder(vcardXML.length());
        sb.append("<![CDATA[").append(vcardXML).append("]]>");
508
        // Save mapping as an XML property
509
        JiveGlobals.setProperty("ldap.vcard-mapping", sb.toString());
510 511

        // Set that the vcard provider is LdapVCardProvider
512
        JiveGlobals.setProperty("provider.vcard.className", LdapVCardProvider.class.getName());
513 514

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

        // Store the DB storage variable in the actual database.
519
        JiveGlobals.setProperty("ldap.override.avatar", avatarStoredInDB.toString());
520 521 522
    }

    /**
523 524
     * Returns true if the vCard mappings where successfully loaded from the XML/DB
     * properties.
525 526 527 528
     *
     * @return true if mappings where loaded from saved property.
     */
    public boolean loadFromProperties() {
529
        String xmlProperty = JiveGlobals.getProperty("ldap.vcard-mapping");
530 531 532 533 534
        if (xmlProperty == null || xmlProperty.trim().length() == 0) {
            return false;
        }

        try {
535 536 537 538 539
            // Remove CDATA wrapping element
            if (xmlProperty.startsWith("<![CDATA[")) {
                xmlProperty = xmlProperty.substring(9, xmlProperty.length()-3);
            }
            // Parse XML
540 541 542 543 544 545 546 547 548 549 550 551 552
            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) {
553
                fullName = element.getTextTrim();
554 555 556
            }
            element = vCard.element("NICKNAME");
            if (element != null) {
557
                nickname = element.getTextTrim();
558 559 560
            }
            element = vCard.element("BDAY");
            if (element != null) {
561
                birthday = element.getTextTrim();
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 637 638 639 640 641 642 643 644
            // 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");
                }
            }
645
            avatarStoredInDB = JiveGlobals.getBooleanProperty("ldap.override.avatar", false);
646 647 648 649 650
        }
        catch (DocumentException e) {
            Log.error("Error loading vcard mappings from property", e);
            return false;
        }
651

652
        return true;
653 654
    }
}