Download gravatar if email is known. (#320)
Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
@@ -36,6 +36,7 @@ public class UserPrincipal implements UserDetails, Serializable {
|
|||||||
private final String lastname;
|
private final String lastname;
|
||||||
private final String loginname;
|
private final String loginname;
|
||||||
private final String tenant;
|
private final String tenant;
|
||||||
|
private final String email;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param username
|
* @param username
|
||||||
@@ -48,14 +49,17 @@ public class UserPrincipal implements UserDetails, Serializable {
|
|||||||
* the login name of user
|
* the login name of user
|
||||||
* @param tenant
|
* @param tenant
|
||||||
* the tenant of the user
|
* 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,
|
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.username = username;
|
||||||
this.firstname = firstname;
|
this.firstname = firstname;
|
||||||
this.lastname = lastname;
|
this.lastname = lastname;
|
||||||
this.loginname = loginname;
|
this.loginname = loginname;
|
||||||
this.tenant = tenant;
|
this.tenant = tenant;
|
||||||
|
this.email = email;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -94,68 +98,35 @@ public class UserPrincipal implements UserDetails, Serializable {
|
|||||||
return tenant;
|
return tenant;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
public String getEmail() {
|
||||||
* (non-Javadoc)
|
return email;
|
||||||
*
|
}
|
||||||
* @see
|
|
||||||
* org.springframework.security.core.userdetails.UserDetails#getAuthorities(
|
|
||||||
* )
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<SimpleGrantedAuthority> getAuthorities() {
|
public Collection<SimpleGrantedAuthority> getAuthorities() {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
*
|
|
||||||
* @see
|
|
||||||
* org.springframework.security.core.userdetails.UserDetails#getPassword()
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
*
|
|
||||||
* @see org.springframework.security.core.userdetails.UserDetails#
|
|
||||||
* isAccountNonExpired()
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAccountNonExpired() {
|
public boolean isAccountNonExpired() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
*
|
|
||||||
* @see org.springframework.security.core.userdetails.UserDetails#
|
|
||||||
* isAccountNonLocked()
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAccountNonLocked() {
|
public boolean isAccountNonLocked() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
*
|
|
||||||
* @see org.springframework.security.core.userdetails.UserDetails#
|
|
||||||
* isCredentialsNonExpired()
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCredentialsNonExpired() {
|
public boolean isCredentialsNonExpired() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* (non-Javadoc)
|
|
||||||
*
|
|
||||||
* @see
|
|
||||||
* org.springframework.security.core.userdetails.UserDetails#isEnabled()
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
package org.eclipse.hawkbit.ui.common;
|
package org.eclipse.hawkbit.ui.common;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.eclipse.hawkbit.im.authentication.UserPrincipal;
|
import org.eclipse.hawkbit.im.authentication.UserPrincipal;
|
||||||
@@ -155,6 +156,20 @@ public final class UserDetailsFormatter {
|
|||||||
return trimAndFormatDetail(userPrincipal.getTenant(), 8);
|
return trimAndFormatDetail(userPrincipal.getTenant(), 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return logged in users Email address
|
||||||
|
*/
|
||||||
|
public static Optional<String> 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() {
|
private static UserDetails getCurrentUser() {
|
||||||
final SecurityContext context = (SecurityContext) VaadinService.getCurrentRequest().getWrappedSession()
|
final SecurityContext context = (SecurityContext) VaadinService.getCurrentRequest().getWrappedSession()
|
||||||
.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
|
.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
|
|
||||||
import com.vaadin.server.FontAwesome;
|
import com.vaadin.server.FontAwesome;
|
||||||
import com.vaadin.server.Page;
|
import com.vaadin.server.Page;
|
||||||
|
import com.vaadin.server.Resource;
|
||||||
import com.vaadin.server.ThemeResource;
|
import com.vaadin.server.ThemeResource;
|
||||||
import com.vaadin.shared.ui.label.ContentMode;
|
import com.vaadin.shared.ui.label.ContentMode;
|
||||||
import com.vaadin.spring.annotation.SpringComponent;
|
import com.vaadin.spring.annotation.SpringComponent;
|
||||||
@@ -184,11 +185,18 @@ public final class DashboardMenu extends CustomComponent {
|
|||||||
return links;
|
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() {
|
private Component buildUserMenu() {
|
||||||
final MenuBar settings = new MenuBar();
|
final MenuBar settings = new MenuBar();
|
||||||
settings.addStyleName("user-menu");
|
settings.addStyleName("user-menu");
|
||||||
settings.setHtmlContentAllowed(true);
|
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 formattedTenant = UserDetailsFormatter.formatCurrentTenant();
|
||||||
final String formattedUsername = UserDetailsFormatter.formatCurrentUsername();
|
final String formattedUsername = UserDetailsFormatter.formatCurrentUsername();
|
||||||
|
|||||||
@@ -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 <a href="https://en.gravatar.com/site/implement/images/">Gravatar Image
|
||||||
|
* Requests</a>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user