diff --git a/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/im/authentication/UserPrincipal.java b/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/im/authentication/UserPrincipal.java index 7719a18b9..454be1f57 100644 --- a/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/im/authentication/UserPrincipal.java +++ b/hawkbit-security-core/src/main/java/org/eclipse/hawkbit/im/authentication/UserPrincipal.java @@ -36,6 +36,7 @@ public class UserPrincipal implements UserDetails, Serializable { private final String lastname; private final String loginname; private final String tenant; + private final String email; /** * @param username @@ -48,14 +49,17 @@ public class UserPrincipal implements UserDetails, Serializable { * the login name of user * @param tenant * the tenant of the user + * @param email + * address of the user */ public UserPrincipal(final String username, final String firstname, final String lastname, final String loginname, - final String tenant) { + final String tenant, final String email) { this.username = username; this.firstname = firstname; this.lastname = lastname; this.loginname = loginname; this.tenant = tenant; + this.email = email; } /** @@ -94,68 +98,35 @@ public class UserPrincipal implements UserDetails, Serializable { return tenant; } - /* - * (non-Javadoc) - * - * @see - * org.springframework.security.core.userdetails.UserDetails#getAuthorities( - * ) - */ + public String getEmail() { + return email; + } + @Override public Collection getAuthorities() { return Collections.emptyList(); } - /* - * (non-Javadoc) - * - * @see - * org.springframework.security.core.userdetails.UserDetails#getPassword() - */ @Override public String getPassword() { return null; } - /* - * (non-Javadoc) - * - * @see org.springframework.security.core.userdetails.UserDetails# - * isAccountNonExpired() - */ @Override public boolean isAccountNonExpired() { return true; } - /* - * (non-Javadoc) - * - * @see org.springframework.security.core.userdetails.UserDetails# - * isAccountNonLocked() - */ @Override public boolean isAccountNonLocked() { return false; } - /* - * (non-Javadoc) - * - * @see org.springframework.security.core.userdetails.UserDetails# - * isCredentialsNonExpired() - */ @Override public boolean isCredentialsNonExpired() { return true; } - /* - * (non-Javadoc) - * - * @see - * org.springframework.security.core.userdetails.UserDetails#isEnabled() - */ @Override public boolean isEnabled() { return true; diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/UserDetailsFormatter.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/UserDetailsFormatter.java index d9a964b04..aeafd22bc 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/UserDetailsFormatter.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/common/UserDetailsFormatter.java @@ -9,6 +9,7 @@ package org.eclipse.hawkbit.ui.common; import java.util.Collections; +import java.util.Optional; import org.apache.commons.lang3.StringUtils; import org.eclipse.hawkbit.im.authentication.UserPrincipal; @@ -155,6 +156,20 @@ public final class UserDetailsFormatter { return trimAndFormatDetail(userPrincipal.getTenant(), 8); } + /** + * @return logged in users Email address + */ + public static Optional getCurrentUserEmail() { + final UserDetails userDetails = getCurrentUser(); + if (!(userDetails instanceof UserPrincipal)) { + return Optional.empty(); + } + + final UserPrincipal userPrincipal = (UserPrincipal) userDetails; + + return Optional.ofNullable(userPrincipal.getEmail()); + } + private static UserDetails getCurrentUser() { final SecurityContext context = (SecurityContext) VaadinService.getCurrentRequest().getWrappedSession() .getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY); diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/menu/DashboardMenu.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/menu/DashboardMenu.java index c35378e20..aebc88663 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/menu/DashboardMenu.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/menu/DashboardMenu.java @@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired; import com.vaadin.server.FontAwesome; import com.vaadin.server.Page; +import com.vaadin.server.Resource; import com.vaadin.server.ThemeResource; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.spring.annotation.SpringComponent; @@ -184,11 +185,18 @@ public final class DashboardMenu extends CustomComponent { return links; } + private static Resource getImage() { + return UserDetailsFormatter.getCurrentUserEmail().map(email -> (Resource) new GravatarResource(email)) + .orElse(new ThemeResource("images/profile-pic-57px.jpg")); + + } + private Component buildUserMenu() { final MenuBar settings = new MenuBar(); settings.addStyleName("user-menu"); settings.setHtmlContentAllowed(true); - final MenuItem settingsItem = settings.addItem("", new ThemeResource("images/profile-pic-57px.jpg"), null); + + final MenuItem settingsItem = settings.addItem("", getImage(), null); final String formattedTenant = UserDetailsFormatter.formatCurrentTenant(); final String formattedUsername = UserDetailsFormatter.formatCurrentUsername(); diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/menu/GravatarResource.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/menu/GravatarResource.java new file mode 100644 index 000000000..6971ea6b4 --- /dev/null +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/menu/GravatarResource.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.ui.menu; + +import org.springframework.util.DigestUtils; + +import com.vaadin.server.ExternalResource; + +/** + * {@link ExternalResource} for user profile pictures hosted by gravatar. + * + * @see Gravatar Image + * Requests + * + */ +public class GravatarResource extends ExternalResource { + private static final long serialVersionUID = 1L; + + /** + * Construct based on given email address. Generates external resource + * pointing to gravatar with rating + * "g: suitable for display on all websites with any audience type." and + * "mystery-man" icon as backup as secure request. + * + * @param email + * to generate resource for + */ + public GravatarResource(final String email) { + super("https://www.gravatar.com/avatar/" + DigestUtils.md5DigestAsHex(email.getBytes()) + ".jpg?s=56&r=g&d=mm"); + } + +}