LdapUserProfile.java 21.6 KB
Newer Older
1
/*
2
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * 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.
15 16 17 18
 */

package org.jivesoftware.admin;

19 20
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
21 22
import org.jivesoftware.openfire.ldap.LdapManager;
import org.jivesoftware.openfire.ldap.LdapVCardProvider;
23
import org.jivesoftware.util.JiveGlobals;
24
import org.jivesoftware.util.XMLWriter;
25 26
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
27 28 29

import java.io.IOException;
import java.io.StringWriter;
30
import java.util.Iterator;
31 32 33

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

40 41
    private static final Logger Log = LoggerFactory.getLogger(LdapUserProfile.class);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

651
        return true;
652 653
    }
}