Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2010 Västra Götalandsregionen This library is free software; you can redistribute it and/or modify it under the terms of version 2.1 of the GNU Lesser General Public License as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 
 package se.vgregion.userupdate.svc;
 
 import java.util.Date;
 import java.util.List;
 
 
 
 
Created by IntelliJ IDEA. Created: 2011-11-28 15:16

Author(s):
David Rosell
 
 public class UserUpdateService {
     private static final Logger LOGGER = LoggerFactory.getLogger(UserUpdateService.class);
     private final DateFormat df = new SimpleDateFormat("yyyyMMdd");
     private final Lock dfLock = new ReentrantLock();
 
     @Autowired
 
     @Autowired
 
     @Autowired
 
     @Autowired
     private UserGroupHelper userGroupHelper;
 
     @Autowired
 
     @Value("${ip_for_external_access}")
     private String ipForExternalAccess;
 
     private static final String POSTFIX_INTERNAL_ONLY = "_internal_only";

    
Updates the birthday of a Liferay user with value from a LDAP catalog. If no user is found in the LDAP catalog or person identity number is not set, birthday will be unmodified.

Parameters:
user the Liferay user to update
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes
 
     public void updateBirthday(User userUserLdapAttributes userLdapAttributes) {
         PersonNummer personNummer = userLdapAttributes.getPersonNummer();
         if (personNummer.getType() != ..) {
             try {
                 Contact contact = user.getContact();
                 final int beginIndex = 0;
                 final int endIndex = 8;
                 .lock();
                 Date pNoDate;
                 try {
                     pNoDate = .parse(personNummer.getFull().substring(beginIndexendIndex));
                 } finally {
                    .unlock();
                }
                Date contactDate = contact.getBirthday();
                if (!pNoDate.equals(contactDate)) {
                    contact.setBirthday(pNoDate);
                    .updateContact(contact);
                }
            } catch (Exception e) {
                String msg = String.format("Failed to update birthday for [%s]"user.getScreenName());
                log(msge);
            }
        } else {
            String msg = String.format("Failed to update birthday, no personIdentityNumber for [%s]",
                    user.getScreenName());
            .info(msg);
        }
    }

    
Updates the gender of a Liferay user with value from a LDAP catalog. If no user is found in the LDAP catalog or person identity number is not set, gender will be unmodified.

Parameters:
user the Liferay user to update
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes
    public void updateGender(User userUserLdapAttributes userLdapAttributes) {
        PersonNummer personNummer = userLdapAttributes.getPersonNummer();
        if (personNummer.getType() != ..) {
            boolean isMale = personNummer.getGender() == ..;
            try {
                Contact contact = user.getContact();
                if (isMale != contact.isMale()) {
                    contact.setMale(isMale);
                    .updateContact(contact);
                }
            } catch (Exception e) {
                String msg = String.format("Failed to update gender for [%s]"user.getScreenName());
                log(msge);
            }
        } else {
            String msg = String.format("Failed to update gender, no personIdentityNumber for [%s]",
                    user.getScreenName());
            .info(msg);
        }
    }

    
Update a com.liferay.portal.model.User's email.

Parameters:
user the com.liferay.portal.model.User
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes which should contain the email address
    public void updateEmail(User userUserLdapAttributes userLdapAttributes) {
        String email = userLdapAttributes.getMail();
        if (email == null) {
            email = "";
        }
        try {
            if (!email.equals(user.getEmailAddress())) {
                user.setEmailAddress(email);
                .updateUser(user);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update email [%s] for [%s]"userLdapAttributes.getMail(),
                    user.getScreenName());
            log(msge);
        }
    }

    
Update a com.liferay.portal.model.User's full name.

Parameters:
user the com.liferay.portal.model.User
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes which should contain the full name
    public void updateFullName(User userUserLdapAttributes userLdapAttributes) {
        // Can be improved if UserLdapAttributes.Type is considered
        String fullName = userLdapAttributes.getDisplayName();
        if (fullName == null) {
            fullName = userLdapAttributes.getFullName();
        }
        if (fullName == null) {
            fullName = "";
        }
        try {
            Contact contact = user.getContact();
            if (!fullName.equals(contact.getUserName())) {
                contact.setUserName(fullName);
                .updateContact(contact);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update fullName [%s] for [%s]"fullNameuser.getScreenName());
            log(msge);
        }
    }

    
Update a com.liferay.portal.model.User's given name.

Parameters:
user the com.liferay.portal.model.User
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes which should contain the given name
    public void updateGivenName(User userUserLdapAttributes userLdapAttributes) {
        // Can be improved if UserLdapAttributes.Type is considered
        String givenName = userLdapAttributes.getGivenName();
        if (givenName == null) {
            givenName = "";
        }
        try {
            if (!givenName.equals(user.getFirstName())) {
                user.setFirstName(givenName);
                .updateUser(user);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update GivenName [%s] for [%s]",
                    userLdapAttributes.getGivenName(), user.getScreenName());
            log(msge);
        }
    }

    
Update a com.liferay.portal.model.User's last name.

Parameters:
user the com.liferay.portal.model.User
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes which should contain the last name
    public void updateLastName(User userUserLdapAttributes userLdapAttributes) {
        // Can be improved if UserLdapAttributes.Type is considered
        String lastName = userLdapAttributes.getSn();
        if (lastName == null) {
            lastName = "";
        }
        try {
            if (!lastName.equals(user.getLastName())) {
                user.setLastName(lastName);
                .updateUser(user);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update LastName [%s] for [%s]"userLdapAttributes.getSn(),
                    user.getScreenName());
            log(msge);
        }
    }

    
Update a com.liferay.portal.model.User's title.

Parameters:
user the com.liferay.portal.model.User
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes which should contain the title
    public void updateTitle(User userUserLdapAttributes userLdapAttributes) {
        String title = userLdapAttributes.getTitle();
        if (title == null) {
            title = "";
        }
        try {
            if (!title.equals(user.getJobTitle())) {
                user.setJobTitle(title);
                .updateUser(user);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update Title [%s] for [%s]"userLdapAttributes.getTitle(),
                    user.getScreenName());
            log(msge);
        }
    }

    
Update a com.liferay.portal.model.User's HSA title.

Parameters:
user the com.liferay.portal.model.User
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes which should contain the HSA title
    public void updateHsaTitle(User userUserLdapAttributes userLdapAttributes) {
        String hsaTitle = userLdapAttributes.getHsaTitle();
        if (hsaTitle == null) {
            hsaTitle = "";
        }
        try {
            .set("hsaTitle"hsaTitleuser);
        } catch (Exception e) {
            String msg = String.format("Failed to set HsaTitle [%s] for [%s]"userLdapAttributes.getHsaTitle(),
                    user.getScreenName());
            log(msge);
        }
    }

    
Updates the prescription code of a Liferay user with value from a LDAP catalog. If no user is found in LDAP catalog prescription code will be cleared.

Parameters:
user the Liferay user to update
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes which should contain the prescription code
    public void updatePrescriptionCode(User userUserLdapAttributes userLdapAttributes) {
        String prescriptionCode = userLdapAttributes.getHsaPersonPrescriptionCode();
        if (prescriptionCode == null) {
            prescriptionCode = "";
        }
        try {
            .set("hsaPrescriptionCode"prescriptionCodeuser);
            String groupName = resolveGroupName(user"PliUsers");
            if (StringUtils.isBlank(prescriptionCode)) {
                .removeUser(groupNameuser);
            } else {
                .addUser(groupNameuser);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to set HsaPersonPerscriptionCode [%s] for [%s]"prescriptionCode,
                    user.getScreenName());
            log(msge);
        }
    }

    
Sets the Domino user flag on a Liferay user if the users has Domino access according to the LDAP catalog. If no user is found in LDAP the Domino user flag is set to false.

Parameters:
user the Liferay user to update
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes which should contain the isDomino attribute
    public void updateIsDominoUser(User userUserLdapAttributes userLdapAttributes) {
        boolean isDominoUser = false;
        if (StringUtils.isNotBlank(userLdapAttributes.getMail())
                && userLdapAttributes.getMail().endsWith("@vgregion.se")) {
            isDominoUser = true;
        }
        try {
            .set("isDominoUser"isDominoUseruser);
            String dominoGroupName = resolveGroupName(user"DominoUsers");
            String notDominoGroupName = resolveGroupName(user"NotDominoUsers");
            if (isDominoUser) {
                .addUser(dominoGroupNameuser);
                .removeUser(notDominoGroupNameuser);
            } else {
                .addUser(notDominoGroupNameuser);
                .removeUser(dominoGroupNameuser);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update domino user state [%s] for [%s]"isDominoUser,
                    user.getScreenName());
            log(msge);
        }
    }

    
Updates the prescription code of a Liferay user with value from a LDAP catalog. If no user is found in LDAP catalog prescription code will be cleared.

Parameters:
user the Liferay user to update
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes which should contain the VGR admin type attribute
    public void updateVgrAdmin(User userUserLdapAttributes userLdapAttributes) {
        String vgrAdmin = userLdapAttributes.getVgrAdminType();
        if (vgrAdmin == null) {
            vgrAdmin = "";
        }
        try {
            .set("vgrAdminType"vgrAdminuser);
            String groupName = resolveGroupName(user"VgrKivAdminUsers");
            if (StringUtils.isNotBlank(vgrAdmin)) {
                .addUser(groupNameuser);
            } else {
                .removeUser(groupNameuser);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update vgrAdminType [%s] for [%s]"vgrAdmin,
                    user.getScreenName());
            log(msge);
        }
    }
    private String resolveGroupName(User userString groupName) {
        .createIfNeeded(groupNameuser.getCompanyId());
        if (.findByName(groupName + user.getCompanyId()) != null) {
            groupName += ;
        }
        return groupName;
    }

    
Update a com.liferay.portal.model.User's isTandvard attribute.

Parameters:
user the com.liferay.portal.model.User
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes of the corresponding LDAP user
    public void updateIsTandvard(User userUserLdapAttributes userLdapAttributes) {
        boolean isTandvard = lookupIsTandvard(userLdapAttributes);
        try {
            .set("isTandvard"isTandvarduser);
            String groupName = resolveGroupName(user"TandvardUsers");
            if (isTandvard) {
                .addUser(groupNameuser);
            } else {
                .removeUser(groupNameuser);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update isTandvard [%s] for [%s]"isTandvard,
                    user.getScreenName());
            log(msge);
        }
    }
    private boolean lookupIsTandvard(UserLdapAttributes userLdapAttributes) {
        boolean tandvard = false;
        String[] strukturGrupp = userLdapAttributes.getStrukturGrupp();
        if (strukturGrupp != null) {
            List<StringstrukturGrupps = Arrays.asList(strukturGrupp);
            List<StringtandvardNames = Arrays.asList("Tandvård""Folktandvården Västra Götaland");
            outer:
            for (String grupp : strukturGrupps) {
                for (String name : tandvardNames) {
                    if (grupp.toLowerCase().contains(name.toLowerCase())) {
                        tandvard = true;
                        break outer;
                    }
                }
            }
        }
        return tandvard;
    }

    
Update a com.liferay.portal.model.User's vgrLabeledURI attribute.

Parameters:
user the Liferay user to update
userOrganizations the se.vgregion.userupdate.domain.UnitLdapAttributes of the user's organizations
    public void updateVgrLabeledURI(User userList<UnitLdapAttributesuserOrganizations) {
        List<StringuriList = new ArrayList<String>();
        if (userOrganizations == null) {
            return;
        }
        if (userOrganizations.size() <= 0) {
            uriList.add("http://intra.vgregion.se/");
        }
        for (UnitLdapAttributes unit : userOrganizations) {
            String vgrLabeledURI = unit.getVgrLabeledURI();
            if (StringUtils.isBlank(vgrLabeledURI)) {
                vgrLabeledURI = unit.getLabeledURI();
            }
            if (StringUtils.isNotBlank(vgrLabeledURI)) {
                uriList.add(vgrLabeledURI);
            }
        }
        try {
            .set("vgrLabeledURI"uriList.toArray(new String[uriList.size()]), user);
        } catch (Exception e) {
            String msg = String.format("Failed to update vgrLabeledURI %s for [%s]",
                    uriList.toString(), user.getScreenName());
            log(msge);
        }
    }

    
Update a com.liferay.portal.model.User's isPrimarvard attribute.

Parameters:
user the Liferay user to update
userOrganizations the se.vgregion.userupdate.domain.UnitLdapAttributes of the user's organizations.
    public void updateIsPrimarvard(User userList<UnitLdapAttributesuserOrganizations) {
        if (userOrganizations == null) {
            return;
        }
        boolean isPrimarvard = lookupIsPrimarvard(userOrganizations);
        try {
            .set("isPrimarvard"isPrimarvarduser);
            String groupName = resolveGroupName(user"VGPrimarvardUsers");
            if (isPrimarvard) {
                .addUser(groupNameuser);
            } else {
                .removeUser(groupNameuser);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update isPrimarvard [%s] for [%s]"isPrimarvard,
                    user.getScreenName());
            log(msge);
        }
    }
    private boolean lookupIsPrimarvard(List<UnitLdapAttributesuserOrganizations) {
        boolean isPrimarvard = false;
        for (UnitLdapAttributes unit : userOrganizations) {
            if (StringUtils.isNotBlank(unit.getVgrVardVal())) {
                isPrimarvard = true;
                break;
            }
        }
        return isPrimarvard;
    }

    
Update a com.liferay.portal.model.User's organizations.

Parameters:
user the Liferay user to update
userLdapAttributes the se.vgregion.userupdate.domain.UserLdapAttributes containing the vgrStrukturPersonDN attribute
    public void updateOrganization(User userUserLdapAttributes userLdapAttributes) {
        List<StringorganizationNames = lookupOrganizationName(userLdapAttributes);
        long companyId = user.getCompanyId();
        try {
            // add to organizations
            List<StringaddUserToOrganization = new ArrayList<String>();
            for (String organizationName : organizationNames) {
                if (!.isMember(organizationNameuser)) {
                    addUserToOrganization.add(organizationName);
                }
            }
            // remove from organizations
            List<StringremoveUserFromOrganization = new ArrayList<String>();
            List<OrganizationoldUserOrganizations = user.getOrganizations();
            for (Organization userOrganization : oldUserOrganizations) {
                if (!organizationNames.contains(userOrganization.getName())) {
                    removeUserFromOrganization.add(userOrganization.getName());
                }
            }
            for (String organizationName : addUserToOrganization) {
                Organization organization = .findByName(organizationNamecompanyId);
                if (organization == null) {
                    organization = .createIfNeeded(organizationNamecompanyId);
                }
                .addUser(organizationuser);
            }
            for (String organizationName : removeUserFromOrganization) {
                Organization organization = .findByName(organizationNamecompanyId);
                .removeUser(organizationuser);
            }
        } catch (Exception e) {
            String msg = String.format("Failed to update organization membership %s for [%s]",
                    organizationNames.toString(), user.getScreenName());
            log(msge);
        }
    }
    private List<StringlookupOrganizationName(UserLdapAttributes userLdapAttributes) {
        List<Stringorganizations = new ArrayList<String>();
        String[] unitDNs = userLdapAttributes.getVgrStrukturPersonDN();
        if (unitDNs != null) {
            for (String unitDN : unitDNs) {
                String orgName = extractOrganization(unitDN);
                if (StringUtils.isNotBlank(orgName)) {
                    organizations.add(orgName);
                }
            }
        }
        return organizations;
    }
    private String extractOrganization(String unitDN) {
        String[] units = unitDN.split(",");
        final int three = 3;
        if (units.length - three >= 0) {
            String[] namePart = units[units.length - three].split("=");
            if (namePart.length == 2) {
                return namePart[1];
            } else {
                String msg = String.format("Strange organization name [%s]"units[units.length - three]);
                .warn(msg);
            }
        }
        return null;
    }

    
Update a com.liferay.portal.model.User's isInternalAccess attribute.

Parameters:
user the Liferay user to update
request the javax.servlet.http.HttpServletRequest which holds information of the access level.
    public void updateInternalAccessOnly(User userHttpServletRequest request) {
        boolean internalAccess = false;
        try {
            internalAccess = internalAccessRule(requestuser);
            .set("isInternalAccess"internalAccessuser);
            .processInternalAccessOnly(user);
        } catch (Exception e) {
            String msg = String.format("Failed to process isInternalAccess [%s] for [%s]"internalAccess,
                    user.getScreenName());
            log(msge);
            request.getSession().invalidate(); // Better to invalidate than risking anyone getting too much access
        }
    }
    private boolean internalAccessRule(HttpServletRequest requestUser user) {
        String header = request.getHeader("x-forwarded-for");
        String[] ipsForExternalAccess = .replaceAll(" """).split(",");
        boolean internal = true;
        if (header != null) {
            // Iterate over the ip:s. We'll find a match if the user is located externally.
            for (String ip : ipsForExternalAccess) {
                if (header.contains(ip)) { // String.contains(...) since the header value may be a comma-separated list.
                    .info("User " + user.getScreenName() + " logged in externally.");
                    return false;
                }
            }
        }
        .info("User " + user.getScreenName() + " logged in internally.");
        return true;
    }
    private void log(String msgThrowable ex) {
        if (.isDebugEnabled()) {
            .warn(msgex);
        } else {
            .warn(msg);
        }
    }
New to GrepCode? Check out our FAQ X