no message

This commit is contained in:
Ioannis Kalyvas 2018-02-01 09:47:24 +02:00
parent e0237e1045
commit 2f462495a1
7 changed files with 90 additions and 46 deletions

View File

@ -90,6 +90,7 @@ public class Login {
}
}
@Transactional
@RequestMapping(method = RequestMethod.POST, value = {"/logout"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<Principal>> logout(Principal principal) {

View File

@ -20,45 +20,49 @@ import org.w3c.dom.Element;
import eu.eudat.utilities.builders.XmlBuilder;
import java.util.Arrays;
import java.util.List;
public class UserManager {
public static eu.eudat.models.user.composite.DatasetProfile generateDatasetProfileModel(eu.eudat.entities.DatasetProfile profile){
Document viewStyleDoc = XmlBuilder.fromXml(profile.getDefinition());
Element root = (Element)viewStyleDoc.getDocumentElement();
eu.eudat.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel viewstyle= new eu.eudat.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel().fromXml(root);
public static eu.eudat.models.user.composite.DatasetProfile generateDatasetProfileModel(eu.eudat.entities.DatasetProfile profile) {
Document viewStyleDoc = XmlBuilder.fromXml(profile.getDefinition());
Element root = (Element) viewStyleDoc.getDocumentElement();
eu.eudat.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel viewstyle = new eu.eudat.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel().fromXml(root);
eu.eudat.models.user.composite.DatasetProfile datasetprofile = new eu.eudat.models.user.composite.DatasetProfile();
datasetprofile.buildProfile(viewstyle);
return datasetprofile;
}
eu.eudat.models.user.composite.DatasetProfile datasetprofile = new eu.eudat.models.user.composite.DatasetProfile();
datasetprofile.buildProfile(viewstyle);
public static DataTableData<UserListingModel> getPaged(UserInfoDao userInfoDao , UserInfoTableRequestItem userInfoTableRequestItem) throws Exception {
QueryableList<eu.eudat.entities.UserInfo> users = userInfoDao.getWithCriteria(userInfoTableRequestItem.getCriteria());
QueryableList<eu.eudat.entities.UserInfo> pagedUsers = PaginationManager.applyPaging(users,userInfoTableRequestItem);
List<UserListingModel> modelUsers = new DomainModelConverter<eu.eudat.entities.UserInfo,UserListingModel>().fromDataModel(pagedUsers.toList(),UserListingModel.class);
DataTableData<UserListingModel> dataTableData = new DataTableData<>();
dataTableData.setData(modelUsers);
dataTableData.setTotalCount(users.count());
return dataTableData;
}
return datasetprofile;
}
public static void editRoles(ApiContext apiContext, UserListingModel user){
eu.eudat.entities.UserInfo userInfo = apiContext.getDatabaseRepository().getUserInfoDao().find(user.getId());
userInfo.getUserRoles().removeAll(userInfo.getUserRoles());
userInfo = apiContext.getDatabaseRepository().getUserInfoDao().createOrUpdate(userInfo);
for(Integer role : user.getAppRoles()){
UserRole userRole = new UserRole();
userRole.setRole(role);
userRole.setUserInfo(userInfo);
apiContext.getDatabaseRepository().getUserRoleDao().createOrUpdate(userRole);
}
}
public static DataTableData<UserListingModel> getPaged(UserInfoDao userInfoDao, UserInfoTableRequestItem userInfoTableRequestItem) throws Exception {
QueryableList<eu.eudat.entities.UserInfo> users = userInfoDao.getWithCriteria(userInfoTableRequestItem.getCriteria());
QueryableList<eu.eudat.entities.UserInfo> pagedUsers = PaginationManager.applyPaging(users, userInfoTableRequestItem);
if (userInfoTableRequestItem.getSelection() != null && userInfoTableRequestItem.getSelection().getFields() != null) {
pagedUsers.withFields(Arrays.asList(userInfoTableRequestItem.getSelection().getFields()));
}
List<UserListingModel> modelUsers = new DomainModelConverter<eu.eudat.entities.UserInfo, UserListingModel>().fromDataModel(pagedUsers.toList(), UserListingModel.class);
DataTableData<UserListingModel> dataTableData = new DataTableData<>();
dataTableData.setData(modelUsers);
dataTableData.setTotalCount(users.count());
return dataTableData;
}
public static Principal authenticate(AuthenticationService authenticationService, Credentials credentials){
Principal principal = authenticationService.Touch(credentials);
if(principal == null) throw new UnauthorisedException("Could not Sign In User");
return principal;
}
public static void editRoles(ApiContext apiContext, UserListingModel user) {
eu.eudat.entities.UserInfo userInfo = apiContext.getDatabaseRepository().getUserInfoDao().find(user.getId());
userInfo.getUserRoles().removeAll(userInfo.getUserRoles());
userInfo = apiContext.getDatabaseRepository().getUserInfoDao().createOrUpdate(userInfo);
for (Integer role : user.getAppRoles()) {
UserRole userRole = new UserRole();
userRole.setRole(role);
userRole.setUserInfo(userInfo);
apiContext.getDatabaseRepository().getUserRoleDao().createOrUpdate(userRole);
}
}
public static Principal authenticate(AuthenticationService authenticationService, Credentials credentials) {
Principal principal = authenticationService.Touch(credentials);
if (principal == null) throw new UnauthorisedException("Could not Sign In User");
return principal;
}
}

View File

@ -0,0 +1,17 @@
package eu.eudat.models.helpers.common;
/**
* Created by ikalyvas on 1/31/2018.
*/
public class SelectionFields {
private String[] fields;
public String[] getFields() {
return fields;
}
public void setFields(String[] fields) {
this.fields = fields;
}
}

View File

@ -1,9 +1,11 @@
package eu.eudat.models.helpers.requests;
import eu.eudat.models.helpers.common.ColumnOrderings;
import eu.eudat.models.helpers.common.SelectionFields;
public abstract class TableRequest<T> extends RequestItem<T> {
private ColumnOrderings orderings;
private SelectionFields selection;
private Integer length;
private Integer offset;
@ -30,4 +32,12 @@ public abstract class TableRequest<T> extends RequestItem<T> {
public void setOrderings(ColumnOrderings orderings) {
this.orderings = orderings;
}
public SelectionFields getSelection() {
return selection;
}
public void setSelection(SelectionFields selection) {
this.selection = selection;
}
}

View File

@ -35,5 +35,7 @@ public interface QueryableList<T extends DataEntity<T>> {
QueryableList<T> withHint(String hint);
QueryableList<T> withFields(List<String> fields);
Long count();
}

View File

@ -24,6 +24,7 @@ public class QueryableHibernateList<T extends DataEntity<T>> implements Queryabl
private Root<T> root;
private List<Predicate> predicates = new LinkedList<Predicate>();
private List<Order> orderings = new LinkedList<>();
private List<Selection<?>> fields = new LinkedList<>();
private Integer length;
private Integer offset;
private Set<String> hints;
@ -102,6 +103,14 @@ public class QueryableHibernateList<T extends DataEntity<T>> implements Queryabl
return this;
}
@Override
public QueryableList<T> withFields(List<String> fields) {
for (String field : fields) {
this.fields.add(this.root.get(field));
}
return this;
}
public Long count() {
CriteriaBuilder criteriaBuilder = this.manager.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
@ -114,6 +123,7 @@ public class QueryableHibernateList<T extends DataEntity<T>> implements Queryabl
this.query.where(this.predicates.toArray(new Predicate[this.predicates.size()]));
if (!this.orderings.isEmpty()) this.query.orderBy(this.orderings);
if (this.fields != null && !this.fields.isEmpty()) this.query.multiselect(this.fields);
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
if (this.offset != null) typedQuery.setFirstResult(this.offset);

View File

@ -38,9 +38,7 @@ public class AuthenticationService {
UserToken tokenEntry = this.apiContext.getDatabaseRepository().getUserTokenDao().find(token);
if (tokenEntry == null || tokenEntry.getExpiresAt().before(new Date())) return null;
Principal principal = this.Touch(tokenEntry);
return principal;
return this.Touch(tokenEntry);
}
public void Logout(UUID token) {
@ -62,7 +60,7 @@ public class AuthenticationService {
List<UserRole> userRoles = this.apiContext.getDatabaseRepository().getUserRoleDao().getUserRoles(user);
for (UserRole item : userRoles) {
if (principal.getAuthz() == null) principal.setAuthorities(new HashSet<Authorities>());
if (principal.getAuthz() == null) principal.setAuthorities(new HashSet<>());
principal.getAuthz().add(Authorities.fromInteger(item.getRole()));
}
return principal;
@ -71,9 +69,10 @@ public class AuthenticationService {
public Principal Touch(Credentials credentials) {
Credential credential = this.apiContext.getDatabaseRepository().getCredentialDao().getLoggedInCredentials(credentials);
if (credential == null && credentials.getUsername().equals(environment.getProperty("autouser.root.username"))) credential = this.autoCreateUser(credentials.getUsername(),credentials.getSecret());
if (credential == null && credentials.getUsername().equals(environment.getProperty("autouser.root.username")))
credential = this.autoCreateUser(credentials.getUsername(), credentials.getSecret());
if(credential == null) return null;
if (credential == null) return null;
UserToken userToken = new UserToken();
userToken.setUser(credential.getUserInfo());
@ -101,7 +100,7 @@ public class AuthenticationService {
credential.setSecret(profile.getSecret());
if (userInfo == null) {
userInfo = new UserInfo();
userInfo.setName((String) profile.getName());
userInfo.setName(profile.getName());
userInfo.setVerified_email(profile.getIsVerified());
userInfo.setEmail(profile.getEmail());
userInfo.setCreated(new Date());
@ -153,16 +152,17 @@ public class AuthenticationService {
@Transactional
private Credential autoCreateUser(String username,String password){
if(!environment.getProperty("autouser.root.username").equals(username) || !environment.getProperty("autouser.root.password").equals(password)) return null;
private Credential autoCreateUser(String username, String password) {
if (!environment.getProperty("autouser.root.username").equals(username) || !environment.getProperty("autouser.root.password").equals(password))
return null;
UserInfo userInfo = new UserInfo();
userInfo.setName(username);
userInfo.setEmail(environment.getProperty("autouser.root.email"));
userInfo.setCreated(new Date());
userInfo.setLastloggedin(new Date());
userInfo.setAuthorization_level((short)1);
userInfo.setUsertype((short)1);
userInfo.setAuthorization_level((short) 1);
userInfo.setUsertype((short) 1);
userInfo = this.apiContext.getDatabaseRepository().getUserInfoDao().createOrUpdate(userInfo);
UserRole role = new UserRole();
@ -174,7 +174,7 @@ public class AuthenticationService {
credential.setUserInfo(userInfo);
credential.setPublicValue(username);
credential.setSecret(password);
credential.setProvider((int)TokenValidatorFactoryImpl.LoginProvider.NATIVELOGIN.getValue());
credential.setProvider((int) TokenValidatorFactoryImpl.LoginProvider.NATIVELOGIN.getValue());
credential.setCreationTime(new Date());
credential.setLastUpdateTime(new Date());
credential.setStatus(0);