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 static final DateFormat DF = new SimpleDateFormat("yyyyMMdd");
 
     @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
 
     public void updateBirthday(User userUserLdapAttributes userLdapAttributes) {
         PersonNummer personNummer = userLdapAttributes.getPersonNummer();
         if (personNummer.getType() != ..) {
             try {
                 Contact contact = user.getContact();
                 Date pNoDate = .parse(personNummer.getFull().substring(0, 8));
                 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
    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);
        }
    }
    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);
        }
    }
    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);
        }
    }
    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);
        }
    }
    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);
        }
    }
    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);
        }
    }
    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
    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
    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
    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;
    }
    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");
            for (String name : tandvardNames) {
                if (strukturGrupps.contains(name)) {
                    tandvard = true;
                    break;
                }
            }
        }
        return tandvard;
    }

    

Parameters:
user the Liferay user to update
    public void updateVgrLabeledURI(User userList<UnitLdapAttributesuserOrganizations) {
        List<StringuriList = new ArrayList<String>();
        if (userOrganizations == nullreturn;
        if (userOrganizations.size() <= 0) {
            uriList.add("http://intra.vgregion.se/");
        }
        for (UnitLdapAttributes unituserOrganizations) {
            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[] {}), user);
        } catch (Exception e) {
            String msg = String.format("Failed to update vgrLabeledURI %s for [%s]",
                    uriList.toString(), user.getScreenName());
            log(msge);
        }
    }
    public void updateIsPrimarvard(User userList<UnitLdapAttributesuserOrganizations) {
        if (userOrganizations == nullreturn;
        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;
    }
    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(",");
        if (units.length - 3 >= 0) {
            String[] namePart = units[units.length - 3].split("=");
            if (namePart.length == 2) {
                return namePart[1];
            } else {
                String msg = String.format("Strange organization name [%s]"units[units.length - 3]);
                .warn(msg);
            }
        }
        return null;
    }
    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");
        if (header != null && header.contains()) { //there may be a comma-separated list of IPs
            .info("User " + user.getScreenName() + " logged in externally.");
            return false;
        } else {
            .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