Download gravatar if email is known. (#320)

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
Kai Zimmermann
2016-10-20 12:49:08 +02:00
committed by GitHub
parent ec874232d3
commit 84ddb340d7
4 changed files with 71 additions and 39 deletions

View File

@@ -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<SimpleGrantedAuthority> 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;

View File

@@ -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<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() {
final SecurityContext context = (SecurityContext) VaadinService.getCurrentRequest().getWrappedSession()
.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);

View File

@@ -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();

View File

@@ -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");
}
}