[master | DONE | Added] New user profile table in DB to save if user has consent in actions logging or not.
1. UserProfile.java: Define class UserProfile with fields String id, private String aaiId, private Boolean consent = false. 2. UserProfileDAO.java & MongoDBUserProfileDAO.java: Added DAO methods for getting/ saving/ deleting user profiles on DB. 3. UserProfileController.java: Added API endpoints GET /user and POST /user/save for authenticated users. 4. IrishMongoConnection.java: Open a new connection to DB with mongoTemplateRef = "mongoIrishTemplate".
This commit is contained in:
parent
1a4edf5092
commit
2e20aeb984
|
@ -0,0 +1,45 @@
|
|||
package eu.dnetlib.irishmonitorservice.configuration.mongo;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoCredential;
|
||||
import com.mongodb.ServerAddress;
|
||||
import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = {"eu.dnetlib.irishmonitorservice.dao"}, mongoTemplateRef = "mongoIrishTemplate")
|
||||
public class IrishMongoConnection {
|
||||
|
||||
@Autowired
|
||||
private MongoConfig mongoConfig;
|
||||
|
||||
@Bean
|
||||
public MongoDbFactory mongoDbFactory() {
|
||||
return new SimpleMongoDbFactory(getMongoClient(), mongoConfig.getDatabase());
|
||||
}
|
||||
|
||||
@Bean(name = "mongoIrishTemplate")
|
||||
public MongoTemplate getMongoTemplate() {
|
||||
return new MongoTemplate(mongoDbFactory());
|
||||
}
|
||||
|
||||
private MongoClient getMongoClient() {
|
||||
if(mongoConfig.getUsername() != null && mongoConfig.getPassword() != null){
|
||||
return new MongoClient(Collections.singletonList(
|
||||
new ServerAddress(mongoConfig.getHost(), mongoConfig.getPort())),
|
||||
Collections.singletonList(MongoCredential.createCredential(mongoConfig.getUsername(), mongoConfig.getDatabase(), mongoConfig.getPassword().toCharArray())));
|
||||
} else {
|
||||
return new MongoClient(Collections.singletonList(new ServerAddress(mongoConfig.getHost(), mongoConfig.getPort())));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package eu.dnetlib.irishmonitorservice.controllers;
|
||||
|
||||
import eu.dnetlib.irishmonitorservice.dao.UserProfileDAO;
|
||||
import eu.dnetlib.irishmonitorservice.entities.UserProfile;
|
||||
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
|
||||
import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException;
|
||||
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin(origins = "*")
|
||||
@RequestMapping("/user")
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
public class UserProfileController {
|
||||
private final Logger log = LogManager.getLogger(this.getClass());
|
||||
|
||||
@Autowired
|
||||
private RolesUtils rolesUtils;
|
||||
|
||||
@Autowired
|
||||
private UserProfileDAO userDAO;
|
||||
|
||||
@RequestMapping(value = "/save", method = RequestMethod.POST)
|
||||
public UserProfile saveUserProfile(@RequestBody UserProfile userProfile) {
|
||||
String aaiId = rolesUtils.getAaiId();
|
||||
UserProfile existingUserProfile = userDAO.findByAaiId(aaiId);
|
||||
if(existingUserProfile != null) {
|
||||
userProfile.setId(existingUserProfile.getId());
|
||||
}
|
||||
userProfile.setAaiId(aaiId); // users can only save/ update their own profiles
|
||||
return userDAO.save(userProfile);
|
||||
}
|
||||
|
||||
// @RequestMapping(value = "/delete/{aaiId}", method = RequestMethod.DELETE)
|
||||
// public Boolean deleteUserProfileByAaiId(@PathVariable("aaiId") String aaiId) {
|
||||
// // maybe portal admin could delete any user profile?
|
||||
//
|
||||
// String currentAaiId = rolesUtils.getAaiId();
|
||||
// if(aaiId != null && currentAaiId.equals(aaiId)) {
|
||||
// throw new ForbiddenException("User with id: "+currentAaiId + " has not access to update user with id: "+aaiId);
|
||||
// }
|
||||
// UserProfile user = userDAO.findByAaiId(aaiId);
|
||||
// if(user == null) {
|
||||
// // EXCEPTION - Entity Not Found
|
||||
// throw new ContentNotFoundException("Delete user profile: No user profile found for : " + aaiId);
|
||||
// } else {
|
||||
// userDAO.delete(user.getId());
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
@RequestMapping(value = "", method = RequestMethod.GET)
|
||||
public UserProfile getUserProfile() {
|
||||
String aaiId = rolesUtils.getAaiId();
|
||||
UserProfile user = userDAO.findByAaiId(aaiId);
|
||||
if(user == null) {
|
||||
// EXCEPTION - Entity Not Found
|
||||
throw new ContentNotFoundException("No user profile found for: " + aaiId);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package eu.dnetlib.irishmonitorservice.dao;
|
||||
|
||||
import eu.dnetlib.irishmonitorservice.entities.UserProfile;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface MongoDBUserProfileDAO extends UserProfileDAO, MongoRepository<UserProfile, String> {
|
||||
List<UserProfile> findAll();
|
||||
|
||||
UserProfile findById(String Id);
|
||||
|
||||
UserProfile findByAaiId(String aaiId);
|
||||
|
||||
UserProfile save(UserProfile user);
|
||||
|
||||
void deleteAll();
|
||||
|
||||
void delete(String id);
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package eu.dnetlib.irishmonitorservice.dao;
|
||||
|
||||
import eu.dnetlib.irishmonitorservice.entities.UserProfile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserProfileDAO {
|
||||
List<UserProfile> findAll();
|
||||
|
||||
UserProfile findById(String Id);
|
||||
|
||||
UserProfile findByAaiId(String aaiId);
|
||||
|
||||
UserProfile save(UserProfile user);
|
||||
|
||||
void deleteAll();
|
||||
|
||||
void delete(String id);
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package eu.dnetlib.irishmonitorservice.entities;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.index.Indexed;
|
||||
|
||||
public class UserProfile {
|
||||
@Id
|
||||
@JsonProperty("_id")
|
||||
private String id;
|
||||
|
||||
@Indexed(unique = true)
|
||||
private String aaiId;
|
||||
private Boolean consent = false;
|
||||
|
||||
public UserProfile() {}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAaiId() {
|
||||
return aaiId;
|
||||
}
|
||||
|
||||
public void setAaiId(String aaiId) {
|
||||
this.aaiId = aaiId;
|
||||
}
|
||||
|
||||
public Boolean getConsent() {
|
||||
return consent;
|
||||
}
|
||||
|
||||
public void setConsent(Boolean consent) {
|
||||
this.consent = consent;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue