Skip to content
This repository has been archived by the owner on Mar 31, 2022. It is now read-only.

Commit

Permalink
The "/userInfo" endpoint should return all user attributes #76
Browse files Browse the repository at this point in the history
  • Loading branch information
Desire456 committed Aug 18, 2021
1 parent 580c45e commit f682c5c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package io.jmix.rest.impl.controller;

import io.jmix.rest.impl.service.filter.data.UserInfo;
import io.jmix.rest.impl.service.UserInfoControllerManager;
import io.jmix.rest.impl.service.filter.data.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.beans.factory.annotation.Autowired;

/**
* REST controller that is used for getting an information about the current user
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@

package io.jmix.rest.impl.service;

import io.jmix.core.Metadata;
import io.jmix.core.MetadataTools;
import io.jmix.core.annotation.Secret;
import io.jmix.core.entity.EntityValues;
import io.jmix.core.metamodel.model.MetaClass;
import io.jmix.core.metamodel.model.MetaProperty;
import io.jmix.core.security.CurrentAuthentication;
import io.jmix.rest.impl.controller.UserInfoController;
import io.jmix.rest.impl.service.filter.data.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map;

/**
* Class that is used by the {@link UserInfoController} for getting an information
Expand All @@ -31,13 +40,41 @@
@Component("rest_UserInfoControllerManager")
public class UserInfoControllerManager {

private static final Logger log = LoggerFactory.getLogger(UserInfoControllerManager.class);

@Autowired
protected CurrentAuthentication currentAuthentication;

@Autowired
protected Metadata metadata;

@Autowired
protected MetadataTools metadataTools;

/**
* @return all user attributes if the user stored in db implements {@link UserDetails}. Username and locale otherwise
*/
public UserInfo getUserInfo() {
UserDetails user = currentAuthentication.getUser();
UserInfo userInfo = new UserInfo(user);
userInfo.setLocale(currentAuthentication.getLocale().toString());

MetaClass userMetaClass = metadata.findClass(user.getClass());
Map<String, Object> attributes = userInfo.getAttributes();
if (userMetaClass != null) {
//using metadata we assume that the pojo contains only the "correct" field-getter-setter namings
for (MetaProperty metaProperty : userMetaClass.getProperties()) {
if (metadataTools.isAnnotationPresent(user, metaProperty.getName(), Secret.class)
|| metaProperty.getRange().isClass()) {
continue;
}
try {
attributes.put(metaProperty.getName(), EntityValues.getValue(user, metaProperty.getName()));
} catch (Exception e) {
log.warn(String.format("Can not get value for %s", metaProperty.getName()));
}
}
}
return userInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

import org.springframework.security.core.userdetails.UserDetails;

import java.util.LinkedHashMap;
import java.util.Map;

public class UserInfo {
public String username;
public String locale;
protected String username;
protected String locale;
protected Map<String, Object> attributes = new LinkedHashMap<>();

public UserInfo(UserDetails user) {
this.username = user.getUsername();
Expand All @@ -37,4 +41,12 @@ public String getLocale() {
public void setLocale(String locale) {
this.locale = locale;
}

public Map<String, Object> getAttributes() {
return attributes;
}

public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
}

0 comments on commit f682c5c

Please sign in to comment.