|
| 1 | +package org.sakaiproject.profile2.tool.entityprovider; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import lombok.Setter; |
| 5 | +import org.apache.commons.codec.binary.Base64; |
| 6 | +import org.json.simple.JSONObject; |
| 7 | +import org.sakaiproject.entitybroker.EntityView; |
| 8 | +import org.sakaiproject.entitybroker.entityprovider.EntityProvider; |
| 9 | +import org.sakaiproject.entitybroker.entityprovider.annotations.EntityCustomAction; |
| 10 | +import org.sakaiproject.entitybroker.entityprovider.capabilities.ActionsExecutable; |
| 11 | +import org.sakaiproject.entitybroker.entityprovider.capabilities.AutoRegisterEntityProvider; |
| 12 | +import org.sakaiproject.entitybroker.entityprovider.capabilities.Describeable; |
| 13 | +import org.sakaiproject.entitybroker.entityprovider.capabilities.Outputable; |
| 14 | +import org.sakaiproject.entitybroker.entityprovider.extension.Formats; |
| 15 | +import org.sakaiproject.entitybroker.util.AbstractEntityProvider; |
| 16 | +import org.sakaiproject.profile2.logic.ProfileImageLogic; |
| 17 | +import org.sakaiproject.profile2.service.ProfileImageService; |
| 18 | +import org.sakaiproject.profile2.util.ProfileConstants; |
| 19 | +import org.sakaiproject.tool.api.SessionManager; |
| 20 | +import org.sakaiproject.user.api.User; |
| 21 | +import org.sakaiproject.user.cover.UserDirectoryService; |
| 22 | + |
| 23 | +import java.io.OutputStream; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.UUID; |
| 26 | + |
| 27 | +@Setter @Slf4j |
| 28 | +public class ProfileImageEntityProvider extends AbstractEntityProvider implements EntityProvider, AutoRegisterEntityProvider, Outputable, Describeable, ActionsExecutable { |
| 29 | + |
| 30 | + private ProfileImageLogic imageLogic; |
| 31 | + private ProfileImageService profileImageService; |
| 32 | + private SessionManager sessionManager; |
| 33 | + |
| 34 | + @Override |
| 35 | + public String[] getHandledOutputFormats() { |
| 36 | + return new String[] { Formats.JSON }; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String getEntityPrefix() { |
| 41 | + return "profile-image"; |
| 42 | + } |
| 43 | + |
| 44 | + @EntityCustomAction(action = "upload", viewKey = EntityView.VIEW_NEW) |
| 45 | + public String upload(EntityView view, Map<String, Object> params) { |
| 46 | + |
| 47 | + JSONObject result = new JSONObject(); |
| 48 | + |
| 49 | + result.put("status", "ERROR"); |
| 50 | + |
| 51 | + if (!checkCSRFToken(params)) { |
| 52 | + return result.toJSONString(); |
| 53 | + } |
| 54 | + |
| 55 | + User currentUser = UserDirectoryService.getCurrentUser(); |
| 56 | + String currentUserId = currentUser.getId(); |
| 57 | + |
| 58 | + if (currentUserId == null) { |
| 59 | + log.warn("Access denied"); |
| 60 | + return result.toJSONString(); |
| 61 | + } |
| 62 | + |
| 63 | + String mimeType = "image/png"; |
| 64 | + String fileName = UUID.randomUUID().toString(); |
| 65 | + String base64 = (String) params.get("base64"); |
| 66 | + byte[] imageBytes = Base64.decodeBase64(base64.getBytes()); |
| 67 | + |
| 68 | + if (imageLogic.setUploadedProfileImage(currentUserId, imageBytes, mimeType, fileName)) { |
| 69 | + profileImageService.resetCachedProfileImageId(currentUserId); |
| 70 | + result.put("status", "SUCCESS"); |
| 71 | + } |
| 72 | + |
| 73 | + return result.toJSONString(); |
| 74 | + } |
| 75 | + |
| 76 | + @EntityCustomAction(action = "details", viewKey = EntityView.VIEW_LIST) |
| 77 | + public Object getProfileImage(OutputStream out, EntityView view, Map<String,Object> params) { |
| 78 | + |
| 79 | + JSONObject result = new JSONObject(); |
| 80 | + |
| 81 | + result.put("status", "ERROR"); |
| 82 | + |
| 83 | + User currentUser = UserDirectoryService.getCurrentUser(); |
| 84 | + String currentUserId = currentUser.getId(); |
| 85 | + |
| 86 | + if (currentUserId == null) { |
| 87 | + log.warn("Access denied"); |
| 88 | + return result.toJSONString(); |
| 89 | + } |
| 90 | + |
| 91 | + String imageUrl = imageLogic.getProfileImageEntityUrl(currentUserId, ProfileConstants.PROFILE_IMAGE_MAIN); |
| 92 | + |
| 93 | + result.put("url", imageUrl); |
| 94 | + result.put("isDefault", imageLogic.profileImageIsDefault(currentUserId)); |
| 95 | + result.put("csrf_token", sessionManager.getCurrentSession().getAttribute("sakai.csrf.token")); |
| 96 | + result.put("status", "SUCCESS"); |
| 97 | + |
| 98 | + return result.toJSONString(); |
| 99 | + } |
| 100 | + |
| 101 | + @EntityCustomAction(action = "remove", viewKey = EntityView.VIEW_NEW) |
| 102 | + public String remove(EntityView view, Map<String, Object> params) { |
| 103 | + |
| 104 | + JSONObject result = new JSONObject(); |
| 105 | + |
| 106 | + result.put("status", "ERROR"); |
| 107 | + |
| 108 | + if (!checkCSRFToken(params)) { |
| 109 | + return result.toJSONString(); |
| 110 | + } |
| 111 | + |
| 112 | + User currentUser = UserDirectoryService.getCurrentUser(); |
| 113 | + String currentUserId = currentUser.getId(); |
| 114 | + |
| 115 | + if (currentUserId == null) { |
| 116 | + log.warn("Access denied"); |
| 117 | + return result.toJSONString(); |
| 118 | + } |
| 119 | + |
| 120 | + if (imageLogic.resetProfileImage(currentUserId)) { |
| 121 | + profileImageService.resetCachedProfileImageId(currentUserId); |
| 122 | + result.put("status", "SUCCESS"); |
| 123 | + } |
| 124 | + |
| 125 | + return result.toJSONString(); |
| 126 | + } |
| 127 | + |
| 128 | + private boolean checkCSRFToken(Map<String, Object> params) { |
| 129 | + |
| 130 | + Object sessionToken = sessionManager.getCurrentSession().getAttribute("sakai.csrf.token"); |
| 131 | + |
| 132 | + if (sessionToken == null || !sessionToken.equals(params.get("sakai_csrf_token"))) { |
| 133 | + log.warn("CSRF token validation failed"); |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | +} |
0 commit comments