package org.sakaiproject.profile2.logic; import java.util.HashMap; import org.apache.log4j.Logger; import org.sakaiproject.memory.api.Cache; import org.sakaiproject.profile2.cache.CacheManager; import org.sakaiproject.profile2.dao.ProfileDao; import org.sakaiproject.profile2.model.ProfilePrivacy; import org.sakaiproject.profile2.util.ProfileConstants; /** * Implementation of ProfilePrivacyLogic API * * @author Steve Swinsburg (steve.swinsburg@gmail.com) * */ public class ProfilePrivacyLogicImpl implements ProfilePrivacyLogic { private static final Logger log = Logger.getLogger(ProfilePrivacyLogicImpl.class); private Cache cache; private final String CACHE_NAME = "org.sakaiproject.profile2.cache.privacy"; /** * {@inheritDoc} */ public ProfilePrivacy getPrivacyRecordForUser(final String userId) { return getPrivacyRecordForUser(userId, true); } /** * {@inheritDoc} */ public ProfilePrivacy getPrivacyRecordForUser(final String userId, final boolean useCache) { if(userId == null){ throw new IllegalArgumentException("Null argument in ProfileLogic.getPrivacyRecordForUser"); } //check cache if(useCache) { if(cache.containsKey(userId)){ log.debug("Fetching privacy record from cache for: " + userId); return (ProfilePrivacy)cache.get(userId); } } //will stay null if we can't get or create one ProfilePrivacy privacy = null; privacy = dao.getPrivacyRecord(userId); log.debug("Fetching privacy record from dao for: " + userId); //if none, create and persist a default if(privacy == null) { privacy = dao.addNewPrivacyRecord(getDefaultPrivacyRecord(userId)); if(privacy != null) { sakaiProxy.postEvent(ProfileConstants.EVENT_PRIVACY_NEW, "/profile/"+userId, true); log.info("Created default privacy record for user: " + userId); } } //add to cache if(privacy != null) { log.debug("Adding privacy record to cache for: " + userId); cache.put(userId, privacy); } return privacy; } /** * {@inheritDoc} */ public boolean savePrivacyRecord(ProfilePrivacy privacy) { //if changes not allowed if(!sakaiProxy.isPrivacyChangeAllowedGlobally()) { log.warn("Privacy changes are not permitted as per sakai.properties setting 'profile2.privacy.change.enabled'."); return false; } //save if(dao.updatePrivacyRecord(privacy)) { log.info("Saved privacy record for user: " + privacy.getUserUuid()); //update cache log.debug("Updated privacy record in cache for: " + privacy.getUserUuid()); cache.put(privacy.getUserUuid(), privacy); return true; } return false; } /** * {@inheritDoc} */ public boolean isUserXProfileImageVisibleByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for userX ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); //pass to main return isUserXProfileImageVisibleByUserY(userX, profilePrivacy, userY, friend); } /** * {@inheritDoc} */ public boolean isUserXProfileImageVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if no privacy record, return whatever the flag is set as by default /* deprecated by PRFL-86, privacy object will never be null now it will always be default or overridden default if(profilePrivacy == null) { return ProfileConstants.DEFAULT_PROFILEIMAGE_VISIBILITY; } */ //if restricted to only self, not allowed /* DEPRECATED via PRFL-24 when the privacy settings were relaxed if(profilePrivacy.getProfile() == ProfilePrivacyManager.PRIVACY_OPTION_ONLYME) { return false; } */ //if user is friend and friends are allowed if(friend && profilePrivacy.getProfileImage() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getProfileImage() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getProfileImage() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserProfileImageVisibleByCurrentUser. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXBasicInfoVisibleByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for userX ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); //pass to main return isUserXBasicInfoVisibleByUserY(userX, profilePrivacy, userY, friend); } /** * {@inheritDoc} */ public boolean isUserXBasicInfoVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if no privacy record, return whatever the flag is set as by default /* deprecated by PRFL-86, privacy object will never be null now it will always be default or overridden default if(profilePrivacy == null) { return ProfileConstants.DEFAULT_BASICINFO_VISIBILITY; } */ //if restricted to only self, not allowed if(profilePrivacy.getBasicInfo() == ProfileConstants.PRIVACY_OPTION_ONLYME) { return false; } //if user is friend and friends are allowed if(friend && profilePrivacy.getBasicInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getBasicInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getBasicInfo() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXBasicInfoVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXContactInfoVisibleByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for this user ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); //pass to main return isUserXContactInfoVisibleByUserY(userX, profilePrivacy, userY, friend); } /** * {@inheritDoc} */ public boolean isUserXContactInfoVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if no privacy record, return whatever the flag is set as by default /* deprecated by PRFL-86, privacy object will never be null now it will always be default or overridden default if(profilePrivacy == null) { return ProfileConstants.DEFAULT_CONTACTINFO_VISIBILITY; } */ //if restricted to only self, not allowed if(profilePrivacy.getContactInfo() == ProfileConstants.PRIVACY_OPTION_ONLYME) { return false; } //if user is friend and friends are allowed if(friend && profilePrivacy.getContactInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getContactInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getContactInfo() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXContactInfoVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXStaffInfoVisibleByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for this user ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); //pass to main return isUserXStaffInfoVisibleByUserY(userX, profilePrivacy, userY, friend); } /** * {@inheritDoc} */ public boolean isUserXStaffInfoVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if restricted to only self, not allowed if(profilePrivacy.getStaffInfo() == ProfileConstants.PRIVACY_OPTION_ONLYME) { return false; } //if user is friend and friends are allowed if(friend && profilePrivacy.getStaffInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getStaffInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getStaffInfo() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXStaffInfoVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXStudentInfoVisibleByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for this user ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); //pass to main return isUserXStudentInfoVisibleByUserY(userX, profilePrivacy, userY, friend); } /** * {@inheritDoc} */ public boolean isUserXStudentInfoVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if restricted to only self, not allowed if(profilePrivacy.getStudentInfo() == ProfileConstants.PRIVACY_OPTION_ONLYME) { return false; } //if user is friend and friends are allowed if(friend && profilePrivacy.getStudentInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getStudentInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getStudentInfo() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXStudentInfoVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXBusinessInfoVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if restricted to only self, not allowed if(profilePrivacy.getBusinessInfo() == ProfileConstants.PRIVACY_OPTION_ONLYME) { return false; } //if user is friend and friends are allowed if(friend && profilePrivacy.getBusinessInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getBusinessInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getBusinessInfo() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXBusinessInfoVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXSocialNetworkingInfoVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if restricted to only self, not allowed if(profilePrivacy.getSocialNetworkingInfo() == ProfileConstants.PRIVACY_OPTION_ONLYME) { return false; } //if user is friend and friends are allowed if(friend && profilePrivacy.getSocialNetworkingInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getSocialNetworkingInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getSocialNetworkingInfo() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXSocialNetworkingInfoVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXPersonalInfoVisibleByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for this user ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); //pass to main return isUserXPersonalInfoVisibleByUserY(userX, profilePrivacy, userY, friend); } /** * {@inheritDoc} */ public boolean isUserXPersonalInfoVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if no privacy record, return whatever the flag is set as by default /* deprecated by PRFL-86, privacy object will never be null now it will always be default or overridden default if(profilePrivacy == null) { return ProfileConstants.DEFAULT_PERSONALINFO_VISIBILITY; } */ //if restricted to only self, not allowed if(profilePrivacy.getPersonalInfo() == ProfileConstants.PRIVACY_OPTION_ONLYME) { return false; } //if user is friend and friends are allowed if(friend && profilePrivacy.getPersonalInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getPersonalInfo() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getPersonalInfo() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXPersonalInfoVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXFriendsListVisibleByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for this user ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); //pass to main return isUserXFriendsListVisibleByUserY(userX, profilePrivacy, userY, friend); } /** * {@inheritDoc} */ public boolean isUserXFriendsListVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if no privacy record, return whatever the flag is set as by default /* deprecated by PRFL-86, privacy object will never be null now it will always be default or overridden default if(profilePrivacy == null) { return ProfileConstants.DEFAULT_MYFRIENDS_VISIBILITY; } */ //if restricted to only self, not allowed if(profilePrivacy.getMyFriends() == ProfileConstants.PRIVACY_OPTION_ONLYME) { return false; } //if user is friend and friends are allowed if(friend && profilePrivacy.getMyFriends() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getMyFriends() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getMyFriends() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXFriendsListVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXGalleryVisibleByUser(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { // current user if(userY.equals(userX)) { return true; } // friend and friends allowed if (friend && profilePrivacy.getMyPictures() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } // everyone else if(profilePrivacy.getMyPictures() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } else { return false; } } /** * {@inheritDoc} */ public boolean isUserXStatusVisibleByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for this user ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); //pass to main return isUserXStatusVisibleByUserY(userX, profilePrivacy, userY, friend); } /** * {@inheritDoc} */ public boolean isUserXStatusVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if no privacy record, return whatever the flag is set as by default /* deprecated by PRFL-86, privacy object will never be null now it will always be default or overridden default if(profilePrivacy == null) { return ProfileConstants.DEFAULT_MYSTATUS_VISIBILITY; } */ //if restricted to only self, not allowed /* DEPRECATED via PRFL-24 when the privacy settings were relaxed if(profilePrivacy.getMyStatus() == ProfilePrivacyManager.PRIVACY_OPTION_ONLYME) { return false; } */ //if user is friend and friends are allowed if(friend && profilePrivacy.getMyStatus() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getMyStatus() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getMyStatus() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXStatusVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isUserXKudosVisibleByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for this user ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); //pass to main return isUserXKudosVisibleByUserY(userX, profilePrivacy, userY, friend); } /** * {@inheritDoc} */ public boolean isUserXKudosVisibleByUserY(String userX, ProfilePrivacy profilePrivacy, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //if user is friend and friends are allowed if(friend && profilePrivacy.getMyKudos() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getMyKudos() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //if everyone is allowed if(profilePrivacy.getMyKudos() == ProfileConstants.PRIVACY_OPTION_EVERYONE) { return true; } //uncaught rule, return false log.error("ProfileLogic.isUserXKudosVisibleByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend); return false; } /** * {@inheritDoc} */ public boolean isBirthYearVisible(String userId) { //get privacy record for this user ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userId); return isBirthYearVisible(profilePrivacy); } /** * {@inheritDoc} */ public boolean isBirthYearVisible(ProfilePrivacy profilePrivacy) { //return value or whatever the flag is set as by default /* deprecated by PRFL-86, privacy object will never be null now it will always be default or overridden default if(profilePrivacy == null) { return ProfileConstants.DEFAULT_BIRTHYEAR_VISIBILITY; } else { } */ return profilePrivacy.isShowBirthYear(); } /** * {@inheritDoc} */ public boolean isUserXAbleToBeMessagedByUserY(String userX, String userY, boolean friend) { //if user is requesting own info, they ARE allowed if(userY.equals(userX)) { return true; } //get privacy record for this user ProfilePrivacy profilePrivacy = getPrivacyRecordForUser(userX); if(profilePrivacy == null) { log.error("ProfilePrivacyLogic.isUserXAbleToBeMessagedByUserY. Couldn't get a ProfilePrivacy record for userX: " + userX); return false; } //if nobody allowed if(profilePrivacy.getMessages() == ProfileConstants.PRIVACY_OPTION_NOBODY) { return false; } //if user is friend and friends are allowed if(friend && profilePrivacy.getMessages() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return true; } //if not friend and set to friends only if(!friend && profilePrivacy.getMessages() == ProfileConstants.PRIVACY_OPTION_ONLYFRIENDS) { return false; } //uncaught rule, return false log.error("ProfilePrivacyLogic.isUserXAbleToBeMessagedByUserY. Uncaught rule. userX: " + userX + ", userY: " + userY + ", friend: " + friend +", setting: " + profilePrivacy.getMessages()); return false; } /** * Create a privacy record according to the defaults. * * @param userId uuid of the user to create the record for */ private ProfilePrivacy getDefaultPrivacyRecord(String userId) { //get the overriden privacy settings. they'll be defaults if not specified HashMap<String, Object> props = sakaiProxy.getOverriddenPrivacySettings(); //using the props, set them into the ProfilePrivacy object ProfilePrivacy privacy = new ProfilePrivacy(); privacy.setUserUuid(userId); privacy.setProfileImage((Integer)props.get("profileImage")); privacy.setBasicInfo((Integer)props.get("basicInfo")); privacy.setContactInfo((Integer)props.get("contactInfo")); privacy.setStaffInfo((Integer)props.get("staffInfo")); privacy.setStudentInfo((Integer)props.get("studentInfo")); privacy.setPersonalInfo((Integer)props.get("personalInfo")); privacy.setShowBirthYear((Boolean)props.get("birthYear")); privacy.setMyFriends((Integer)props.get("myFriends")); privacy.setMyStatus((Integer)props.get("myStatus")); privacy.setMyPictures((Integer)props.get("myPictures")); privacy.setMessages((Integer)props.get("messages")); privacy.setBusinessInfo((Integer)props.get("businessInfo")); privacy.setMyKudos((Integer)props.get("myKudos")); privacy.setSocialNetworkingInfo((Integer)props.get("socialInfo")); return privacy; } public void init() { cache = cacheManager.createCache(CACHE_NAME); } private SakaiProxy sakaiProxy; public void setSakaiProxy(SakaiProxy sakaiProxy) { this.sakaiProxy = sakaiProxy; } private ProfileDao dao; public void setDao(ProfileDao dao) { this.dao = dao; } private CacheManager cacheManager; public void setCacheManager(CacheManager cacheManager) { this.cacheManager = cacheManager; } }